blob: 5378f48416e8ca95842a200e097dc06a62afaae1 [file] [log] [blame]
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02001#include "dbus_environment.hpp"
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +00002#include "helpers.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01003#include "mocks/json_storage_mock.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02004#include "mocks/metric_mock.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02005#include "mocks/report_manager_mock.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02006#include "params/report_params.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02007#include "report.hpp"
8#include "report_manager.hpp"
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +02009#include "utils/conv_container.hpp"
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010010#include "utils/set_exception.hpp"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000011#include "utils/tstring.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020012
13#include <sdbusplus/exception.hpp>
14
15using namespace testing;
16using namespace std::literals::string_literals;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020017using namespace std::chrono_literals;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000018namespace tstring = utils::tstring;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020019
20class TestReport : public Test
21{
22 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010023 ReportParams defaultParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020024
25 std::unique_ptr<ReportManagerMock> reportManagerMock =
Wludzik, Jozefe2362792020-10-27 17:23:55 +010026 std::make_unique<NiceMock<ReportManagerMock>>();
27 testing::NiceMock<StorageMock> storageMock;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020028 std::vector<std::shared_ptr<MetricMock>> metricMocks = {
29 std::make_shared<NiceMock<MetricMock>>(),
30 std::make_shared<NiceMock<MetricMock>>(),
31 std::make_shared<NiceMock<MetricMock>>()};
32 std::unique_ptr<Report> sut;
33
Wludzik, Jozefe2362792020-10-27 17:23:55 +010034 MockFunction<void()> checkPoint;
35
36 TestReport()
37 {
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000038 ON_CALL(*metricMocks[0], getReading())
39 .WillByDefault(ReturnRefOfCopy(MetricValue{"a", "b", 17.1, 114}));
40 ON_CALL(*metricMocks[1], getReading())
41 .WillByDefault(ReturnRefOfCopy(MetricValue{"aa", "bb", 42.0, 74}));
42 ON_CALL(*metricMocks[2], getReading())
43 .WillByDefault(
44 ReturnRefOfCopy(MetricValue{"aaa", "bbb", 100.7, 21}));
Wludzik, Jozefe2362792020-10-27 17:23:55 +010045
46 for (size_t i = 0; i < metricMocks.size(); ++i)
47 {
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000048 using namespace std::string_literals;
49
50 auto id = std::to_string(i);
51
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000052 auto sensorParameters =
53 LabeledSensorParameters("service"s + id, "path"s + id);
54 auto metricParameters = LabeledMetricParameters(
55 std::move(sensorParameters), utils::toOperationType(i),
56 "id"s + id, "metadata"s + id);
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000057
58 ON_CALL(*metricMocks[i], dumpConfiguration())
59 .WillByDefault(Return(std::move(metricParameters)));
Wludzik, Jozefe2362792020-10-27 17:23:55 +010060 }
61 }
62
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020063 void SetUp() override
64 {
65 sut = makeReport(ReportParams());
66 }
67
Wludzik, Jozefe2362792020-10-27 17:23:55 +010068 static interfaces::JsonStorage::FilePath to_file_path(std::string name)
69 {
70 return interfaces::JsonStorage::FilePath(
71 std::to_string(std::hash<std::string>{}(name)));
72 }
73
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020074 std::unique_ptr<Report> makeReport(const ReportParams& params)
75 {
76 return std::make_unique<Report>(
77 DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
78 params.reportName(), params.reportingType(),
Wludzik, Jozefe2362792020-10-27 17:23:55 +010079 params.emitReadingUpdate(), params.logToMetricReportCollection(),
80 params.interval(), params.readingParameters(), *reportManagerMock,
81 storageMock,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020082 utils::convContainer<std::shared_ptr<interfaces::Metric>>(
83 metricMocks));
84 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020085
86 template <class T>
87 static T getProperty(const std::string& path, const std::string& property)
88 {
89 std::promise<T> propertyPromise;
90 sdbusplus::asio::getProperty<T>(
91 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
92 Report::reportIfaceName, property,
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010093 [&propertyPromise](boost::system::error_code) {
94 utils::setException(propertyPromise, "GetProperty failed");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020095 },
96 [&propertyPromise](T t) { propertyPromise.set_value(t); });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010097 return DbusEnvironment::waitForFuture(propertyPromise.get_future());
98 }
99
100 boost::system::error_code call(const std::string& path,
101 const std::string& interface,
102 const std::string& method)
103 {
104 std::promise<boost::system::error_code> methodPromise;
105 DbusEnvironment::getBus()->async_method_call(
106 [&methodPromise](boost::system::error_code ec) {
107 methodPromise.set_value(ec);
108 },
109 DbusEnvironment::serviceName(), path, interface, method);
110 return DbusEnvironment::waitForFuture(methodPromise.get_future());
111 }
112
113 boost::system::error_code update(const std::string& path)
114 {
115 return call(path, Report::reportIfaceName, "Update");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200116 }
117
118 template <class T>
119 static boost::system::error_code setProperty(const std::string& path,
120 const std::string& property,
121 const T& newValue)
122 {
123 std::promise<boost::system::error_code> setPromise;
124 sdbusplus::asio::setProperty(
125 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
126 Report::reportIfaceName, property, std::move(newValue),
127 [&setPromise](boost::system::error_code ec) {
128 setPromise.set_value(ec);
129 },
130 [&setPromise]() {
131 setPromise.set_value(boost::system::error_code{});
132 });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100133 return DbusEnvironment::waitForFuture(setPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200134 }
135
136 boost::system::error_code deleteReport(const std::string& path)
137 {
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100138 return call(path, Report::deleteIfaceName, "Delete");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200139 }
140};
141
142TEST_F(TestReport, verifyIfPropertiesHaveValidValue)
143{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200144 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100145 Eq(defaultParams.interval().count()));
146 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200147 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100148 Eq(defaultParams.emitReadingUpdate()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200149 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200150 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100151 Eq(defaultParams.logToMetricReportCollection()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200152 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200153 getProperty<ReadingParameters>(sut->getPath(), "ReadingParameters"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100154 Eq(defaultParams.readingParameters()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200155}
156
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200157TEST_F(TestReport, readingsAreInitialyEmpty)
158{
159 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
160 Eq(Readings{}));
161}
162
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200163TEST_F(TestReport, setIntervalWithValidValue)
164{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100165 uint64_t newValue = defaultParams.interval().count() + 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200166 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200167 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200168 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
169 Eq(newValue));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200170}
171
172TEST_F(TestReport, settingIntervalWithInvalidValueDoesNotChangeProperty)
173{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100174 uint64_t newValue = defaultParams.interval().count() - 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200175 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200176 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200177 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100178 Eq(defaultParams.interval().count()));
179}
180
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200181TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect)
182{
183 EXPECT_THAT(setProperty(sut->getPath(), "EmitsReadingsUpdate",
184 !defaultParams.emitReadingUpdate())
185 .value(),
186 Eq(boost::system::errc::read_only_file_system));
187 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
188 Eq(defaultParams.emitReadingUpdate()));
189}
190
191TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect)
192{
193 EXPECT_THAT(setProperty(sut->getPath(), "LogToMetricReportsCollection",
194 !defaultParams.logToMetricReportCollection())
195 .value(),
196 Eq(boost::system::errc::read_only_file_system));
197 EXPECT_THAT(
198 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
199 Eq(defaultParams.logToMetricReportCollection()));
200}
201
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100202TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage)
203{
204 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
205
206 bool persistency = false;
207 EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(),
208 Eq(boost::system::errc::success));
209 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"),
210 Eq(persistency));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200211}
212
213TEST_F(TestReport, deleteReport)
214{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200215 EXPECT_CALL(*reportManagerMock, removeReport(sut.get()));
216 auto ec = deleteReport(sut->getPath());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200217 EXPECT_THAT(ec, Eq(boost::system::errc::success));
218}
219
220TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor)
221{
222 auto ec = deleteReport(Report::reportDir + "NonExisting"s);
223 EXPECT_THAT(ec.value(), Eq(EBADR));
224}
225
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100226TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage)
227{
228 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
229 auto ec = deleteReport(sut->getPath());
230 EXPECT_THAT(ec, Eq(boost::system::errc::success));
231}
232
233class TestReportStore :
234 public TestReport,
235 public WithParamInterface<std::pair<std::string, nlohmann::json>>
236{
237 public:
238 void SetUp() override
239 {}
240
241 nlohmann::json storedConfiguration;
242};
243
244INSTANTIATE_TEST_SUITE_P(
245 _, TestReportStore,
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000246 Values(std::make_pair("Version"s, nlohmann::json(3)),
247 std::make_pair("Name"s, nlohmann::json(ReportParams().reportName())),
248 std::make_pair("ReportingType",
249 nlohmann::json(ReportParams().reportingType())),
250 std::make_pair("EmitsReadingsUpdate",
251 nlohmann::json(ReportParams().emitReadingUpdate())),
252 std::make_pair(
253 "LogToMetricReportsCollection",
254 nlohmann::json(ReportParams().logToMetricReportCollection())),
255 std::make_pair("Interval",
256 nlohmann::json(ReportParams().interval().count())),
257 std::make_pair(
258 "ReadingParameters",
259 nlohmann::json(
260 {{{tstring::SensorPath::str(),
261 {{"service", "service0"}, {"path", "path0"}}},
262 {tstring::OperationType::str(), OperationType::single},
263 {tstring::Id::str(), "id0"},
264 {tstring::MetricMetadata::str(), "metadata0"}},
265 {{tstring::SensorPath::str(),
266 {{"service", "service1"}, {"path", "path1"}}},
267 {tstring::OperationType::str(), OperationType::max},
268 {tstring::Id::str(), "id1"},
269 {tstring::MetricMetadata::str(), "metadata1"}},
270 {{tstring::SensorPath::str(),
271 {{"service", "service2"}, {"path", "path2"}}},
272 {tstring::OperationType::str(), OperationType::min},
273 {tstring::Id::str(), "id2"},
274 {tstring::MetricMetadata::str(), "metadata2"}}}))));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100275
276TEST_P(TestReportStore, settingPersistencyToTrueStoresReport)
277{
278 sut = makeReport(ReportParams());
279
280 {
281 InSequence seq;
282 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
283 EXPECT_CALL(checkPoint, Call());
284 EXPECT_CALL(storageMock, store(to_file_path(sut->getName()), _))
285 .WillOnce(SaveArg<1>(&storedConfiguration));
286 }
287
288 setProperty(sut->getPath(), "Persistency", false);
289 checkPoint.Call();
290 setProperty(sut->getPath(), "Persistency", true);
291
292 const auto& [key, value] = GetParam();
293
294 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
295}
296
297TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated)
298{
299 EXPECT_CALL(storageMock,
300 store(to_file_path(ReportParams().reportName()), _))
301 .WillOnce(SaveArg<1>(&storedConfiguration));
302
303 sut = makeReport(ReportParams());
304
305 const auto& [key, value] = GetParam();
306
307 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
308}
309
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200310class TestReportValidNames :
311 public TestReport,
312 public WithParamInterface<ReportParams>
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200313{
314 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200315 void SetUp() override
316 {}
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200317};
318
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200319INSTANTIATE_TEST_SUITE_P(
320 ValidNames, TestReportValidNames,
321 Values(ReportParams().reportName("Valid_1"),
322 ReportParams().reportName("Valid_1/Valid_2"),
323 ReportParams().reportName("Valid_1/Valid_2/Valid_3")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200324
325TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName)
326{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200327 EXPECT_NO_THROW(makeReport(GetParam()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200328}
329
330class TestReportInvalidNames :
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200331 public TestReport,
332 public WithParamInterface<ReportParams>
333{
334 public:
335 void SetUp() override
336 {}
337};
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200338
339INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidNames,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200340 Values(ReportParams().reportName("/"),
341 ReportParams().reportName("/Invalid"),
342 ReportParams().reportName("Invalid/"),
343 ReportParams().reportName("Invalid/Invalid/"),
344 ReportParams().reportName("Invalid?")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200345
346TEST_P(TestReportInvalidNames, reportCtorThrowOnInvalidName)
347{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200348 EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError);
349}
350
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100351TEST_F(TestReportInvalidNames, reportCtorThrowOnInvalidNameAndNoStoreIsCalled)
352{
353 EXPECT_CALL(storageMock, store).Times(0);
354 EXPECT_THROW(makeReport(ReportParams().reportName("/Invalid")),
355 sdbusplus::exception::SdBusError);
356}
357
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200358class TestReportAllReportTypes :
359 public TestReport,
360 public WithParamInterface<ReportParams>
361{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200362 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200363 void SetUp() override
364 {
365 sut = makeReport(GetParam());
366 }
367};
368
369INSTANTIATE_TEST_SUITE_P(_, TestReportAllReportTypes,
370 Values(ReportParams().reportingType("OnRequest"),
371 ReportParams().reportingType("OnChange"),
372 ReportParams().reportingType("Periodic")));
373
374TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType)
375{
376 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"),
377 Eq(GetParam().reportingType()));
378}
379
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100380TEST_P(TestReportAllReportTypes, updateReadingsCallUpdateReadingsProperty)
381{
382 const uint64_t expectedTime = std::time(0);
383
384 sut->updateReadings();
385
386 const auto [timestamp, readings] =
387 getProperty<Readings>(sut->getPath(), "Readings");
388
389 EXPECT_THAT(timestamp, Ge(expectedTime));
390}
391
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100392class TestReportOnRequestType : public TestReport
393{
394 void SetUp() override
395 {
396 sut = makeReport(ReportParams().reportingType("OnRequest"));
397 }
398};
399
400TEST_F(TestReportOnRequestType, updatesReadingTimestamp)
401{
402 const uint64_t expectedTime = std::time(0);
403
404 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
405
406 const auto [timestamp, readings] =
407 getProperty<Readings>(sut->getPath(), "Readings");
408
409 EXPECT_THAT(timestamp, Ge(expectedTime));
410}
411
412TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled)
413{
414 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
415
416 const auto [timestamp, readings] =
417 getProperty<Readings>(sut->getPath(), "Readings");
418
419 EXPECT_THAT(readings,
420 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000421 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
422 std::make_tuple("aaa"s, "bbb"s, 100.7, 21u)));
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100423}
424
425class TestReportNonOnRequestType :
426 public TestReport,
427 public WithParamInterface<ReportParams>
428{
429 void SetUp() override
430 {
431 sut = makeReport(GetParam());
432 }
433};
434
435INSTANTIATE_TEST_SUITE_P(_, TestReportNonOnRequestType,
436 Values(ReportParams().reportingType("Periodic"),
437 ReportParams().reportingType("OnChange")));
438
439TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall)
440{
441 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
442
443 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
444 Eq(Readings{}));
445}
446
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200447class TestReportNonPeriodicReport :
448 public TestReport,
449 public WithParamInterface<ReportParams>
450{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200451 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200452 void SetUp() override
453 {
454 sut = makeReport(GetParam());
455 }
456};
457
458INSTANTIATE_TEST_SUITE_P(_, TestReportNonPeriodicReport,
459 Values(ReportParams().reportingType("OnRequest"),
460 ReportParams().reportingType("OnChange")));
461
462TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires)
463{
464 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
465
466 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
467 Eq(Readings{}));
468}
469
470class TestReportPeriodicReport : public TestReport
471{
472 void SetUp() override
473 {
474 sut = makeReport(ReportParams().reportingType("Periodic"));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200475 }
476};
477
478TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires)
479{
480 const uint64_t expectedTime = std::time(0);
481 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
482
483 const auto [timestamp, readings] =
484 getProperty<Readings>(sut->getPath(), "Readings");
485
486 EXPECT_THAT(timestamp, Ge(expectedTime));
487}
488
489TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires)
490{
491 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
492
493 const auto [timestamp, readings] =
494 getProperty<Readings>(sut->getPath(), "Readings");
495
496 EXPECT_THAT(readings,
497 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000498 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
499 std::make_tuple("aaa"s, "bbb"s, 100.7, 21u)));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200500}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100501
502class TestReportInitialization : public TestReport
503{
504 public:
505 void SetUp() override
506 {}
507
508 void monitorProc(sdbusplus::message::message& msg)
509 {
510 std::string iface;
511 std::vector<std::pair<std::string, std::variant<Readings>>>
512 changed_properties;
513 std::vector<std::string> invalidated_properties;
514
515 msg.read(iface, changed_properties, invalidated_properties);
516
517 if (iface == Report::reportIfaceName)
518 {
519 for (const auto& [name, value] : changed_properties)
520 {
521 if (name == "Readings")
522 {
523 readingsUpdated.Call();
524 }
525 }
526 }
527 }
528
529 void makeMonitor()
530 {
531 monitor = std::make_unique<sdbusplus::bus::match::match>(
532 *DbusEnvironment::getBus(),
533 sdbusplus::bus::match::rules::propertiesChanged(
534 sut->getPath(), Report::reportIfaceName),
535 [this](auto& msg) { monitorProc(msg); });
536 }
537
538 std::unique_ptr<sdbusplus::bus::match::match> monitor;
539 MockFunction<void()> readingsUpdated;
540};
541
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100542TEST_F(TestReportInitialization, metricsAreInitializedWhenConstructed)
543{
544 for (auto& metric : metricMocks)
545 {
546 EXPECT_CALL(*metric, initialize());
547 }
548
549 sut = makeReport(ReportParams());
550}
551
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200552TEST_F(TestReportInitialization,
553 emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100554{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200555 EXPECT_CALL(readingsUpdated, Call())
556 .WillOnce(
557 InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated")));
558
559 const auto elapsed = DbusEnvironment::measureTime([this] {
560 sut = makeReport(
561 defaultParams.reportingType("Periodic").emitReadingUpdate(true));
562 makeMonitor();
563 EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated"));
564 });
565
566 EXPECT_THAT(elapsed, AllOf(Ge(defaultParams.interval()),
567 Lt(defaultParams.interval() * 2)));
568}
569
570TEST_F(TestReportInitialization,
571 emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits)
572{
573 EXPECT_CALL(readingsUpdated, Call()).Times(0);
574
575 sut = makeReport(
576 defaultParams.reportingType("Periodic").emitReadingUpdate(false));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100577 makeMonitor();
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200578 DbusEnvironment::sleepFor(defaultParams.interval() * 2);
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100579}