blob: 813fa19bc4f5948bd977559f30759376dd8ff25e [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());
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010057 return DbusEnvironment::waitForFuture(addReportPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020058 }
59
60 template <class T>
61 static T getProperty(std::string property)
62 {
63 std::promise<T> propertyPromise;
64 sdbusplus::asio::getProperty<T>(
65 *DbusEnvironment::getBus(), DbusEnvironment::serviceName(),
66 ReportManager::reportManagerPath,
67 ReportManager::reportManagerIfaceName, property,
68 [&propertyPromise](boost::system::error_code ec) {
69 EXPECT_THAT(static_cast<bool>(ec), ::testing::Eq(false));
70 propertyPromise.set_value(T{});
71 },
72 [&propertyPromise](T t) { propertyPromise.set_value(t); });
Krzysztof Grobelnyf32f6fe2020-10-30 13:51:58 +010073 return DbusEnvironment::waitForFuture(propertyPromise.get_future());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020074 }
75};
76
77TEST_F(TestReportManager, minInterval)
78{
79 EXPECT_THAT(getProperty<uint64_t>("MinInterval"),
80 Eq(static_cast<uint64_t>(ReportManager::minInterval.count())));
81}
82
83TEST_F(TestReportManager, maxReports)
84{
85 EXPECT_THAT(getProperty<uint32_t>("MaxReports"),
86 Eq(ReportManager::maxReports));
87}
88
89TEST_F(TestReportManager, addReport)
90{
91 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +010092 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +020093 auto& reportMock = *reportMockPtr;
94
95 EXPECT_CALL(reportFactoryMock,
Wludzik, Jozefe2362792020-10-27 17:23:55 +010096 make(_, reportParams.reportName(), reportParams.reportingType(),
97 reportParams.emitReadingUpdate(),
98 reportParams.logToMetricReportCollection(),
99 reportParams.interval(), reportParams.readingParameters(),
100 Ref(*sut), Ref(storageMock)))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200101 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
102
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100103 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200104 EXPECT_THAT(ec.value(), Eq(boost::system::errc::success));
105 EXPECT_THAT(path, Eq(reportMock.getPath()));
106}
107
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100108TEST_F(TestReportManager, DISABLED_failToAddReportTwice)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200109{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100110 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _));
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200111
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100112 addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200113
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100114 auto [ec, path] = addReport(reportParams);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200115 EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists));
116 EXPECT_THAT(path, Eq(std::string()));
117}
118
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100119TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval)
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200120{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100121 EXPECT_CALL(reportFactoryMock, make).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{
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100132 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _))
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{
152 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100153 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200154 auto& reportMock = *reportMockPtr;
155
156 {
157 InSequence seq;
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100158 EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200159 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
160 EXPECT_CALL(reportMock, Die());
161 EXPECT_CALL(checkPoint, Call("end"));
162 }
163
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100164 addReport(reportParams);
165 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200166 checkPoint.Call("end");
167}
168
169TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect)
170{
171 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100172 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200173 auto& reportMock = *reportMockPtr;
174
175 {
176 InSequence seq;
177 EXPECT_CALL(checkPoint, Call("end"));
178 EXPECT_CALL(reportMock, Die());
179 }
180
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100181 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200182 checkPoint.Call("end");
183}
184
185TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect)
186{
187 auto reportMockPtr =
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100188 std::make_unique<NiceMock<ReportMock>>(reportParams.reportName());
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200189 auto& reportMock = *reportMockPtr;
190
191 {
192 InSequence seq;
193 EXPECT_CALL(reportFactoryMock,
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100194 make(_, reportParams.reportName(), _, _, _, _, _, _, _))
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200195 .WillOnce(Return(ByMove(std::move(reportMockPtr))));
196 EXPECT_CALL(reportMock, Die());
197 EXPECT_CALL(checkPoint, Call("end"));
198 }
199
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100200 addReport(reportParams);
201 sut->removeReport(&reportMock);
202 sut->removeReport(&reportMock);
Wludzik, Jozef2f9f9b82020-10-13 09:07:45 +0200203 checkPoint.Call("end");
204}
Wludzik, Jozefe2362792020-10-27 17:23:55 +0100205
206class TestReportManagerStorage : public TestReportManager
207{
208 public:
209 using FilePath = interfaces::JsonStorage::FilePath;
210 using DirectoryPath = interfaces::JsonStorage::DirectoryPath;
211
212 void SetUp() override
213 {
214 ON_CALL(storageMock, list())
215 .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")}));
216 ON_CALL(storageMock, load(FilePath("report1")))
217 .WillByDefault(Return(data));
218 }
219
220 void makeReportManager()
221 {
222 sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr),
223 std::move(storageMockPtr),
224 DbusEnvironment::getObjServer());
225 }
226
227 nlohmann::json data = nlohmann::json{
228 {"Version", Report::reportVersion},
229 {"Name", reportParams.reportName()},
230 {"ReportingType", reportParams.reportingType()},
231 {"EmitsReadingsUpdate", reportParams.emitReadingUpdate()},
232 {"LogToMetricReportsCollection",
233 reportParams.logToMetricReportCollection()},
234 {"Interval", reportParams.interval().count()},
235 {"ReadingParameters",
236 utils::transform(reportParams.readingParameters(),
237 [](const auto& item) {
238 return LabeledReadingParameter::to_json(item);
239 })}};
240};
241
242TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage)
243{
244 EXPECT_CALL(reportFactoryMock,
245 make(_, reportParams.reportName(), reportParams.reportingType(),
246 reportParams.emitReadingUpdate(),
247 reportParams.logToMetricReportCollection(),
248 reportParams.interval(), reportParams.readingParameters(),
249 _, Ref(storageMock)));
250
251 makeReportManager();
252}
253
254TEST_F(TestReportManagerStorage,
255 reportManagerCtorRemoveFileIfVersionDoesNotMatch)
256{
257 data["Version"] = Report::reportVersion - 1;
258
259 ON_CALL(storageMock, load(FilePath("report1"))).WillByDefault(Return(data));
260 EXPECT_CALL(storageMock, remove(FilePath("report1")));
261
262 makeReportManager();
263}
264
265TEST_F(TestReportManagerStorage,
266 reportManagerCtorRemoveFileIfIntervalHasWrongType)
267{
268 data["Interval"] = "1000";
269
270 ON_CALL(storageMock, load(FilePath("report1"))).WillByDefault(Return(data));
271 EXPECT_CALL(storageMock, remove(FilePath("report1")));
272
273 makeReportManager();
274}