blob: c7805569ee98cf0f9e70c998c0e7b649bc5d144f [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{
90 EXPECT_THAT(getProperty<uint32_t>("MaxReports"),
91 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, Jozefe2362792020-10-27 17:23:55 +0100123 reportParams.interval(reportParams.interval() - 1ms);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200124
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100125 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200126 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
127 EXPECT_THAT(path, Eq(std::string()));
128}
129
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100130TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200131{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000132 reportFactoryMock.expectMake(_, std::nullopt, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200133 .Times(ReportManager::maxReports);
134
135 for (size_t i = 0; i < ReportManager::maxReports; i++)
136 {
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100137 reportParams.reportName(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200138
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100139 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200140 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
141 }
142
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100143 reportParams.reportName(reportParams.reportName() +
144 std::to_string(ReportManager::maxReports));
145 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200146 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
147 EXPECT_THAT(path, Eq(std::string()));
148}
149
150TEST_F(TestReportManager, removeReport)
151{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200152 {
153 InSequence seq;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000154 reportFactoryMock
155 .expectMake(_, reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200156 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
157 EXPECT_CALL(reportMock, Die());
158 EXPECT_CALL(checkPoint, Call("end"));
159 }
160
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100161 addReport(reportParams);
162 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200163 checkPoint.Call("end");
164}
165
166TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
167{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200168 {
169 InSequence seq;
170 EXPECT_CALL(checkPoint, Call("end"));
171 EXPECT_CALL(reportMock, Die());
172 }
173
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100174 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200175 checkPoint.Call("end");
176}
177
178TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
179{
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200180 {
181 InSequence seq;
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000182 reportFactoryMock
183 .expectMake(_, reportParams, Ref(*sut), Ref(storageMock))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200184 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
185 EXPECT_CALL(reportMock, Die());
186 EXPECT_CALL(checkPoint, Call("end"));
187 }
188
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100189 addReport(reportParams);
190 sut->removeReport(&reportMock);
191 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200192 checkPoint.Call("end");
193}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100194
195class TestReportManagerStorage : public TestReportManager
196{
197 public:
198 using FilePath = interfaces::JsonStorage::FilePath;
199 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
200
201 void SetUp() override
202 {
203 ON_CALL(storageMock, list())
204 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
205 ON_CALL(storageMock, load(FilePath("report1")))
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100206 .WillByDefault(InvokeWithoutArgs([this] { return data; }));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100207 }
208
209 void makeReportManager()
210 {
211 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
212 std::move(storageMockPtr),
213 DbusEnvironment::getObjServer());
214 }
215
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000216 static std::vector<LabeledMetricParameters>
217 convertToLabeled(const ReadingParameters& params)
218 {
219 return utils::transform(params, [](const auto& item) {
220 return LabeledMetricParameters(
221 utils::transform(std::get<0>(item),
222 [](const auto& elem) {
223 return LabeledSensorParameters("service",
224 elem);
225 }),
226 std::get<1>(item), std::get<2>(item), std::get<3>(item));
227 });
228 }
229
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100230 nlohmann::json data = nlohmann::json{
231 {"Version", Report::reportVersion},
232 {"Name", reportParams.reportName()},
233 {"ReportingType", reportParams.reportingType()},
234 {"EmitsReadingsUpdate", reportParams.emitReadingUpdate()},
235 {"LogToMetricReportsCollection",
236 reportParams.logToMetricReportCollection()},
237 {"Interval", reportParams.interval().count()},
238 {"ReadingParameters",
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000239 convertToLabeled(reportParams.readingParameters())}};
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100240};
241
242TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
243{
Krzysztof Grobelnyd2238192020-12-02 09:27:28 +0000244 reportFactoryMock.expectMake(
245 reportParams, _, Ref(storageMock),
246 ElementsAreArray(convertToLabeled(reportParams.readingParameters())));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100247
248 makeReportManager();
249}
250
251TEST_F(TestReportManagerStorage,
252 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
253{
254 data["Version"] = Report::reportVersion - 1;
255
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100256 EXPECT_CALL(storageMock, remove(FilePath("report1")));
257
258 makeReportManager();
259}
260
261TEST_F(TestReportManagerStorage,
262 reportManagerCtorRemoveFileIfIntervalHasWrongType)
263{
264 data["Interval"] = "1000";
265
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100266 EXPECT_CALL(storageMock, remove(FilePath("report1")));
267
268 makeReportManager();
269}