blob: f593fd4b9b26ce22d16830d9ca6f6397a240ae0a [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"
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +01003#include "interfaces/trigger_manager.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01004#include "mocks/json_storage_mock.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02005#include "mocks/report_factory_mock.hpp"
Szymon Dompkeb4ef22e2022-02-07 15:15:12 +01006#include "mocks/trigger_manager_mock.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01007#include "params/report_params.hpp"
8#include "report.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02009#include "report_manager.hpp"
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000010#include "utils/conversion.hpp"
Szymon Dompke32305f12022-07-05 15:37:21 +020011#include "utils/dbus_path_utils.hpp"
12#include "utils/string_utils.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +010013#include "utils/transform.hpp"
Szymon Dompke32305f12022-07-05 15:37:21 +020014#include "utils/tstring.hpp"
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020015#include "utils/variant_utils.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020016
17using namespace testing;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000018using namespace std::string_literals;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010019using namespace std::chrono_literals;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020020
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020021using AddReportFutureVersionVariantForSet =
22 utils::WithoutMonostate<AddReportFutureVersionVariant>;
23
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020024class TestReportManager : public Test
25{
26 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010027 ReportParams reportParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020028
29 std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr =
30 std::make_unique<StrictMock<ReportFactoryMock>>();
31 ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010032
33 std::unique_ptr<StorageMock> storageMockPtr =
34 std::make_unique<NiceMock<StorageMock>>();
35 StorageMock& storageMock = *storageMockPtr;
36
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000037 std::unique_ptr<ReportMock> reportMockPtr =
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010038 std::make_unique<NiceMock<ReportMock>>(reportParams.reportId());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000039 ReportMock& reportMock = *reportMockPtr;
40
Wludzik, Jozefe2362792020-10-27 17:23:55 +010041 std::unique_ptr<ReportManager> sut;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020042
43 MockFunction<void(std::string)> checkPoint;
44
Wludzik, Jozefe2362792020-10-27 17:23:55 +010045 void SetUp() override
46 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000047 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _))
48 .Times(AnyNumber());
49
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010050 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
51 std::move(storageMockPtr),
52 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +010053 }
54
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020055 void TearDown() override
56 {
57 DbusEnvironment::synchronizeIoc();
58 }
59
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020060 std::pair<boost::system::error_code, std::string>
61 addReport(const std::vector<
62 std::pair<std::string, AddReportFutureVersionVariantForSet>>&
63 properties)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020064 {
65 std::promise<std::pair<boost::system::error_code, std::string>>
66 addReportPromise;
67 DbusEnvironment::getBus()->async_method_call(
68 [&addReportPromise](boost::system::error_code ec,
69 const std::string& path) {
70 addReportPromise.set_value({ec, path});
71 },
72 DbusEnvironment::serviceName(), ReportManager::reportManagerPath,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000073 ReportManager::reportManagerIfaceName, "AddReportFutureVersion",
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020074 properties);
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010075 return DbusEnvironment::waitForFuture(addReportPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020076 }
77
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010078 auto addReport(const ReportParams& params)
79 {
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020080 std::vector<std::pair<std::string, AddReportFutureVersionVariantForSet>>
81 properties;
82
83 properties.emplace_back("Id", params.reportId());
84 properties.emplace_back("Name", params.reportName());
85 properties.emplace_back("ReportingType",
86 utils::enumToString(params.reportingType()));
87 properties.emplace_back("ReportUpdates",
88 utils::enumToString(params.reportUpdates()));
89 properties.emplace_back("AppendLimit", params.appendLimit());
90 properties.emplace_back("Enabled", params.enabled());
91 properties.emplace_back(
92 "ReportActions",
93 utils::transform(params.reportActions(), [](const auto v) {
94 return utils::enumToString(v);
95 }));
96 properties.emplace_back("Interval", params.interval().count());
97 properties.emplace_back("MetricParams",
98 toReadingParameters(params.metricParameters()));
99
100 return addReport(properties);
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100101 }
102
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200103 template <class T>
Szymon Dompkee28aa532021-10-27 12:33:12 +0200104 static T getProperty(const std::string& property)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200105 {
Szymon Dompkee28aa532021-10-27 12:33:12 +0200106 return DbusEnvironment::getProperty<T>(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200107 ReportManager::reportManagerPath,
Szymon Dompkee28aa532021-10-27 12:33:12 +0200108 ReportManager::reportManagerIfaceName, property);
Karol Niczyj32859b62021-05-19 10:20:46 +0200109 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200110};
111
112TEST_F(TestReportManager, minInterval)
113{
114 EXPECT_THAT(getProperty<uint64_t>("MinInterval"),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000115 Eq(ReportManager::minInterval.count()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200116}
117
118TEST_F(TestReportManager, maxReports)
119{
Wludzik, Jozef503c1582020-12-11 14:48:01 +0100120 EXPECT_THAT(getProperty<size_t>("MaxReports"),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200121 Eq(ReportManager::maxReports));
122}
123
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100124TEST_F(TestReportManager, returnsPropertySupportedOperationTypes)
125{
126 EXPECT_THAT(
127 getProperty<std::vector<std::string>>("SupportedOperationTypes"),
128 UnorderedElementsAre("Maximum", "Minimum", "Average", "Summation"));
129}
130
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200131TEST_F(TestReportManager, addReport)
132{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000133 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
134 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200135 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
136
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100137 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200138 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
139 EXPECT_THAT(path, Eq(reportMock.getPath()));
140}
141
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200142TEST_F(TestReportManager, addDisabledReport)
143{
144 reportParams.enabled(false);
145
146 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
147 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
148 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
149
150 auto [ec, path] = addReport(reportParams);
151 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
152 EXPECT_THAT(path, Eq(reportMock.getPath()));
153}
154
155TEST_F(TestReportManager, addReportWithOnlyDefaultParams)
156{
157 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
158 EXPECT_CALL(reportFactoryMock,
159 make("Report"s, "Report"s, ReportingType::onRequest,
160 std::vector<ReportAction>{}, Milliseconds{}, 0,
161 ReportUpdates::overwrite, _, _,
162 std::vector<LabeledMetricParameters>{}, true, Readings{}))
163 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
164
165 auto [ec, path] = addReport(
166 std::vector<
167 std::pair<std::string, AddReportFutureVersionVariantForSet>>{});
168 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
169 EXPECT_THAT(path, Eq(reportMock.getPath()));
170}
171
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100172TEST_F(TestReportManager, addOnChangeReport)
173{
174 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
175 reportFactoryMock
176 .expectMake(reportParams.reportingType(ReportingType::onChange),
177 Ref(*sut), Ref(storageMock))
178 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
179
180 auto [ec, path] = addReport(reportParams);
181 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
182 EXPECT_THAT(path, Eq(reportMock.getPath()));
183}
184
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100185TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsEmptyInAddReport)
Karol Niczyj32859b62021-05-19 10:20:46 +0200186{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100187 reportParams.reportId("ReportName");
188 reportParams.reportName("ReportName");
189
190 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
191
192 auto [ec, path] = addReport(reportParams.reportId(""));
193
194 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
195 EXPECT_THAT(path, Eq("/ReportName"));
196}
197
198TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsNamespace)
199{
200 reportParams.reportId("Prefix/ReportName");
201 reportParams.reportName("ReportName");
202
203 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
204
205 auto [ec, path] = addReport(reportParams.reportId("Prefix/"));
206
207 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
208 EXPECT_THAT(path, Eq("/Prefix/ReportName"));
209}
210
211TEST_F(TestReportManager, addReportWithMaxLengthId)
212{
Szymon Dompke32305f12022-07-05 15:37:21 +0200213 std::string reportId = utils::string_utils::getMaxId();
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100214 reportParams.reportId(reportId);
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000215 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
Karol Niczyj32859b62021-05-19 10:20:46 +0200216
217 auto [ec, path] = addReport(reportParams);
218
219 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100220 EXPECT_THAT(path, Eq("/"s + reportId));
Karol Niczyj32859b62021-05-19 10:20:46 +0200221}
222
Szymon Dompke32305f12022-07-05 15:37:21 +0200223TEST_F(TestReportManager, addReportWithMaxLengthPrefix)
224{
225 std::string reportId = utils::string_utils::getMaxPrefix() + "/MyId";
226 reportParams.reportId(reportId);
227 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
228
229 auto [ec, path] = addReport(reportParams);
230
231 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
232 EXPECT_THAT(path, Eq("/"s + reportId));
233}
234
235TEST_F(TestReportManager, addReportWithMaxLengthName)
236{
237 reportParams.reportName(utils::string_utils::getMaxName());
238 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
239
240 auto [ec, path] = addReport(reportParams);
241
242 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
243 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
244}
245
246TEST_F(TestReportManager, addReportWithMaxLengthMetricId)
247{
248 namespace ts = utils::tstring;
249 std::vector<LabeledMetricParameters> newMetricParams{
250 {LabeledMetricParameters{
251 {LabeledSensorInfo{"Service",
252 "/xyz/openbmc_project/sensors/power/p1",
253 "metadata1"}},
254 OperationType::avg,
255 utils::string_utils::getMaxId(),
256 CollectionTimeScope::point,
257 CollectionDuration(Milliseconds(0u))}}};
258
259 reportParams.metricParameters(newMetricParams);
260 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
261
262 auto [ec, path] = addReport(reportParams);
263
264 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
265 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
266}
267
268TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongFullId)
Karol Niczyj32859b62021-05-19 10:20:46 +0200269{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000270 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Karol Niczyj32859b62021-05-19 10:20:46 +0200271 .Times(0);
272
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100273 reportParams.reportId(
Szymon Dompke32305f12022-07-05 15:37:21 +0200274 std::string(utils::constants::maxReportFullIdLength + 1, 'z'));
275
276 auto [ec, path] = addReport(reportParams);
277
278 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
279 EXPECT_THAT(path, Eq(std::string()));
280}
281
282TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongId)
283{
284 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
285 .Times(0);
286
287 reportParams.reportId(utils::string_utils::getTooLongId());
288
289 auto [ec, path] = addReport(reportParams);
290
291 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
292 EXPECT_THAT(path, Eq(std::string()));
293}
294
295TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongPrefix)
296{
297 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
298 .Times(0);
299
300 reportParams.reportId(utils::string_utils::getTooLongPrefix() + "/MyId");
301
302 auto [ec, path] = addReport(reportParams);
303
304 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
305 EXPECT_THAT(path, Eq(std::string()));
306}
307
308TEST_F(TestReportManager, DISABLED_failToAddReportWithTooManyPrefixes)
309{
310 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
311 .Times(0);
312
313 std::string reportId;
314 for (size_t i = 0; i < utils::constants::maxPrefixesInId + 1; i++)
315 {
316 reportId += "prefix/";
317 }
318 reportId += "MyId";
319
320 reportParams.reportId(reportId);
321
322 auto [ec, path] = addReport(reportParams);
323
324 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
325 EXPECT_THAT(path, Eq(std::string()));
326}
327
328TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName)
329{
330 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
331 .Times(0);
332
333 reportParams.reportName(utils::string_utils::getTooLongName());
334
335 auto [ec, path] = addReport(reportParams);
336
337 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
338 EXPECT_THAT(path, Eq(std::string()));
339}
340
341TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongMetricId)
342{
343 namespace ts = utils::tstring;
344
345 std::vector<LabeledMetricParameters> newMetricParams{
346 {LabeledMetricParameters{
347 {LabeledSensorInfo{"Service",
348 "/xyz/openbmc_project/sensors/power/p1",
349 "metadata1"}},
350 OperationType::avg,
351 utils::string_utils::getTooLongId(),
352 CollectionTimeScope::point,
353 CollectionDuration(Milliseconds(0u))}}};
354
355 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
356 .Times(0);
357
358 reportParams.metricParameters(newMetricParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200359
360 auto [ec, path] = addReport(reportParams);
361
362 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
363 EXPECT_THAT(path, Eq(std::string()));
364}
365
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100366TEST_F(TestReportManager, DISABLED_failToAddReportTwice)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200367{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000368 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000369 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200370
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100371 addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200372
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100373 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200374
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200375 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
376 EXPECT_THAT(path, Eq(std::string()));
377}
378
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100379TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200380{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000381 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000382 .Times(0);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200383
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100384 reportParams.reportingType(ReportingType::periodic);
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200385 reportParams.interval(ReportManager::minInterval - 1ms);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200386
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100387 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200388
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200389 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
390 EXPECT_THAT(path, Eq(std::string()));
391}
392
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100393TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidReportingType)
394{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000395 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100396 .Times(0);
397
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200398 auto [ec, path] = addReport({{"Name", reportParams.reportName()},
399 {"ReportingType", "InvalidReportingType"}});
Karol Niczyj32859b62021-05-19 10:20:46 +0200400
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100401 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
402 EXPECT_THAT(path, Eq(std::string()));
403}
404
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000405TEST_F(TestReportManager,
406 DISABLED_failToAddReportWithMoreMetricPropertiesThanExpected)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100407{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000408 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100409 .Times(0);
410
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000411 reportParams.metricParameters(
412 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100413 {LabeledSensorInfo{"Service",
414 "/xyz/openbmc_project/sensors/power/p1",
415 "Metadata1"}},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100416 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000417 "MetricId1",
418 CollectionTimeScope::point,
419 CollectionDuration(Milliseconds(0u))}}});
420
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000421 auto metricParams = reportParams.metricParameters();
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000422 auto& metricParamsVec =
423 metricParams[0].at_label<utils::tstring::SensorPath>();
424
425 for (size_t i = 0; i < ReportManager::maxNumberMetrics; i++)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100426 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100427 metricParamsVec.emplace_back(LabeledSensorInfo{
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000428 "Service", "/xyz/openbmc_project/sensors/power/p1", "Metadata1"});
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100429 }
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000430
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000431 reportParams.metricParameters(std::move(metricParams));
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100432
433 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200434
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100435 EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long));
436 EXPECT_THAT(path, Eq(std::string()));
437}
438
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000439TEST_F(TestReportManager, DISABLED_failToAddReportWithMoreMetricsThanExpected)
440{
441 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
442 .Times(0);
443
444 auto metricParams = std::vector<LabeledMetricParameters>{};
445
446 for (size_t i = 0; i < ReportManager::maxNumberMetrics + 1; i++)
447 {
448 metricParams.emplace_back(
449 LabeledMetricParameters{{},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100450 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000451 "MetricId1",
452 CollectionTimeScope::point,
453 CollectionDuration(Milliseconds(0u))});
454 }
455
456 reportParams.metricParameters(std::move(metricParams));
457
458 auto [ec, path] = addReport(reportParams);
459
460 EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long));
461 EXPECT_THAT(path, Eq(std::string()));
462}
463
464TEST_F(TestReportManager, DISABLED_failToAddReportWithAppendLimitGreaterThanMax)
465{
466 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
467 .Times(0);
468
469 reportParams.appendLimit(ReportManager::maxAppendLimit + 1);
470
471 auto [ec, path] = addReport(reportParams);
472
473 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
474 EXPECT_THAT(path, Eq(std::string()));
475}
476
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100477TEST_F(TestReportManager, addReportWithAppendLimitEqualToUint64MaxIsAllowed)
478{
479 reportParams.appendLimit(std::numeric_limits<uint64_t>::max());
480
481 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
482 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
483 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
484
485 auto [ec, path] = addReport(reportParams);
486 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
487 EXPECT_THAT(path, Eq(reportMock.getPath()));
488}
489
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100490TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200491{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000492 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200493 .Times(ReportManager::maxReports);
494
495 for (size_t i = 0; i < ReportManager::maxReports; i++)
496 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100497 reportParams.reportId(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200498
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100499 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200500 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
501 }
502
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100503 reportParams.reportId(reportParams.reportName() +
504 std::to_string(ReportManager::maxReports));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100505 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200506
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200507 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
508 EXPECT_THAT(path, Eq(std::string()));
509}
510
511TEST_F(TestReportManager, removeReport)
512{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200513 {
514 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000515 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
516 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200517 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
518 EXPECT_CALL(reportMock, Die());
519 EXPECT_CALL(checkPoint, Call("end"));
520 }
521
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100522 addReport(reportParams);
523 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200524 checkPoint.Call("end");
525}
526
527TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
528{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200529 {
530 InSequence seq;
531 EXPECT_CALL(checkPoint, Call("end"));
532 EXPECT_CALL(reportMock, Die());
533 }
534
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100535 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200536 checkPoint.Call("end");
537}
538
539TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
540{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200541 {
542 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000543 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
544 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200545 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
546 EXPECT_CALL(reportMock, Die());
547 EXPECT_CALL(checkPoint, Call("end"));
548 }
549
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100550 addReport(reportParams);
551 sut->removeReport(&reportMock);
552 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200553 checkPoint.Call("end");
554}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100555
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000556class TestReportManagerWithAggregationOperationType :
557 public TestReportManager,
558 public WithParamInterface<OperationType>
559{
560 public:
561 OperationType operationType = GetParam();
562};
563
564INSTANTIATE_TEST_SUITE_P(_, TestReportManagerWithAggregationOperationType,
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100565 Values(OperationType::max, OperationType::min,
566 OperationType::avg, OperationType::sum));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000567
568TEST_P(TestReportManagerWithAggregationOperationType,
569 addReportWithDifferentOperationTypes)
570{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000571 reportParams.metricParameters(
572 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100573 {LabeledSensorInfo{"Service",
574 "/xyz/openbmc_project/sensors/power/p1",
575 "Metadata1"}},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000576 operationType,
577 "MetricId1",
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000578 CollectionTimeScope::point,
579 CollectionDuration(Milliseconds(0u))}}});
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000580
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000581 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000582 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
583
584 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200585
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000586 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100587 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000588}
589
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100590class TestReportManagerStorage : public TestReportManager
591{
592 public:
593 using FilePath = interfaces::JsonStorage::FilePath;
594 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
595
596 void SetUp() override
597 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000598 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)).Times(0);
599
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100600 ON_CALL(storageMock, list())
601 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
602 ON_CALL(storageMock, load(FilePath("report1")))
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100603 .WillByDefault(InvokeWithoutArgs([this] { return data; }));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100604 }
605
606 void makeReportManager()
607 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100608 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
609 std::move(storageMockPtr),
610 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100611 }
612
613 nlohmann::json data = nlohmann::json{
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200614 {"Enabled", reportParams.enabled()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100615 {"Version", Report::reportVersion},
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100616 {"Id", reportParams.reportId()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100617 {"Name", reportParams.reportName()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100618 {"ReportingType", utils::toUnderlying(reportParams.reportingType())},
619 {"ReportActions", reportParams.reportActions()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100620 {"Interval", reportParams.interval().count()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100621 {"ReportUpdates", utils::toUnderlying(reportParams.reportUpdates())},
Szymon Dompke3eb56862021-09-20 15:32:04 +0200622 {"AppendLimit", reportParams.appendLimit()},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000623 {"ReadingParameters", reportParams.metricParameters()}};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100624};
625
626TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
627{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000628 reportFactoryMock.expectMake(reportParams, _, Ref(storageMock));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100629
630 makeReportManager();
631}
632
633TEST_F(TestReportManagerStorage,
634 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
635{
636 data["Version"] = Report::reportVersion - 1;
637
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100638 EXPECT_CALL(storageMock, remove(FilePath("report1")));
639
640 makeReportManager();
641}
642
643TEST_F(TestReportManagerStorage,
644 reportManagerCtorRemoveFileIfIntervalHasWrongType)
645{
646 data["Interval"] = "1000";
647
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100648 EXPECT_CALL(storageMock, remove(FilePath("report1")));
649
650 makeReportManager();
651}