blob: 30628d6fb4a8fd039a68840f32fbe2cc6990057b [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"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01003#include "mocks/json_storage_mock.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02004#include "mocks/report_factory_mock.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01005#include "params/report_params.hpp"
6#include "report.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02007#include "report_manager.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01008#include "utils/transform.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02009
10using namespace testing;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010011using namespace std::chrono_literals;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020012
13class TestReportManager : public Test
14{
15 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010016 ReportParams reportParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020017
18 std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr =
19 std::make_unique<StrictMock<ReportFactoryMock>>();
20 ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010021
22 std::unique_ptr<StorageMock> storageMockPtr =
23 std::make_unique<NiceMock<StorageMock>>();
24 StorageMock& storageMock = *storageMockPtr;
25
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000026 std::unique_ptr<ReportMock> reportMockPtr =
27 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
28 ReportMock& reportMock = *reportMockPtr;
29
Wludzik, Jozefe2362792020-10-27 17:23:55 +010030 std::unique_ptr<ReportManager> sut;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020031
32 MockFunction<void(std::string)> checkPoint;
33
Wludzik, Jozefe2362792020-10-27 17:23:55 +010034 void SetUp() override
35 {
36 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
37 std::move(storageMockPtr),
38 DbusEnvironment::getObjServer());
39 }
40
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020041 void TearDown() override
42 {
43 DbusEnvironment::synchronizeIoc();
44 }
45
46 std::pair<boost::system::error_code, std::string>
Wludzik, Jozefe2362792020-10-27 17:23:55 +010047 addReport(const ReportParams& params)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020048 {
49 std::promise<std::pair<boost::system::error_code, std::string>>
50 addReportPromise;
51 DbusEnvironment::getBus()->async_method_call(
52 [&addReportPromise](boost::system::error_code ec,
53 const std::string& path) {
54 addReportPromise.set_value({ec, path});
55 },
56 DbusEnvironment::serviceName(), ReportManager::reportManagerPath,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010057 ReportManager::reportManagerIfaceName, "AddReport",
58 params.reportName(), params.reportingType(),
59 params.emitReadingUpdate(), params.logToMetricReportCollection(),
60 static_cast<uint64_t>(params.interval().count()),
61 params.readingParameters());
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010062 return DbusEnvironment::waitForFuture(addReportPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020063 }
64
65 template <class T>
66 static T getProperty(std::string property)
67 {
68 std::promise<T> propertyPromise;
69 sdbusplus::asio::getProperty<T>(
70 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(),
71 ReportManager::reportManagerPath,
72 ReportManager::reportManagerIfaceName, property,
73 [&propertyPromise](boost::system::error_code ec) {
74 EXPECT_THAT(static_cast<bool>(ec), ::testing::Eq(false));
75 propertyPromise.set_value(T{});
76 },
77 [&propertyPromise](T t) { propertyPromise.set_value(t); });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010078 return DbusEnvironment::waitForFuture(propertyPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020079 }
80};
81
82TEST_F(TestReportManager, minInterval)
83{
84 EXPECT_THAT(getProperty<uint64_t>("MinInterval"),
85 Eq(static_cast<uint64_t>(ReportManager::minInterval.count())));
86}
87
88TEST_F(TestReportManager, maxReports)
89{
Wludzik, Jozef503c1582020-12-11 14:48:01 +010090 EXPECT_THAT(getProperty<size_t>("MaxReports"),
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020091 Eq(ReportManager::maxReports));
92}
93
94TEST_F(TestReportManager, addReport)
95{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +000096 reportFactoryMock.expectMake(_, reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020097 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
98
Wludzik, Jozefe2362792020-10-27 17:23:55 +010099 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200100 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
101 EXPECT_THAT(path, Eq(reportMock.getPath()));
102}
103
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100104TEST_F(TestReportManager, DISABLED_failToAddReportTwice)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200105{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000106 reportFactoryMock.expectMake(_, reportParams, Ref(*sut), Ref(storageMock))
107 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200108
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100109 addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200110
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100111 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200112 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
113 EXPECT_THAT(path, Eq(std::string()));
114}
115
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100116TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200117{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000118 reportFactoryMock.expectMake(_, std::nullopt, Ref(*sut), Ref(storageMock))
119 .Times(0);
120 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock), _)
121 .Times(0);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200122
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100123 reportParams.reportingType("Periodic");
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100124 reportParams.interval(reportParams.interval() - 1ms);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200125
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::invalid_argument));
128 EXPECT_THAT(path, Eq(std::string()));
129}
130
Wludzik, Jozefbc766b42020-12-08 16:06:22 +0100131TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidReportingType)
132{
133 reportFactoryMock.expectMake(_, std::nullopt, Ref(*sut), Ref(storageMock))
134 .Times(0);
135 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock), _)
136 .Times(0);
137
138 reportParams.reportingType("Invalid");
139
140 auto [ec, path] = addReport(reportParams);
141 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
142 EXPECT_THAT(path, Eq(std::string()));
143}
144
145TEST_F(TestReportManager, DISABLED_failToAddReportWithMoreSensorsThanExpected)
146{
147 reportFactoryMock.expectMake(_, std::nullopt, Ref(*sut), Ref(storageMock))
148 .Times(0);
149 reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock), _)
150 .Times(0);
151
152 auto readingParams = reportParams.readingParameters();
153 for (size_t i = 0; i < ReportManager::maxReadingParams + 1; i++)
154 {
155 readingParams.push_back(readingParams.front());
156 }
157 reportParams.readingParameters(std::move(readingParams));
158
159 auto [ec, path] = addReport(reportParams);
160 EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long));
161 EXPECT_THAT(path, Eq(std::string()));
162}
163
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100164TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200165{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000166 reportFactoryMock.expectMake(_, std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200167 .Times(ReportManager::maxReports);
168
169 for (size_t i = 0; i < ReportManager::maxReports; i++)
170 {
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100171 reportParams.reportName(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200172
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100173 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200174 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
175 }
176
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100177 reportParams.reportName(reportParams.reportName() +
178 std::to_string(ReportManager::maxReports));
179 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200180 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
181 EXPECT_THAT(path, Eq(std::string()));
182}
183
184TEST_F(TestReportManager, removeReport)
185{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200186 {
187 InSequence seq;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000188 reportFactoryMock
189 .expectMake(_, reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200190 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
191 EXPECT_CALL(reportMock, Die());
192 EXPECT_CALL(checkPoint, Call("end"));
193 }
194
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100195 addReport(reportParams);
196 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200197 checkPoint.Call("end");
198}
199
200TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
201{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200202 {
203 InSequence seq;
204 EXPECT_CALL(checkPoint, Call("end"));
205 EXPECT_CALL(reportMock, Die());
206 }
207
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100208 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200209 checkPoint.Call("end");
210}
211
212TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
213{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200214 {
215 InSequence seq;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000216 reportFactoryMock
217 .expectMake(_, reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200218 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
219 EXPECT_CALL(reportMock, Die());
220 EXPECT_CALL(checkPoint, Call("end"));
221 }
222
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100223 addReport(reportParams);
224 sut->removeReport(&reportMock);
225 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200226 checkPoint.Call("end");
227}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100228
Wludzik, Jozefd960e1f2021-01-08 09:25:59 +0100229TEST_F(TestReportManager, updateReportCallsUpdateReadingsForExistReport)
230{
231 reportFactoryMock.expectMake(_, reportParams, Ref(*sut), Ref(storageMock))
232 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
233 EXPECT_CALL(reportMock, updateReadings());
234
235 addReport(reportParams);
236 sut->updateReport(reportParams.reportName());
237}
238
239TEST_F(TestReportManager, updateReportDoNothingIfReportDoesNotExist)
240{
241 reportFactoryMock.expectMake(_, reportParams, Ref(*sut), Ref(storageMock))
242 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
243 EXPECT_CALL(reportMock, updateReadings()).Times(0);
244
245 addReport(reportParams);
246 sut->updateReport("NotAReport");
247}
248
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100249class TestReportManagerStorage : public TestReportManager
250{
251 public:
252 using FilePath = interfaces::JsonStorage::FilePath;
253 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
254
255 void SetUp() override
256 {
257 ON_CALL(storageMock, list())
258 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
259 ON_CALL(storageMock, load(FilePath("report1")))
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100260 .WillByDefault(InvokeWithoutArgs([this] { return data; }));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100261 }
262
263 void makeReportManager()
264 {
265 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
266 std::move(storageMockPtr),
267 DbusEnvironment::getObjServer());
268 }
269
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000270 static std::vector<LabeledMetricParameters>
271 convertToLabeled(const ReadingParameters& params)
272 {
273 return utils::transform(params, [](const auto& item) {
274 return LabeledMetricParameters(
275 utils::transform(std::get<0>(item),
276 [](const auto& elem) {
277 return LabeledSensorParameters("service",
278 elem);
279 }),
280 std::get<1>(item), std::get<2>(item), std::get<3>(item));
281 });
282 }
283
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100284 nlohmann::json data = nlohmann::json{
285 {"Version", Report::reportVersion},
286 {"Name", reportParams.reportName()},
287 {"ReportingType", reportParams.reportingType()},
288 {"EmitsReadingsUpdate", reportParams.emitReadingUpdate()},
289 {"LogToMetricReportsCollection",
290 reportParams.logToMetricReportCollection()},
291 {"Interval", reportParams.interval().count()},
292 {"ReadingParameters",
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000293 convertToLabeled(reportParams.readingParameters())}};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100294};
295
296TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
297{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000298 reportFactoryMock.expectMake(
299 reportParams, _, Ref(storageMock),
300 ElementsAreArray(convertToLabeled(reportParams.readingParameters())));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100301
302 makeReportManager();
303}
304
305TEST_F(TestReportManagerStorage,
306 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
307{
308 data["Version"] = Report::reportVersion - 1;
309
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100310 EXPECT_CALL(storageMock, remove(FilePath("report1")));
311
312 makeReportManager();
313}
314
315TEST_F(TestReportManagerStorage,
316 reportManagerCtorRemoveFileIfIntervalHasWrongType)
317{
318 data["Interval"] = "1000";
319
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100320 EXPECT_CALL(storageMock, remove(FilePath("report1")));
321
322 makeReportManager();
323}