blob: 8f2228e99bd86ab4119bb97c3b7ee51aa992fe56 [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"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020011
12#include <sdbusplus/exception.hpp>
13
14using namespace testing;
15using namespace std::literals::string_literals;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020016using namespace std::chrono_literals;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020017
18class TestReport : public Test
19{
20 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010021 ReportParams defaultParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020022
23 std::unique_ptr<ReportManagerMock> reportManagerMock =
Wludzik, Jozefe2362792020-10-27 17:23:55 +010024 std::make_unique<NiceMock<ReportManagerMock>>();
25 testing::NiceMock<StorageMock> storageMock;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020026 std::vector<std::shared_ptr<MetricMock>> metricMocks = {
27 std::make_shared<NiceMock<MetricMock>>(),
28 std::make_shared<NiceMock<MetricMock>>(),
29 std::make_shared<NiceMock<MetricMock>>()};
30 std::unique_ptr<Report> sut;
31
Wludzik, Jozefe2362792020-10-27 17:23:55 +010032 MockFunction<void()> checkPoint;
33
34 TestReport()
35 {
36 ON_CALL(*metricMocks[0], getReadings())
37 .WillByDefault(ReturnRefOfCopy(std::vector<MetricValue>(
38 {MetricValue{"a", "b", 17.1, 114},
39 MetricValue{"aaa", "bbb", 21.7, 100}})));
40 ON_CALL(*metricMocks[1], getReadings())
41 .WillByDefault(ReturnRefOfCopy(
42 std::vector<MetricValue>({MetricValue{"aa", "bb", 42.0, 74}})));
43
44 for (size_t i = 0; i < metricMocks.size(); ++i)
45 {
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000046 using namespace std::string_literals;
47
48 auto id = std::to_string(i);
49
50 auto sensorParameters = std::vector<LabeledSensorParameters>(
51 {LabeledSensorParameters("service"s + id, "path"s + id)});
52 auto metricParameters =
53 LabeledMetricParameters(std::move(sensorParameters), "op"s + id,
54 "id"s + id, "metadata"s + id);
55
56 ON_CALL(*metricMocks[i], dumpConfiguration())
57 .WillByDefault(Return(std::move(metricParameters)));
Wludzik, Jozefe2362792020-10-27 17:23:55 +010058 }
59 }
60
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020061 void SetUp() override
62 {
63 sut = makeReport(ReportParams());
64 }
65
Wludzik, Jozefe2362792020-10-27 17:23:55 +010066 static interfaces::JsonStorage::FilePath to_file_path(std::string name)
67 {
68 return interfaces::JsonStorage::FilePath(
69 std::to_string(std::hash<std::string>{}(name)));
70 }
71
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020072 std::unique_ptr<Report> makeReport(const ReportParams& params)
73 {
74 return std::make_unique<Report>(
75 DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
76 params.reportName(), params.reportingType(),
Wludzik, Jozefe2362792020-10-27 17:23:55 +010077 params.emitReadingUpdate(), params.logToMetricReportCollection(),
78 params.interval(), params.readingParameters(), *reportManagerMock,
79 storageMock,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020080 utils::convContainer<std::shared_ptr<interfaces::Metric>>(
81 metricMocks));
82 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020083
84 template <class T>
85 static T getProperty(const std::string& path, const std::string& property)
86 {
87 std::promise<T> propertyPromise;
88 sdbusplus::asio::getProperty<T>(
89 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
90 Report::reportIfaceName, property,
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010091 [&propertyPromise](boost::system::error_code) {
92 utils::setException(propertyPromise, "GetProperty failed");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020093 },
94 [&propertyPromise](T t) { propertyPromise.set_value(t); });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010095 return DbusEnvironment::waitForFuture(propertyPromise.get_future());
96 }
97
98 boost::system::error_code call(const std::string& path,
99 const std::string& interface,
100 const std::string& method)
101 {
102 std::promise<boost::system::error_code> methodPromise;
103 DbusEnvironment::getBus()->async_method_call(
104 [&methodPromise](boost::system::error_code ec) {
105 methodPromise.set_value(ec);
106 },
107 DbusEnvironment::serviceName(), path, interface, method);
108 return DbusEnvironment::waitForFuture(methodPromise.get_future());
109 }
110
111 boost::system::error_code update(const std::string& path)
112 {
113 return call(path, Report::reportIfaceName, "Update");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200114 }
115
116 template <class T>
117 static boost::system::error_code setProperty(const std::string& path,
118 const std::string& property,
119 const T& newValue)
120 {
121 std::promise<boost::system::error_code> setPromise;
122 sdbusplus::asio::setProperty(
123 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
124 Report::reportIfaceName, property, std::move(newValue),
125 [&setPromise](boost::system::error_code ec) {
126 setPromise.set_value(ec);
127 },
128 [&setPromise]() {
129 setPromise.set_value(boost::system::error_code{});
130 });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100131 return DbusEnvironment::waitForFuture(setPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200132 }
133
134 boost::system::error_code deleteReport(const std::string& path)
135 {
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100136 return call(path, Report::deleteIfaceName, "Delete");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200137 }
138};
139
140TEST_F(TestReport, verifyIfPropertiesHaveValidValue)
141{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200142 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100143 Eq(defaultParams.interval().count()));
144 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200145 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100146 Eq(defaultParams.emitReadingUpdate()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200147 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200148 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100149 Eq(defaultParams.logToMetricReportCollection()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200150 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200151 getProperty<ReadingParameters>(sut->getPath(), "ReadingParameters"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100152 Eq(defaultParams.readingParameters()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200153}
154
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200155TEST_F(TestReport, readingsAreInitialyEmpty)
156{
157 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
158 Eq(Readings{}));
159}
160
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200161TEST_F(TestReport, setIntervalWithValidValue)
162{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100163 uint64_t newValue = defaultParams.interval().count() + 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200164 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200165 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200166 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
167 Eq(newValue));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200168}
169
170TEST_F(TestReport, settingIntervalWithInvalidValueDoesNotChangeProperty)
171{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100172 uint64_t newValue = defaultParams.interval().count() - 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200173 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200174 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200175 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100176 Eq(defaultParams.interval().count()));
177}
178
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200179TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect)
180{
181 EXPECT_THAT(setProperty(sut->getPath(), "EmitsReadingsUpdate",
182 !defaultParams.emitReadingUpdate())
183 .value(),
184 Eq(boost::system::errc::read_only_file_system));
185 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
186 Eq(defaultParams.emitReadingUpdate()));
187}
188
189TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect)
190{
191 EXPECT_THAT(setProperty(sut->getPath(), "LogToMetricReportsCollection",
192 !defaultParams.logToMetricReportCollection())
193 .value(),
194 Eq(boost::system::errc::read_only_file_system));
195 EXPECT_THAT(
196 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
197 Eq(defaultParams.logToMetricReportCollection()));
198}
199
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100200TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage)
201{
202 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
203
204 bool persistency = false;
205 EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(),
206 Eq(boost::system::errc::success));
207 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"),
208 Eq(persistency));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200209}
210
211TEST_F(TestReport, deleteReport)
212{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200213 EXPECT_CALL(*reportManagerMock, removeReport(sut.get()));
214 auto ec = deleteReport(sut->getPath());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200215 EXPECT_THAT(ec, Eq(boost::system::errc::success));
216}
217
218TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor)
219{
220 auto ec = deleteReport(Report::reportDir + "NonExisting"s);
221 EXPECT_THAT(ec.value(), Eq(EBADR));
222}
223
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100224TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage)
225{
226 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
227 auto ec = deleteReport(sut->getPath());
228 EXPECT_THAT(ec, Eq(boost::system::errc::success));
229}
230
231class TestReportStore :
232 public TestReport,
233 public WithParamInterface<std::pair<std::string, nlohmann::json>>
234{
235 public:
236 void SetUp() override
237 {}
238
239 nlohmann::json storedConfiguration;
240};
241
242INSTANTIATE_TEST_SUITE_P(
243 _, TestReportStore,
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000244 Values(
245 std::make_pair("Version"s, nlohmann::json(2)),
246 std::make_pair("Name"s, nlohmann::json(ReportParams().reportName())),
247 std::make_pair("ReportingType",
248 nlohmann::json(ReportParams().reportingType())),
249 std::make_pair("EmitsReadingsUpdate",
250 nlohmann::json(ReportParams().emitReadingUpdate())),
251 std::make_pair(
252 "LogToMetricReportsCollection",
253 nlohmann::json(ReportParams().logToMetricReportCollection())),
254 std::make_pair("Interval",
255 nlohmann::json(ReportParams().interval().count())),
256 std::make_pair(
257 "ReadingParameters",
258 nlohmann::json({{{"sensorPaths",
259 {{{"service", "service0"}, {"path", "path0"}}}},
260 {"operationType", "op0"},
261 {"id", "id0"},
262 {"metricMetadata", "metadata0"}},
263 {{"sensorPaths",
264 {{{"service", "service1"}, {"path", "path1"}}}},
265 {"operationType", "op1"},
266 {"id", "id1"},
267 {"metricMetadata", "metadata1"}},
268 {{"sensorPaths",
269 {{{"service", "service2"}, {"path", "path2"}}}},
270 {"operationType", "op2"},
271 {"id", "id2"},
272 {"metricMetadata", "metadata2"}}}))));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100273
274TEST_P(TestReportStore, settingPersistencyToTrueStoresReport)
275{
276 sut = makeReport(ReportParams());
277
278 {
279 InSequence seq;
280 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
281 EXPECT_CALL(checkPoint, Call());
282 EXPECT_CALL(storageMock, store(to_file_path(sut->getName()), _))
283 .WillOnce(SaveArg<1>(&storedConfiguration));
284 }
285
286 setProperty(sut->getPath(), "Persistency", false);
287 checkPoint.Call();
288 setProperty(sut->getPath(), "Persistency", true);
289
290 const auto& [key, value] = GetParam();
291
292 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
293}
294
295TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated)
296{
297 EXPECT_CALL(storageMock,
298 store(to_file_path(ReportParams().reportName()), _))
299 .WillOnce(SaveArg<1>(&storedConfiguration));
300
301 sut = makeReport(ReportParams());
302
303 const auto& [key, value] = GetParam();
304
305 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
306}
307
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200308class TestReportValidNames :
309 public TestReport,
310 public WithParamInterface<ReportParams>
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200311{
312 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200313 void SetUp() override
314 {}
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200315};
316
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200317INSTANTIATE_TEST_SUITE_P(
318 ValidNames, TestReportValidNames,
319 Values(ReportParams().reportName("Valid_1"),
320 ReportParams().reportName("Valid_1/Valid_2"),
321 ReportParams().reportName("Valid_1/Valid_2/Valid_3")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200322
323TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName)
324{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200325 EXPECT_NO_THROW(makeReport(GetParam()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200326}
327
328class TestReportInvalidNames :
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200329 public TestReport,
330 public WithParamInterface<ReportParams>
331{
332 public:
333 void SetUp() override
334 {}
335};
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200336
337INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidNames,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200338 Values(ReportParams().reportName("/"),
339 ReportParams().reportName("/Invalid"),
340 ReportParams().reportName("Invalid/"),
341 ReportParams().reportName("Invalid/Invalid/"),
342 ReportParams().reportName("Invalid?")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200343
344TEST_P(TestReportInvalidNames, reportCtorThrowOnInvalidName)
345{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200346 EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError);
347}
348
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100349TEST_F(TestReportInvalidNames, reportCtorThrowOnInvalidNameAndNoStoreIsCalled)
350{
351 EXPECT_CALL(storageMock, store).Times(0);
352 EXPECT_THROW(makeReport(ReportParams().reportName("/Invalid")),
353 sdbusplus::exception::SdBusError);
354}
355
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200356class TestReportAllReportTypes :
357 public TestReport,
358 public WithParamInterface<ReportParams>
359{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200360 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200361 void SetUp() override
362 {
363 sut = makeReport(GetParam());
364 }
365};
366
367INSTANTIATE_TEST_SUITE_P(_, TestReportAllReportTypes,
368 Values(ReportParams().reportingType("OnRequest"),
369 ReportParams().reportingType("OnChange"),
370 ReportParams().reportingType("Periodic")));
371
372TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType)
373{
374 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"),
375 Eq(GetParam().reportingType()));
376}
377
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100378class TestReportOnRequestType : public TestReport
379{
380 void SetUp() override
381 {
382 sut = makeReport(ReportParams().reportingType("OnRequest"));
383 }
384};
385
386TEST_F(TestReportOnRequestType, updatesReadingTimestamp)
387{
388 const uint64_t expectedTime = std::time(0);
389
390 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
391
392 const auto [timestamp, readings] =
393 getProperty<Readings>(sut->getPath(), "Readings");
394
395 EXPECT_THAT(timestamp, Ge(expectedTime));
396}
397
398TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled)
399{
400 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
401
402 const auto [timestamp, readings] =
403 getProperty<Readings>(sut->getPath(), "Readings");
404
405 EXPECT_THAT(readings,
406 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
407 std::make_tuple("aaa"s, "bbb"s, 21.7, 100u),
408 std::make_tuple("aa"s, "bb"s, 42.0, 74u)));
409}
410
411class TestReportNonOnRequestType :
412 public TestReport,
413 public WithParamInterface<ReportParams>
414{
415 void SetUp() override
416 {
417 sut = makeReport(GetParam());
418 }
419};
420
421INSTANTIATE_TEST_SUITE_P(_, TestReportNonOnRequestType,
422 Values(ReportParams().reportingType("Periodic"),
423 ReportParams().reportingType("OnChange")));
424
425TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall)
426{
427 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
428
429 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
430 Eq(Readings{}));
431}
432
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200433class TestReportNonPeriodicReport :
434 public TestReport,
435 public WithParamInterface<ReportParams>
436{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200437 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200438 void SetUp() override
439 {
440 sut = makeReport(GetParam());
441 }
442};
443
444INSTANTIATE_TEST_SUITE_P(_, TestReportNonPeriodicReport,
445 Values(ReportParams().reportingType("OnRequest"),
446 ReportParams().reportingType("OnChange")));
447
448TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires)
449{
450 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
451
452 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
453 Eq(Readings{}));
454}
455
456class TestReportPeriodicReport : public TestReport
457{
458 void SetUp() override
459 {
460 sut = makeReport(ReportParams().reportingType("Periodic"));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200461 }
462};
463
464TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires)
465{
466 const uint64_t expectedTime = std::time(0);
467 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
468
469 const auto [timestamp, readings] =
470 getProperty<Readings>(sut->getPath(), "Readings");
471
472 EXPECT_THAT(timestamp, Ge(expectedTime));
473}
474
475TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires)
476{
477 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
478
479 const auto [timestamp, readings] =
480 getProperty<Readings>(sut->getPath(), "Readings");
481
482 EXPECT_THAT(readings,
483 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
484 std::make_tuple("aaa"s, "bbb"s, 21.7, 100u),
485 std::make_tuple("aa"s, "bb"s, 42.0, 74u)));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200486}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100487
488class TestReportInitialization : public TestReport
489{
490 public:
491 void SetUp() override
492 {}
493
494 void monitorProc(sdbusplus::message::message& msg)
495 {
496 std::string iface;
497 std::vector<std::pair<std::string, std::variant<Readings>>>
498 changed_properties;
499 std::vector<std::string> invalidated_properties;
500
501 msg.read(iface, changed_properties, invalidated_properties);
502
503 if (iface == Report::reportIfaceName)
504 {
505 for (const auto& [name, value] : changed_properties)
506 {
507 if (name == "Readings")
508 {
509 readingsUpdated.Call();
510 }
511 }
512 }
513 }
514
515 void makeMonitor()
516 {
517 monitor = std::make_unique<sdbusplus::bus::match::match>(
518 *DbusEnvironment::getBus(),
519 sdbusplus::bus::match::rules::propertiesChanged(
520 sut->getPath(), Report::reportIfaceName),
521 [this](auto& msg) { monitorProc(msg); });
522 }
523
524 std::unique_ptr<sdbusplus::bus::match::match> monitor;
525 MockFunction<void()> readingsUpdated;
526};
527
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100528TEST_F(TestReportInitialization, metricsAreInitializedWhenConstructed)
529{
530 for (auto& metric : metricMocks)
531 {
532 EXPECT_CALL(*metric, initialize());
533 }
534
535 sut = makeReport(ReportParams());
536}
537
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200538TEST_F(TestReportInitialization,
539 emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100540{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200541 EXPECT_CALL(readingsUpdated, Call())
542 .WillOnce(
543 InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated")));
544
545 const auto elapsed = DbusEnvironment::measureTime([this] {
546 sut = makeReport(
547 defaultParams.reportingType("Periodic").emitReadingUpdate(true));
548 makeMonitor();
549 EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated"));
550 });
551
552 EXPECT_THAT(elapsed, AllOf(Ge(defaultParams.interval()),
553 Lt(defaultParams.interval() * 2)));
554}
555
556TEST_F(TestReportInitialization,
557 emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits)
558{
559 EXPECT_CALL(readingsUpdated, Call()).Times(0);
560
561 sut = makeReport(
562 defaultParams.reportingType("Periodic").emitReadingUpdate(false));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100563 makeMonitor();
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200564 DbusEnvironment::sleepFor(defaultParams.interval() * 2);
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100565}