Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 2 | #include "fakes/clock_fake.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 3 | #include "helpers.hpp" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 4 | #include "messages/collect_trigger_id.hpp" |
| 5 | #include "messages/trigger_presence_changed_ind.hpp" |
| 6 | #include "messages/update_report_ind.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 7 | #include "mocks/json_storage_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 8 | #include "mocks/metric_mock.hpp" |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 9 | #include "mocks/report_factory_mock.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 10 | #include "mocks/report_manager_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 11 | #include "params/report_params.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 12 | #include "report.hpp" |
| 13 | #include "report_manager.hpp" |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 14 | #include "utils/clock.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 15 | #include "utils/contains.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 16 | #include "utils/conv_container.hpp" |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 17 | #include "utils/dbus_path_utils.hpp" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 18 | #include "utils/messanger.hpp" |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 19 | #include "utils/string_utils.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 20 | #include "utils/transform.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 21 | #include "utils/tstring.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 22 | |
| 23 | #include <sdbusplus/exception.hpp> |
| 24 | |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 25 | #include <ranges> |
| 26 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 27 | using namespace testing; |
| 28 | using namespace std::literals::string_literals; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 29 | using namespace std::chrono_literals; |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 30 | using sdbusplus::message::object_path; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 31 | namespace tstring = utils::tstring; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 32 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 33 | using ErrorMessageDbusType = std::tuple<std::string, std::string>; |
| 34 | using ErrorMessagesDbusType = std::vector<ErrorMessageDbusType>; |
| 35 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 36 | constexpr Milliseconds systemTimestamp = 55ms; |
| 37 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 38 | namespace |
| 39 | { |
| 40 | |
| 41 | ReportParams defaultParams() |
| 42 | { |
| 43 | return ReportParams(); |
| 44 | } |
| 45 | |
| 46 | ReportParams defaultOnChangeParams() |
| 47 | { |
| 48 | return defaultParams().reportingType(ReportingType::onChange); |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 53 | class TestReport : public Test |
| 54 | { |
| 55 | public: |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 56 | std::unique_ptr<ReportManagerMock> reportManagerMock = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 57 | std::make_unique<NiceMock<ReportManagerMock>>(); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 58 | std::unique_ptr<ReportFactoryMock> reportFactoryMock = |
| 59 | std::make_unique<NiceMock<ReportFactoryMock>>(); |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 60 | nlohmann::json storedConfiguration; |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 61 | NiceMock<StorageMock> storageMock; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 62 | std::vector<std::shared_ptr<MetricMock>> metricMocks; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 63 | std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>(); |
| 64 | ClockFake& clockFake = *clockFakePtr; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 65 | std::unique_ptr<Report> sut; |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 66 | utils::Messanger messanger; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 67 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 68 | MockFunction<void()> checkPoint; |
| 69 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 70 | TestReport() : messanger(DbusEnvironment::getIoc()) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 71 | { |
| 72 | clockFake.system.set(systemTimestamp); |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 73 | ON_CALL(storageMock, store(to_file_path(ReportParams().reportId()), _)) |
| 74 | .WillByDefault(SaveArg<1>(&storedConfiguration)); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 77 | void initMetricMocks( |
| 78 | const std::vector<LabeledMetricParameters>& metricParameters) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 79 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 80 | for (auto i = metricMocks.size(); i < metricParameters.size(); ++i) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 81 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 82 | metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>()); |
| 83 | } |
| 84 | metricMocks.resize(metricParameters.size()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 85 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 86 | std::vector<MetricValue> readings{{MetricValue{"a", "b", 17.1, 114}, |
| 87 | MetricValue{"aa", "bb", 42.0, 74}}}; |
Szymon Dompke | bcf045a | 2022-09-16 15:23:30 +0200 | [diff] [blame] | 88 | |
| 89 | ASSERT_THAT(readings.size(), Ge(metricParameters.size())); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 90 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 91 | for (size_t i = 0; i < metricParameters.size(); ++i) |
| 92 | { |
Krzysztof Grobelny | 9e8da54 | 2022-02-17 10:40:16 +0100 | [diff] [blame] | 93 | ON_CALL(*metricMocks[i], getUpdatedReadings()) |
| 94 | .WillByDefault(ReturnRefOfCopy(std::vector({readings[i]}))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 95 | ON_CALL(*metricMocks[i], dumpConfiguration()) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 96 | .WillByDefault(Return(metricParameters[i])); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 100 | std::vector<std::shared_ptr<interfaces::Metric>> |
| 101 | getMetricsFromReadingParams(const ReadingParameters& params) |
| 102 | { |
| 103 | const auto metricParameters = |
| 104 | reportFactoryMock->convertMetricParams(params); |
| 105 | std::vector<std::shared_ptr<MetricMock>> metricMocks; |
| 106 | |
| 107 | for (size_t i = 0; i < metricParameters.size(); ++i) |
| 108 | { |
| 109 | metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>()); |
| 110 | ON_CALL(*metricMocks[i], dumpConfiguration()) |
| 111 | .WillByDefault(Return(metricParameters[i])); |
| 112 | } |
| 113 | |
| 114 | return utils::convContainer<std::shared_ptr<interfaces::Metric>>( |
| 115 | metricMocks); |
| 116 | } |
| 117 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 118 | void SetUp() override |
| 119 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 120 | sut = makeReport(defaultParams()); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 123 | static interfaces::JsonStorage::FilePath to_file_path(std::string id) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 124 | { |
| 125 | return interfaces::JsonStorage::FilePath( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 126 | std::to_string(std::hash<std::string>{}(id))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 129 | std::unique_ptr<Report> makeReport(const ReportParams& params) |
| 130 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 131 | initMetricMocks(params.metricParameters()); |
| 132 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 133 | return std::make_unique<Report>( |
| 134 | DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 135 | params.reportId(), params.reportName(), params.reportingType(), |
| 136 | params.reportActions(), params.interval(), params.appendLimit(), |
| 137 | params.reportUpdates(), *reportManagerMock, storageMock, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 138 | utils::convContainer<std::shared_ptr<interfaces::Metric>>( |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 139 | metricMocks), |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 140 | *reportFactoryMock, params.enabled(), std::move(clockFakePtr), |
| 141 | params.readings()); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 142 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 143 | |
| 144 | template <class T> |
| 145 | static T getProperty(const std::string& path, const std::string& property) |
| 146 | { |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 147 | return DbusEnvironment::getProperty<T>(path, Report::reportIfaceName, |
| 148 | property); |
| 149 | } |
| 150 | |
| 151 | template <class T> |
| 152 | static boost::system::error_code setProperty(const std::string& path, |
| 153 | const std::string& property, |
| 154 | const T& newValue) |
| 155 | { |
| 156 | return DbusEnvironment::setProperty<T>(path, Report::reportIfaceName, |
| 157 | property, newValue); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 160 | template <class T> |
| 161 | struct ChangePropertyParams |
| 162 | { |
| 163 | Matcher<T> valueBefore = _; |
| 164 | T newValue; |
| 165 | Matcher<boost::system::error_code> ec = |
| 166 | Eq(boost::system::errc::success); |
| 167 | Matcher<T> valueAfter = Eq(newValue); |
| 168 | }; |
| 169 | |
| 170 | template <class T> |
| 171 | static void changeProperty(const std::string& path, |
| 172 | const std::string& property, |
| 173 | ChangePropertyParams<T> p) |
| 174 | { |
| 175 | ASSERT_THAT(getProperty<T>(path, property), p.valueBefore); |
| 176 | ASSERT_THAT(setProperty<T>(path, property, p.newValue), p.ec); |
| 177 | EXPECT_THAT(getProperty<T>(path, property), p.valueAfter); |
| 178 | } |
| 179 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 180 | boost::system::error_code call(const std::string& path, |
| 181 | const std::string& interface, |
| 182 | const std::string& method) |
| 183 | { |
| 184 | std::promise<boost::system::error_code> methodPromise; |
| 185 | DbusEnvironment::getBus()->async_method_call( |
| 186 | [&methodPromise](boost::system::error_code ec) { |
| 187 | methodPromise.set_value(ec); |
| 188 | }, |
| 189 | DbusEnvironment::serviceName(), path, interface, method); |
| 190 | return DbusEnvironment::waitForFuture(methodPromise.get_future()); |
| 191 | } |
| 192 | |
| 193 | boost::system::error_code update(const std::string& path) |
| 194 | { |
| 195 | return call(path, Report::reportIfaceName, "Update"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 198 | boost::system::error_code deleteReport(const std::string& path) |
| 199 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 200 | return call(path, Report::deleteIfaceName, "Delete"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 201 | } |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 202 | |
| 203 | static std::pair<std::string, std::vector<std::string>> |
| 204 | makeStateDetail(const std::string& detailType, |
| 205 | std::vector<std::string> detailArgs) |
| 206 | { |
| 207 | return make_pair(detailType, detailArgs); |
| 208 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 209 | }; |
| 210 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 211 | TEST_F(TestReport, returnsId) |
| 212 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 213 | EXPECT_THAT(sut->getId(), Eq(defaultParams().reportId())); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 216 | TEST_F(TestReport, verifyIfPropertiesHaveValidValue) |
| 217 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 218 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 219 | Eq(defaultParams().enabled())); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 220 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 221 | Eq(defaultParams().interval().count())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 222 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true)); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 223 | EXPECT_THAT( |
| 224 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 225 | Eq(utils::transform(defaultParams().reportActions(), [](const auto v) { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 226 | return utils::enumToString(v); |
| 227 | }))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 228 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 229 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 230 | ReportAction::emitsReadingsUpdate))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 231 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "AppendLimit"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 232 | Eq(defaultParams().appendLimit())); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 233 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 234 | Eq(utils::enumToString(defaultParams().reportingType()))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 235 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportUpdates"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 236 | Eq(utils::enumToString(defaultParams().reportUpdates()))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 237 | EXPECT_THAT( |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 238 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 239 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 240 | ReportAction::logToMetricReportsCollection))); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 241 | EXPECT_THAT(getProperty<ReadingParameters>( |
| 242 | sut->getPath(), "ReadingParametersFutureVersion"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 243 | Eq(toReadingParameters(defaultParams().metricParameters()))); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 244 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 245 | Eq(defaultParams().reportName())); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 246 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 247 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 248 | IsEmpty()); |
| 249 | EXPECT_THAT( |
| 250 | getProperty<ErrorMessagesDbusType>(sut->getPath(), "ErrorMessages"), |
| 251 | IsEmpty()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 252 | } |
| 253 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 254 | TEST_F(TestReport, readingsAreInitialyEmpty) |
| 255 | { |
| 256 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 257 | Eq(Readings{})); |
| 258 | } |
| 259 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 260 | TEST_F(TestReport, setReadingParametersWithNewParams) |
| 261 | { |
| 262 | ReadingParameters newParams = toReadingParameters( |
| 263 | std::vector<LabeledMetricParameters>{{LabeledMetricParameters{ |
| 264 | {LabeledSensorInfo{"Service", |
| 265 | "/xyz/openbmc_project/sensors/power/psu", |
| 266 | "NewMetadata123"}}, |
| 267 | OperationType::avg, |
| 268 | "NewMetricId123", |
| 269 | CollectionTimeScope::startup, |
| 270 | CollectionDuration(250ms)}}}); |
| 271 | auto metrics = getMetricsFromReadingParams(newParams); |
| 272 | |
| 273 | EXPECT_CALL(*reportFactoryMock, updateMetrics(_, _, _)) |
| 274 | .WillOnce(SetArgReferee<0>(metrics)); |
| 275 | EXPECT_THAT( |
| 276 | setProperty(sut->getPath(), "ReadingParametersFutureVersion", newParams) |
| 277 | .value(), |
| 278 | Eq(boost::system::errc::success)); |
| 279 | EXPECT_THAT(getProperty<ReadingParameters>( |
| 280 | sut->getPath(), "ReadingParametersFutureVersion"), |
| 281 | Eq(newParams)); |
| 282 | } |
| 283 | |
Szymon Dompke | 32305f1 | 2022-07-05 15:37:21 +0200 | [diff] [blame] | 284 | TEST_F(TestReport, setReadingParametersWithTooLongMetricId) |
| 285 | { |
| 286 | const ReadingParameters currentValue = |
| 287 | toReadingParameters(defaultParams().metricParameters()); |
| 288 | |
| 289 | ReadingParameters newParams = toReadingParameters( |
| 290 | std::vector<LabeledMetricParameters>{{LabeledMetricParameters{ |
| 291 | {LabeledSensorInfo{"Service", |
| 292 | "/xyz/openbmc_project/sensors/power/psu", |
| 293 | "NewMetadata123"}}, |
| 294 | OperationType::avg, |
| 295 | utils::string_utils::getTooLongId(), |
| 296 | CollectionTimeScope::startup, |
| 297 | CollectionDuration(250ms)}}}); |
| 298 | |
| 299 | changeProperty<ReadingParameters>( |
| 300 | sut->getPath(), "ReadingParametersFutureVersion", |
| 301 | {.valueBefore = Eq(currentValue), |
| 302 | .newValue = newParams, |
| 303 | .ec = Eq(boost::system::errc::invalid_argument), |
| 304 | .valueAfter = Eq(currentValue)}); |
| 305 | } |
| 306 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 307 | TEST_F(TestReport, setReportingTypeWithValidNewType) |
| 308 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 309 | changeProperty<std::string>( |
| 310 | sut->getPath(), "ReportingType", |
| 311 | {.valueBefore = Not(Eq(utils::enumToString(ReportingType::onRequest))), |
| 312 | .newValue = utils::enumToString(ReportingType::onRequest)}); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | TEST_F(TestReport, setReportingTypeWithInvalidType) |
| 316 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 317 | const std::string currentValue = |
| 318 | utils::enumToString(defaultParams().reportingType()); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 319 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 320 | changeProperty<std::string>( |
| 321 | sut->getPath(), "ReportingType", |
| 322 | {.valueBefore = Eq(currentValue), |
| 323 | .newValue = "Periodic_ABC", |
| 324 | .ec = Eq(boost::system::errc::invalid_argument), |
| 325 | .valueAfter = Eq(currentValue)}); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | TEST_F(TestReport, setReportActionsWithValidNewActions) |
| 329 | { |
| 330 | std::vector<std::string> newActions = {"EmitsReadingsUpdate"}; |
| 331 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 332 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 333 | [](const auto v) { return utils::enumToString(v); }); |
| 334 | |
| 335 | EXPECT_THAT(newActions, Ne(currActions)); |
| 336 | EXPECT_THAT( |
| 337 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 338 | Eq(boost::system::errc::success)); |
| 339 | EXPECT_THAT( |
| 340 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 341 | UnorderedElementsAre("EmitsReadingsUpdate", |
| 342 | "LogToMetricReportsCollection")); |
| 343 | } |
| 344 | |
| 345 | TEST_F(TestReport, setReportActionsWithValidUnsortedActions) |
| 346 | { |
| 347 | std::vector<std::string> newActions = {"LogToMetricReportsCollection", |
| 348 | "EmitsReadingsUpdate"}; |
| 349 | std::vector<std::string> expectedActions = {"EmitsReadingsUpdate", |
| 350 | "LogToMetricReportsCollection"}; |
| 351 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 352 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 353 | [](const auto v) { return utils::enumToString(v); }); |
| 354 | |
| 355 | EXPECT_THAT(newActions, Ne(currActions)); |
| 356 | EXPECT_THAT( |
| 357 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 358 | Eq(boost::system::errc::success)); |
| 359 | EXPECT_THAT( |
| 360 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 361 | Eq(expectedActions)); |
| 362 | } |
| 363 | |
| 364 | TEST_F(TestReport, setReportActionsWithEmptyActions) |
| 365 | { |
| 366 | std::vector<std::string> newActions = {}; |
| 367 | std::vector<std::string> expectedActions = {"LogToMetricReportsCollection"}; |
| 368 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 369 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 370 | [](const auto v) { return utils::enumToString(v); }); |
| 371 | |
| 372 | EXPECT_THAT(newActions, Ne(currActions)); |
| 373 | EXPECT_THAT( |
| 374 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 375 | Eq(boost::system::errc::success)); |
| 376 | EXPECT_THAT( |
| 377 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 378 | Eq(expectedActions)); |
| 379 | } |
| 380 | |
| 381 | TEST_F(TestReport, setReportActionsWithInvalidActions) |
| 382 | { |
| 383 | std::vector<std::string> invalidActions = {"EmitsReadingsUpdate_1"}; |
| 384 | EXPECT_THAT( |
| 385 | setProperty(sut->getPath(), "ReportActions", invalidActions).value(), |
| 386 | Eq(boost::system::errc::invalid_argument)); |
| 387 | EXPECT_THAT( |
| 388 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 389 | Eq(utils::transform(defaultParams().reportActions(), [](const auto v) { |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 390 | return utils::enumToString(v); |
| 391 | }))); |
| 392 | } |
| 393 | |
| 394 | TEST_F(TestReport, createReportWithEmptyActions) |
| 395 | { |
| 396 | std::vector<std::string> expectedActions = {"LogToMetricReportsCollection"}; |
| 397 | |
| 398 | sut = makeReport(ReportParams().reportId("TestId_1").reportActions({})); |
| 399 | EXPECT_THAT( |
| 400 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 401 | Eq(expectedActions)); |
| 402 | } |
| 403 | |
| 404 | TEST_F(TestReport, createReportWithValidUnsortedActions) |
| 405 | { |
| 406 | std::vector<std::string> newActions = {"LogToMetricReportsCollection", |
| 407 | "EmitsReadingsUpdate"}; |
| 408 | std::vector<std::string> expectedActions = {"EmitsReadingsUpdate", |
| 409 | "LogToMetricReportsCollection"}; |
| 410 | |
| 411 | sut = makeReport( |
| 412 | ReportParams() |
| 413 | .reportId("TestId_1") |
| 414 | .reportActions(utils::transform(newActions, [](const auto& action) { |
| 415 | return utils::toReportAction(action); |
| 416 | }))); |
| 417 | EXPECT_THAT( |
| 418 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 419 | Eq(expectedActions)); |
| 420 | } |
| 421 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 422 | TEST_F(TestReport, setEnabledWithNewValue) |
| 423 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 424 | bool newValue = !defaultParams().enabled(); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 425 | EXPECT_THAT(setProperty(sut->getPath(), "Enabled", newValue).value(), |
| 426 | Eq(boost::system::errc::success)); |
| 427 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(newValue)); |
| 428 | } |
| 429 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 430 | TEST_F(TestReport, setIntervalWithValidValue) |
| 431 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 432 | uint64_t newValue = ReportManager::minInterval.count() * 42; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 433 | EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 434 | Eq(boost::system::errc::success)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 435 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
| 436 | Eq(newValue)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 439 | TEST_F( |
| 440 | TestReport, |
| 441 | settingIntervalWithInvalidValueDoesNotChangePropertyAndReturnsInvalidArgument) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 442 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 443 | uint64_t newValue = ReportManager::minInterval.count() - 1; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 444 | EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(), |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 445 | Eq(boost::system::errc::invalid_argument)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 446 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 447 | Eq(defaultParams().interval().count())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 450 | TEST_F(TestReport, settingInvalidReportingTypeCreatesErrorMessage) |
| 451 | { |
| 452 | auto report = makeReport(defaultParams() |
| 453 | .reportId("report2") |
| 454 | .reportingType(ReportingType::onRequest) |
| 455 | .interval(Milliseconds{0})); |
| 456 | |
| 457 | EXPECT_THAT( |
| 458 | setProperty<std::string>(report->getPath(), "ReportingType", "Periodic") |
| 459 | .value(), |
| 460 | Eq(boost::system::errc::success)); |
| 461 | |
| 462 | EXPECT_THAT(getProperty<std::string>(report->getPath(), "ReportingType"), |
| 463 | Eq("Periodic")); |
| 464 | EXPECT_THAT( |
| 465 | getProperty<ErrorMessagesDbusType>(report->getPath(), "ErrorMessages"), |
| 466 | UnorderedElementsAre( |
| 467 | ErrorMessageDbusType( |
| 468 | utils::enumToString(ErrorType::propertyConflict), "Interval"), |
| 469 | ErrorMessageDbusType( |
| 470 | utils::enumToString(ErrorType::propertyConflict), |
| 471 | "ReportingType"))); |
| 472 | } |
| 473 | |
| 474 | TEST_F(TestReport, settingValidReportingTypeRemovesErrors) |
| 475 | { |
| 476 | auto report = makeReport(defaultParams() |
| 477 | .reportId("report2") |
| 478 | .reportingType(ReportingType::onRequest) |
| 479 | .interval(Milliseconds{0})); |
| 480 | |
| 481 | EXPECT_THAT( |
| 482 | setProperty<std::string>(report->getPath(), "ReportingType", "Periodic") |
| 483 | .value(), |
| 484 | Eq(boost::system::errc::success)); |
| 485 | EXPECT_THAT(setProperty<std::string>(report->getPath(), "ReportingType", |
| 486 | "OnRequest") |
| 487 | .value(), |
| 488 | Eq(boost::system::errc::success)); |
| 489 | |
| 490 | EXPECT_THAT(getProperty<std::string>(report->getPath(), "ReportingType"), |
| 491 | Eq("OnRequest")); |
| 492 | EXPECT_THAT( |
| 493 | getProperty<ErrorMessagesDbusType>(report->getPath(), "ErrorMessages"), |
| 494 | IsEmpty()); |
| 495 | } |
| 496 | |
| 497 | TEST_F(TestReport, settingInvalidIntervalDisablesReport) |
| 498 | { |
| 499 | auto report = makeReport(defaultParams() |
| 500 | .reportId("report2") |
| 501 | .reportingType(ReportingType::periodic) |
| 502 | .interval(ReportManager::minInterval)); |
| 503 | |
| 504 | EXPECT_THAT(setProperty<uint64_t>(report->getPath(), "Interval", 0).value(), |
| 505 | Eq(boost::system::errc::success)); |
| 506 | |
| 507 | EXPECT_THAT(getProperty<uint64_t>(report->getPath(), "Interval"), Eq(0u)); |
| 508 | EXPECT_THAT( |
| 509 | getProperty<ErrorMessagesDbusType>(report->getPath(), "ErrorMessages"), |
| 510 | UnorderedElementsAre( |
| 511 | ErrorMessageDbusType( |
| 512 | utils::enumToString(ErrorType::propertyConflict), "Interval"), |
| 513 | ErrorMessageDbusType( |
| 514 | utils::enumToString(ErrorType::propertyConflict), |
| 515 | "ReportingType"))); |
| 516 | } |
| 517 | |
| 518 | TEST_F(TestReport, settingValidIntervalEnablesReport) |
| 519 | { |
| 520 | auto report = makeReport(defaultParams() |
| 521 | .reportId("report2") |
| 522 | .reportingType(ReportingType::periodic) |
| 523 | .interval(ReportManager::minInterval)); |
| 524 | |
| 525 | EXPECT_THAT(setProperty<uint64_t>(report->getPath(), "Interval", 0).value(), |
| 526 | Eq(boost::system::errc::success)); |
| 527 | EXPECT_THAT(setProperty<uint64_t>(report->getPath(), "Interval", |
| 528 | ReportManager::minInterval.count()) |
| 529 | .value(), |
| 530 | Eq(boost::system::errc::success)); |
| 531 | |
| 532 | EXPECT_THAT(getProperty<uint64_t>(report->getPath(), "Interval"), |
| 533 | Eq(ReportManager::minInterval.count())); |
| 534 | EXPECT_THAT( |
| 535 | getProperty<ErrorMessagesDbusType>(report->getPath(), "ErrorMessages"), |
| 536 | IsEmpty()); |
| 537 | } |
| 538 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 539 | TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect) |
| 540 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 541 | EXPECT_THAT( |
| 542 | setProperty(sut->getPath(), "EmitsReadingsUpdate", true).value(), |
| 543 | Eq(boost::system::errc::read_only_file_system)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 544 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 545 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 546 | ReportAction::emitsReadingsUpdate))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect) |
| 550 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 551 | EXPECT_THAT( |
| 552 | setProperty(sut->getPath(), "LogToMetricReportsCollection", true) |
| 553 | .value(), |
| 554 | Eq(boost::system::errc::read_only_file_system)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 555 | EXPECT_THAT( |
| 556 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 557 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 558 | ReportAction::logToMetricReportsCollection))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 561 | TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage) |
| 562 | { |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 563 | EXPECT_CALL(storageMock, store(_, _)).Times(0); |
| 564 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))) |
| 565 | .Times(AtLeast(1)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 566 | |
| 567 | bool persistency = false; |
| 568 | EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(), |
| 569 | Eq(boost::system::errc::success)); |
| 570 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), |
| 571 | Eq(persistency)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | TEST_F(TestReport, deleteReport) |
| 575 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 576 | EXPECT_CALL(*reportManagerMock, removeReport(sut.get())); |
| 577 | auto ec = deleteReport(sut->getPath()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 578 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 579 | } |
| 580 | |
| 581 | TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor) |
| 582 | { |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 583 | auto ec = |
| 584 | deleteReport(utils::constants::reportDirPath.str + "NonExisting"s); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 585 | EXPECT_THAT(ec.value(), Eq(EBADR)); |
| 586 | } |
| 587 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 588 | TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage) |
| 589 | { |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 590 | EXPECT_CALL(storageMock, store(_, _)).Times(0); |
| 591 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))) |
| 592 | .Times(AtLeast(1)); |
| 593 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 594 | auto ec = deleteReport(sut->getPath()); |
| 595 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 596 | } |
| 597 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 598 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsAdded) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 599 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 600 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 601 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 602 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 603 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 604 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 605 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 606 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 607 | messages::Presence::Exist, "trigger2", {"someOtherReport"}}); |
| 608 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 609 | messages::Presence::Exist, |
| 610 | "trigger3", |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 611 | {"someOtherReport", defaultParams().reportId()}}); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 612 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 613 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 614 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
| 615 | UnorderedElementsAre(utils::constants::triggerDirPath / "trigger1", |
| 616 | utils::constants::triggerDirPath / "trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 617 | } |
| 618 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 619 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsRemoved) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 620 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 621 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
| 622 | |
| 623 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 624 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 625 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 626 | messages::Presence::Exist, "trigger2", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 627 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 628 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 629 | |
| 630 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 631 | messages::Presence::Removed, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 632 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 633 | messages::Presence::Removed, "trigger2", {}}); |
| 634 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 635 | messages::Presence::Removed, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 636 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 637 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 638 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
| 639 | UnorderedElementsAre(utils::constants::triggerDirPath / "trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 640 | } |
| 641 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 642 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsModified) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 643 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 644 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 645 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 646 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 647 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 648 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 649 | messages::Presence::Exist, "trigger2", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 650 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 651 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 652 | |
| 653 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 654 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 655 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 656 | messages::Presence::Exist, "trigger2", {}}); |
| 657 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 658 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 659 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 660 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 661 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
| 662 | UnorderedElementsAre(utils::constants::triggerDirPath / "trigger1", |
| 663 | utils::constants::triggerDirPath / "trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 666 | class TestReportStore : |
| 667 | public TestReport, |
| 668 | public WithParamInterface<std::pair<std::string, nlohmann::json>> |
| 669 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 670 | void SetUp() override |
| 671 | {} |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 672 | }; |
| 673 | |
| 674 | INSTANTIATE_TEST_SUITE_P( |
| 675 | _, TestReportStore, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 676 | Values( |
| 677 | std::make_pair("Enabled"s, nlohmann::json(defaultParams().enabled())), |
| 678 | std::make_pair("Version"s, nlohmann::json(6)), |
| 679 | std::make_pair("Id"s, nlohmann::json(defaultParams().reportId())), |
| 680 | std::make_pair("Name"s, nlohmann::json(defaultParams().reportName())), |
| 681 | std::make_pair("ReportingType", |
| 682 | nlohmann::json(defaultParams().reportingType())), |
| 683 | std::make_pair("ReportActions", nlohmann::json(utils::transform( |
| 684 | defaultParams().reportActions(), |
| 685 | [](const auto v) { |
| 686 | return utils::toUnderlying(v); |
| 687 | }))), |
| 688 | std::make_pair("Interval", |
| 689 | nlohmann::json(defaultParams().interval().count())), |
| 690 | std::make_pair("AppendLimit", |
| 691 | nlohmann::json(ReportParams().appendLimit())), |
| 692 | std::make_pair( |
| 693 | "ReadingParameters", |
| 694 | nlohmann::json( |
| 695 | {{{tstring::SensorPath::str(), |
| 696 | {{{tstring::Service::str(), "Service"}, |
| 697 | {tstring::Path::str(), |
| 698 | "/xyz/openbmc_project/sensors/power/p1"}, |
| 699 | {tstring::Metadata::str(), "metadata1"}}}}, |
| 700 | {tstring::OperationType::str(), OperationType::avg}, |
| 701 | {tstring::Id::str(), "MetricId1"}, |
| 702 | {tstring::CollectionTimeScope::str(), |
| 703 | CollectionTimeScope::point}, |
| 704 | {tstring::CollectionDuration::str(), 0}}, |
| 705 | {{tstring::SensorPath::str(), |
| 706 | {{{tstring::Service::str(), "Service"}, |
| 707 | {tstring::Path::str(), |
| 708 | "/xyz/openbmc_project/sensors/power/p2"}, |
| 709 | {tstring::Metadata::str(), "metadata2"}}}}, |
| 710 | {tstring::OperationType::str(), OperationType::avg}, |
| 711 | {tstring::Id::str(), "MetricId2"}, |
| 712 | {tstring::CollectionTimeScope::str(), |
| 713 | CollectionTimeScope::point}, |
| 714 | {tstring::CollectionDuration::str(), 0}}})))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 715 | |
| 716 | TEST_P(TestReportStore, settingPersistencyToTrueStoresReport) |
| 717 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 718 | sut = makeReport(defaultParams()); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 719 | |
| 720 | { |
| 721 | InSequence seq; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 722 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 723 | EXPECT_CALL(checkPoint, Call()); |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 724 | EXPECT_CALL(storageMock, store(to_file_path(sut->getId()), _)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | setProperty(sut->getPath(), "Persistency", false); |
| 728 | checkPoint.Call(); |
| 729 | setProperty(sut->getPath(), "Persistency", true); |
| 730 | |
| 731 | const auto& [key, value] = GetParam(); |
| 732 | |
| 733 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 734 | } |
| 735 | |
| 736 | TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated) |
| 737 | { |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 738 | EXPECT_CALL(storageMock, |
| 739 | store(to_file_path(defaultParams().reportId()), _)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 740 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 741 | sut = makeReport(defaultParams()); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 742 | |
| 743 | const auto& [key, value] = GetParam(); |
| 744 | |
| 745 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 746 | } |
| 747 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 748 | class TestReportValidNames : |
| 749 | public TestReport, |
| 750 | public WithParamInterface<ReportParams> |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 751 | { |
| 752 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 753 | void SetUp() override |
| 754 | {} |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 755 | }; |
| 756 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 757 | INSTANTIATE_TEST_SUITE_P( |
| 758 | ValidNames, TestReportValidNames, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 759 | Values(defaultParams().reportName("Valid_1"), |
| 760 | defaultParams().reportName("Valid_1/Valid_2"), |
| 761 | defaultParams().reportName("Valid_1/Valid_2/Valid_3"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 762 | |
| 763 | TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName) |
| 764 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 765 | EXPECT_NO_THROW(makeReport(GetParam())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 768 | class TestReportInvalidIds : |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 769 | public TestReport, |
| 770 | public WithParamInterface<ReportParams> |
| 771 | { |
| 772 | public: |
| 773 | void SetUp() override |
| 774 | {} |
| 775 | }; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 776 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 777 | INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidIds, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 778 | Values(defaultParams().reportId("/"), |
| 779 | defaultParams().reportId("/Invalid"), |
| 780 | defaultParams().reportId("Invalid/"), |
| 781 | defaultParams().reportId("Invalid/Invalid/"), |
| 782 | defaultParams().reportId("Invalid?"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 783 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 784 | TEST_P(TestReportInvalidIds, failsToCreateReportWithInvalidName) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 785 | { |
| 786 | EXPECT_CALL(storageMock, store).Times(0); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 787 | |
| 788 | EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 791 | class TestReportAllReportTypes : |
| 792 | public TestReport, |
| 793 | public WithParamInterface<ReportParams> |
| 794 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 795 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 796 | void SetUp() override |
| 797 | { |
| 798 | sut = makeReport(GetParam()); |
| 799 | } |
| 800 | }; |
| 801 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 802 | INSTANTIATE_TEST_SUITE_P( |
| 803 | _, TestReportAllReportTypes, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 804 | Values(defaultParams().reportingType(ReportingType::onRequest), |
| 805 | defaultParams().reportingType(ReportingType::onChange), |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 806 | defaultParams() |
| 807 | .reportingType(ReportingType::periodic) |
| 808 | .interval(ReportManager::minInterval))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 809 | |
| 810 | TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType) |
| 811 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 812 | EXPECT_THAT(utils::toReportingType( |
| 813 | getProperty<std::string>(sut->getPath(), "ReportingType")), |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 814 | Eq(GetParam().reportingType())); |
| 815 | } |
| 816 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 817 | TEST_P(TestReportAllReportTypes, readingsAreUpdated) |
| 818 | { |
| 819 | clockFake.system.advance(10ms); |
| 820 | |
| 821 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
| 822 | const auto [timestamp, readings] = |
| 823 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 824 | |
| 825 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
| 826 | } |
| 827 | |
| 828 | TEST_P(TestReportAllReportTypes, readingsAreNotUpdatedWhenReportIsDisabled) |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 829 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 830 | clockFake.system.advance(10ms); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 831 | |
| 832 | setProperty(sut->getPath(), "Enabled", false); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 833 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 834 | const auto [timestamp, readings] = |
| 835 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 836 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 837 | EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms)); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 840 | TEST_P(TestReportAllReportTypes, readingsAreNotUpdatedWhenReportIdDiffers) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 841 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 842 | clockFake.system.advance(10ms); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 843 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 844 | messanger.send(messages::UpdateReportInd{{sut->getId() + "x"s}}); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 845 | const auto [timestamp, readings] = |
| 846 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 847 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 848 | EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms)); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 849 | } |
| 850 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 851 | class TestReportOnRequestType : public TestReport |
| 852 | { |
| 853 | void SetUp() override |
| 854 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 855 | sut = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 856 | makeReport(defaultParams().reportingType(ReportingType::onRequest)); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 857 | } |
| 858 | }; |
| 859 | |
| 860 | TEST_F(TestReportOnRequestType, updatesReadingTimestamp) |
| 861 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 862 | clockFake.system.advance(10ms); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 863 | |
| 864 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 865 | |
| 866 | const auto [timestamp, readings] = |
| 867 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 868 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 869 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled) |
| 873 | { |
| 874 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 875 | |
| 876 | const auto [timestamp, readings] = |
| 877 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 878 | |
| 879 | EXPECT_THAT(readings, |
| 880 | ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 881 | std::make_tuple("aa"s, "bb"s, 42.0, 74u))); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | class TestReportNonOnRequestType : |
| 885 | public TestReport, |
| 886 | public WithParamInterface<ReportParams> |
| 887 | { |
| 888 | void SetUp() override |
| 889 | { |
| 890 | sut = makeReport(GetParam()); |
| 891 | } |
| 892 | }; |
| 893 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 894 | INSTANTIATE_TEST_SUITE_P( |
| 895 | _, TestReportNonOnRequestType, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 896 | Values(defaultParams().reportingType(ReportingType::periodic), |
| 897 | defaultParams().reportingType(ReportingType::onChange))); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 898 | |
| 899 | TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall) |
| 900 | { |
| 901 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 902 | |
| 903 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 904 | Eq(Readings{})); |
| 905 | } |
| 906 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 907 | class TestReportNonPeriodicReport : |
| 908 | public TestReport, |
| 909 | public WithParamInterface<ReportParams> |
| 910 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 911 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 912 | void SetUp() override |
| 913 | { |
| 914 | sut = makeReport(GetParam()); |
| 915 | } |
| 916 | }; |
| 917 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 918 | INSTANTIATE_TEST_SUITE_P( |
| 919 | _, TestReportNonPeriodicReport, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 920 | Values(defaultParams().reportingType(ReportingType::onRequest), |
| 921 | defaultParams().reportingType(ReportingType::onChange))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 922 | |
| 923 | TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires) |
| 924 | { |
| 925 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 926 | |
| 927 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 928 | Eq(Readings{})); |
| 929 | } |
| 930 | |
| 931 | class TestReportPeriodicReport : public TestReport |
| 932 | { |
| 933 | void SetUp() override |
| 934 | { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 935 | sut = makeReport(defaultParams() |
| 936 | .reportingType(ReportingType::periodic) |
| 937 | .interval(ReportManager::minInterval)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 938 | } |
| 939 | }; |
| 940 | |
| 941 | TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires) |
| 942 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 943 | clockFake.system.advance(10ms); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 944 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 945 | |
| 946 | const auto [timestamp, readings] = |
| 947 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 948 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 949 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires) |
| 953 | { |
| 954 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 955 | |
| 956 | const auto [timestamp, readings] = |
| 957 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 958 | |
| 959 | EXPECT_THAT(readings, |
| 960 | ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 961 | std::make_tuple("aa"s, "bb"s, 42.0, 74u))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 962 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 963 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 964 | struct ReportUpdatesReportParams |
| 965 | { |
| 966 | ReportParams reportParams; |
| 967 | std::vector<ReadingData> expectedReadings; |
| 968 | bool expectedEnabled; |
| 969 | }; |
| 970 | |
| 971 | class TestReportWithReportUpdatesAndLimit : |
| 972 | public TestReport, |
| 973 | public WithParamInterface<ReportUpdatesReportParams> |
| 974 | { |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 975 | public: |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 976 | void SetUp() override |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 977 | {} |
| 978 | |
| 979 | void changeReport(ReportingType rt, Milliseconds interval) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 980 | { |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 981 | setProperty<std::string>(sut->getPath(), "ReportingType", |
| 982 | utils::enumToString(rt)); |
| 983 | setProperty<uint64_t>(sut->getPath(), "Interval", interval.count()); |
| 984 | } |
| 985 | |
| 986 | auto readings() |
| 987 | { |
| 988 | auto [timestamp, readings] = |
| 989 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 990 | return readings; |
| 991 | } |
| 992 | |
| 993 | void updateReportFourTimes() |
| 994 | { |
| 995 | for (int i = 0; i < 4; i++) |
| 996 | { |
| 997 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
| 998 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 999 | } |
| 1000 | }; |
| 1001 | |
| 1002 | INSTANTIATE_TEST_SUITE_P( |
| 1003 | _, TestReportWithReportUpdatesAndLimit, |
| 1004 | Values( |
| 1005 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1006 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1007 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 1008 | .appendLimit(5), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1009 | std::vector<ReadingData>{{std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1010 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1011 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1012 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1013 | std::make_tuple("a"s, "b"s, 17.1, 114u)}}, |
| 1014 | true}, |
| 1015 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1016 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1017 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 1018 | .appendLimit(4), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1019 | std::vector<ReadingData>{ |
| 1020 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1021 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1022 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1023 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 1024 | true}, |
| 1025 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1026 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1027 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 1028 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1029 | std::vector<ReadingData>{}, true}, |
| 1030 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1031 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1032 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1033 | .appendLimit(10), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1034 | std::vector<ReadingData>{ |
| 1035 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1036 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1037 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1038 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1039 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1040 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1041 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1042 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 1043 | true}, |
| 1044 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1045 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1046 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1047 | .appendLimit(5), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1048 | std::vector<ReadingData>{{std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1049 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1050 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1051 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1052 | std::make_tuple("a"s, "b"s, 17.1, 114u)}}, |
| 1053 | false}, |
| 1054 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1055 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1056 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1057 | .appendLimit(4), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1058 | std::vector<ReadingData>{ |
| 1059 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1060 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 1061 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1062 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 1063 | false}, |
| 1064 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1065 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1066 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1067 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1068 | std::vector<ReadingData>{}, false}, |
| 1069 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1070 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1071 | .reportUpdates(ReportUpdates::overwrite) |
| 1072 | .appendLimit(500), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1073 | std::vector<ReadingData>{ |
| 1074 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1075 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 1076 | true}, |
| 1077 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1078 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1079 | .reportUpdates(ReportUpdates::overwrite) |
| 1080 | .appendLimit(1), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1081 | std::vector<ReadingData>{ |
| 1082 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1083 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 1084 | true}, |
| 1085 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1086 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1087 | .reportUpdates(ReportUpdates::overwrite) |
| 1088 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1089 | std::vector<ReadingData>{ |
| 1090 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1091 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1092 | true}, |
| 1093 | ReportUpdatesReportParams{ |
| 1094 | defaultParams() |
| 1095 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1096 | .appendLimit(std::numeric_limits<uint64_t>::max()), |
| 1097 | std::vector<ReadingData>{ |
| 1098 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 1099 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 1100 | false})); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1101 | |
| 1102 | TEST_P(TestReportWithReportUpdatesAndLimit, |
| 1103 | readingsAreUpdatedAfterIntervalExpires) |
| 1104 | { |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1105 | sut = makeReport(ReportParams(GetParam().reportParams) |
| 1106 | .reportingType(ReportingType::periodic) |
| 1107 | .interval(std::chrono::hours(1000))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1108 | |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1109 | updateReportFourTimes(); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1110 | |
Krzysztof Grobelny | a8182be | 2022-07-04 11:26:20 +0200 | [diff] [blame] | 1111 | EXPECT_THAT(readings(), ElementsAreArray(GetParam().expectedReadings)); |
| 1112 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), |
| 1113 | Eq(GetParam().expectedEnabled)); |
| 1114 | } |
| 1115 | |
| 1116 | TEST_P(TestReportWithReportUpdatesAndLimit, |
| 1117 | appendLimitIsRespectedAfterChangingToPeriodic) |
| 1118 | { |
| 1119 | sut = makeReport(ReportParams(GetParam().reportParams) |
| 1120 | .reportingType(ReportingType::onRequest) |
| 1121 | .interval(std::chrono::hours(0))); |
| 1122 | |
| 1123 | changeReport(ReportingType::periodic, std::chrono::hours(1000)); |
| 1124 | updateReportFourTimes(); |
| 1125 | |
| 1126 | EXPECT_THAT(readings(), ElementsAreArray(GetParam().expectedReadings)); |
| 1127 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), |
| 1128 | Eq(GetParam().expectedEnabled)); |
| 1129 | } |
| 1130 | |
| 1131 | TEST_P(TestReportWithReportUpdatesAndLimit, |
| 1132 | appendLimitIsIgnoredAfterChangingToOnRequest) |
| 1133 | { |
| 1134 | sut = makeReport(ReportParams(GetParam().reportParams) |
| 1135 | .reportingType(ReportingType::periodic) |
| 1136 | .interval(std::chrono::hours(1000))); |
| 1137 | |
| 1138 | changeReport(ReportingType::onRequest, Milliseconds{0}); |
| 1139 | updateReportFourTimes(); |
| 1140 | |
| 1141 | EXPECT_THAT(readings(), SizeIs(2u)); |
| 1142 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(true)); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1145 | class TestReportInitialization : public TestReport |
| 1146 | { |
| 1147 | public: |
| 1148 | void SetUp() override |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1149 | { |
| 1150 | initMetricMocks(defaultParams().metricParameters()); |
| 1151 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1152 | |
Patrick Williams | 39cc6ac | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 1153 | void monitorProc(sdbusplus::message_t& msg) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1154 | { |
| 1155 | std::string iface; |
| 1156 | std::vector<std::pair<std::string, std::variant<Readings>>> |
| 1157 | changed_properties; |
| 1158 | std::vector<std::string> invalidated_properties; |
| 1159 | |
| 1160 | msg.read(iface, changed_properties, invalidated_properties); |
| 1161 | |
| 1162 | if (iface == Report::reportIfaceName) |
| 1163 | { |
| 1164 | for (const auto& [name, value] : changed_properties) |
| 1165 | { |
| 1166 | if (name == "Readings") |
| 1167 | { |
| 1168 | readingsUpdated.Call(); |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | void makeMonitor() |
| 1175 | { |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 1176 | monitor = std::make_unique<sdbusplus::bus::match_t>( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1177 | *DbusEnvironment::getBus(), |
| 1178 | sdbusplus::bus::match::rules::propertiesChanged( |
| 1179 | sut->getPath(), Report::reportIfaceName), |
| 1180 | [this](auto& msg) { monitorProc(msg); }); |
| 1181 | } |
| 1182 | |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 1183 | std::unique_ptr<sdbusplus::bus::match_t> monitor; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1184 | MockFunction<void()> readingsUpdated; |
| 1185 | }; |
| 1186 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1187 | TEST_F(TestReportInitialization, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1188 | registersForMetricUpdatesWhenOnChangeReportCreated) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1189 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1190 | std::vector<const interfaces::MetricListener*> args; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1191 | for (auto& metric : metricMocks) |
| 1192 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1193 | EXPECT_CALL(*metric, registerForUpdates(_)) |
| 1194 | .WillOnce(Invoke([&args](const interfaces::MetricListener& report) { |
| 1195 | args.emplace_back(&report); |
| 1196 | })); |
| 1197 | ; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1198 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1199 | |
| 1200 | sut = makeReport(defaultParams().reportingType(ReportingType::onChange)); |
| 1201 | |
| 1202 | EXPECT_THAT(args, SizeIs(metricMocks.size())); |
| 1203 | for (const auto* reportPtr : args) |
| 1204 | { |
| 1205 | EXPECT_THAT(reportPtr, Eq(sut.get())); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | TEST_F(TestReportInitialization, |
| 1210 | deregistersForMetricUpdatesWhenOnChangeReportDestroyed) |
| 1211 | { |
| 1212 | sut = makeReport(defaultParams().reportingType(ReportingType::onChange)); |
| 1213 | |
| 1214 | for (auto& metric : metricMocks) |
| 1215 | { |
| 1216 | EXPECT_CALL(*metric, |
| 1217 | unregisterFromUpdates(Ref( |
| 1218 | static_cast<interfaces::MetricListener&>(*sut.get())))); |
| 1219 | } |
| 1220 | |
| 1221 | sut = nullptr; |
| 1222 | } |
| 1223 | |
| 1224 | TEST_F(TestReportInitialization, |
| 1225 | metricsAreInitializedWhenEnabledReportConstructed) |
| 1226 | { |
| 1227 | for (auto& metric : metricMocks) |
| 1228 | { |
| 1229 | EXPECT_CALL(*metric, initialize()); |
| 1230 | } |
| 1231 | sut = makeReport(defaultParams().enabled(true)); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1232 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1233 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1234 | TEST_F(TestReportInitialization, |
| 1235 | metricsAreNotInitializedWhenDisabledReportConstructed) |
| 1236 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1237 | for (auto& metric : metricMocks) |
| 1238 | { |
| 1239 | EXPECT_CALL(*metric, initialize()).Times(0); |
| 1240 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1241 | sut = makeReport(defaultParams().enabled(false)); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1242 | } |
| 1243 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1244 | TEST_F(TestReportInitialization, |
| 1245 | emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1246 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1247 | EXPECT_CALL(readingsUpdated, Call()) |
| 1248 | .WillOnce( |
| 1249 | InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated"))); |
| 1250 | |
| 1251 | const auto elapsed = DbusEnvironment::measureTime([this] { |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 1252 | sut = makeReport(defaultParams() |
| 1253 | .reportingType(ReportingType::periodic) |
| 1254 | .reportActions({ReportAction::emitsReadingsUpdate}) |
| 1255 | .interval(ReportManager::minInterval)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1256 | makeMonitor(); |
| 1257 | EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated")); |
| 1258 | }); |
| 1259 | |
Krzysztof Grobelny | 973b4bb | 2022-04-25 17:07:27 +0200 | [diff] [blame] | 1260 | EXPECT_THAT(elapsed, AllOf(Ge(ReportManager::minInterval), |
| 1261 | Lt(ReportManager::minInterval * 2))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | TEST_F(TestReportInitialization, |
| 1265 | emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits) |
| 1266 | { |
| 1267 | EXPECT_CALL(readingsUpdated, Call()).Times(0); |
| 1268 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1269 | sut = makeReport(defaultParams() |
| 1270 | .reportingType(ReportingType::periodic) |
| 1271 | .reportActions({})); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1272 | makeMonitor(); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1273 | DbusEnvironment::sleepFor(defaultParams().interval() * 2); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1274 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1275 | |
| 1276 | TEST_F(TestReportInitialization, appendLimitDeducedProperly) |
| 1277 | { |
| 1278 | sut = makeReport( |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1279 | defaultParams().appendLimit(std::numeric_limits<uint64_t>::max())); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1280 | auto appendLimit = getProperty<uint64_t>(sut->getPath(), "AppendLimit"); |
| 1281 | EXPECT_EQ(appendLimit, 2ull); |
| 1282 | } |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 1283 | |
| 1284 | TEST_F(TestReportInitialization, appendLimitSetToUintMaxIsStoredCorrectly) |
| 1285 | { |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 1286 | sut = makeReport( |
| 1287 | ReportParams().appendLimit(std::numeric_limits<uint64_t>::max())); |
| 1288 | |
| 1289 | ASSERT_THAT(storedConfiguration.at("AppendLimit"), |
| 1290 | Eq(std::numeric_limits<uint64_t>::max())); |
| 1291 | } |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 1292 | |
| 1293 | TEST_F(TestReportInitialization, triggerIdsPropertyIsInitialzed) |
| 1294 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 1295 | for (const auto& triggerId : {"trigger1", "trigger2"}) |
| 1296 | { |
| 1297 | messanger.on_receive<messages::CollectTriggerIdReq>( |
Szymon Dompke | e0ed508 | 2022-05-20 10:54:25 +0200 | [diff] [blame] | 1298 | [this, triggerId](const auto& msg) { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 1299 | messanger.send(messages::CollectTriggerIdResp{triggerId}); |
| 1300 | }); |
| 1301 | } |
| 1302 | |
| 1303 | sut = makeReport(ReportParams()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 1304 | |
| 1305 | EXPECT_THAT( |
Szymon Dompke | 1cdd7e4 | 2022-06-08 14:43:13 +0200 | [diff] [blame] | 1306 | getProperty<std::vector<object_path>>(sut->getPath(), "Triggers"), |
| 1307 | UnorderedElementsAre(utils::constants::triggerDirPath / "trigger1", |
| 1308 | utils::constants::triggerDirPath / "trigger2")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 1309 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1310 | |
Krzysztof Grobelny | 493e62e | 2022-02-14 10:55:50 +0100 | [diff] [blame] | 1311 | TEST_F(TestReportInitialization, |
| 1312 | metricValuesAreNotStoredForReportUpdatesDifferentThanAppendStopsWhenFull) |
| 1313 | { |
| 1314 | sut = makeReport(ReportParams() |
| 1315 | .reportingType(ReportingType::periodic) |
| 1316 | .interval(1h) |
| 1317 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 1318 | .readings(Readings{{}, {{}}})); |
| 1319 | |
| 1320 | ASSERT_THAT(storedConfiguration.find("MetricValues"), |
| 1321 | Eq(storedConfiguration.end())); |
| 1322 | } |
| 1323 | |
| 1324 | TEST_F(TestReportInitialization, metricValuesAreNotStoredForOnRequestReport) |
| 1325 | { |
| 1326 | sut = makeReport(ReportParams() |
| 1327 | .reportingType(ReportingType::onRequest) |
| 1328 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1329 | .readings(Readings{{}, {{}}})); |
| 1330 | |
| 1331 | ASSERT_THAT(storedConfiguration.find("MetricValues"), |
| 1332 | Eq(storedConfiguration.end())); |
| 1333 | } |
| 1334 | |
| 1335 | TEST_F(TestReportInitialization, |
| 1336 | metricValuesAreStoredForNonOnRequestReportWithAppendStopsWhenFull) |
| 1337 | { |
| 1338 | const auto readings = Readings{{}, {{}}}; |
| 1339 | |
| 1340 | sut = makeReport(ReportParams() |
| 1341 | .reportingType(ReportingType::periodic) |
| 1342 | .interval(1h) |
| 1343 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 1344 | .readings(readings)); |
| 1345 | |
| 1346 | ASSERT_THAT(storedConfiguration.at("MetricValues").get<LabeledReadings>(), |
| 1347 | Eq(utils::toLabeledReadings(readings))); |
| 1348 | } |
| 1349 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame] | 1350 | class TestReportInitializationOnChangeReport : public TestReportInitialization |
| 1351 | { |
| 1352 | public: |
| 1353 | void SetUp() override |
| 1354 | { |
| 1355 | initMetricMocks(params.metricParameters()); |
| 1356 | } |
| 1357 | |
| 1358 | ReportParams params = defaultOnChangeParams(); |
| 1359 | }; |
| 1360 | |
| 1361 | TEST_F(TestReportInitializationOnChangeReport, |
| 1362 | doesntUpdateReadingsWhenNotRequired) |
| 1363 | { |
| 1364 | EXPECT_CALL(*metricMocks[0], updateReadings(_)).Times(0); |
| 1365 | |
| 1366 | ON_CALL(*metricMocks[0], isTimerRequired()).WillByDefault(Return(false)); |
| 1367 | |
| 1368 | sut = makeReport(params); |
| 1369 | |
| 1370 | DbusEnvironment::sleepFor(500ms); |
| 1371 | } |
| 1372 | |
| 1373 | TEST_F(TestReportInitializationOnChangeReport, updatesReadingsWhenRequired) |
| 1374 | { |
| 1375 | EXPECT_CALL(*metricMocks[0], updateReadings(_)) |
| 1376 | .WillOnce(Return()) |
| 1377 | .WillOnce( |
| 1378 | InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated"))) |
| 1379 | .WillRepeatedly(Return()); |
| 1380 | |
| 1381 | ON_CALL(*metricMocks[0], isTimerRequired()).WillByDefault(Return(true)); |
| 1382 | |
| 1383 | sut = makeReport(params); |
| 1384 | |
| 1385 | DbusEnvironment::waitForFuture("readingsUpdated"); |
| 1386 | } |