blob: 482434c206b456d5a84e87a9ff606b5438c5e20b [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"
5#include "report.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02006#include "report_manager.hpp"
Wludzik, Jozefe2362792020-10-27 17:23:55 +01007#include "utils/transform.hpp"
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +02008
9using namespace testing;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010010using namespace std::chrono_literals;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020011
12class TestReportManager : public Test
13{
14 public:
Wludzik, Jozefe2362792020-10-27 17:23:55 +010015 ReportParams reportParams;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020016
17 std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr =
18 std::make_unique<StrictMock<ReportFactoryMock>>();
19 ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr;
Wludzik, Jozefe2362792020-10-27 17:23:55 +010020
21 std::unique_ptr<StorageMock> storageMockPtr =
22 std::make_unique<NiceMock<StorageMock>>();
23 StorageMock& storageMock = *storageMockPtr;
24
25 std::unique_ptr<ReportManager> sut;
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020026
27 MockFunction<void(std::string)> checkPoint;
28
Wludzik, Jozefe2362792020-10-27 17:23:55 +010029 void SetUp() override
30 {
31 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
32 std::move(storageMockPtr),
33 DbusEnvironment::getObjServer());
34 }
35
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020036 void TearDown() override
37 {
38 DbusEnvironment::synchronizeIoc();
39 }
40
41 std::pair<boost::system::error_code, std::string>
Wludzik, Jozefe2362792020-10-27 17:23:55 +010042 addReport(const ReportParams& params)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020043 {
44 std::promise<std::pair<boost::system::error_code, std::string>>
45 addReportPromise;
46 DbusEnvironment::getBus()->async_method_call(
47 [&addReportPromise](boost::system::error_code ec,
48 const std::string& path) {
49 addReportPromise.set_value({ec, path});
50 },
51 DbusEnvironment::serviceName(), ReportManager::reportManagerPath,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010052 ReportManager::reportManagerIfaceName, "AddReport",
53 params.reportName(), params.reportingType(),
54 params.emitReadingUpdate(), params.logToMetricReportCollection(),
55 static_cast<uint64_t>(params.interval().count()),
56 params.readingParameters());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020057 return DbusEnvironment::waitForFuture(addReportPromise.get_future())
58 .value_or(std::pair<boost::system::error_code, std::string>{});
59 }
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); });
74 return DbusEnvironment::waitForFuture(propertyPromise.get_future())
75 .value_or(T{});
76 }
77};
78
79TEST_F(TestReportManager, minInterval)
80{
81 EXPECT_THAT(getProperty<uint64_t>("MinInterval"),
82 Eq(static_cast<uint64_t>(ReportManager::minInterval.count())));
83}
84
85TEST_F(TestReportManager, maxReports)
86{
87 EXPECT_THAT(getProperty<uint32_t>("MaxReports"),
88 Eq(ReportManager::maxReports));
89}
90
91TEST_F(TestReportManager, addReport)
92{
93 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +010094 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020095 auto& reportMock = *reportMockPtr;
96
97 EXPECT_CALL(reportFactoryMock,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010098 make(_, reportParams.reportName(), reportParams.reportingType(),
99 reportParams.emitReadingUpdate(),
100 reportParams.logToMetricReportCollection(),
101 reportParams.interval(), reportParams.readingParameters(),
102 Ref(*sut), Ref(storageMock)))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200103 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
104
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100105 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200106 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
107 EXPECT_THAT(path, Eq(reportMock.getPath()));
108}
109
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100110TEST_F(TestReportManager, DISABLED_failToAddReportTwice)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200111{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100112 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200113
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100114 addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200115
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100116 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200117 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
118 EXPECT_THAT(path, Eq(std::string()));
119}
120
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100121TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200122{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100123 EXPECT_CALL(reportFactoryMock, make).Times(0);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200124
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100125 reportParams.interval(reportParams.interval() - 1ms);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200126
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100127 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200128 EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument));
129 EXPECT_THAT(path, Eq(std::string()));
130}
131
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100132TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200133{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100134 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200135 .Times(ReportManager::maxReports);
136
137 for (size_t i = 0; i < ReportManager::maxReports; i++)
138 {
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100139 reportParams.reportName(reportParams.reportName() + std::to_string(i));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200140
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100141 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200142 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
143 }
144
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100145 reportParams.reportName(reportParams.reportName() +
146 std::to_string(ReportManager::maxReports));
147 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200148 EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open));
149 EXPECT_THAT(path, Eq(std::string()));
150}
151
152TEST_F(TestReportManager, removeReport)
153{
154 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100155 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200156 auto& reportMock = *reportMockPtr;
157
158 {
159 InSequence seq;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100160 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200161 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
162 EXPECT_CALL(reportMock, Die());
163 EXPECT_CALL(checkPoint, Call("end"));
164 }
165
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100166 addReport(reportParams);
167 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200168 checkPoint.Call("end");
169}
170
171TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
172{
173 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100174 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200175 auto& reportMock = *reportMockPtr;
176
177 {
178 InSequence seq;
179 EXPECT_CALL(checkPoint, Call("end"));
180 EXPECT_CALL(reportMock, Die());
181 }
182
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100183 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200184 checkPoint.Call("end");
185}
186
187TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
188{
189 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100190 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200191 auto& reportMock = *reportMockPtr;
192
193 {
194 InSequence seq;
195 EXPECT_CALL(reportFactoryMock,
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100196 make(_, reportParams.reportName(), _, _, _, _, _, _, _))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200197 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
198 EXPECT_CALL(reportMock, Die());
199 EXPECT_CALL(checkPoint, Call("end"));
200 }
201
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100202 addReport(reportParams);
203 sut->removeReport(&reportMock);
204 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200205 checkPoint.Call("end");
206}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100207
208class TestReportManagerStorage : public TestReportManager
209{
210 public:
211 using FilePath = interfaces::JsonStorage::FilePath;
212 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
213
214 void SetUp() override
215 {
216 ON_CALL(storageMock, list())
217 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
218 ON_CALL(storageMock, load(FilePath("report1")))
219 .WillByDefault(Return(data));
220 }
221
222 void makeReportManager()
223 {
224 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
225 std::move(storageMockPtr),
226 DbusEnvironment::getObjServer());
227 }
228
229 nlohmann::json data = nlohmann::json{
230 {"Version", Report::reportVersion},
231 {"Name", reportParams.reportName()},
232 {"ReportingType", reportParams.reportingType()},
233 {"EmitsReadingsUpdate", reportParams.emitReadingUpdate()},
234 {"LogToMetricReportsCollection",
235 reportParams.logToMetricReportCollection()},
236 {"Interval", reportParams.interval().count()},
237 {"ReadingParameters",
238 utils::transform(reportParams.readingParameters(),
239 [](const auto& item) {
240 return LabeledReadingParameter::to_json(item);
241 })}};
242};
243
244TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
245{
246 EXPECT_CALL(reportFactoryMock,
247 make(_, reportParams.reportName(), reportParams.reportingType(),
248 reportParams.emitReadingUpdate(),
249 reportParams.logToMetricReportCollection(),
250 reportParams.interval(), reportParams.readingParameters(),
251 _, Ref(storageMock)));
252
253 makeReportManager();
254}
255
256TEST_F(TestReportManagerStorage,
257 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
258{
259 data["Version"] = Report::reportVersion - 1;
260
261 ON_CALL(storageMock, load(FilePath("report1"))).WillByDefault(Return(data));
262 EXPECT_CALL(storageMock, remove(FilePath("report1")));
263
264 makeReportManager();
265}
266
267TEST_F(TestReportManagerStorage,
268 reportManagerCtorRemoveFileIfIntervalHasWrongType)
269{
270 data["Interval"] = "1000";
271
272 ON_CALL(storageMock, load(FilePath("report1"))).WillByDefault(Return(data));
273 EXPECT_CALL(storageMock, remove(FilePath("report1")));
274
275 makeReportManager();
276}