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