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" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 8 | #include "utils/conversion.hpp" |
Ed Tanous | 0e7ae5d | 2021-02-23 14:06:49 -0800 | [diff] [blame] | 9 | #include "utils/set_exception.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 10 | #include "utils/transform.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 11 | |
| 12 | using namespace testing; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 13 | using namespace std::string_literals; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 14 | using namespace std::chrono_literals; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 15 | |
| 16 | class TestReportManager : public Test |
| 17 | { |
| 18 | public: |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 19 | ReportParams reportParams; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 20 | |
| 21 | std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr = |
| 22 | std::make_unique<StrictMock<ReportFactoryMock>>(); |
| 23 | ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 24 | |
| 25 | std::unique_ptr<StorageMock> storageMockPtr = |
| 26 | std::make_unique<NiceMock<StorageMock>>(); |
| 27 | StorageMock& storageMock = *storageMockPtr; |
| 28 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 29 | std::unique_ptr<ReportMock> reportMockPtr = |
| 30 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportName()); |
| 31 | ReportMock& reportMock = *reportMockPtr; |
| 32 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 33 | std::unique_ptr<ReportManager> sut; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 34 | |
| 35 | MockFunction<void(std::string)> checkPoint; |
| 36 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 37 | void SetUp() override |
| 38 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 39 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)) |
| 40 | .Times(AnyNumber()); |
| 41 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 42 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 43 | std::move(storageMockPtr), |
| 44 | DbusEnvironment::getObjServer()); |
| 45 | } |
| 46 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 47 | void TearDown() override |
| 48 | { |
| 49 | DbusEnvironment::synchronizeIoc(); |
| 50 | } |
| 51 | |
| 52 | std::pair<boost::system::error_code, std::string> |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 53 | addReport(const ReportParams& params) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 54 | { |
| 55 | std::promise<std::pair<boost::system::error_code, std::string>> |
| 56 | addReportPromise; |
| 57 | DbusEnvironment::getBus()->async_method_call( |
| 58 | [&addReportPromise](boost::system::error_code ec, |
| 59 | const std::string& path) { |
| 60 | addReportPromise.set_value({ec, path}); |
| 61 | }, |
| 62 | DbusEnvironment::serviceName(), ReportManager::reportManagerPath, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 63 | ReportManager::reportManagerIfaceName, "AddReportFutureVersion", |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 64 | params.reportName(), params.reportingType(), |
| 65 | params.emitReadingUpdate(), params.logToMetricReportCollection(), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 66 | params.interval().count(), |
| 67 | toReadingParameters(params.metricParameters())); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 68 | return DbusEnvironment::waitForFuture(addReportPromise.get_future()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | template <class T> |
| 72 | static T getProperty(std::string property) |
| 73 | { |
Ed Tanous | 0e7ae5d | 2021-02-23 14:06:49 -0800 | [diff] [blame] | 74 | auto propertyPromise = std::promise<T>(); |
| 75 | auto propertyFuture = propertyPromise.get_future(); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 76 | sdbusplus::asio::getProperty<T>( |
| 77 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), |
| 78 | ReportManager::reportManagerPath, |
| 79 | ReportManager::reportManagerIfaceName, property, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 80 | [&propertyPromise](const boost::system::error_code& ec, T t) { |
Ed Tanous | 0e7ae5d | 2021-02-23 14:06:49 -0800 | [diff] [blame] | 81 | if (ec) |
| 82 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 83 | utils::setException(propertyPromise, "Get property failed"); |
Ed Tanous | 0e7ae5d | 2021-02-23 14:06:49 -0800 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | propertyPromise.set_value(t); |
| 87 | }); |
| 88 | return DbusEnvironment::waitForFuture(std::move(propertyFuture)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 89 | } |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 90 | |
| 91 | static std::string prepareReportNameWithLength(size_t length) |
| 92 | { |
| 93 | std::stringstream reportNameStream; |
| 94 | for (size_t i = 0; i < length; ++i) |
| 95 | { |
| 96 | reportNameStream << "z"; |
| 97 | } |
| 98 | return reportNameStream.str(); |
| 99 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | TEST_F(TestReportManager, minInterval) |
| 103 | { |
| 104 | EXPECT_THAT(getProperty<uint64_t>("MinInterval"), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 105 | Eq(ReportManager::minInterval.count())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST_F(TestReportManager, maxReports) |
| 109 | { |
Wludzik, Jozef | 503c158 | 2020-12-11 14:48:01 +0100 | [diff] [blame] | 110 | EXPECT_THAT(getProperty<size_t>("MaxReports"), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 111 | Eq(ReportManager::maxReports)); |
| 112 | } |
| 113 | |
| 114 | TEST_F(TestReportManager, addReport) |
| 115 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 116 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 117 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 118 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 119 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 120 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 121 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 122 | EXPECT_THAT(path, Eq(reportMock.getPath())); |
| 123 | } |
| 124 | |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 125 | TEST_F(TestReportManager, addReportWithMaxLengthName) |
| 126 | { |
| 127 | std::string reportName = |
| 128 | prepareReportNameWithLength(ReportManager::maxReportNameLength); |
| 129 | reportParams.reportName(reportName); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 130 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 131 | |
| 132 | auto [ec, path] = addReport(reportParams); |
| 133 | |
| 134 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 135 | EXPECT_THAT(path, Eq("/"s + reportName)); |
| 136 | } |
| 137 | |
| 138 | TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName) |
| 139 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 140 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 141 | .Times(0); |
| 142 | |
| 143 | reportParams.reportName( |
| 144 | prepareReportNameWithLength(ReportManager::maxReportNameLength + 1)); |
| 145 | |
| 146 | auto [ec, path] = addReport(reportParams); |
| 147 | |
| 148 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 149 | EXPECT_THAT(path, Eq(std::string())); |
| 150 | } |
| 151 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 152 | TEST_F(TestReportManager, DISABLED_failToAddReportTwice) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 153 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 154 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 155 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 156 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 157 | addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 158 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 159 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 160 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 161 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists)); |
| 162 | EXPECT_THAT(path, Eq(std::string())); |
| 163 | } |
| 164 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 165 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 166 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 167 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 168 | .Times(0); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 169 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 170 | reportParams.reportingType("Periodic"); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 171 | reportParams.interval(reportParams.interval() - 1ms); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 172 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 173 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 174 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 175 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 176 | EXPECT_THAT(path, Eq(std::string())); |
| 177 | } |
| 178 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 179 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidReportingType) |
| 180 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 181 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 182 | .Times(0); |
| 183 | |
| 184 | reportParams.reportingType("Invalid"); |
| 185 | |
| 186 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 187 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 188 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 189 | EXPECT_THAT(path, Eq(std::string())); |
| 190 | } |
| 191 | |
| 192 | TEST_F(TestReportManager, DISABLED_failToAddReportWithMoreSensorsThanExpected) |
| 193 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 194 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 195 | .Times(0); |
| 196 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 197 | auto metricParams = reportParams.metricParameters(); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 198 | for (size_t i = 0; i < ReportManager::maxReadingParams + 1; i++) |
| 199 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 200 | metricParams.push_back(metricParams.front()); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 201 | } |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 202 | reportParams.metricParameters(std::move(metricParams)); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 203 | |
| 204 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 205 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 206 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long)); |
| 207 | EXPECT_THAT(path, Eq(std::string())); |
| 208 | } |
| 209 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 210 | TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 211 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 212 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 213 | .Times(ReportManager::maxReports); |
| 214 | |
| 215 | for (size_t i = 0; i < ReportManager::maxReports; i++) |
| 216 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 217 | reportParams.reportName(reportParams.reportName() + std::to_string(i)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 218 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 219 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 220 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 221 | } |
| 222 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 223 | reportParams.reportName(reportParams.reportName() + |
| 224 | std::to_string(ReportManager::maxReports)); |
| 225 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 226 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 227 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open)); |
| 228 | EXPECT_THAT(path, Eq(std::string())); |
| 229 | } |
| 230 | |
| 231 | TEST_F(TestReportManager, removeReport) |
| 232 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 233 | { |
| 234 | InSequence seq; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 235 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 236 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 237 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 238 | EXPECT_CALL(reportMock, Die()); |
| 239 | EXPECT_CALL(checkPoint, Call("end")); |
| 240 | } |
| 241 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 242 | addReport(reportParams); |
| 243 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 244 | checkPoint.Call("end"); |
| 245 | } |
| 246 | |
| 247 | TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect) |
| 248 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 249 | { |
| 250 | InSequence seq; |
| 251 | EXPECT_CALL(checkPoint, Call("end")); |
| 252 | EXPECT_CALL(reportMock, Die()); |
| 253 | } |
| 254 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 255 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 256 | checkPoint.Call("end"); |
| 257 | } |
| 258 | |
| 259 | TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect) |
| 260 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 261 | { |
| 262 | InSequence seq; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 263 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 264 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 265 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 266 | EXPECT_CALL(reportMock, Die()); |
| 267 | EXPECT_CALL(checkPoint, Call("end")); |
| 268 | } |
| 269 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 270 | addReport(reportParams); |
| 271 | sut->removeReport(&reportMock); |
| 272 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 273 | checkPoint.Call("end"); |
| 274 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 275 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 276 | TEST_F(TestReportManager, updateReportCallsUpdateReadingsForExistReport) |
| 277 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 278 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 279 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 280 | EXPECT_CALL(reportMock, updateReadings()); |
| 281 | |
| 282 | addReport(reportParams); |
| 283 | sut->updateReport(reportParams.reportName()); |
| 284 | } |
| 285 | |
| 286 | TEST_F(TestReportManager, updateReportDoNothingIfReportDoesNotExist) |
| 287 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 288 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 289 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 290 | EXPECT_CALL(reportMock, updateReadings()).Times(0); |
| 291 | |
| 292 | addReport(reportParams); |
| 293 | sut->updateReport("NotAReport"); |
| 294 | } |
| 295 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 296 | class TestReportManagerWithAggregationOperationType : |
| 297 | public TestReportManager, |
| 298 | public WithParamInterface<OperationType> |
| 299 | { |
| 300 | public: |
| 301 | OperationType operationType = GetParam(); |
| 302 | }; |
| 303 | |
| 304 | INSTANTIATE_TEST_SUITE_P(_, TestReportManagerWithAggregationOperationType, |
| 305 | Values(OperationType::single, OperationType::max, |
| 306 | OperationType::min, OperationType::avg, |
| 307 | OperationType::sum)); |
| 308 | |
| 309 | TEST_P(TestReportManagerWithAggregationOperationType, |
| 310 | addReportWithDifferentOperationTypes) |
| 311 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 312 | reportParams.metricParameters( |
| 313 | std::vector<LabeledMetricParameters>{{LabeledMetricParameters{ |
| 314 | {LabeledSensorParameters{"Service", |
| 315 | "/xyz/openbmc_project/sensors/power/p1"}}, |
| 316 | operationType, |
| 317 | "MetricId1", |
| 318 | "Metadata1", |
| 319 | CollectionTimeScope::point, |
| 320 | CollectionDuration(Milliseconds(0u))}}}); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 321 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 322 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 323 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 324 | |
| 325 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 326 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 327 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 328 | EXPECT_THAT(path, Eq("/"s + reportParams.reportName())); |
| 329 | } |
| 330 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 331 | class TestReportManagerStorage : public TestReportManager |
| 332 | { |
| 333 | public: |
| 334 | using FilePath = interfaces::JsonStorage::FilePath; |
| 335 | using DirectoryPath = interfaces::JsonStorage::DirectoryPath; |
| 336 | |
| 337 | void SetUp() override |
| 338 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 339 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)).Times(0); |
| 340 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 341 | ON_CALL(storageMock, list()) |
| 342 | .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")})); |
| 343 | ON_CALL(storageMock, load(FilePath("report1"))) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 344 | .WillByDefault(InvokeWithoutArgs([this] { return data; })); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void makeReportManager() |
| 348 | { |
| 349 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 350 | std::move(storageMockPtr), |
| 351 | DbusEnvironment::getObjServer()); |
| 352 | } |
| 353 | |
| 354 | nlohmann::json data = nlohmann::json{ |
| 355 | {"Version", Report::reportVersion}, |
| 356 | {"Name", reportParams.reportName()}, |
| 357 | {"ReportingType", reportParams.reportingType()}, |
| 358 | {"EmitsReadingsUpdate", reportParams.emitReadingUpdate()}, |
| 359 | {"LogToMetricReportsCollection", |
| 360 | reportParams.logToMetricReportCollection()}, |
| 361 | {"Interval", reportParams.interval().count()}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 362 | {"ReadingParameters", reportParams.metricParameters()}}; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage) |
| 366 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 367 | reportFactoryMock.expectMake(reportParams, _, Ref(storageMock)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 368 | |
| 369 | makeReportManager(); |
| 370 | } |
| 371 | |
| 372 | TEST_F(TestReportManagerStorage, |
| 373 | reportManagerCtorRemoveFileIfVersionDoesNotMatch) |
| 374 | { |
| 375 | data["Version"] = Report::reportVersion - 1; |
| 376 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 377 | EXPECT_CALL(storageMock, remove(FilePath("report1"))); |
| 378 | |
| 379 | makeReportManager(); |
| 380 | } |
| 381 | |
| 382 | TEST_F(TestReportManagerStorage, |
| 383 | reportManagerCtorRemoveFileIfIntervalHasWrongType) |
| 384 | { |
| 385 | data["Interval"] = "1000"; |
| 386 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 387 | EXPECT_CALL(storageMock, remove(FilePath("report1"))); |
| 388 | |
| 389 | makeReportManager(); |
| 390 | } |