blob: 55b5a3c86f44cdb680dd005628378b4a66aeba88 [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"
Wludzik, Jozefe2362792020-10-27 17:23:55 +010011#include "utils/transform.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020012
13using namespace testing;
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +000014using namespace std::string_literals;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010015using namespace std::chrono_literals;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020016
17class TestReportManager : public Test
18{
19 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010020 ReportParams reportParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020021
22 std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr =
23 std::make_unique<StrictMock<ReportFactoryMock>>();
24 ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010025
26 std::unique_ptr<StorageMock> storageMockPtr =
27 std::make_unique<NiceMock<StorageMock>>();
28 StorageMock& storageMock = *storageMockPtr;
29
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000030 std::unique_ptr<ReportMock> reportMockPtr =
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010031 std::make_unique<NiceMock<ReportMock>>(reportParams.reportId());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000032 ReportMock& reportMock = *reportMockPtr;
33
Wludzik, Jozefe2362792020-10-27 17:23:55 +010034 std::unique_ptr<ReportManager> sut;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020035
36 MockFunction<void(std::string)> checkPoint;
37
Wludzik, Jozefe2362792020-10-27 17:23:55 +010038 void SetUp() override
39 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000040 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _))
41 .Times(AnyNumber());
42
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010043 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
44 std::move(storageMockPtr),
45 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +010046 }
47
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020048 void TearDown() override
49 {
50 DbusEnvironment::synchronizeIoc();
51 }
52
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010053 template <class... Args>
54 requires(sizeof...(Args) > 1)
55 std::pair<boost::system::error_code, std::string> addReport(
56 Args&&... args)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020057 {
58 std::promise<std::pair<boost::system::error_code, std::string>>
59 addReportPromise;
60 DbusEnvironment::getBus()->async_method_call(
61 [&addReportPromise](boost::system::error_code ec,
62 const std::string& path) {
63 addReportPromise.set_value({ec, path});
64 },
65 DbusEnvironment::serviceName(), ReportManager::reportManagerPath,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000066 ReportManager::reportManagerIfaceName, "AddReportFutureVersion",
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010067 std::forward<Args>(args)...);
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010068 return DbusEnvironment::waitForFuture(addReportPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020069 }
70
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010071 auto addReport(const ReportParams& params)
72 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010073 return addReport(params.reportId(), params.reportName(),
74 utils::enumToString(params.reportingType()),
75 utils::enumToString(params.reportUpdates()),
76 params.appendLimit(),
77 utils::transform(params.reportActions(),
78 [](const auto v) {
79 return utils::enumToString(v);
80 }),
81 params.interval().count(),
82 toReadingParameters(params.metricParameters()));
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010083 }
84
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020085 template <class T>
Szymon Dompkee28aa532021-10-27 12:33:12 +020086 static T getProperty(const std::string& property)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020087 {
Szymon Dompkee28aa532021-10-27 12:33:12 +020088 return DbusEnvironment::getProperty<T>(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020089 ReportManager::reportManagerPath,
Szymon Dompkee28aa532021-10-27 12:33:12 +020090 ReportManager::reportManagerIfaceName, property);
Karol Niczyj32859b62021-05-19 10:20:46 +020091 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020092};
93
94TEST_F(TestReportManager, minInterval)
95{
96 EXPECT_THAT(getProperty<uint64_t>("MinInterval"),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000097 Eq(ReportManager::minInterval.count()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020098}
99
100TEST_F(TestReportManager, maxReports)
101{
Wludzik, Jozef503c1582020-12-11 14:48:01 +0100102 EXPECT_THAT(getProperty<size_t>("MaxReports"),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200103 Eq(ReportManager::maxReports));
104}
105
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100106TEST_F(TestReportManager, returnsPropertySupportedOperationTypes)
107{
108 EXPECT_THAT(
109 getProperty<std::vector<std::string>>("SupportedOperationTypes"),
110 UnorderedElementsAre("Maximum", "Minimum", "Average", "Summation"));
111}
112
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200113TEST_F(TestReportManager, addReport)
114{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000115 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
116 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200117 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
118
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100119 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200120 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
121 EXPECT_THAT(path, Eq(reportMock.getPath()));
122}
123
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100124TEST_F(TestReportManager, addOnChangeReport)
125{
126 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
127 reportFactoryMock
128 .expectMake(reportParams.reportingType(ReportingType::onChange),
129 Ref(*sut), Ref(storageMock))
130 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
131
132 auto [ec, path] = addReport(reportParams);
133 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
134 EXPECT_THAT(path, Eq(reportMock.getPath()));
135}
136
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100137TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsEmptyInAddReport)
Karol Niczyj32859b62021-05-19 10:20:46 +0200138{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100139 reportParams.reportId("ReportName");
140 reportParams.reportName("ReportName");
141
142 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
143
144 auto [ec, path] = addReport(reportParams.reportId(""));
145
146 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
147 EXPECT_THAT(path, Eq("/ReportName"));
148}
149
150TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsNamespace)
151{
152 reportParams.reportId("Prefix/ReportName");
153 reportParams.reportName("ReportName");
154
155 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
156
157 auto [ec, path] = addReport(reportParams.reportId("Prefix/"));
158
159 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
160 EXPECT_THAT(path, Eq("/Prefix/ReportName"));
161}
162
163TEST_F(TestReportManager, addReportWithMaxLengthId)
164{
165 std::string reportId(ReportManager::maxReportIdLength, 'z');
166 reportParams.reportId(reportId);
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000167 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
Karol Niczyj32859b62021-05-19 10:20:46 +0200168
169 auto [ec, path] = addReport(reportParams);
170
171 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100172 EXPECT_THAT(path, Eq("/"s + reportId));
Karol Niczyj32859b62021-05-19 10:20:46 +0200173}
174
175TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName)
176{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000177 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Karol Niczyj32859b62021-05-19 10:20:46 +0200178 .Times(0);
179
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100180 reportParams.reportId(
181 std::string(ReportManager::maxReportIdLength + 1, 'z'));
Karol Niczyj32859b62021-05-19 10:20:46 +0200182
183 auto [ec, path] = addReport(reportParams);
184
185 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
186 EXPECT_THAT(path, Eq(std::string()));
187}
188
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100189TEST_F(TestReportManager, DISABLED_failToAddReportTwice)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200190{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000191 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000192 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200193
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100194 addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200195
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100196 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200197
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200198 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
199 EXPECT_THAT(path, Eq(std::string()));
200}
201
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100202TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200203{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000204 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000205 .Times(0);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200206
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100207 reportParams.reportingType(ReportingType::periodic);
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100208 reportParams.interval(reportParams.interval() - 1ms);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200209
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100210 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200211
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200212 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
213 EXPECT_THAT(path, Eq(std::string()));
214}
215
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100216TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidReportingType)
217{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000218 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100219 .Times(0);
220
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100221 auto [ec, path] = addReport(
222 reportParams.reportName(), "InvalidReportingType",
223 utils::transform(reportParams.reportActions(),
224 [](const auto v) { return utils::enumToString(v); }),
225 reportParams.interval().count(),
226 toReadingParameters(reportParams.metricParameters()));
Karol Niczyj32859b62021-05-19 10:20:46 +0200227
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100228 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
229 EXPECT_THAT(path, Eq(std::string()));
230}
231
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000232TEST_F(TestReportManager,
233 DISABLED_failToAddReportWithMoreMetricPropertiesThanExpected)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100234{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000235 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100236 .Times(0);
237
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000238 reportParams.metricParameters(
239 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100240 {LabeledSensorInfo{"Service",
241 "/xyz/openbmc_project/sensors/power/p1",
242 "Metadata1"}},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100243 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000244 "MetricId1",
245 CollectionTimeScope::point,
246 CollectionDuration(Milliseconds(0u))}}});
247
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000248 auto metricParams = reportParams.metricParameters();
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000249 auto& metricParamsVec =
250 metricParams[0].at_label<utils::tstring::SensorPath>();
251
252 for (size_t i = 0; i < ReportManager::maxNumberMetrics; i++)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100253 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100254 metricParamsVec.emplace_back(LabeledSensorInfo{
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000255 "Service", "/xyz/openbmc_project/sensors/power/p1", "Metadata1"});
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100256 }
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000257
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000258 reportParams.metricParameters(std::move(metricParams));
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100259
260 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200261
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100262 EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long));
263 EXPECT_THAT(path, Eq(std::string()));
264}
265
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000266TEST_F(TestReportManager, DISABLED_failToAddReportWithMoreMetricsThanExpected)
267{
268 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
269 .Times(0);
270
271 auto metricParams = std::vector<LabeledMetricParameters>{};
272
273 for (size_t i = 0; i < ReportManager::maxNumberMetrics + 1; i++)
274 {
275 metricParams.emplace_back(
276 LabeledMetricParameters{{},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100277 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000278 "MetricId1",
279 CollectionTimeScope::point,
280 CollectionDuration(Milliseconds(0u))});
281 }
282
283 reportParams.metricParameters(std::move(metricParams));
284
285 auto [ec, path] = addReport(reportParams);
286
287 EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long));
288 EXPECT_THAT(path, Eq(std::string()));
289}
290
291TEST_F(TestReportManager, DISABLED_failToAddReportWithAppendLimitGreaterThanMax)
292{
293 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
294 .Times(0);
295
296 reportParams.appendLimit(ReportManager::maxAppendLimit + 1);
297
298 auto [ec, path] = addReport(reportParams);
299
300 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
301 EXPECT_THAT(path, Eq(std::string()));
302}
303
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100304TEST_F(TestReportManager, addReportWithAppendLimitEqualToUint64MaxIsAllowed)
305{
306 reportParams.appendLimit(std::numeric_limits<uint64_t>::max());
307
308 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
309 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
310 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
311
312 auto [ec, path] = addReport(reportParams);
313 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
314 EXPECT_THAT(path, Eq(reportMock.getPath()));
315}
316
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100317TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200318{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000319 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200320 .Times(ReportManager::maxReports);
321
322 for (size_t i = 0; i < ReportManager::maxReports; i++)
323 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100324 reportParams.reportId(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200325
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100326 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200327 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
328 }
329
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100330 reportParams.reportId(reportParams.reportName() +
331 std::to_string(ReportManager::maxReports));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100332 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200333
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200334 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
335 EXPECT_THAT(path, Eq(std::string()));
336}
337
338TEST_F(TestReportManager, removeReport)
339{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200340 {
341 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000342 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
343 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200344 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
345 EXPECT_CALL(reportMock, Die());
346 EXPECT_CALL(checkPoint, Call("end"));
347 }
348
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100349 addReport(reportParams);
350 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200351 checkPoint.Call("end");
352}
353
354TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
355{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200356 {
357 InSequence seq;
358 EXPECT_CALL(checkPoint, Call("end"));
359 EXPECT_CALL(reportMock, Die());
360 }
361
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100362 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200363 checkPoint.Call("end");
364}
365
366TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
367{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200368 {
369 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000370 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
371 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200372 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
373 EXPECT_CALL(reportMock, Die());
374 EXPECT_CALL(checkPoint, Call("end"));
375 }
376
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100377 addReport(reportParams);
378 sut->removeReport(&reportMock);
379 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200380 checkPoint.Call("end");
381}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100382
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000383class TestReportManagerWithAggregationOperationType :
384 public TestReportManager,
385 public WithParamInterface<OperationType>
386{
387 public:
388 OperationType operationType = GetParam();
389};
390
391INSTANTIATE_TEST_SUITE_P(_, TestReportManagerWithAggregationOperationType,
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100392 Values(OperationType::max, OperationType::min,
393 OperationType::avg, OperationType::sum));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000394
395TEST_P(TestReportManagerWithAggregationOperationType,
396 addReportWithDifferentOperationTypes)
397{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000398 reportParams.metricParameters(
399 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100400 {LabeledSensorInfo{"Service",
401 "/xyz/openbmc_project/sensors/power/p1",
402 "Metadata1"}},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000403 operationType,
404 "MetricId1",
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000405 CollectionTimeScope::point,
406 CollectionDuration(Milliseconds(0u))}}});
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000407
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000408 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000409 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
410
411 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200412
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000413 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100414 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000415}
416
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100417class TestReportManagerStorage : public TestReportManager
418{
419 public:
420 using FilePath = interfaces::JsonStorage::FilePath;
421 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
422
423 void SetUp() override
424 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000425 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)).Times(0);
426
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100427 ON_CALL(storageMock, list())
428 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
429 ON_CALL(storageMock, load(FilePath("report1")))
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100430 .WillByDefault(InvokeWithoutArgs([this] { return data; }));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100431 }
432
433 void makeReportManager()
434 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100435 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
436 std::move(storageMockPtr),
437 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100438 }
439
440 nlohmann::json data = nlohmann::json{
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200441 {"Enabled", reportParams.enabled()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100442 {"Version", Report::reportVersion},
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100443 {"Id", reportParams.reportId()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100444 {"Name", reportParams.reportName()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100445 {"ReportingType", utils::toUnderlying(reportParams.reportingType())},
446 {"ReportActions", reportParams.reportActions()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100447 {"Interval", reportParams.interval().count()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100448 {"ReportUpdates", utils::toUnderlying(reportParams.reportUpdates())},
Szymon Dompke3eb56862021-09-20 15:32:04 +0200449 {"AppendLimit", reportParams.appendLimit()},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000450 {"ReadingParameters", reportParams.metricParameters()}};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100451};
452
453TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
454{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000455 reportFactoryMock.expectMake(reportParams, _, Ref(storageMock));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100456
457 makeReportManager();
458}
459
460TEST_F(TestReportManagerStorage,
461 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
462{
463 data["Version"] = Report::reportVersion - 1;
464
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100465 EXPECT_CALL(storageMock, remove(FilePath("report1")));
466
467 makeReportManager();
468}
469
470TEST_F(TestReportManagerStorage,
471 reportManagerCtorRemoveFileIfIntervalHasWrongType)
472{
473 data["Interval"] = "1000";
474
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100475 EXPECT_CALL(storageMock, remove(FilePath("report1")));
476
477 makeReportManager();
478}