blob: be199c7c46c503230f39af6b16cb7d0a6fafdcc9 [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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200237TEST_F(TestReportManager, 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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200251TEST_F(TestReportManager, failToAddReportWithTooLongId)
Szymon Dompke32305f12022-07-05 15:37:21 +0200252{
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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200264TEST_F(TestReportManager, failToAddReportWithTooLongPrefix)
Szymon Dompke32305f12022-07-05 15:37:21 +0200265{
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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200277TEST_F(TestReportManager, failToAddReportWithTooManyPrefixes)
Szymon Dompke32305f12022-07-05 15:37:21 +0200278{
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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200297TEST_F(TestReportManager, failToAddReportWithTooLongName)
Szymon Dompke32305f12022-07-05 15:37:21 +0200298{
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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200310TEST_F(TestReportManager, 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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200323TEST_F(TestReportManager, 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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200337TEST_F(TestReportManager, failToAddReportWithInvalidReportingType)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100338{
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
Michal Orzelb47b7db2024-09-16 10:02:29 +0200351TEST_F(TestReportManager, failToAddReportWithMoreMetricPropertiesThanExpected)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100352{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000353 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100354 .Times(0);
355
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000356 reportParams.metricParameters(
357 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100358 {LabeledSensorInfo{"Service",
359 "/xyz/openbmc_project/sensors/power/p1",
360 "Metadata1"}},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100361 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000362 CollectionTimeScope::point,
363 CollectionDuration(Milliseconds(0u))}}});
364
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000365 auto metricParams = reportParams.metricParameters();
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000366 auto& metricParamsVec =
367 metricParams[0].at_label<utils::tstring::SensorPath>();
368
369 for (size_t i = 0; i < ReportManager::maxNumberMetrics; i++)
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100370 {
Szymon Dompke94f71c52021-12-10 07:16:33 +0100371 metricParamsVec.emplace_back(LabeledSensorInfo{
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000372 "Service", "/xyz/openbmc_project/sensors/power/p1", "Metadata1"});
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100373 }
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000374
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000375 reportParams.metricParameters(std::move(metricParams));
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100376
377 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200378
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200379 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100380 EXPECT_THAT(path, Eq(std::string()));
381}
382
Michal Orzelb47b7db2024-09-16 10:02:29 +0200383TEST_F(TestReportManager, failToAddReportWithMoreMetricsThanExpected)
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000384{
385 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
386 .Times(0);
387
388 auto metricParams = std::vector<LabeledMetricParameters>{};
389
390 for (size_t i = 0; i < ReportManager::maxNumberMetrics + 1; i++)
391 {
392 metricParams.emplace_back(
393 LabeledMetricParameters{{},
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100394 OperationType::avg,
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000395 CollectionTimeScope::point,
396 CollectionDuration(Milliseconds(0u))});
397 }
398
399 reportParams.metricParameters(std::move(metricParams));
400
401 auto [ec, path] = addReport(reportParams);
402
Krzysztof Grobelny62c08e92022-09-16 10:28:53 +0200403 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000404 EXPECT_THAT(path, Eq(std::string()));
405}
406
Michal Orzelb47b7db2024-09-16 10:02:29 +0200407TEST_F(TestReportManager, failToAddReportWithAppendLimitGreaterThanMax)
Ankita Vilas Gawadecd5b0b72022-01-20 20:55:04 +0000408{
409 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
410 .Times(0);
411
412 reportParams.appendLimit(ReportManager::maxAppendLimit + 1);
413
414 auto [ec, path] = addReport(reportParams);
415
416 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
417 EXPECT_THAT(path, Eq(std::string()));
418}
419
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100420TEST_F(TestReportManager, addReportWithAppendLimitEqualToUint64MaxIsAllowed)
421{
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100422 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000423 reportFactoryMock
424 .expectMake(reportParams.appendLimit(ReportManager::maxAppendLimit),
425 Ref(*sut), Ref(storageMock))
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100426 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
427
Krzysztof Grobelnycff70c12022-10-27 07:16:08 +0000428 auto [ec, path] = addReport(
429 reportParams.appendLimit(std::numeric_limits<uint64_t>::max()));
Krzysztof Grobelnye6c417c2022-02-02 17:25:53 +0100430 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
431 EXPECT_THAT(path, Eq(reportMock.getPath()));
432}
433
Michal Orzelb47b7db2024-09-16 10:02:29 +0200434TEST_F(TestReportManager, failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200435{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000436 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200437 .Times(ReportManager::maxReports);
438
439 for (size_t i = 0; i < ReportManager::maxReports; i++)
440 {
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100441 reportParams.reportId(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200442
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100443 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200444 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
445 }
446
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100447 reportParams.reportId(reportParams.reportName() +
448 std::to_string(ReportManager::maxReports));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100449 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200450
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200451 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
452 EXPECT_THAT(path, Eq(std::string()));
453}
454
455TEST_F(TestReportManager, removeReport)
456{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200457 {
458 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000459 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
460 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200461 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
462 EXPECT_CALL(reportMock, Die());
463 EXPECT_CALL(checkPoint, Call("end"));
464 }
465
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100466 addReport(reportParams);
467 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200468 checkPoint.Call("end");
469}
470
471TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
472{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200473 {
474 InSequence seq;
475 EXPECT_CALL(checkPoint, Call("end"));
476 EXPECT_CALL(reportMock, Die());
477 }
478
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100479 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200480 checkPoint.Call("end");
481}
482
483TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
484{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200485 {
486 InSequence seq;
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000487 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _));
488 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200489 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
490 EXPECT_CALL(reportMock, Die());
491 EXPECT_CALL(checkPoint, Call("end"));
492 }
493
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100494 addReport(reportParams);
495 sut->removeReport(&reportMock);
496 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200497 checkPoint.Call("end");
498}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100499
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000500class TestReportManagerWithAggregationOperationType :
501 public TestReportManager,
502 public WithParamInterface<OperationType>
503{
504 public:
505 OperationType operationType = GetParam();
506};
507
508INSTANTIATE_TEST_SUITE_P(_, TestReportManagerWithAggregationOperationType,
Krzysztof Grobelny60fee072022-01-13 16:25:04 +0100509 Values(OperationType::max, OperationType::min,
510 OperationType::avg, OperationType::sum));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000511
512TEST_P(TestReportManagerWithAggregationOperationType,
513 addReportWithDifferentOperationTypes)
514{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000515 reportParams.metricParameters(
516 std::vector<LabeledMetricParameters>{{LabeledMetricParameters{
Szymon Dompke94f71c52021-12-10 07:16:33 +0100517 {LabeledSensorInfo{"Service",
518 "/xyz/openbmc_project/sensors/power/p1",
519 "Metadata1"}},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000520 operationType,
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000521 CollectionTimeScope::point,
522 CollectionDuration(Milliseconds(0u))}}});
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000523
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000524 reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock))
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000525 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
526
527 auto [ec, path] = addReport(reportParams);
Karol Niczyj32859b62021-05-19 10:20:46 +0200528
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000529 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100530 EXPECT_THAT(path, Eq("/"s + reportParams.reportId()));
Krzysztof Grobelnye8fc5752021-02-05 14:30:45 +0000531}
532
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100533class TestReportManagerStorage : public TestReportManager
534{
535 public:
536 using FilePath = interfaces::JsonStorage::FilePath;
537 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
538
539 void SetUp() override
540 {
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000541 EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)).Times(0);
542
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100543 ON_CALL(storageMock, list())
544 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
545 ON_CALL(storageMock, load(FilePath("report1")))
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100546 .WillByDefault(InvokeWithoutArgs([this] { return data; }));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100547 }
548
549 void makeReportManager()
550 {
Krzysztof Grobelnye6d48872022-02-08 13:41:30 +0100551 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
552 std::move(storageMockPtr),
553 DbusEnvironment::getObjServer());
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100554 }
555
556 nlohmann::json data = nlohmann::json{
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +0200557 {"Enabled", reportParams.enabled()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100558 {"Version", Report::reportVersion},
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +0100559 {"Id", reportParams.reportId()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100560 {"Name", reportParams.reportName()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100561 {"ReportingType", utils::toUnderlying(reportParams.reportingType())},
562 {"ReportActions", reportParams.reportActions()},
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100563 {"Interval", reportParams.interval().count()},
Krzysztof Grobelny51497a02021-11-09 14:56:22 +0100564 {"ReportUpdates", utils::toUnderlying(reportParams.reportUpdates())},
Szymon Dompke3eb56862021-09-20 15:32:04 +0200565 {"AppendLimit", reportParams.appendLimit()},
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000566 {"ReadingParameters", reportParams.metricParameters()}};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100567};
568
569TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
570{
Krzysztof Grobelnydcc4e192021-03-08 09:09:34 +0000571 reportFactoryMock.expectMake(reportParams, _, Ref(storageMock));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100572
573 makeReportManager();
574}
575
576TEST_F(TestReportManagerStorage,
577 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
578{
579 data["Version"] = Report::reportVersion - 1;
580
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100581 EXPECT_CALL(storageMock, remove(FilePath("report1")));
582
583 makeReportManager();
584}
585
586TEST_F(TestReportManagerStorage,
587 reportManagerCtorRemoveFileIfIntervalHasWrongType)
588{
589 data["Interval"] = "1000";
590
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100591 EXPECT_CALL(storageMock, remove(FilePath("report1")));
592
593 makeReportManager();
594}