blob: 6ce0f1fad31d571bbc6184a0b1217331d08827d9 [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"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020015
16using namespace testing;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000017using namespace std::string_literals;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010018using namespace std::chrono_literals;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020019
20class TestReportManager : public Test
21{
22 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010023 ReportParams reportParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020024
25 std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr =
26 std::make_unique<StrictMock<ReportFactoryMock>>();
27 ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010028
29 std::unique_ptr<StorageMock> storageMockPtr =
30 std::make_unique<NiceMock<StorageMock>>();
31 StorageMock& storageMock = *storageMockPtr;
32
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000033 std::unique_ptr<ReportMock> reportMockPtr =
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010034 std::make_unique<NiceMock<ReportMock>>(reportParams.reportId());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000035 ReportMock& reportMock = *reportMockPtr;
36
Wludzik, Jozefe2362792020-10-27 17:23:55 +010037 std::unique_ptr<ReportManager> sut;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020038
39 MockFunction<void(std::string)> checkPoint;
40
Wludzik, Jozefe2362792020-10-27 17:23:55 +010041 void SetUp() override
42 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000043 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _))
44 .Times(AnyNumber());
45
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010046 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
47 std::move(storageMockPtr),
48 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +010049 }
50
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020051 void TearDown() override
52 {
53 DbusEnvironment::synchronizeIoc();
54 }
55
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010056 template <class... Args>
57 requires(sizeof...(Args) > 1)
58 std::pair<boost::system::error_code, std::string> addReport(
59 Args&&... args)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020060 {
61 std::promise<std::pair<boost::system::error_code, std::string>>
62 addReportPromise;
63 DbusEnvironment::getBus()->async_method_call(
64 [&addReportPromise](boost::system::error_code ec,
65 const std::string& path) {
66 addReportPromise.set_value({ec, path});
67 },
68 DbusEnvironment::serviceName(), ReportManager::reportManagerPath,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000069 ReportManager::reportManagerIfaceName, "AddReportFutureVersion",
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010070 std::forward<Args>(args)...);
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010071 return DbusEnvironment::waitForFuture(addReportPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020072 }
73
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010074 auto addReport(const ReportParams& params)
75 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010076 return addReport(params.reportId(), params.reportName(),
77 utils::enumToString(params.reportingType()),
78 utils::enumToString(params.reportUpdates()),
79 params.appendLimit(),
80 utils::transform(params.reportActions(),
81 [](const auto v) {
82 return utils::enumToString(v);
83 }),
84 params.interval().count(),
85 toReadingParameters(params.metricParameters()));
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010086 }
87
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020088 template <class T>
Szymon Dompkee28aa532021-10-27 12:33:12 +020089 static T getProperty(const std::string& property)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020090 {
Szymon Dompkee28aa532021-10-27 12:33:12 +020091 return DbusEnvironment::getProperty<T>(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020092 ReportManager::reportManagerPath,
Szymon Dompkee28aa532021-10-27 12:33:12 +020093 ReportManager::reportManagerIfaceName, property);
Karol Niczyj32859b62021-05-19 10:20:46 +020094 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020095};
96
97TEST_F(TestReportManager, minInterval)
98{
99 EXPECT_THAT(getProperty<uint64_t>("MinInterval"),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000100 Eq(ReportManager::minInterval.count()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200101}
102
103TEST_F(TestReportManager, maxReports)
104{
Wludzik, Jozef503c1582020-12-11 14:48:01 +0100105 EXPECT_THAT(getProperty<size_t>("MaxReports"),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200106 Eq(ReportManager::maxReports));
107}
108
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100109TEST_F(TestReportManager, returnsPropertySupportedOperationTypes)
110{
111 EXPECT_THAT(
112 getProperty<std::vector<std::string>>("SupportedOperationTypes"),
113 UnorderedElementsAre("Maximum", "Minimum", "Average", "Summation"));
114}
115
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200116TEST_F(TestReportManager, addReport)
117{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000118 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
119 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200120 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
121
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100122 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200123 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
124 EXPECT_THAT(path, Eq(reportMock.getPath()));
125}
126
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100127TEST_F(TestReportManager, addOnChangeReport)
128{
129 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
130 reportFactoryMock
131 .expectMake(reportParams.reportingType(ReportingType::onChange),
132 Ref(*sut), Ref(storageMock))
133 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
134
135 auto [ec, path] = addReport(reportParams);
136 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
137 EXPECT_THAT(path, Eq(reportMock.getPath()));
138}
139
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100140TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsEmptyInAddReport)
Karol Niczyj32859b62021-05-19 10:20:46 +0200141{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100142 reportParams.reportId("ReportName");
143 reportParams.reportName("ReportName");
144
145 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
146
147 auto [ec, path] = addReport(reportParams.reportId(""));
148
149 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
150 EXPECT_THAT(path, Eq("/ReportName"));
151}
152
153TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsNamespace)
154{
155 reportParams.reportId("Prefix/ReportName");
156 reportParams.reportName("ReportName");
157
158 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
159
160 auto [ec, path] = addReport(reportParams.reportId("Prefix/"));
161
162 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
163 EXPECT_THAT(path, Eq("/Prefix/ReportName"));
164}
165
166TEST_F(TestReportManager, addReportWithMaxLengthId)
167{
Szymon Dompke32305f12022-07-05 15:37:21 +0200168 std::string reportId = utils::string_utils::getMaxId();
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100169 reportParams.reportId(reportId);
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000170 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
Karol Niczyj32859b62021-05-19 10:20:46 +0200171
172 auto [ec, path] = addReport(reportParams);
173
174 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100175 EXPECT_THAT(path, Eq("/"s + reportId));
Karol Niczyj32859b62021-05-19 10:20:46 +0200176}
177
Szymon Dompke32305f12022-07-05 15:37:21 +0200178TEST_F(TestReportManager, addReportWithMaxLengthPrefix)
179{
180 std::string reportId = utils::string_utils::getMaxPrefix() + "/MyId";
181 reportParams.reportId(reportId);
182 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
183
184 auto [ec, path] = addReport(reportParams);
185
186 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
187 EXPECT_THAT(path, Eq("/"s + reportId));
188}
189
190TEST_F(TestReportManager, addReportWithMaxLengthName)
191{
192 reportParams.reportName(utils::string_utils::getMaxName());
193 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
194
195 auto [ec, path] = addReport(reportParams);
196
197 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
198 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
199}
200
201TEST_F(TestReportManager, addReportWithMaxLengthMetricId)
202{
203 namespace ts = utils::tstring;
204 std::vector<LabeledMetricParameters> newMetricParams{
205 {LabeledMetricParameters{
206 {LabeledSensorInfo{"Service",
207 "/xyz/openbmc_project/sensors/power/p1",
208 "metadata1"}},
209 OperationType::avg,
210 utils::string_utils::getMaxId(),
211 CollectionTimeScope::point,
212 CollectionDuration(Milliseconds(0u))}}};
213
214 reportParams.metricParameters(newMetricParams);
215 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
216
217 auto [ec, path] = addReport(reportParams);
218
219 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
220 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
221}
222
223TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongFullId)
Karol Niczyj32859b62021-05-19 10:20:46 +0200224{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000225 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Karol Niczyj32859b62021-05-19 10:20:46 +0200226 .Times(0);
227
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100228 reportParams.reportId(
Szymon Dompke32305f12022-07-05 15:37:21 +0200229 std::string(utils::constants::maxReportFullIdLength + 1, 'z'));
230
231 auto [ec, path] = addReport(reportParams);
232
233 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
234 EXPECT_THAT(path, Eq(std::string()));
235}
236
237TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongId)
238{
239 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
240 .Times(0);
241
242 reportParams.reportId(utils::string_utils::getTooLongId());
243
244 auto [ec, path] = addReport(reportParams);
245
246 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
247 EXPECT_THAT(path, Eq(std::string()));
248}
249
250TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongPrefix)
251{
252 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
253 .Times(0);
254
255 reportParams.reportId(utils::string_utils::getTooLongPrefix() + "/MyId");
256
257 auto [ec, path] = addReport(reportParams);
258
259 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
260 EXPECT_THAT(path, Eq(std::string()));
261}
262
263TEST_F(TestReportManager, DISABLED_failToAddReportWithTooManyPrefixes)
264{
265 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
266 .Times(0);
267
268 std::string reportId;
269 for (size_t i = 0; i < utils::constants::maxPrefixesInId + 1; i++)
270 {
271 reportId += "prefix/";
272 }
273 reportId += "MyId";
274
275 reportParams.reportId(reportId);
276
277 auto [ec, path] = addReport(reportParams);
278
279 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
280 EXPECT_THAT(path, Eq(std::string()));
281}
282
283TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName)
284{
285 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
286 .Times(0);
287
288 reportParams.reportName(utils::string_utils::getTooLongName());
289
290 auto [ec, path] = addReport(reportParams);
291
292 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
293 EXPECT_THAT(path, Eq(std::string()));
294}
295
296TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongMetricId)
297{
298 namespace ts = utils::tstring;
299
300 std::vector<LabeledMetricParameters> newMetricParams{
301 {LabeledMetricParameters{
302 {LabeledSensorInfo{"Service",
303 "/xyz/openbmc_project/sensors/power/p1",
304 "metadata1"}},
305 OperationType::avg,
306 utils::string_utils::getTooLongId(),
307 CollectionTimeScope::point,
308 CollectionDuration(Milliseconds(0u))}}};
309
310 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
311 .Times(0);
312
313 reportParams.metricParameters(newMetricParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200314
315 auto [ec, path] = addReport(reportParams);
316
317 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
318 EXPECT_THAT(path, Eq(std::string()));
319}
320
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100321TEST_F(TestReportManager, DISABLED_failToAddReportTwice)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200322{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000323 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000324 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200325
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100326 addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200327
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100328 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200329
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200330 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
331 EXPECT_THAT(path, Eq(std::string()));
332}
333
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100334TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200335{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000336 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000337 .Times(0);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200338
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100339 reportParams.reportingType(ReportingType::periodic);
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200340 reportParams.interval(ReportManager::minInterval - 1ms);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200341
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100342 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200343
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200344 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
345 EXPECT_THAT(path, Eq(std::string()));
346}
347
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100348TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidReportingType)
349{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000350 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100351 .Times(0);
352
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100353 auto [ec, path] = addReport(
354 reportParams.reportName(), "InvalidReportingType",
355 utils::transform(reportParams.reportActions(),
356 [](const auto v) { return utils::enumToString(v); }),
357 reportParams.interval().count(),
358 toReadingParameters(reportParams.metricParameters()));
Karol Niczyj32859b62021-05-19 10:20:46 +0200359
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100360 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
361 EXPECT_THAT(path, Eq(std::string()));
362}
363
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000364TEST_F(TestReportManager,
365 DISABLED_failToAddReportWithMoreMetricPropertiesThanExpected)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100366{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000367 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100368 .Times(0);
369
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000370 reportParams.metricParameters(
371 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100372 {LabeledSensorInfo{"Service",
373 "/xyz/openbmc_project/sensors/power/p1",
374 "Metadata1"}},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100375 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000376 "MetricId1",
377 CollectionTimeScope::point,
378 CollectionDuration(Milliseconds(0u))}}});
379
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000380 auto metricParams = reportParams.metricParameters();
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000381 auto& metricParamsVec =
382 metricParams[0].at_label<utils::tstring::SensorPath>();
383
384 for (size_t i = 0; i < ReportManager::maxNumberMetrics; i++)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100385 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100386 metricParamsVec.emplace_back(LabeledSensorInfo{
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000387 "Service", "/xyz/openbmc_project/sensors/power/p1", "Metadata1"});
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100388 }
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000389
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000390 reportParams.metricParameters(std::move(metricParams));
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100391
392 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200393
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100394 EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long));
395 EXPECT_THAT(path, Eq(std::string()));
396}
397
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000398TEST_F(TestReportManager, DISABLED_failToAddReportWithMoreMetricsThanExpected)
399{
400 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
401 .Times(0);
402
403 auto metricParams = std::vector<LabeledMetricParameters>{};
404
405 for (size_t i = 0; i < ReportManager::maxNumberMetrics + 1; i++)
406 {
407 metricParams.emplace_back(
408 LabeledMetricParameters{{},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100409 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000410 "MetricId1",
411 CollectionTimeScope::point,
412 CollectionDuration(Milliseconds(0u))});
413 }
414
415 reportParams.metricParameters(std::move(metricParams));
416
417 auto [ec, path] = addReport(reportParams);
418
419 EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long));
420 EXPECT_THAT(path, Eq(std::string()));
421}
422
423TEST_F(TestReportManager, DISABLED_failToAddReportWithAppendLimitGreaterThanMax)
424{
425 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
426 .Times(0);
427
428 reportParams.appendLimit(ReportManager::maxAppendLimit + 1);
429
430 auto [ec, path] = addReport(reportParams);
431
432 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
433 EXPECT_THAT(path, Eq(std::string()));
434}
435
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100436TEST_F(TestReportManager, addReportWithAppendLimitEqualToUint64MaxIsAllowed)
437{
438 reportParams.appendLimit(std::numeric_limits<uint64_t>::max());
439
440 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
441 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
442 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
443
444 auto [ec, path] = addReport(reportParams);
445 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
446 EXPECT_THAT(path, Eq(reportMock.getPath()));
447}
448
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100449TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200450{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000451 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200452 .Times(ReportManager::maxReports);
453
454 for (size_t i = 0; i < ReportManager::maxReports; i++)
455 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100456 reportParams.reportId(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200457
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100458 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200459 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
460 }
461
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100462 reportParams.reportId(reportParams.reportName() +
463 std::to_string(ReportManager::maxReports));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100464 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200465
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200466 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
467 EXPECT_THAT(path, Eq(std::string()));
468}
469
470TEST_F(TestReportManager, removeReport)
471{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200472 {
473 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000474 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
475 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200476 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
477 EXPECT_CALL(reportMock, Die());
478 EXPECT_CALL(checkPoint, Call("end"));
479 }
480
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100481 addReport(reportParams);
482 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200483 checkPoint.Call("end");
484}
485
486TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
487{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200488 {
489 InSequence seq;
490 EXPECT_CALL(checkPoint, Call("end"));
491 EXPECT_CALL(reportMock, Die());
492 }
493
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100494 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200495 checkPoint.Call("end");
496}
497
498TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
499{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200500 {
501 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000502 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
503 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200504 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
505 EXPECT_CALL(reportMock, Die());
506 EXPECT_CALL(checkPoint, Call("end"));
507 }
508
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100509 addReport(reportParams);
510 sut->removeReport(&reportMock);
511 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200512 checkPoint.Call("end");
513}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100514
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000515class TestReportManagerWithAggregationOperationType :
516 public TestReportManager,
517 public WithParamInterface<OperationType>
518{
519 public:
520 OperationType operationType = GetParam();
521};
522
523INSTANTIATE_TEST_SUITE_P(_, TestReportManagerWithAggregationOperationType,
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100524 Values(OperationType::max, OperationType::min,
525 OperationType::avg, OperationType::sum));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000526
527TEST_P(TestReportManagerWithAggregationOperationType,
528 addReportWithDifferentOperationTypes)
529{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000530 reportParams.metricParameters(
531 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100532 {LabeledSensorInfo{"Service",
533 "/xyz/openbmc_project/sensors/power/p1",
534 "Metadata1"}},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000535 operationType,
536 "MetricId1",
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000537 CollectionTimeScope::point,
538 CollectionDuration(Milliseconds(0u))}}});
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000539
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000540 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000541 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
542
543 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200544
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000545 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100546 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000547}
548
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100549class TestReportManagerStorage : public TestReportManager
550{
551 public:
552 using FilePath = interfaces::JsonStorage::FilePath;
553 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
554
555 void SetUp() override
556 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000557 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)).Times(0);
558
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100559 ON_CALL(storageMock, list())
560 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
561 ON_CALL(storageMock, load(FilePath("report1")))
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100562 .WillByDefault(InvokeWithoutArgs([this] { return data; }));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100563 }
564
565 void makeReportManager()
566 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100567 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
568 std::move(storageMockPtr),
569 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100570 }
571
572 nlohmann::json data = nlohmann::json{
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200573 {"Enabled", reportParams.enabled()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100574 {"Version", Report::reportVersion},
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100575 {"Id", reportParams.reportId()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100576 {"Name", reportParams.reportName()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100577 {"ReportingType", utils::toUnderlying(reportParams.reportingType())},
578 {"ReportActions", reportParams.reportActions()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100579 {"Interval", reportParams.interval().count()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100580 {"ReportUpdates", utils::toUnderlying(reportParams.reportUpdates())},
Szymon Dompke3eb56862021-09-20 15:32:04 +0200581 {"AppendLimit", reportParams.appendLimit()},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000582 {"ReadingParameters", reportParams.metricParameters()}};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100583};
584
585TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
586{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000587 reportFactoryMock.expectMake(reportParams, _, Ref(storageMock));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100588
589 makeReportManager();
590}
591
592TEST_F(TestReportManagerStorage,
593 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
594{
595 data["Version"] = Report::reportVersion - 1;
596
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100597 EXPECT_CALL(storageMock, remove(FilePath("report1")));
598
599 makeReportManager();
600}
601
602TEST_F(TestReportManagerStorage,
603 reportManagerCtorRemoveFileIfIntervalHasWrongType)
604{
605 data["Interval"] = "1000";
606
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100607 EXPECT_CALL(storageMock, remove(FilePath("report1")));
608
609 makeReportManager();
610}