blob: 83c0a1aa8f62c991eacc0e11a3f87e855aed43eb [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 Grobelnycff70c12022-10-27 07:16:08 +000021using AddReportVariantForSet = utils::WithoutMonostate<AddReportVariant>;
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +020022
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020023class TestReportManager : public Test
24{
25 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010026 ReportParams reportParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020027
28 std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr =
29 std::make_unique<StrictMock<ReportFactoryMock>>();
30 ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010031
32 std::unique_ptr<StorageMock> storageMockPtr =
33 std::make_unique<NiceMock<StorageMock>>();
34 StorageMock& storageMock = *storageMockPtr;
35
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000036 std::unique_ptr<ReportMock> reportMockPtr =
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +010037 std::make_unique<NiceMock<ReportMock>>(reportParams.reportId());
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000038 ReportMock& reportMock = *reportMockPtr;
39
Wludzik, Jozefe2362792020-10-27 17:23:55 +010040 std::unique_ptr<ReportManager> sut;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020041
42 MockFunction<void(std::string)> checkPoint;
43
Wludzik, Jozefe2362792020-10-27 17:23:55 +010044 void SetUp() override
45 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +000046 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _))
47 .Times(AnyNumber());
48
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +010049 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
50 std::move(storageMockPtr),
51 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +010052 }
53
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020054 void TearDown() override
55 {
56 DbusEnvironment::synchronizeIoc();
57 }
58
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000059 template <class... Args>
60 requires(sizeof...(Args) > 1)
61 std::pair<boost::system::error_code, std::string> addReport(Args&&... args)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020062 {
63 std::promise<std::pair<boost::system::error_code, std::string>>
64 addReportPromise;
65 DbusEnvironment::getBus()->async_method_call(
66 [&addReportPromise](boost::system::error_code ec,
67 const std::string& path) {
Patrick Williams3a1c2972023-05-10 07:51:04 -050068 addReportPromise.set_value({ec, path});
Patrick Williamsc7935fa2023-10-20 11:19:30 -050069 },
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020070 DbusEnvironment::serviceName(), ReportManager::reportManagerPath,
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000071 ReportManager::reportManagerIfaceName, "AddReport",
72 std::forward<Args>(args)...);
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010073 return DbusEnvironment::waitForFuture(addReportPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020074 }
75
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010076 auto addReport(const ReportParams& params)
77 {
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +000078 return addReport(
79 params.reportId(), params.reportName(),
80 utils::enumToString(params.reportingType()),
81 utils::enumToString(params.reportUpdates()), params.appendLimit(),
82 utils::transform(
83 params.reportActions(),
84 [](const auto v) { return utils::enumToString(v); }),
85 params.interval().count(),
86 toReadingParameters(params.metricParameters()), params.enabled());
Krzysztof Grobelny51497a02021-11-09 14:56:22 +010087 }
88
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020089 template <class T>
Szymon Dompkee28aa532021-10-27 12:33:12 +020090 static T getProperty(const std::string& property)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020091 {
Szymon Dompkee28aa532021-10-27 12:33:12 +020092 return DbusEnvironment::getProperty<T>(
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020093 ReportManager::reportManagerPath,
Szymon Dompkee28aa532021-10-27 12:33:12 +020094 ReportManager::reportManagerIfaceName, property);
Karol Niczyj32859b62021-05-19 10:20:46 +020095 }
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020096};
97
98TEST_F(TestReportManager, minInterval)
99{
100 EXPECT_THAT(getProperty<uint64_t>("MinInterval"),
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000101 Eq(ReportManager::minInterval.count()));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200102}
103
104TEST_F(TestReportManager, maxReports)
105{
Wludzik, Jozef503c1582020-12-11 14:48:01 +0100106 EXPECT_THAT(getProperty<size_t>("MaxReports"),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200107 Eq(ReportManager::maxReports));
108}
109
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100110TEST_F(TestReportManager, returnsPropertySupportedOperationTypes)
111{
112 EXPECT_THAT(
113 getProperty<std::vector<std::string>>("SupportedOperationTypes"),
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000114 UnorderedElementsAre(utils::enumToString(OperationType::max),
115 utils::enumToString(OperationType::min),
116 utils::enumToString(OperationType::avg),
117 utils::enumToString(OperationType::sum)));
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100118}
119
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200120TEST_F(TestReportManager, addReport)
121{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000122 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
123 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200124 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
125
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100126 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200127 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
128 EXPECT_THAT(path, Eq(reportMock.getPath()));
129}
130
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200131TEST_F(TestReportManager, addDisabledReport)
132{
133 reportParams.enabled(false);
134
135 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
136 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
137 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
138
139 auto [ec, path] = addReport(reportParams);
140 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
141 EXPECT_THAT(path, Eq(reportMock.getPath()));
142}
143
144TEST_F(TestReportManager, addReportWithOnlyDefaultParams)
145{
146 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
147 EXPECT_CALL(reportFactoryMock,
148 make("Report"s, "Report"s, ReportingType::onRequest,
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200149 std::vector<ReportAction>{}, Milliseconds{}, 256,
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200150 ReportUpdates::overwrite, _, _,
151 std::vector<LabeledMetricParameters>{}, true, Readings{}))
152 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
153
154 auto [ec, path] = addReport(
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000155 "", "", "", "", std::numeric_limits<uint64_t>::max(),
156 std::vector<std::string>(), std::numeric_limits<uint64_t>::max(),
157 ReadingParameters(), true);
158
Krzysztof Grobelnya8182be2022-07-04 11:26:20 +0200159 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
160 EXPECT_THAT(path, Eq(reportMock.getPath()));
161}
162
Krzysztof Grobelnyf7ea2992022-01-27 11:04:58 +0100163TEST_F(TestReportManager, addOnChangeReport)
164{
165 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
166 reportFactoryMock
167 .expectMake(reportParams.reportingType(ReportingType::onChange),
168 Ref(*sut), Ref(storageMock))
169 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
170
171 auto [ec, path] = addReport(reportParams);
172 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
173 EXPECT_THAT(path, Eq(reportMock.getPath()));
174}
175
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100176TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsEmptyInAddReport)
Karol Niczyj32859b62021-05-19 10:20:46 +0200177{
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100178 reportParams.reportId("ReportName");
179 reportParams.reportName("ReportName");
180
181 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
182
183 auto [ec, path] = addReport(reportParams.reportId(""));
184
185 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
186 EXPECT_THAT(path, Eq("/ReportName"));
187}
188
189TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsNamespace)
190{
191 reportParams.reportId("Prefix/ReportName");
192 reportParams.reportName("ReportName");
193
194 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
195
196 auto [ec, path] = addReport(reportParams.reportId("Prefix/"));
197
198 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
199 EXPECT_THAT(path, Eq("/Prefix/ReportName"));
200}
201
202TEST_F(TestReportManager, addReportWithMaxLengthId)
203{
Szymon Dompke32305f12022-07-05 15:37:21 +0200204 std::string reportId = utils::string_utils::getMaxId();
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100205 reportParams.reportId(reportId);
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000206 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
Karol Niczyj32859b62021-05-19 10:20:46 +0200207
208 auto [ec, path] = addReport(reportParams);
209
210 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100211 EXPECT_THAT(path, Eq("/"s + reportId));
Karol Niczyj32859b62021-05-19 10:20:46 +0200212}
213
Szymon Dompke32305f12022-07-05 15:37:21 +0200214TEST_F(TestReportManager, addReportWithMaxLengthPrefix)
215{
216 std::string reportId = utils::string_utils::getMaxPrefix() + "/MyId";
217 reportParams.reportId(reportId);
218 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
219
220 auto [ec, path] = addReport(reportParams);
221
222 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
223 EXPECT_THAT(path, Eq("/"s + reportId));
224}
225
226TEST_F(TestReportManager, addReportWithMaxLengthName)
227{
228 reportParams.reportName(utils::string_utils::getMaxName());
229 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock));
230
231 auto [ec, path] = addReport(reportParams);
232
233 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
234 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
235}
236
Szymon Dompke32305f12022-07-05 15:37:21 +0200237TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongFullId)
Karol Niczyj32859b62021-05-19 10:20:46 +0200238{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000239 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Karol Niczyj32859b62021-05-19 10:20:46 +0200240 .Times(0);
241
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100242 reportParams.reportId(
Szymon Dompke32305f12022-07-05 15:37:21 +0200243 std::string(utils::constants::maxReportFullIdLength + 1, 'z'));
244
245 auto [ec, path] = addReport(reportParams);
246
247 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
248 EXPECT_THAT(path, Eq(std::string()));
249}
250
251TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongId)
252{
253 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
254 .Times(0);
255
256 reportParams.reportId(utils::string_utils::getTooLongId());
257
258 auto [ec, path] = addReport(reportParams);
259
260 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
261 EXPECT_THAT(path, Eq(std::string()));
262}
263
264TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongPrefix)
265{
266 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
267 .Times(0);
268
269 reportParams.reportId(utils::string_utils::getTooLongPrefix() + "/MyId");
270
271 auto [ec, path] = addReport(reportParams);
272
273 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
274 EXPECT_THAT(path, Eq(std::string()));
275}
276
277TEST_F(TestReportManager, DISABLED_failToAddReportWithTooManyPrefixes)
278{
279 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
280 .Times(0);
281
282 std::string reportId;
283 for (size_t i = 0; i < utils::constants::maxPrefixesInId + 1; i++)
284 {
285 reportId += "prefix/";
286 }
287 reportId += "MyId";
288
289 reportParams.reportId(reportId);
290
291 auto [ec, path] = addReport(reportParams);
292
293 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
294 EXPECT_THAT(path, Eq(std::string()));
295}
296
297TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName)
298{
299 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
300 .Times(0);
301
302 reportParams.reportName(utils::string_utils::getTooLongName());
303
304 auto [ec, path] = addReport(reportParams);
305
306 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
307 EXPECT_THAT(path, Eq(std::string()));
308}
309
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100310TEST_F(TestReportManager, DISABLED_failToAddReportTwice)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200311{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000312 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000313 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200314
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100315 addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200316
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100317 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200318
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200319 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
320 EXPECT_THAT(path, Eq(std::string()));
321}
322
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100323TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200324{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000325 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000326 .Times(0);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200327
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100328 reportParams.reportingType(ReportingType::periodic);
Krzysztof Grobelny973b4bb2022-04-25 17:07:27 +0200329 reportParams.interval(ReportManager::minInterval - 1ms);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200330
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100331 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200332
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200333 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
334 EXPECT_THAT(path, Eq(std::string()));
335}
336
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100337TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidReportingType)
338{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000339 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100340 .Times(0);
341
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000342 auto [ec, path] = addReport(
343 "", "", "InvalidReportingType", "",
344 std::numeric_limits<uint64_t>::max(), std::vector<std::string>(),
345 std::numeric_limits<uint64_t>::max(), ReadingParameters(), false);
Karol Niczyj32859b62021-05-19 10:20:46 +0200346
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100347 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
348 EXPECT_THAT(path, Eq(std::string()));
349}
350
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000351TEST_F(TestReportManager,
352 DISABLED_failToAddReportWithMoreMetricPropertiesThanExpected)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100353{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000354 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100355 .Times(0);
356
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000357 reportParams.metricParameters(
358 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100359 {LabeledSensorInfo{"Service",
360 "/xyz/openbmc_project/sensors/power/p1",
361 "Metadata1"}},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100362 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000363 CollectionTimeScope::point,
364 CollectionDuration(Milliseconds(0u))}}});
365
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000366 auto metricParams = reportParams.metricParameters();
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000367 auto& metricParamsVec =
368 metricParams[0].at_label<utils::tstring::SensorPath>();
369
370 for (size_t i = 0; i < ReportManager::maxNumberMetrics; i++)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100371 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100372 metricParamsVec.emplace_back(LabeledSensorInfo{
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000373 "Service", "/xyz/openbmc_project/sensors/power/p1", "Metadata1"});
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100374 }
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000375
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000376 reportParams.metricParameters(std::move(metricParams));
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100377
378 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200379
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200380 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100381 EXPECT_THAT(path, Eq(std::string()));
382}
383
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000384TEST_F(TestReportManager, DISABLED_failToAddReportWithMoreMetricsThanExpected)
385{
386 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
387 .Times(0);
388
389 auto metricParams = std::vector<LabeledMetricParameters>{};
390
391 for (size_t i = 0; i < ReportManager::maxNumberMetrics + 1; i++)
392 {
393 metricParams.emplace_back(
394 LabeledMetricParameters{{},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100395 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000396 CollectionTimeScope::point,
397 CollectionDuration(Milliseconds(0u))});
398 }
399
400 reportParams.metricParameters(std::move(metricParams));
401
402 auto [ec, path] = addReport(reportParams);
403
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200404 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000405 EXPECT_THAT(path, Eq(std::string()));
406}
407
408TEST_F(TestReportManager, DISABLED_failToAddReportWithAppendLimitGreaterThanMax)
409{
410 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
411 .Times(0);
412
413 reportParams.appendLimit(ReportManager::maxAppendLimit + 1);
414
415 auto [ec, path] = addReport(reportParams);
416
417 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
418 EXPECT_THAT(path, Eq(std::string()));
419}
420
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100421TEST_F(TestReportManager, addReportWithAppendLimitEqualToUint64MaxIsAllowed)
422{
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100423 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000424 reportFactoryMock
425 .expectMake(reportParams.appendLimit(ReportManager::maxAppendLimit),
426 Ref(*sut), Ref(storageMock))
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100427 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
428
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000429 auto [ec, path] = addReport(
430 reportParams.appendLimit(std::numeric_limits<uint64_t>::max()));
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100431 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
432 EXPECT_THAT(path, Eq(reportMock.getPath()));
433}
434
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100435TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200436{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000437 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200438 .Times(ReportManager::maxReports);
439
440 for (size_t i = 0; i < ReportManager::maxReports; i++)
441 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100442 reportParams.reportId(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200443
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100444 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200445 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
446 }
447
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100448 reportParams.reportId(reportParams.reportName() +
449 std::to_string(ReportManager::maxReports));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100450 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200451
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200452 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
453 EXPECT_THAT(path, Eq(std::string()));
454}
455
456TEST_F(TestReportManager, removeReport)
457{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200458 {
459 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000460 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
461 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200462 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
463 EXPECT_CALL(reportMock, Die());
464 EXPECT_CALL(checkPoint, Call("end"));
465 }
466
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100467 addReport(reportParams);
468 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200469 checkPoint.Call("end");
470}
471
472TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
473{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200474 {
475 InSequence seq;
476 EXPECT_CALL(checkPoint, Call("end"));
477 EXPECT_CALL(reportMock, Die());
478 }
479
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100480 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200481 checkPoint.Call("end");
482}
483
484TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
485{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200486 {
487 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000488 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
489 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200490 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
491 EXPECT_CALL(reportMock, Die());
492 EXPECT_CALL(checkPoint, Call("end"));
493 }
494
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100495 addReport(reportParams);
496 sut->removeReport(&reportMock);
497 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200498 checkPoint.Call("end");
499}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100500
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000501class TestReportManagerWithAggregationOperationType :
502 public TestReportManager,
503 public WithParamInterface<OperationType>
504{
505 public:
506 OperationType operationType = GetParam();
507};
508
509INSTANTIATE_TEST_SUITE_P(_, TestReportManagerWithAggregationOperationType,
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100510 Values(OperationType::max, OperationType::min,
511 OperationType::avg, OperationType::sum));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000512
513TEST_P(TestReportManagerWithAggregationOperationType,
514 addReportWithDifferentOperationTypes)
515{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000516 reportParams.metricParameters(
517 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100518 {LabeledSensorInfo{"Service",
519 "/xyz/openbmc_project/sensors/power/p1",
520 "Metadata1"}},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000521 operationType,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000522 CollectionTimeScope::point,
523 CollectionDuration(Milliseconds(0u))}}});
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000524
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000525 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000526 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
527
528 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200529
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000530 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100531 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000532}
533
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100534class TestReportManagerStorage : public TestReportManager
535{
536 public:
537 using FilePath = interfaces::JsonStorage::FilePath;
538 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
539
540 void SetUp() override
541 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000542 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)).Times(0);
543
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100544 ON_CALL(storageMock, list())
545 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
546 ON_CALL(storageMock, load(FilePath("report1")))
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100547 .WillByDefault(InvokeWithoutArgs([this] { return data; }));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100548 }
549
550 void makeReportManager()
551 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100552 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
553 std::move(storageMockPtr),
554 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100555 }
556
557 nlohmann::json data = nlohmann::json{
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200558 {"Enabled", reportParams.enabled()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100559 {"Version", Report::reportVersion},
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100560 {"Id", reportParams.reportId()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100561 {"Name", reportParams.reportName()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100562 {"ReportingType", utils::toUnderlying(reportParams.reportingType())},
563 {"ReportActions", reportParams.reportActions()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100564 {"Interval", reportParams.interval().count()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100565 {"ReportUpdates", utils::toUnderlying(reportParams.reportUpdates())},
Szymon Dompke3eb56862021-09-20 15:32:04 +0200566 {"AppendLimit", reportParams.appendLimit()},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000567 {"ReadingParameters", reportParams.metricParameters()}};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100568};
569
570TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
571{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000572 reportFactoryMock.expectMake(reportParams, _, Ref(storageMock));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100573
574 makeReportManager();
575}
576
577TEST_F(TestReportManagerStorage,
578 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
579{
580 data["Version"] = Report::reportVersion - 1;
581
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100582 EXPECT_CALL(storageMock, remove(FilePath("report1")));
583
584 makeReportManager();
585}
586
587TEST_F(TestReportManagerStorage,
588 reportManagerCtorRemoveFileIfIntervalHasWrongType)
589{
590 data["Interval"] = "1000";
591
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100592 EXPECT_CALL(storageMock, remove(FilePath("report1")));
593
594 makeReportManager();
595}