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()); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame^] | 57 | return DbusEnvironment::waitForFuture(addReportPromise.get_future()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 58 | } |
| 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 Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame^] | 73 | return DbusEnvironment::waitForFuture(propertyPromise.get_future()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
| 77 | TEST_F(TestReportManager, minInterval) |
| 78 | { |
| 79 | EXPECT_THAT(getProperty<uint64_t>("MinInterval"), |
| 80 | Eq(static_cast<uint64_t>(ReportManager::minInterval.count()))); |
| 81 | } |
| 82 | |
| 83 | TEST_F(TestReportManager, maxReports) |
| 84 | { |
| 85 | EXPECT_THAT(getProperty<uint32_t>("MaxReports"), |
| 86 | Eq(ReportManager::maxReports)); |
| 87 | } |
| 88 | |
| 89 | TEST_F(TestReportManager, addReport) |
| 90 | { |
| 91 | auto reportMockPtr = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 92 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 93 | auto& reportMock = *reportMockPtr; |
| 94 | |
| 95 | EXPECT_CALL(reportFactoryMock, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 96 | make(_, reportParams.reportName(), reportParams.reportingType(), |
| 97 | reportParams.emitReadingUpdate(), |
| 98 | reportParams.logToMetricReportCollection(), |
| 99 | reportParams.interval(), reportParams.readingParameters(), |
| 100 | Ref(*sut), Ref(storageMock))) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 101 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 102 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 103 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 104 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 105 | EXPECT_THAT(path, Eq(reportMock.getPath())); |
| 106 | } |
| 107 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 108 | TEST_F(TestReportManager, DISABLED_failToAddReportTwice) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 109 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 110 | EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _)); |
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 | addReport(reportParams); |
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 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 115 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists)); |
| 116 | EXPECT_THAT(path, Eq(std::string())); |
| 117 | } |
| 118 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 119 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 120 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 121 | EXPECT_CALL(reportFactoryMock, make).Times(0); |
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 | reportParams.interval(reportParams.interval() - 1ms); |
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 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 126 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 127 | EXPECT_THAT(path, Eq(std::string())); |
| 128 | } |
| 129 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 130 | TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 131 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 132 | EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 133 | .Times(ReportManager::maxReports); |
| 134 | |
| 135 | for (size_t i = 0; i < ReportManager::maxReports; i++) |
| 136 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 137 | reportParams.reportName(reportParams.reportName() + std::to_string(i)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 138 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 139 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 140 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 141 | } |
| 142 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 143 | reportParams.reportName(reportParams.reportName() + |
| 144 | std::to_string(ReportManager::maxReports)); |
| 145 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 146 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open)); |
| 147 | EXPECT_THAT(path, Eq(std::string())); |
| 148 | } |
| 149 | |
| 150 | TEST_F(TestReportManager, removeReport) |
| 151 | { |
| 152 | auto reportMockPtr = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 153 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 154 | auto& reportMock = *reportMockPtr; |
| 155 | |
| 156 | { |
| 157 | InSequence seq; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 158 | EXPECT_CALL(reportFactoryMock, make(_, _, _, _, _, _, _, _, _)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 159 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 160 | EXPECT_CALL(reportMock, Die()); |
| 161 | EXPECT_CALL(checkPoint, Call("end")); |
| 162 | } |
| 163 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 164 | addReport(reportParams); |
| 165 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 166 | checkPoint.Call("end"); |
| 167 | } |
| 168 | |
| 169 | TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect) |
| 170 | { |
| 171 | auto reportMockPtr = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 172 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 173 | auto& reportMock = *reportMockPtr; |
| 174 | |
| 175 | { |
| 176 | InSequence seq; |
| 177 | EXPECT_CALL(checkPoint, Call("end")); |
| 178 | EXPECT_CALL(reportMock, Die()); |
| 179 | } |
| 180 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 181 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 182 | checkPoint.Call("end"); |
| 183 | } |
| 184 | |
| 185 | TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect) |
| 186 | { |
| 187 | auto reportMockPtr = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 188 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 189 | auto& reportMock = *reportMockPtr; |
| 190 | |
| 191 | { |
| 192 | InSequence seq; |
| 193 | EXPECT_CALL(reportFactoryMock, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 194 | make(_, reportParams.reportName(), _, _, _, _, _, _, _)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 195 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 196 | EXPECT_CALL(reportMock, Die()); |
| 197 | EXPECT_CALL(checkPoint, Call("end")); |
| 198 | } |
| 199 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 200 | addReport(reportParams); |
| 201 | sut->removeReport(&reportMock); |
| 202 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 203 | checkPoint.Call("end"); |
| 204 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 205 | |
| 206 | class 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 | |
| 242 | TEST_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 | |
| 254 | TEST_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 | |
| 265 | TEST_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 | } |