Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 2 | #include "mocks/json_storage_mock.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 3 | #include "mocks/report_factory_mock.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 4 | #include "params/report_params.hpp" |
| 5 | #include "report.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 6 | #include "report_manager.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 7 | #include "utils/transform.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 8 | |
| 9 | using namespace testing; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 10 | using namespace std::chrono_literals; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 11 | |
| 12 | class TestReportManager : public Test |
| 13 | { |
| 14 | public: |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 15 | ReportParams reportParams; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 16 | |
| 17 | std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr = |
| 18 | std::make_unique<StrictMock<ReportFactoryMock>>(); |
| 19 | ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 20 | |
| 21 | std::unique_ptr<StorageMock> storageMockPtr = |
| 22 | std::make_unique<NiceMock<StorageMock>>(); |
| 23 | StorageMock& storageMock = *storageMockPtr; |
| 24 | |
| 25 | std::unique_ptr<ReportManager> sut; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 26 | |
| 27 | MockFunction<void(std::string)> checkPoint; |
| 28 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 29 | void SetUp() override |
| 30 | { |
| 31 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 32 | std::move(storageMockPtr), |
| 33 | DbusEnvironment::getObjServer()); |
| 34 | } |
| 35 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 36 | void TearDown() override |
| 37 | { |
| 38 | DbusEnvironment::synchronizeIoc(); |
| 39 | } |
| 40 | |
| 41 | std::pair<boost::system::error_code, std::string> |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 42 | addReport(const ReportParams& params) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 43 | { |
| 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, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 52 | 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, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 57 | 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 | |
| 79 | TEST_F(TestReportManager, minInterval) |
| 80 | { |
| 81 | EXPECT_THAT(getProperty<uint64_t>("MinInterval"), |
| 82 | Eq(static_cast<uint64_t>(ReportManager::minInterval.count()))); |
| 83 | } |
| 84 | |
| 85 | TEST_F(TestReportManager, maxReports) |
| 86 | { |
| 87 | EXPECT_THAT(getProperty<uint32_t>("MaxReports"), |
| 88 | Eq(ReportManager::maxReports)); |
| 89 | } |
| 90 | |
| 91 | TEST_F(TestReportManager, addReport) |
| 92 | { |
| 93 | auto reportMockPtr = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 94 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 95 | auto& reportMock = *reportMockPtr; |
| 96 | |
| 97 | EXPECT_CALL(reportFactoryMock, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 98 | make(_, reportParams.reportName(), reportParams.reportingType(), |
| 99 | reportParams.emitReadingUpdate(), |
| 100 | reportParams.logToMetricReportCollection(), |
| 101 | reportParams.interval(), reportParams.readingParameters(), |
| 102 | Ref(*sut), Ref(storageMock))) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 103 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 104 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 105 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 106 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 107 | EXPECT_THAT(path, Eq(reportMock.getPath())); |
| 108 | } |
| 109 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 110 | TEST_F(TestReportManager, DISABLED_failToAddReportTwice) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 111 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 112 | EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 113 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 114 | addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 115 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 116 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 117 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists)); |
| 118 | EXPECT_THAT(path, Eq(std::string())); |
| 119 | } |
| 120 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 121 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 122 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 123 | EXPECT_CALL(reportFactoryMock, make).Times(0); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 124 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 125 | reportParams.interval(reportParams.interval() - 1ms); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 126 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 127 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 128 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 129 | EXPECT_THAT(path, Eq(std::string())); |
| 130 | } |
| 131 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 132 | TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 133 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 134 | EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 135 | .Times(ReportManager::maxReports); |
| 136 | |
| 137 | for (size_t i = 0; i < ReportManager::maxReports; i++) |
| 138 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 139 | reportParams.reportName(reportParams.reportName() + std::to_string(i)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 140 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 141 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 142 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 143 | } |
| 144 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 145 | reportParams.reportName(reportParams.reportName() + |
| 146 | std::to_string(ReportManager::maxReports)); |
| 147 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 148 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open)); |
| 149 | EXPECT_THAT(path, Eq(std::string())); |
| 150 | } |
| 151 | |
| 152 | TEST_F(TestReportManager, removeReport) |
| 153 | { |
| 154 | auto reportMockPtr = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 155 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 156 | auto& reportMock = *reportMockPtr; |
| 157 | |
| 158 | { |
| 159 | InSequence seq; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 160 | EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 161 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 162 | EXPECT_CALL(reportMock, Die()); |
| 163 | EXPECT_CALL(checkPoint, Call("end")); |
| 164 | } |
| 165 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 166 | addReport(reportParams); |
| 167 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 168 | checkPoint.Call("end"); |
| 169 | } |
| 170 | |
| 171 | TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect) |
| 172 | { |
| 173 | auto reportMockPtr = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 174 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 175 | auto& reportMock = *reportMockPtr; |
| 176 | |
| 177 | { |
| 178 | InSequence seq; |
| 179 | EXPECT_CALL(checkPoint, Call("end")); |
| 180 | EXPECT_CALL(reportMock, Die()); |
| 181 | } |
| 182 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 183 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 184 | checkPoint.Call("end"); |
| 185 | } |
| 186 | |
| 187 | TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect) |
| 188 | { |
| 189 | auto reportMockPtr = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 190 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 191 | auto& reportMock = *reportMockPtr; |
| 192 | |
| 193 | { |
| 194 | InSequence seq; |
| 195 | EXPECT_CALL(reportFactoryMock, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 196 | make(_, reportParams.reportName(), _, _, _, _, _, _, _)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 197 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 198 | EXPECT_CALL(reportMock, Die()); |
| 199 | EXPECT_CALL(checkPoint, Call("end")); |
| 200 | } |
| 201 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 202 | addReport(reportParams); |
| 203 | sut->removeReport(&reportMock); |
| 204 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 205 | checkPoint.Call("end"); |
| 206 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame^] | 207 | |
| 208 | class 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 | |
| 244 | TEST_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 | |
| 256 | TEST_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 | |
| 267 | TEST_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 | } |