blob: 90043bdacddaf354044b5ec24562791ed252cf40 [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),
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +000056 "id"s + id, "metadata"s + id, CollectionTimeScope::point,
57 CollectionDuration(0ms));
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000058
59 ON_CALL(*metricMocks[i], dumpConfiguration())
60 .WillByDefault(Return(std::move(metricParameters)));
Wludzik, Jozefe2362792020-10-27 17:23:55 +010061 }
62 }
63
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020064 void SetUp() override
65 {
66 sut = makeReport(ReportParams());
67 }
68
Wludzik, Jozefe2362792020-10-27 17:23:55 +010069 static interfaces::JsonStorage::FilePath to_file_path(std::string name)
70 {
71 return interfaces::JsonStorage::FilePath(
72 std::to_string(std::hash<std::string>{}(name)));
73 }
74
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020075 std::unique_ptr<Report> makeReport(const ReportParams& params)
76 {
77 return std::make_unique<Report>(
78 DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(),
79 params.reportName(), params.reportingType(),
Wludzik, Jozefe2362792020-10-27 17:23:55 +010080 params.emitReadingUpdate(), params.logToMetricReportCollection(),
81 params.interval(), params.readingParameters(), *reportManagerMock,
82 storageMock,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +020083 utils::convContainer<std::shared_ptr<interfaces::Metric>>(
84 metricMocks));
85 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020086
87 template <class T>
88 static T getProperty(const std::string& path, const std::string& property)
89 {
90 std::promise<T> propertyPromise;
91 sdbusplus::asio::getProperty<T>(
92 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
93 Report::reportIfaceName, property,
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010094 [&propertyPromise](boost::system::error_code) {
95 utils::setException(propertyPromise, "GetProperty failed");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020096 },
97 [&propertyPromise](T t) { propertyPromise.set_value(t); });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010098 return DbusEnvironment::waitForFuture(propertyPromise.get_future());
99 }
100
101 boost::system::error_code call(const std::string& path,
102 const std::string& interface,
103 const std::string& method)
104 {
105 std::promise<boost::system::error_code> methodPromise;
106 DbusEnvironment::getBus()->async_method_call(
107 [&methodPromise](boost::system::error_code ec) {
108 methodPromise.set_value(ec);
109 },
110 DbusEnvironment::serviceName(), path, interface, method);
111 return DbusEnvironment::waitForFuture(methodPromise.get_future());
112 }
113
114 boost::system::error_code update(const std::string& path)
115 {
116 return call(path, Report::reportIfaceName, "Update");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200117 }
118
119 template <class T>
120 static boost::system::error_code setProperty(const std::string& path,
121 const std::string& property,
122 const T& newValue)
123 {
124 std::promise<boost::system::error_code> setPromise;
125 sdbusplus::asio::setProperty(
126 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path,
127 Report::reportIfaceName, property, std::move(newValue),
128 [&setPromise](boost::system::error_code ec) {
129 setPromise.set_value(ec);
130 },
131 [&setPromise]() {
132 setPromise.set_value(boost::system::error_code{});
133 });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100134 return DbusEnvironment::waitForFuture(setPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200135 }
136
137 boost::system::error_code deleteReport(const std::string& path)
138 {
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100139 return call(path, Report::deleteIfaceName, "Delete");
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200140 }
141};
142
143TEST_F(TestReport, verifyIfPropertiesHaveValidValue)
144{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200145 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100146 Eq(defaultParams.interval().count()));
147 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200148 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100149 Eq(defaultParams.emitReadingUpdate()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200150 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200151 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100152 Eq(defaultParams.logToMetricReportCollection()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200153 EXPECT_THAT(
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200154 getProperty<ReadingParameters>(sut->getPath(), "ReadingParameters"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100155 Eq(defaultParams.readingParameters()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200156}
157
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200158TEST_F(TestReport, readingsAreInitialyEmpty)
159{
160 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
161 Eq(Readings{}));
162}
163
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200164TEST_F(TestReport, setIntervalWithValidValue)
165{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100166 uint64_t newValue = defaultParams.interval().count() + 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200167 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200168 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200169 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
170 Eq(newValue));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200171}
172
173TEST_F(TestReport, settingIntervalWithInvalidValueDoesNotChangeProperty)
174{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100175 uint64_t newValue = defaultParams.interval().count() - 1;
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200176 EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200177 Eq(boost::system::errc::success));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200178 EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"),
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100179 Eq(defaultParams.interval().count()));
180}
181
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200182TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect)
183{
184 EXPECT_THAT(setProperty(sut->getPath(), "EmitsReadingsUpdate",
185 !defaultParams.emitReadingUpdate())
186 .value(),
187 Eq(boost::system::errc::read_only_file_system));
188 EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"),
189 Eq(defaultParams.emitReadingUpdate()));
190}
191
192TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect)
193{
194 EXPECT_THAT(setProperty(sut->getPath(), "LogToMetricReportsCollection",
195 !defaultParams.logToMetricReportCollection())
196 .value(),
197 Eq(boost::system::errc::read_only_file_system));
198 EXPECT_THAT(
199 getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"),
200 Eq(defaultParams.logToMetricReportCollection()));
201}
202
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100203TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage)
204{
205 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
206
207 bool persistency = false;
208 EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(),
209 Eq(boost::system::errc::success));
210 EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"),
211 Eq(persistency));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200212}
213
214TEST_F(TestReport, deleteReport)
215{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200216 EXPECT_CALL(*reportManagerMock, removeReport(sut.get()));
217 auto ec = deleteReport(sut->getPath());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200218 EXPECT_THAT(ec, Eq(boost::system::errc::success));
219}
220
221TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor)
222{
223 auto ec = deleteReport(Report::reportDir + "NonExisting"s);
224 EXPECT_THAT(ec.value(), Eq(EBADR));
225}
226
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100227TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage)
228{
229 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
230 auto ec = deleteReport(sut->getPath());
231 EXPECT_THAT(ec, Eq(boost::system::errc::success));
232}
233
234class TestReportStore :
235 public TestReport,
236 public WithParamInterface<std::pair<std::string, nlohmann::json>>
237{
238 public:
239 void SetUp() override
240 {}
241
242 nlohmann::json storedConfiguration;
243};
244
245INSTANTIATE_TEST_SUITE_P(
246 _, TestReportStore,
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000247 Values(std::make_pair("Version"s, nlohmann::json(3)),
248 std::make_pair("Name"s, nlohmann::json(ReportParams().reportName())),
249 std::make_pair("ReportingType",
250 nlohmann::json(ReportParams().reportingType())),
251 std::make_pair("EmitsReadingsUpdate",
252 nlohmann::json(ReportParams().emitReadingUpdate())),
253 std::make_pair(
254 "LogToMetricReportsCollection",
255 nlohmann::json(ReportParams().logToMetricReportCollection())),
256 std::make_pair("Interval",
257 nlohmann::json(ReportParams().interval().count())),
258 std::make_pair(
259 "ReadingParameters",
260 nlohmann::json(
261 {{{tstring::SensorPath::str(),
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +0000262 {{tstring::Service::str(), "service0"},
263 {tstring::Path::str(), "path0"}}},
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000264 {tstring::OperationType::str(), OperationType::single},
265 {tstring::Id::str(), "id0"},
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +0000266 {tstring::MetricMetadata::str(), "metadata0"},
267 {tstring::CollectionTimeScope::str(),
268 CollectionTimeScope::point},
269 {tstring::CollectionDuration::str(), 0}},
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000270 {{tstring::SensorPath::str(),
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +0000271 {{tstring::Service::str(), "service1"},
272 {tstring::Path::str(), "path1"}}},
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000273 {tstring::OperationType::str(), OperationType::max},
274 {tstring::Id::str(), "id1"},
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +0000275 {tstring::MetricMetadata::str(), "metadata1"},
276 {tstring::CollectionTimeScope::str(),
277 CollectionTimeScope::point},
278 {tstring::CollectionDuration::str(), 0}},
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000279 {{tstring::SensorPath::str(),
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +0000280 {{tstring::Service::str(), "service2"},
281 {tstring::Path::str(), "path2"}}},
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000282 {tstring::OperationType::str(), OperationType::min},
283 {tstring::Id::str(), "id2"},
Krzysztof Grobelny753e4b32021-02-11 12:58:58 +0000284 {tstring::MetricMetadata::str(), "metadata2"},
285 {tstring::CollectionTimeScope::str(),
286 CollectionTimeScope::point},
287 {tstring::CollectionDuration::str(), 0}}}))));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100288
289TEST_P(TestReportStore, settingPersistencyToTrueStoresReport)
290{
291 sut = makeReport(ReportParams());
292
293 {
294 InSequence seq;
295 EXPECT_CALL(storageMock, remove(to_file_path(sut->getName())));
296 EXPECT_CALL(checkPoint, Call());
297 EXPECT_CALL(storageMock, store(to_file_path(sut->getName()), _))
298 .WillOnce(SaveArg<1>(&storedConfiguration));
299 }
300
301 setProperty(sut->getPath(), "Persistency", false);
302 checkPoint.Call();
303 setProperty(sut->getPath(), "Persistency", true);
304
305 const auto& [key, value] = GetParam();
306
307 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
308}
309
310TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated)
311{
312 EXPECT_CALL(storageMock,
313 store(to_file_path(ReportParams().reportName()), _))
314 .WillOnce(SaveArg<1>(&storedConfiguration));
315
316 sut = makeReport(ReportParams());
317
318 const auto& [key, value] = GetParam();
319
320 ASSERT_THAT(storedConfiguration.at(key), Eq(value));
321}
322
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200323class TestReportValidNames :
324 public TestReport,
325 public WithParamInterface<ReportParams>
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200326{
327 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200328 void SetUp() override
329 {}
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200330};
331
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200332INSTANTIATE_TEST_SUITE_P(
333 ValidNames, TestReportValidNames,
334 Values(ReportParams().reportName("Valid_1"),
335 ReportParams().reportName("Valid_1/Valid_2"),
336 ReportParams().reportName("Valid_1/Valid_2/Valid_3")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200337
338TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName)
339{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200340 EXPECT_NO_THROW(makeReport(GetParam()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200341}
342
343class TestReportInvalidNames :
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200344 public TestReport,
345 public WithParamInterface<ReportParams>
346{
347 public:
348 void SetUp() override
349 {}
350};
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200351
352INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidNames,
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200353 Values(ReportParams().reportName("/"),
354 ReportParams().reportName("/Invalid"),
355 ReportParams().reportName("Invalid/"),
356 ReportParams().reportName("Invalid/Invalid/"),
357 ReportParams().reportName("Invalid?")));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200358
359TEST_P(TestReportInvalidNames, reportCtorThrowOnInvalidName)
360{
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200361 EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError);
362}
363
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100364TEST_F(TestReportInvalidNames, reportCtorThrowOnInvalidNameAndNoStoreIsCalled)
365{
366 EXPECT_CALL(storageMock, store).Times(0);
367 EXPECT_THROW(makeReport(ReportParams().reportName("/Invalid")),
368 sdbusplus::exception::SdBusError);
369}
370
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200371class TestReportAllReportTypes :
372 public TestReport,
373 public WithParamInterface<ReportParams>
374{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200375 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200376 void SetUp() override
377 {
378 sut = makeReport(GetParam());
379 }
380};
381
382INSTANTIATE_TEST_SUITE_P(_, TestReportAllReportTypes,
383 Values(ReportParams().reportingType("OnRequest"),
384 ReportParams().reportingType("OnChange"),
385 ReportParams().reportingType("Periodic")));
386
387TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType)
388{
389 EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"),
390 Eq(GetParam().reportingType()));
391}
392
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100393TEST_P(TestReportAllReportTypes, updateReadingsCallUpdateReadingsProperty)
394{
395 const uint64_t expectedTime = std::time(0);
396
397 sut->updateReadings();
398
399 const auto [timestamp, readings] =
400 getProperty<Readings>(sut->getPath(), "Readings");
401
402 EXPECT_THAT(timestamp, Ge(expectedTime));
403}
404
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100405class TestReportOnRequestType : public TestReport
406{
407 void SetUp() override
408 {
409 sut = makeReport(ReportParams().reportingType("OnRequest"));
410 }
411};
412
413TEST_F(TestReportOnRequestType, updatesReadingTimestamp)
414{
415 const uint64_t expectedTime = std::time(0);
416
417 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
418
419 const auto [timestamp, readings] =
420 getProperty<Readings>(sut->getPath(), "Readings");
421
422 EXPECT_THAT(timestamp, Ge(expectedTime));
423}
424
425TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled)
426{
427 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
428
429 const auto [timestamp, readings] =
430 getProperty<Readings>(sut->getPath(), "Readings");
431
432 EXPECT_THAT(readings,
433 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000434 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
435 std::make_tuple("aaa"s, "bbb"s, 100.7, 21u)));
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +0100436}
437
438class TestReportNonOnRequestType :
439 public TestReport,
440 public WithParamInterface<ReportParams>
441{
442 void SetUp() override
443 {
444 sut = makeReport(GetParam());
445 }
446};
447
448INSTANTIATE_TEST_SUITE_P(_, TestReportNonOnRequestType,
449 Values(ReportParams().reportingType("Periodic"),
450 ReportParams().reportingType("OnChange")));
451
452TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall)
453{
454 ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success));
455
456 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
457 Eq(Readings{}));
458}
459
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200460class TestReportNonPeriodicReport :
461 public TestReport,
462 public WithParamInterface<ReportParams>
463{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200464 public:
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200465 void SetUp() override
466 {
467 sut = makeReport(GetParam());
468 }
469};
470
471INSTANTIATE_TEST_SUITE_P(_, TestReportNonPeriodicReport,
472 Values(ReportParams().reportingType("OnRequest"),
473 ReportParams().reportingType("OnChange")));
474
475TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires)
476{
477 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
478
479 EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"),
480 Eq(Readings{}));
481}
482
483class TestReportPeriodicReport : public TestReport
484{
485 void SetUp() override
486 {
487 sut = makeReport(ReportParams().reportingType("Periodic"));
Krzysztof Grobelnyc8e3a642020-10-23 12:29:16 +0200488 }
489};
490
491TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires)
492{
493 const uint64_t expectedTime = std::time(0);
494 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
495
496 const auto [timestamp, readings] =
497 getProperty<Readings>(sut->getPath(), "Readings");
498
499 EXPECT_THAT(timestamp, Ge(expectedTime));
500}
501
502TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires)
503{
504 DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms);
505
506 const auto [timestamp, readings] =
507 getProperty<Readings>(sut->getPath(), "Readings");
508
509 EXPECT_THAT(readings,
510 ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u),
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000511 std::make_tuple("aa"s, "bb"s, 42.0, 74u),
512 std::make_tuple("aaa"s, "bbb"s, 100.7, 21u)));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200513}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100514
515class TestReportInitialization : public TestReport
516{
517 public:
518 void SetUp() override
519 {}
520
521 void monitorProc(sdbusplus::message::message& msg)
522 {
523 std::string iface;
524 std::vector<std::pair<std::string, std::variant<Readings>>>
525 changed_properties;
526 std::vector<std::string> invalidated_properties;
527
528 msg.read(iface, changed_properties, invalidated_properties);
529
530 if (iface == Report::reportIfaceName)
531 {
532 for (const auto& [name, value] : changed_properties)
533 {
534 if (name == "Readings")
535 {
536 readingsUpdated.Call();
537 }
538 }
539 }
540 }
541
542 void makeMonitor()
543 {
544 monitor = std::make_unique<sdbusplus::bus::match::match>(
545 *DbusEnvironment::getBus(),
546 sdbusplus::bus::match::rules::propertiesChanged(
547 sut->getPath(), Report::reportIfaceName),
548 [this](auto& msg) { monitorProc(msg); });
549 }
550
551 std::unique_ptr<sdbusplus::bus::match::match> monitor;
552 MockFunction<void()> readingsUpdated;
553};
554
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100555TEST_F(TestReportInitialization, metricsAreInitializedWhenConstructed)
556{
557 for (auto& metric : metricMocks)
558 {
559 EXPECT_CALL(*metric, initialize());
560 }
561
562 sut = makeReport(ReportParams());
563}
564
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200565TEST_F(TestReportInitialization,
566 emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits)
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100567{
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200568 EXPECT_CALL(readingsUpdated, Call())
569 .WillOnce(
570 InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated")));
571
572 const auto elapsed = DbusEnvironment::measureTime([this] {
573 sut = makeReport(
574 defaultParams.reportingType("Periodic").emitReadingUpdate(true));
575 makeMonitor();
576 EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated"));
577 });
578
579 EXPECT_THAT(elapsed, AllOf(Ge(defaultParams.interval()),
580 Lt(defaultParams.interval() * 2)));
581}
582
583TEST_F(TestReportInitialization,
584 emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits)
585{
586 EXPECT_CALL(readingsUpdated, Call()).Times(0);
587
588 sut = makeReport(
589 defaultParams.reportingType("Periodic").emitReadingUpdate(false));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100590 makeMonitor();
Wludzik, Jozefb1ff1f62020-10-23 13:20:52 +0200591 DbusEnvironment::sleepFor(defaultParams.interval() * 2);
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100592}