Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 2 | #include "helpers.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 3 | #include "mocks/json_storage_mock.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 4 | #include "mocks/report_factory_mock.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 5 | #include "params/report_params.hpp" |
| 6 | #include "report.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 7 | #include "report_manager.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 8 | #include "utils/transform.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 9 | |
| 10 | using namespace testing; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 11 | using namespace std::chrono_literals; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 12 | |
| 13 | class TestReportManager : public Test |
| 14 | { |
| 15 | public: |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 16 | ReportParams reportParams; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 17 | |
| 18 | std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr = |
| 19 | std::make_unique<StrictMock<ReportFactoryMock>>(); |
| 20 | ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 21 | |
| 22 | std::unique_ptr<StorageMock> storageMockPtr = |
| 23 | std::make_unique<NiceMock<StorageMock>>(); |
| 24 | StorageMock& storageMock = *storageMockPtr; |
| 25 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 26 | std::unique_ptr<ReportMock> reportMockPtr = |
| 27 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
| 28 | ReportMock& reportMock = *reportMockPtr; |
| 29 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 30 | std::unique_ptr<ReportManager> sut; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 31 | |
| 32 | MockFunction<void(std::string)> checkPoint; |
| 33 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 34 | void SetUp() override |
| 35 | { |
| 36 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 37 | std::move(storageMockPtr), |
| 38 | DbusEnvironment::getObjServer()); |
| 39 | } |
| 40 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 41 | void TearDown() override |
| 42 | { |
| 43 | DbusEnvironment::synchronizeIoc(); |
| 44 | } |
| 45 | |
| 46 | std::pair<boost::system::error_code, std::string> |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 47 | addReport(const ReportParams& params) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 48 | { |
| 49 | std::promise<std::pair<boost::system::error_code, std::string>> |
| 50 | addReportPromise; |
| 51 | DbusEnvironment::getBus()->async_method_call( |
| 52 | [&addReportPromise](boost::system::error_code ec, |
| 53 | const std::string& path) { |
| 54 | addReportPromise.set_value({ec, path}); |
| 55 | }, |
| 56 | DbusEnvironment::serviceName(), ReportManager::reportManagerPath, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 57 | ReportManager::reportManagerIfaceName, "AddReport", |
| 58 | params.reportName(), params.reportingType(), |
| 59 | params.emitReadingUpdate(), params.logToMetricReportCollection(), |
| 60 | static_cast<uint64_t>(params.interval().count()), |
| 61 | params.readingParameters()); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 62 | return DbusEnvironment::waitForFuture(addReportPromise.get_future()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | template <class T> |
| 66 | static T getProperty(std::string property) |
| 67 | { |
| 68 | std::promise<T> propertyPromise; |
| 69 | sdbusplus::asio::getProperty<T>( |
| 70 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), |
| 71 | ReportManager::reportManagerPath, |
| 72 | ReportManager::reportManagerIfaceName, property, |
| 73 | [&propertyPromise](boost::system::error_code ec) { |
| 74 | EXPECT_THAT(static_cast<bool>(ec), ::testing::Eq(false)); |
| 75 | propertyPromise.set_value(T{}); |
| 76 | }, |
| 77 | [&propertyPromise](T t) { propertyPromise.set_value(t); }); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 78 | return DbusEnvironment::waitForFuture(propertyPromise.get_future()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
| 82 | TEST_F(TestReportManager, minInterval) |
| 83 | { |
| 84 | EXPECT_THAT(getProperty<uint64_t>("MinInterval"), |
| 85 | Eq(static_cast<uint64_t>(ReportManager::minInterval.count()))); |
| 86 | } |
| 87 | |
| 88 | TEST_F(TestReportManager, maxReports) |
| 89 | { |
| 90 | EXPECT_THAT(getProperty<uint32_t>("MaxReports"), |
| 91 | Eq(ReportManager::maxReports)); |
| 92 | } |
| 93 | |
| 94 | TEST_F(TestReportManager, addReport) |
| 95 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 96 | reportFactoryMock.expectMake(_, reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 97 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 98 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 99 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 100 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 101 | EXPECT_THAT(path, Eq(reportMock.getPath())); |
| 102 | } |
| 103 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 104 | TEST_F(TestReportManager, DISABLED_failToAddReportTwice) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 105 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 106 | reportFactoryMock.expectMake(_, reportParams, Ref(*sut), Ref(storageMock)) |
| 107 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 108 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 109 | addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 110 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 111 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 112 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists)); |
| 113 | EXPECT_THAT(path, Eq(std::string())); |
| 114 | } |
| 115 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 116 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 117 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 118 | reportFactoryMock.expectMake(_, std::nullopt, Ref(*sut), Ref(storageMock)) |
| 119 | .Times(0); |
| 120 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock), _) |
| 121 | .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 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 132 | reportFactoryMock.expectMake(_, std::nullopt, Ref(*sut), Ref(storageMock)) |
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 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 152 | { |
| 153 | InSequence seq; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 154 | reportFactoryMock |
| 155 | .expectMake(_, reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 156 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 157 | EXPECT_CALL(reportMock, Die()); |
| 158 | EXPECT_CALL(checkPoint, Call("end")); |
| 159 | } |
| 160 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 161 | addReport(reportParams); |
| 162 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 163 | checkPoint.Call("end"); |
| 164 | } |
| 165 | |
| 166 | TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect) |
| 167 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 168 | { |
| 169 | InSequence seq; |
| 170 | EXPECT_CALL(checkPoint, Call("end")); |
| 171 | EXPECT_CALL(reportMock, Die()); |
| 172 | } |
| 173 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 174 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 175 | checkPoint.Call("end"); |
| 176 | } |
| 177 | |
| 178 | TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect) |
| 179 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 180 | { |
| 181 | InSequence seq; |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 182 | reportFactoryMock |
| 183 | .expectMake(_, reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 184 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 185 | EXPECT_CALL(reportMock, Die()); |
| 186 | EXPECT_CALL(checkPoint, Call("end")); |
| 187 | } |
| 188 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 189 | addReport(reportParams); |
| 190 | sut->removeReport(&reportMock); |
| 191 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 192 | checkPoint.Call("end"); |
| 193 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 194 | |
| 195 | class TestReportManagerStorage : public TestReportManager |
| 196 | { |
| 197 | public: |
| 198 | using FilePath = interfaces::JsonStorage::FilePath; |
| 199 | using DirectoryPath = interfaces::JsonStorage::DirectoryPath; |
| 200 | |
| 201 | void SetUp() override |
| 202 | { |
| 203 | ON_CALL(storageMock, list()) |
| 204 | .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")})); |
| 205 | ON_CALL(storageMock, load(FilePath("report1"))) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 206 | .WillByDefault(InvokeWithoutArgs([this] { return data; })); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void makeReportManager() |
| 210 | { |
| 211 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 212 | std::move(storageMockPtr), |
| 213 | DbusEnvironment::getObjServer()); |
| 214 | } |
| 215 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 216 | static std::vector<LabeledMetricParameters> |
| 217 | convertToLabeled(const ReadingParameters& params) |
| 218 | { |
| 219 | return utils::transform(params, [](const auto& item) { |
| 220 | return LabeledMetricParameters( |
| 221 | utils::transform(std::get<0>(item), |
| 222 | [](const auto& elem) { |
| 223 | return LabeledSensorParameters("service", |
| 224 | elem); |
| 225 | }), |
| 226 | std::get<1>(item), std::get<2>(item), std::get<3>(item)); |
| 227 | }); |
| 228 | } |
| 229 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 230 | nlohmann::json data = nlohmann::json{ |
| 231 | {"Version", Report::reportVersion}, |
| 232 | {"Name", reportParams.reportName()}, |
| 233 | {"ReportingType", reportParams.reportingType()}, |
| 234 | {"EmitsReadingsUpdate", reportParams.emitReadingUpdate()}, |
| 235 | {"LogToMetricReportsCollection", |
| 236 | reportParams.logToMetricReportCollection()}, |
| 237 | {"Interval", reportParams.interval().count()}, |
| 238 | {"ReadingParameters", |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 239 | convertToLabeled(reportParams.readingParameters())}}; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage) |
| 243 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame^] | 244 | reportFactoryMock.expectMake( |
| 245 | reportParams, _, Ref(storageMock), |
| 246 | ElementsAreArray(convertToLabeled(reportParams.readingParameters()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 247 | |
| 248 | makeReportManager(); |
| 249 | } |
| 250 | |
| 251 | TEST_F(TestReportManagerStorage, |
| 252 | reportManagerCtorRemoveFileIfVersionDoesNotMatch) |
| 253 | { |
| 254 | data["Version"] = Report::reportVersion - 1; |
| 255 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 256 | EXPECT_CALL(storageMock, remove(FilePath("report1"))); |
| 257 | |
| 258 | makeReportManager(); |
| 259 | } |
| 260 | |
| 261 | TEST_F(TestReportManagerStorage, |
| 262 | reportManagerCtorRemoveFileIfIntervalHasWrongType) |
| 263 | { |
| 264 | data["Interval"] = "1000"; |
| 265 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 266 | EXPECT_CALL(storageMock, remove(FilePath("report1"))); |
| 267 | |
| 268 | makeReportManager(); |
| 269 | } |