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