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" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 9 | #include "utils/transform.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 10 | |
| 11 | using namespace testing; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 12 | using namespace std::string_literals; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 13 | using namespace std::chrono_literals; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 14 | |
| 15 | class TestReportManager : public Test |
| 16 | { |
| 17 | public: |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 18 | ReportParams reportParams; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 19 | |
| 20 | std::unique_ptr<ReportFactoryMock> reportFactoryMockPtr = |
| 21 | std::make_unique<StrictMock<ReportFactoryMock>>(); |
| 22 | ReportFactoryMock& reportFactoryMock = *reportFactoryMockPtr; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 23 | |
| 24 | std::unique_ptr<StorageMock> storageMockPtr = |
| 25 | std::make_unique<NiceMock<StorageMock>>(); |
| 26 | StorageMock& storageMock = *storageMockPtr; |
| 27 | |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 28 | std::unique_ptr<ReportMock> reportMockPtr = |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 29 | std::make_unique<NiceMock<ReportMock>>(reportParams.reportId()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 30 | ReportMock& reportMock = *reportMockPtr; |
| 31 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 32 | std::unique_ptr<ReportManager> sut; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 33 | |
| 34 | MockFunction<void(std::string)> checkPoint; |
| 35 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 36 | void SetUp() override |
| 37 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 38 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)) |
| 39 | .Times(AnyNumber()); |
| 40 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 41 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 42 | std::move(storageMockPtr), |
| 43 | DbusEnvironment::getObjServer()); |
| 44 | } |
| 45 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 46 | void TearDown() override |
| 47 | { |
| 48 | DbusEnvironment::synchronizeIoc(); |
| 49 | } |
| 50 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 51 | template <class... Args> |
| 52 | requires(sizeof...(Args) > 1) |
| 53 | std::pair<boost::system::error_code, std::string> addReport( |
| 54 | Args&&... args) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 55 | { |
| 56 | std::promise<std::pair<boost::system::error_code, std::string>> |
| 57 | addReportPromise; |
| 58 | DbusEnvironment::getBus()->async_method_call( |
| 59 | [&addReportPromise](boost::system::error_code ec, |
| 60 | const std::string& path) { |
| 61 | addReportPromise.set_value({ec, path}); |
| 62 | }, |
| 63 | DbusEnvironment::serviceName(), ReportManager::reportManagerPath, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 64 | ReportManager::reportManagerIfaceName, "AddReportFutureVersion", |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 65 | std::forward<Args>(args)...); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 66 | return DbusEnvironment::waitForFuture(addReportPromise.get_future()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 69 | auto addReport(const ReportParams& params) |
| 70 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 71 | return addReport(params.reportId(), params.reportName(), |
| 72 | utils::enumToString(params.reportingType()), |
| 73 | utils::enumToString(params.reportUpdates()), |
| 74 | params.appendLimit(), |
| 75 | utils::transform(params.reportActions(), |
| 76 | [](const auto v) { |
| 77 | return utils::enumToString(v); |
| 78 | }), |
| 79 | params.interval().count(), |
| 80 | toReadingParameters(params.metricParameters())); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 83 | template <class T> |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 84 | static T getProperty(const std::string& property) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 85 | { |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 86 | return DbusEnvironment::getProperty<T>( |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 87 | ReportManager::reportManagerPath, |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 88 | ReportManager::reportManagerIfaceName, property); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 89 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | TEST_F(TestReportManager, minInterval) |
| 93 | { |
| 94 | EXPECT_THAT(getProperty<uint64_t>("MinInterval"), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 95 | Eq(ReportManager::minInterval.count())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST_F(TestReportManager, maxReports) |
| 99 | { |
Wludzik, Jozef | 503c158 | 2020-12-11 14:48:01 +0100 | [diff] [blame] | 100 | EXPECT_THAT(getProperty<size_t>("MaxReports"), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 101 | Eq(ReportManager::maxReports)); |
| 102 | } |
| 103 | |
| 104 | TEST_F(TestReportManager, addReport) |
| 105 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 106 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 107 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 108 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 109 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 110 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 111 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 112 | EXPECT_THAT(path, Eq(reportMock.getPath())); |
| 113 | } |
| 114 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 115 | TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsEmptyInAddReport) |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 116 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 117 | reportParams.reportId("ReportName"); |
| 118 | reportParams.reportName("ReportName"); |
| 119 | |
| 120 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)); |
| 121 | |
| 122 | auto [ec, path] = addReport(reportParams.reportId("")); |
| 123 | |
| 124 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 125 | EXPECT_THAT(path, Eq("/ReportName")); |
| 126 | } |
| 127 | |
| 128 | TEST_F(TestReportManager, nameIsUsedToGenerateIdWhenIdIsNamespace) |
| 129 | { |
| 130 | reportParams.reportId("Prefix/ReportName"); |
| 131 | reportParams.reportName("ReportName"); |
| 132 | |
| 133 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)); |
| 134 | |
| 135 | auto [ec, path] = addReport(reportParams.reportId("Prefix/")); |
| 136 | |
| 137 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 138 | EXPECT_THAT(path, Eq("/Prefix/ReportName")); |
| 139 | } |
| 140 | |
| 141 | TEST_F(TestReportManager, addReportWithMaxLengthId) |
| 142 | { |
| 143 | std::string reportId(ReportManager::maxReportIdLength, 'z'); |
| 144 | reportParams.reportId(reportId); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 145 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 146 | |
| 147 | auto [ec, path] = addReport(reportParams); |
| 148 | |
| 149 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 150 | EXPECT_THAT(path, Eq("/"s + reportId)); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(TestReportManager, DISABLED_failToAddReportWithTooLongName) |
| 154 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 155 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 156 | .Times(0); |
| 157 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 158 | reportParams.reportId( |
| 159 | std::string(ReportManager::maxReportIdLength + 1, 'z')); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 160 | |
| 161 | auto [ec, path] = addReport(reportParams); |
| 162 | |
| 163 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 164 | EXPECT_THAT(path, Eq(std::string())); |
| 165 | } |
| 166 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 167 | TEST_F(TestReportManager, DISABLED_failToAddReportTwice) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 168 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 169 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 170 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 171 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 172 | addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 173 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 174 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 175 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 176 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::file_exists)); |
| 177 | EXPECT_THAT(path, Eq(std::string())); |
| 178 | } |
| 179 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 180 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidInterval) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 181 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 182 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 183 | .Times(0); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 184 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 185 | reportParams.reportingType(ReportingType::periodic); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 186 | reportParams.interval(reportParams.interval() - 1ms); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 187 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 188 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 189 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 190 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 191 | EXPECT_THAT(path, Eq(std::string())); |
| 192 | } |
| 193 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 194 | TEST_F(TestReportManager, DISABLED_failToAddReportWithInvalidReportingType) |
| 195 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 196 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 197 | .Times(0); |
| 198 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 199 | auto [ec, path] = addReport( |
| 200 | reportParams.reportName(), "InvalidReportingType", |
| 201 | utils::transform(reportParams.reportActions(), |
| 202 | [](const auto v) { return utils::enumToString(v); }), |
| 203 | reportParams.interval().count(), |
| 204 | toReadingParameters(reportParams.metricParameters())); |
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::invalid_argument)); |
| 207 | EXPECT_THAT(path, Eq(std::string())); |
| 208 | } |
| 209 | |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame^] | 210 | TEST_F(TestReportManager, |
| 211 | DISABLED_failToAddReportWithMoreMetricPropertiesThanExpected) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 212 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 213 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 214 | .Times(0); |
| 215 | |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame^] | 216 | reportParams.metricParameters( |
| 217 | std::vector<LabeledMetricParameters>{{LabeledMetricParameters{ |
| 218 | {LabeledSensorParameters{"Service", |
| 219 | "/xyz/openbmc_project/sensors/power/p1", |
| 220 | "Metadata1"}}, |
| 221 | OperationType::single, |
| 222 | "MetricId1", |
| 223 | CollectionTimeScope::point, |
| 224 | CollectionDuration(Milliseconds(0u))}}}); |
| 225 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 226 | auto metricParams = reportParams.metricParameters(); |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame^] | 227 | auto& metricParamsVec = |
| 228 | metricParams[0].at_label<utils::tstring::SensorPath>(); |
| 229 | |
| 230 | for (size_t i = 0; i < ReportManager::maxNumberMetrics; i++) |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 231 | { |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame^] | 232 | metricParamsVec.emplace_back(LabeledSensorParameters{ |
| 233 | "Service", "/xyz/openbmc_project/sensors/power/p1", "Metadata1"}); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 234 | } |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame^] | 235 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 236 | reportParams.metricParameters(std::move(metricParams)); |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 237 | |
| 238 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 239 | |
Wludzik, Jozef | bc766b4 | 2020-12-08 16:06:22 +0100 | [diff] [blame] | 240 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long)); |
| 241 | EXPECT_THAT(path, Eq(std::string())); |
| 242 | } |
| 243 | |
Ankita Vilas Gawade | cd5b0b7 | 2022-01-20 20:55:04 +0000 | [diff] [blame^] | 244 | TEST_F(TestReportManager, DISABLED_failToAddReportWithMoreMetricsThanExpected) |
| 245 | { |
| 246 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 247 | .Times(0); |
| 248 | |
| 249 | auto metricParams = std::vector<LabeledMetricParameters>{}; |
| 250 | |
| 251 | for (size_t i = 0; i < ReportManager::maxNumberMetrics + 1; i++) |
| 252 | { |
| 253 | metricParams.emplace_back( |
| 254 | LabeledMetricParameters{{}, |
| 255 | OperationType::single, |
| 256 | "MetricId1", |
| 257 | CollectionTimeScope::point, |
| 258 | CollectionDuration(Milliseconds(0u))}); |
| 259 | } |
| 260 | |
| 261 | reportParams.metricParameters(std::move(metricParams)); |
| 262 | |
| 263 | auto [ec, path] = addReport(reportParams); |
| 264 | |
| 265 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::argument_list_too_long)); |
| 266 | EXPECT_THAT(path, Eq(std::string())); |
| 267 | } |
| 268 | |
| 269 | TEST_F(TestReportManager, DISABLED_failToAddReportWithAppendLimitGreaterThanMax) |
| 270 | { |
| 271 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
| 272 | .Times(0); |
| 273 | |
| 274 | reportParams.appendLimit(ReportManager::maxAppendLimit + 1); |
| 275 | |
| 276 | auto [ec, path] = addReport(reportParams); |
| 277 | |
| 278 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::invalid_argument)); |
| 279 | EXPECT_THAT(path, Eq(std::string())); |
| 280 | } |
| 281 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 282 | TEST_F(TestReportManager, DISABLED_failToAddReportWhenMaxReportIsReached) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 283 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 284 | reportFactoryMock.expectMake(std::nullopt, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 285 | .Times(ReportManager::maxReports); |
| 286 | |
| 287 | for (size_t i = 0; i < ReportManager::maxReports; i++) |
| 288 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 289 | reportParams.reportId(reportParams.reportName() + std::to_string(i)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 290 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 291 | auto [ec, path] = addReport(reportParams); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 292 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
| 293 | } |
| 294 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 295 | reportParams.reportId(reportParams.reportName() + |
| 296 | std::to_string(ReportManager::maxReports)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 297 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 298 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 299 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::too_many_files_open)); |
| 300 | EXPECT_THAT(path, Eq(std::string())); |
| 301 | } |
| 302 | |
| 303 | TEST_F(TestReportManager, removeReport) |
| 304 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 305 | { |
| 306 | InSequence seq; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 307 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 308 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 309 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 310 | EXPECT_CALL(reportMock, Die()); |
| 311 | EXPECT_CALL(checkPoint, Call("end")); |
| 312 | } |
| 313 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 314 | addReport(reportParams); |
| 315 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 316 | checkPoint.Call("end"); |
| 317 | } |
| 318 | |
| 319 | TEST_F(TestReportManager, removingReportThatIsNotInContainerHasNoEffect) |
| 320 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 321 | { |
| 322 | InSequence seq; |
| 323 | EXPECT_CALL(checkPoint, Call("end")); |
| 324 | EXPECT_CALL(reportMock, Die()); |
| 325 | } |
| 326 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 327 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 328 | checkPoint.Call("end"); |
| 329 | } |
| 330 | |
| 331 | TEST_F(TestReportManager, removingSameReportTwiceHasNoSideEffect) |
| 332 | { |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 333 | { |
| 334 | InSequence seq; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 335 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)); |
| 336 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 337 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 338 | EXPECT_CALL(reportMock, Die()); |
| 339 | EXPECT_CALL(checkPoint, Call("end")); |
| 340 | } |
| 341 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 342 | addReport(reportParams); |
| 343 | sut->removeReport(&reportMock); |
| 344 | sut->removeReport(&reportMock); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 345 | checkPoint.Call("end"); |
| 346 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 347 | |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 348 | TEST_F(TestReportManager, updateReportCallsUpdateReadingsForExistReport) |
| 349 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 350 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 351 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 352 | EXPECT_CALL(reportMock, updateReadings()); |
| 353 | |
| 354 | addReport(reportParams); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 355 | sut->updateReport(reportParams.reportId()); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | TEST_F(TestReportManager, updateReportDoNothingIfReportDoesNotExist) |
| 359 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 360 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 361 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 362 | EXPECT_CALL(reportMock, updateReadings()).Times(0); |
| 363 | |
| 364 | addReport(reportParams); |
| 365 | sut->updateReport("NotAReport"); |
| 366 | } |
| 367 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 368 | class TestReportManagerWithAggregationOperationType : |
| 369 | public TestReportManager, |
| 370 | public WithParamInterface<OperationType> |
| 371 | { |
| 372 | public: |
| 373 | OperationType operationType = GetParam(); |
| 374 | }; |
| 375 | |
| 376 | INSTANTIATE_TEST_SUITE_P(_, TestReportManagerWithAggregationOperationType, |
| 377 | Values(OperationType::single, OperationType::max, |
| 378 | OperationType::min, OperationType::avg, |
| 379 | OperationType::sum)); |
| 380 | |
| 381 | TEST_P(TestReportManagerWithAggregationOperationType, |
| 382 | addReportWithDifferentOperationTypes) |
| 383 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 384 | reportParams.metricParameters( |
| 385 | std::vector<LabeledMetricParameters>{{LabeledMetricParameters{ |
| 386 | {LabeledSensorParameters{"Service", |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 387 | "/xyz/openbmc_project/sensors/power/p1", |
| 388 | "Metadata1"}}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 389 | operationType, |
| 390 | "MetricId1", |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 391 | CollectionTimeScope::point, |
| 392 | CollectionDuration(Milliseconds(0u))}}}); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 393 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 394 | reportFactoryMock.expectMake(reportParams, Ref(*sut), Ref(storageMock)) |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 395 | .WillOnce(Return(ByMove(std::move(reportMockPtr)))); |
| 396 | |
| 397 | auto [ec, path] = addReport(reportParams); |
Karol Niczyj | 32859b6 | 2021-05-19 10:20:46 +0200 | [diff] [blame] | 398 | |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 399 | EXPECT_THAT(ec.value(), Eq(boost::system::errc::success)); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 400 | EXPECT_THAT(path, Eq("/"s + reportParams.reportId())); |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 403 | class TestReportManagerStorage : public TestReportManager |
| 404 | { |
| 405 | public: |
| 406 | using FilePath = interfaces::JsonStorage::FilePath; |
| 407 | using DirectoryPath = interfaces::JsonStorage::DirectoryPath; |
| 408 | |
| 409 | void SetUp() override |
| 410 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 411 | EXPECT_CALL(reportFactoryMock, convertMetricParams(_, _)).Times(0); |
| 412 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 413 | ON_CALL(storageMock, list()) |
| 414 | .WillByDefault(Return(std::vector<FilePath>{FilePath("report1")})); |
| 415 | ON_CALL(storageMock, load(FilePath("report1"))) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 416 | .WillByDefault(InvokeWithoutArgs([this] { return data; })); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void makeReportManager() |
| 420 | { |
| 421 | sut = std::make_unique<ReportManager>(std::move(reportFactoryMockPtr), |
| 422 | std::move(storageMockPtr), |
| 423 | DbusEnvironment::getObjServer()); |
| 424 | } |
| 425 | |
| 426 | nlohmann::json data = nlohmann::json{ |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 427 | {"Enabled", reportParams.enabled()}, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 428 | {"Version", Report::reportVersion}, |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 429 | {"Id", reportParams.reportId()}, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 430 | {"Name", reportParams.reportName()}, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 431 | {"ReportingType", utils::toUnderlying(reportParams.reportingType())}, |
| 432 | {"ReportActions", reportParams.reportActions()}, |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 433 | {"Interval", reportParams.interval().count()}, |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 434 | {"ReportUpdates", utils::toUnderlying(reportParams.reportUpdates())}, |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 435 | {"AppendLimit", reportParams.appendLimit()}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 436 | {"ReadingParameters", reportParams.metricParameters()}}; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | TEST_F(TestReportManagerStorage, reportManagerCtorAddReportFromStorage) |
| 440 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 441 | reportFactoryMock.expectMake(reportParams, _, Ref(storageMock)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 442 | |
| 443 | makeReportManager(); |
| 444 | } |
| 445 | |
| 446 | TEST_F(TestReportManagerStorage, |
| 447 | reportManagerCtorRemoveFileIfVersionDoesNotMatch) |
| 448 | { |
| 449 | data["Version"] = Report::reportVersion - 1; |
| 450 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 451 | EXPECT_CALL(storageMock, remove(FilePath("report1"))); |
| 452 | |
| 453 | makeReportManager(); |
| 454 | } |
| 455 | |
| 456 | TEST_F(TestReportManagerStorage, |
| 457 | reportManagerCtorRemoveFileIfIntervalHasWrongType) |
| 458 | { |
| 459 | data["Interval"] = "1000"; |
| 460 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 461 | EXPECT_CALL(storageMock, remove(FilePath("report1"))); |
| 462 | |
| 463 | makeReportManager(); |
| 464 | } |