blob: 02bf3eccfe75f526b72201102833b50d200da1ad [file] [log] [blame]
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02001#include "dbus_environment.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01002#include "mocks/json_storage_mock.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02003#include "mocks/report_factory_mock.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01004#include "params/report_params.hpp"
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +01005#include "printers.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01006#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
26 std::unique_ptr<ReportManager> sut;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020027
28 MockFunction<void(std::string)> checkPoint;
29
Wludzik, Jozefe2362792020-10-27 17:23:55 +010030 void SetUp() override
31 {
32 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
33 std::move(storageMockPtr),
34 DbusEnvironment::getObjServer());
35 }
36
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020037 void TearDown() override
38 {
39 DbusEnvironment::synchronizeIoc();
40 }
41
42 std::pair<boost::system::error_code, std::string>
Wludzik, Jozefe2362792020-10-27 17:23:55 +010043 addReport(const ReportParams& params)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020044 {
45 std::promise<std::pair<boost::system::error_code, std::string>>
46 addReportPromise;
47 DbusEnvironment::getBus()->async_method_call(
48 [&addReportPromise](boost::system::error_code ec,
49 const std::string& path) {
50 addReportPromise.set_value({ec, path});
51 },
52 DbusEnvironment::serviceName(), ReportManager::reportManagerPath,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010053 ReportManager::reportManagerIfaceName, "AddReport",
54 params.reportName(), params.reportingType(),
55 params.emitReadingUpdate(), params.logToMetricReportCollection(),
56 static_cast<uint64_t>(params.interval().count()),
57 params.readingParameters());
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010058 return DbusEnvironment::waitForFuture(addReportPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020059 }
60
61 template <class T>
62 static T getProperty(std::string property)
63 {
64 std::promise<T> propertyPromise;
65 sdbusplus::asio::getProperty<T>(
66 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(),
67 ReportManager::reportManagerPath,
68 ReportManager::reportManagerIfaceName, property,
69 [&propertyPromise](boost::system::error_code ec) {
70 EXPECT_THAT(static_cast<bool>(ec), ::testing::Eq(false));
71 propertyPromise.set_value(T{});
72 },
73 [&propertyPromise](T t) { propertyPromise.set_value(t); });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010074 return DbusEnvironment::waitForFuture(propertyPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020075 }
76};
77
78TEST_F(TestReportManager, minInterval)
79{
80 EXPECT_THAT(getProperty<uint64_t>("MinInterval"),
81 Eq(static_cast<uint64_t>(ReportManager::minInterval.count())));
82}
83
84TEST_F(TestReportManager, maxReports)
85{
86 EXPECT_THAT(getProperty<uint32_t>("MaxReports"),
87 Eq(ReportManager::maxReports));
88}
89
90TEST_F(TestReportManager, addReport)
91{
92 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +010093 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020094 auto& reportMock = *reportMockPtr;
95
96 EXPECT_CALL(reportFactoryMock,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010097 make(_, reportParams.reportName(), reportParams.reportingType(),
98 reportParams.emitReadingUpdate(),
99 reportParams.logToMetricReportCollection(),
100 reportParams.interval(), reportParams.readingParameters(),
101 Ref(*sut), Ref(storageMock)))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200102 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
103
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100104 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200105 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
106 EXPECT_THAT(path, Eq(reportMock.getPath()));
107}
108
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100109TEST_F(TestReportManager, DISABLED_failToAddReportTwice)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200110{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100111 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200112
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100113 addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200114
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100115 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200116 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
117 EXPECT_THAT(path, Eq(std::string()));
118}
119
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100120TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200121{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100122 EXPECT_CALL(reportFactoryMock, make).Times(0);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200123
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, Jozefe2362792020-10-27 17:23:55 +0100131TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200132{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100133 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200134 .Times(ReportManager::maxReports);
135
136 for (size_t i = 0; i < ReportManager::maxReports; i++)
137 {
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100138 reportParams.reportName(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200139
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100140 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200141 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
142 }
143
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100144 reportParams.reportName(reportParams.reportName() +
145 std::to_string(ReportManager::maxReports));
146 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200147 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
148 EXPECT_THAT(path, Eq(std::string()));
149}
150
151TEST_F(TestReportManager, removeReport)
152{
153 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100154 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200155 auto& reportMock = *reportMockPtr;
156
157 {
158 InSequence seq;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100159 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200160 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
161 EXPECT_CALL(reportMock, Die());
162 EXPECT_CALL(checkPoint, Call("end"));
163 }
164
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100165 addReport(reportParams);
166 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200167 checkPoint.Call("end");
168}
169
170TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
171{
172 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100173 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200174 auto& reportMock = *reportMockPtr;
175
176 {
177 InSequence seq;
178 EXPECT_CALL(checkPoint, Call("end"));
179 EXPECT_CALL(reportMock, Die());
180 }
181
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100182 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200183 checkPoint.Call("end");
184}
185
186TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
187{
188 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100189 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200190 auto& reportMock = *reportMockPtr;
191
192 {
193 InSequence seq;
194 EXPECT_CALL(reportFactoryMock,
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100195 make(_, reportParams.reportName(), _, _, _, _, _, _, _))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200196 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
197 EXPECT_CALL(reportMock, Die());
198 EXPECT_CALL(checkPoint, Call("end"));
199 }
200
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100201 addReport(reportParams);
202 sut->removeReport(&reportMock);
203 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200204 checkPoint.Call("end");
205}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100206
207class TestReportManagerStorage : public TestReportManager
208{
209 public:
210 using FilePath = interfaces::JsonStorage::FilePath;
211 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
212
213 void SetUp() override
214 {
215 ON_CALL(storageMock, list())
216 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
217 ON_CALL(storageMock, load(FilePath("report1")))
Krzysztof Grobelny6ccfcbf2020-11-04 09:31:36 +0100218 .WillByDefault(InvokeWithoutArgs([this] { return data; }));
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100219 }
220
221 void makeReportManager()
222 {
223 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
224 std::move(storageMockPtr),
225 DbusEnvironment::getObjServer());
226 }
227
228 nlohmann::json data = nlohmann::json{
229 {"Version", Report::reportVersion},
230 {"Name", reportParams.reportName()},
231 {"ReportingType", reportParams.reportingType()},
232 {"EmitsReadingsUpdate", reportParams.emitReadingUpdate()},
233 {"LogToMetricReportsCollection",
234 reportParams.logToMetricReportCollection()},
235 {"Interval", reportParams.interval().count()},
236 {"ReadingParameters",
237 utils::transform(reportParams.readingParameters(),
238 [](const auto& item) {
239 return LabeledReadingParameter::to_json(item);
240 })}};
241};
242
243TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
244{
245 EXPECT_CALL(reportFactoryMock,
246 make(_, reportParams.reportName(), reportParams.reportingType(),
247 reportParams.emitReadingUpdate(),
248 reportParams.logToMetricReportCollection(),
249 reportParams.interval(), reportParams.readingParameters(),
250 _, Ref(storageMock)));
251
252 makeReportManager();
253}
254
255TEST_F(TestReportManagerStorage,
256 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
257{
258 data["Version"] = Report::reportVersion - 1;
259
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100260 EXPECT_CALL(storageMock, remove(FilePath("report1")));
261
262 makeReportManager();
263}
264
265TEST_F(TestReportManagerStorage,
266 reportManagerCtorRemoveFileIfIntervalHasWrongType)
267{
268 data["Interval"] = "1000";
269
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100270 EXPECT_CALL(storageMock, remove(FilePath("report1")));
271
272 makeReportManager();
273}