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