Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 2 | #include "fakes/clock_fake.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 3 | #include "helpers.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 4 | #include "mocks/json_storage_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 5 | #include "mocks/metric_mock.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 6 | #include "mocks/report_manager_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 7 | #include "params/report_params.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 8 | #include "report.hpp" |
| 9 | #include "report_manager.hpp" |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 10 | #include "utils/clock.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 11 | #include "utils/contains.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 12 | #include "utils/conv_container.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 13 | #include "utils/transform.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 14 | #include "utils/tstring.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 15 | |
| 16 | #include <sdbusplus/exception.hpp> |
| 17 | |
| 18 | using namespace testing; |
| 19 | using namespace std::literals::string_literals; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 20 | using namespace std::chrono_literals; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 21 | namespace tstring = utils::tstring; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 22 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 23 | constexpr Milliseconds systemTimestamp = 55ms; |
| 24 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 25 | class TestReport : public Test |
| 26 | { |
| 27 | public: |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 28 | ReportParams defaultParams; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 29 | |
| 30 | std::unique_ptr<ReportManagerMock> reportManagerMock = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 31 | std::make_unique<NiceMock<ReportManagerMock>>(); |
| 32 | testing::NiceMock<StorageMock> storageMock; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 33 | std::vector<std::shared_ptr<MetricMock>> metricMocks; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 34 | std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>(); |
| 35 | ClockFake& clockFake = *clockFakePtr; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 36 | std::unique_ptr<Report> sut; |
| 37 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 38 | MockFunction<void()> checkPoint; |
| 39 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 40 | TestReport() |
| 41 | { |
| 42 | clockFake.system.set(systemTimestamp); |
| 43 | } |
| 44 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 45 | void initMetricMocks( |
| 46 | const std::vector<LabeledMetricParameters>& metricParameters) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 47 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 48 | for (auto i = metricMocks.size(); i < metricParameters.size(); ++i) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 49 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 50 | metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>()); |
| 51 | } |
| 52 | metricMocks.resize(metricParameters.size()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 53 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 54 | std::vector<MetricValue> readings{{MetricValue{"a", "b", 17.1, 114}, |
| 55 | MetricValue{"aa", "bb", 42.0, 74}}}; |
| 56 | readings.resize(metricParameters.size()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 57 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 58 | for (size_t i = 0; i < metricParameters.size(); ++i) |
| 59 | { |
| 60 | ON_CALL(*metricMocks[i], getReadings()) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 61 | .WillByDefault(Return(std::vector({readings[i]}))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 62 | ON_CALL(*metricMocks[i], dumpConfiguration()) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 63 | .WillByDefault(Return(metricParameters[i])); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 67 | void SetUp() override |
| 68 | { |
| 69 | sut = makeReport(ReportParams()); |
| 70 | } |
| 71 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 72 | static interfaces::JsonStorage::FilePath to_file_path(std::string id) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 73 | { |
| 74 | return interfaces::JsonStorage::FilePath( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 75 | std::to_string(std::hash<std::string>{}(id))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 78 | std::unique_ptr<Report> makeReport(const ReportParams& params) |
| 79 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 80 | initMetricMocks(params.metricParameters()); |
| 81 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 82 | return std::make_unique<Report>( |
| 83 | DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 84 | params.reportId(), params.reportName(), params.reportingType(), |
| 85 | params.reportActions(), params.interval(), params.appendLimit(), |
| 86 | params.reportUpdates(), *reportManagerMock, storageMock, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 87 | utils::convContainer<std::shared_ptr<interfaces::Metric>>( |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 88 | metricMocks), |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 89 | params.enabled(), std::move(clockFakePtr)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 90 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 91 | |
| 92 | template <class T> |
| 93 | static T getProperty(const std::string& path, const std::string& property) |
| 94 | { |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 95 | return DbusEnvironment::getProperty<T>(path, Report::reportIfaceName, |
| 96 | property); |
| 97 | } |
| 98 | |
| 99 | template <class T> |
| 100 | static boost::system::error_code setProperty(const std::string& path, |
| 101 | const std::string& property, |
| 102 | const T& newValue) |
| 103 | { |
| 104 | return DbusEnvironment::setProperty<T>(path, Report::reportIfaceName, |
| 105 | property, newValue); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | boost::system::error_code call(const std::string& path, |
| 109 | const std::string& interface, |
| 110 | const std::string& method) |
| 111 | { |
| 112 | std::promise<boost::system::error_code> methodPromise; |
| 113 | DbusEnvironment::getBus()->async_method_call( |
| 114 | [&methodPromise](boost::system::error_code ec) { |
| 115 | methodPromise.set_value(ec); |
| 116 | }, |
| 117 | DbusEnvironment::serviceName(), path, interface, method); |
| 118 | return DbusEnvironment::waitForFuture(methodPromise.get_future()); |
| 119 | } |
| 120 | |
| 121 | boost::system::error_code update(const std::string& path) |
| 122 | { |
| 123 | return call(path, Report::reportIfaceName, "Update"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 126 | boost::system::error_code deleteReport(const std::string& path) |
| 127 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 128 | return call(path, Report::deleteIfaceName, "Delete"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 129 | } |
| 130 | }; |
| 131 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 132 | TEST_F(TestReport, returnsId) |
| 133 | { |
| 134 | EXPECT_THAT(sut->getId(), Eq(defaultParams.reportId())); |
| 135 | } |
| 136 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 137 | TEST_F(TestReport, verifyIfPropertiesHaveValidValue) |
| 138 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 139 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), |
| 140 | Eq(defaultParams.enabled())); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 141 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 142 | Eq(defaultParams.interval().count())); |
| 143 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true)); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 144 | EXPECT_THAT( |
| 145 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 146 | Eq(utils::transform(defaultParams.reportActions(), [](const auto v) { |
| 147 | return utils::enumToString(v); |
| 148 | }))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 149 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 150 | Eq(utils::contains(defaultParams.reportActions(), |
| 151 | ReportAction::emitsReadingsUpdate))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 152 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "AppendLimit"), |
| 153 | Eq(defaultParams.appendLimit())); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 154 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"), |
| 155 | Eq(utils::enumToString(defaultParams.reportingType()))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 156 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportUpdates"), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 157 | Eq(utils::enumToString(defaultParams.reportUpdates()))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 158 | EXPECT_THAT( |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 159 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 160 | Eq(utils::contains(defaultParams.reportActions(), |
| 161 | ReportAction::logToMetricReportsCollection))); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 162 | EXPECT_THAT(getProperty<ReadingParameters>( |
| 163 | sut->getPath(), "ReadingParametersFutureVersion"), |
| 164 | Eq(toReadingParameters(defaultParams.metricParameters()))); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 165 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), |
| 166 | Eq(defaultParams.reportName())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 169 | TEST_F(TestReport, readingsAreInitialyEmpty) |
| 170 | { |
| 171 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 172 | Eq(Readings{})); |
| 173 | } |
| 174 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 175 | TEST_F(TestReport, setEnabledWithNewValue) |
| 176 | { |
| 177 | bool newValue = !defaultParams.enabled(); |
| 178 | EXPECT_THAT(setProperty(sut->getPath(), "Enabled", newValue).value(), |
| 179 | Eq(boost::system::errc::success)); |
| 180 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(newValue)); |
| 181 | } |
| 182 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 183 | TEST_F(TestReport, setIntervalWithValidValue) |
| 184 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 185 | uint64_t newValue = defaultParams.interval().count() + 1; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 186 | EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 187 | Eq(boost::system::errc::success)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 188 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
| 189 | Eq(newValue)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 192 | TEST_F( |
| 193 | TestReport, |
| 194 | settingIntervalWithInvalidValueDoesNotChangePropertyAndReturnsInvalidArgument) |
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 | uint64_t newValue = defaultParams.interval().count() - 1; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 197 | EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(), |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 198 | Eq(boost::system::errc::invalid_argument)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 199 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 200 | Eq(defaultParams.interval().count())); |
| 201 | } |
| 202 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 203 | TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect) |
| 204 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 205 | EXPECT_THAT( |
| 206 | setProperty(sut->getPath(), "EmitsReadingsUpdate", true).value(), |
| 207 | Eq(boost::system::errc::read_only_file_system)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 208 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 209 | Eq(utils::contains(defaultParams.reportActions(), |
| 210 | ReportAction::emitsReadingsUpdate))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect) |
| 214 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 215 | EXPECT_THAT( |
| 216 | setProperty(sut->getPath(), "LogToMetricReportsCollection", true) |
| 217 | .value(), |
| 218 | Eq(boost::system::errc::read_only_file_system)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 219 | EXPECT_THAT( |
| 220 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 221 | Eq(utils::contains(defaultParams.reportActions(), |
| 222 | ReportAction::logToMetricReportsCollection))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 225 | TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage) |
| 226 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 227 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 228 | |
| 229 | bool persistency = false; |
| 230 | EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(), |
| 231 | Eq(boost::system::errc::success)); |
| 232 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), |
| 233 | Eq(persistency)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | TEST_F(TestReport, deleteReport) |
| 237 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 238 | EXPECT_CALL(*reportManagerMock, removeReport(sut.get())); |
| 239 | auto ec = deleteReport(sut->getPath()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 240 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 241 | } |
| 242 | |
| 243 | TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor) |
| 244 | { |
| 245 | auto ec = deleteReport(Report::reportDir + "NonExisting"s); |
| 246 | EXPECT_THAT(ec.value(), Eq(EBADR)); |
| 247 | } |
| 248 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 249 | TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage) |
| 250 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 251 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 252 | auto ec = deleteReport(sut->getPath()); |
| 253 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 254 | } |
| 255 | |
| 256 | class TestReportStore : |
| 257 | public TestReport, |
| 258 | public WithParamInterface<std::pair<std::string, nlohmann::json>> |
| 259 | { |
| 260 | public: |
| 261 | void SetUp() override |
| 262 | {} |
| 263 | |
| 264 | nlohmann::json storedConfiguration; |
| 265 | }; |
| 266 | |
| 267 | INSTANTIATE_TEST_SUITE_P( |
| 268 | _, TestReportStore, |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 269 | Values(std::make_pair("Enabled"s, nlohmann::json(ReportParams().enabled())), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 270 | std::make_pair("Version"s, nlohmann::json(6)), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 271 | std::make_pair("Id"s, nlohmann::json(ReportParams().reportId())), |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 272 | std::make_pair("Name"s, nlohmann::json(ReportParams().reportName())), |
| 273 | std::make_pair("ReportingType", |
| 274 | nlohmann::json(ReportParams().reportingType())), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 275 | std::make_pair("ReportActions", nlohmann::json(utils::transform( |
| 276 | ReportParams().reportActions(), |
| 277 | [](const auto v) { |
| 278 | return utils::toUnderlying( |
| 279 | v); |
| 280 | }))), |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 281 | std::make_pair("Interval", |
| 282 | nlohmann::json(ReportParams().interval().count())), |
| 283 | std::make_pair( |
| 284 | "ReadingParameters", |
| 285 | nlohmann::json( |
| 286 | {{{tstring::SensorPath::str(), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 287 | {{{tstring::Service::str(), "Service"}, |
| 288 | {tstring::Path::str(), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 289 | "/xyz/openbmc_project/sensors/power/p1"}, |
| 290 | {tstring::Metadata::str(), "metadata1"}}}}, |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 291 | {tstring::OperationType::str(), OperationType::single}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 292 | {tstring::Id::str(), "MetricId1"}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 293 | {tstring::CollectionTimeScope::str(), |
| 294 | CollectionTimeScope::point}, |
| 295 | {tstring::CollectionDuration::str(), 0}}, |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 296 | {{tstring::SensorPath::str(), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 297 | {{{tstring::Service::str(), "Service"}, |
| 298 | {tstring::Path::str(), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 299 | "/xyz/openbmc_project/sensors/power/p2"}, |
| 300 | {tstring::Metadata::str(), "metadata2"}}}}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 301 | {tstring::OperationType::str(), OperationType::single}, |
| 302 | {tstring::Id::str(), "MetricId2"}, |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 303 | {tstring::CollectionTimeScope::str(), |
| 304 | CollectionTimeScope::point}, |
| 305 | {tstring::CollectionDuration::str(), 0}}})))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 306 | |
| 307 | TEST_P(TestReportStore, settingPersistencyToTrueStoresReport) |
| 308 | { |
| 309 | sut = makeReport(ReportParams()); |
| 310 | |
| 311 | { |
| 312 | InSequence seq; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 313 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 314 | EXPECT_CALL(checkPoint, Call()); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 315 | EXPECT_CALL(storageMock, store(to_file_path(sut->getId()), _)) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 316 | .WillOnce(SaveArg<1>(&storedConfiguration)); |
| 317 | } |
| 318 | |
| 319 | setProperty(sut->getPath(), "Persistency", false); |
| 320 | checkPoint.Call(); |
| 321 | setProperty(sut->getPath(), "Persistency", true); |
| 322 | |
| 323 | const auto& [key, value] = GetParam(); |
| 324 | |
| 325 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 326 | } |
| 327 | |
| 328 | TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated) |
| 329 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 330 | EXPECT_CALL(storageMock, store(to_file_path(ReportParams().reportId()), _)) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 331 | .WillOnce(SaveArg<1>(&storedConfiguration)); |
| 332 | |
| 333 | sut = makeReport(ReportParams()); |
| 334 | |
| 335 | const auto& [key, value] = GetParam(); |
| 336 | |
| 337 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 338 | } |
| 339 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 340 | class TestReportValidNames : |
| 341 | public TestReport, |
| 342 | public WithParamInterface<ReportParams> |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 343 | { |
| 344 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 345 | void SetUp() override |
| 346 | {} |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 347 | }; |
| 348 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 349 | INSTANTIATE_TEST_SUITE_P( |
| 350 | ValidNames, TestReportValidNames, |
| 351 | Values(ReportParams().reportName("Valid_1"), |
| 352 | ReportParams().reportName("Valid_1/Valid_2"), |
| 353 | ReportParams().reportName("Valid_1/Valid_2/Valid_3"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 354 | |
| 355 | TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName) |
| 356 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 357 | EXPECT_NO_THROW(makeReport(GetParam())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 358 | } |
| 359 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 360 | class TestReportInvalidIds : |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 361 | public TestReport, |
| 362 | public WithParamInterface<ReportParams> |
| 363 | { |
| 364 | public: |
| 365 | void SetUp() override |
| 366 | {} |
| 367 | }; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 368 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 369 | INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidIds, |
| 370 | Values(ReportParams().reportId("/"), |
| 371 | ReportParams().reportId("/Invalid"), |
| 372 | ReportParams().reportId("Invalid/"), |
| 373 | ReportParams().reportId("Invalid/Invalid/"), |
| 374 | ReportParams().reportId("Invalid?"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 375 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 376 | TEST_P(TestReportInvalidIds, failsToCreateReportWithInvalidName) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 377 | { |
| 378 | EXPECT_CALL(storageMock, store).Times(0); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 379 | |
| 380 | EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 383 | class TestReportAllReportTypes : |
| 384 | public TestReport, |
| 385 | public WithParamInterface<ReportParams> |
| 386 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 387 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 388 | void SetUp() override |
| 389 | { |
| 390 | sut = makeReport(GetParam()); |
| 391 | } |
| 392 | }; |
| 393 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 394 | INSTANTIATE_TEST_SUITE_P( |
| 395 | _, TestReportAllReportTypes, |
| 396 | Values(ReportParams().reportingType(ReportingType::onRequest), |
| 397 | ReportParams().reportingType(ReportingType::onChange), |
| 398 | ReportParams().reportingType(ReportingType::periodic))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 399 | |
| 400 | TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType) |
| 401 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 402 | EXPECT_THAT(utils::toReportingType( |
| 403 | getProperty<std::string>(sut->getPath(), "ReportingType")), |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 404 | Eq(GetParam().reportingType())); |
| 405 | } |
| 406 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 407 | TEST_P(TestReportAllReportTypes, updateReadingsCallEnabledPropertyOff) |
| 408 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 409 | clockFake.system.advance(10ms); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 410 | |
| 411 | setProperty(sut->getPath(), "Enabled", false); |
| 412 | sut->updateReadings(); |
| 413 | const auto [timestamp, readings] = |
| 414 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 415 | |
| 416 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(false)); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 417 | EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms)); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | TEST_P(TestReportAllReportTypes, updateReadingsCallEnabledPropertyOn) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 421 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 422 | clockFake.system.advance(10ms); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 423 | |
| 424 | sut->updateReadings(); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 425 | const auto [timestamp, readings] = |
| 426 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 427 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 428 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(true)); |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 429 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 432 | class TestReportOnRequestType : public TestReport |
| 433 | { |
| 434 | void SetUp() override |
| 435 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 436 | sut = |
| 437 | makeReport(ReportParams().reportingType(ReportingType::onRequest)); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 438 | } |
| 439 | }; |
| 440 | |
| 441 | TEST_F(TestReportOnRequestType, updatesReadingTimestamp) |
| 442 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 443 | clockFake.system.advance(10ms); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 444 | |
| 445 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 446 | |
| 447 | const auto [timestamp, readings] = |
| 448 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 449 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 450 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled) |
| 454 | { |
| 455 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 456 | |
| 457 | const auto [timestamp, readings] = |
| 458 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 459 | |
| 460 | EXPECT_THAT(readings, |
| 461 | ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 462 | std::make_tuple("aa"s, "bb"s, 42.0, 74u))); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | class TestReportNonOnRequestType : |
| 466 | public TestReport, |
| 467 | public WithParamInterface<ReportParams> |
| 468 | { |
| 469 | void SetUp() override |
| 470 | { |
| 471 | sut = makeReport(GetParam()); |
| 472 | } |
| 473 | }; |
| 474 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 475 | INSTANTIATE_TEST_SUITE_P( |
| 476 | _, TestReportNonOnRequestType, |
| 477 | Values(ReportParams().reportingType(ReportingType::periodic), |
| 478 | ReportParams().reportingType(ReportingType::onChange))); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 479 | |
| 480 | TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall) |
| 481 | { |
| 482 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 483 | |
| 484 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 485 | Eq(Readings{})); |
| 486 | } |
| 487 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 488 | class TestReportNonPeriodicReport : |
| 489 | public TestReport, |
| 490 | public WithParamInterface<ReportParams> |
| 491 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 492 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 493 | void SetUp() override |
| 494 | { |
| 495 | sut = makeReport(GetParam()); |
| 496 | } |
| 497 | }; |
| 498 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 499 | INSTANTIATE_TEST_SUITE_P( |
| 500 | _, TestReportNonPeriodicReport, |
| 501 | Values(ReportParams().reportingType(ReportingType::onRequest), |
| 502 | ReportParams().reportingType(ReportingType::onChange))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 503 | |
| 504 | TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires) |
| 505 | { |
| 506 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 507 | |
| 508 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 509 | Eq(Readings{})); |
| 510 | } |
| 511 | |
| 512 | class TestReportPeriodicReport : public TestReport |
| 513 | { |
| 514 | void SetUp() override |
| 515 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 516 | sut = makeReport(ReportParams().reportingType(ReportingType::periodic)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 517 | } |
| 518 | }; |
| 519 | |
| 520 | TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires) |
| 521 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 522 | clockFake.system.advance(10ms); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 523 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 524 | |
| 525 | const auto [timestamp, readings] = |
| 526 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 527 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 528 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires) |
| 532 | { |
| 533 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 534 | |
| 535 | const auto [timestamp, readings] = |
| 536 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 537 | |
| 538 | EXPECT_THAT(readings, |
| 539 | ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 540 | std::make_tuple("aa"s, "bb"s, 42.0, 74u))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 541 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 542 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 543 | struct ReportUpdatesReportParams |
| 544 | { |
| 545 | ReportParams reportParams; |
| 546 | std::vector<ReadingData> expectedReadings; |
| 547 | bool expectedEnabled; |
| 548 | }; |
| 549 | |
| 550 | class TestReportWithReportUpdatesAndLimit : |
| 551 | public TestReport, |
| 552 | public WithParamInterface<ReportUpdatesReportParams> |
| 553 | { |
| 554 | void SetUp() override |
| 555 | { |
| 556 | sut = makeReport(ReportParams(GetParam().reportParams) |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 557 | .reportingType(ReportingType::periodic) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 558 | .interval(std::chrono::hours(1000))); |
| 559 | } |
| 560 | }; |
| 561 | |
| 562 | INSTANTIATE_TEST_SUITE_P( |
| 563 | _, TestReportWithReportUpdatesAndLimit, |
| 564 | Values( |
| 565 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 566 | ReportParams() |
| 567 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 568 | .appendLimit(5), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 569 | std::vector<ReadingData>{{std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 570 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 571 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 572 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 573 | std::make_tuple("a"s, "b"s, 17.1, 114u)}}, |
| 574 | true}, |
| 575 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 576 | ReportParams() |
| 577 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 578 | .appendLimit(4), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 579 | std::vector<ReadingData>{ |
| 580 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 581 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 582 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 583 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 584 | true}, |
| 585 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 586 | ReportParams() |
| 587 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 588 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 589 | std::vector<ReadingData>{}, true}, |
| 590 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 591 | ReportParams() |
| 592 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 593 | .appendLimit(10), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 594 | std::vector<ReadingData>{ |
| 595 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 596 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 597 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 598 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 599 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 600 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 601 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 602 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 603 | true}, |
| 604 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 605 | ReportParams() |
| 606 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 607 | .appendLimit(5), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 608 | std::vector<ReadingData>{{std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 609 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 610 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 611 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 612 | std::make_tuple("a"s, "b"s, 17.1, 114u)}}, |
| 613 | false}, |
| 614 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 615 | ReportParams() |
| 616 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 617 | .appendLimit(4), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 618 | std::vector<ReadingData>{ |
| 619 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 620 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 621 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 622 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 623 | false}, |
| 624 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 625 | ReportParams() |
| 626 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 627 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 628 | std::vector<ReadingData>{}, false}, |
| 629 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 630 | ReportParams() |
| 631 | .reportUpdates(ReportUpdates::overwrite) |
| 632 | .appendLimit(500), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 633 | std::vector<ReadingData>{ |
| 634 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 635 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 636 | true}, |
| 637 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 638 | ReportParams() |
| 639 | .reportUpdates(ReportUpdates::overwrite) |
| 640 | .appendLimit(1), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 641 | std::vector<ReadingData>{ |
| 642 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 643 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 644 | true}, |
| 645 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 646 | ReportParams() |
| 647 | .reportUpdates(ReportUpdates::overwrite) |
| 648 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 649 | std::vector<ReadingData>{ |
| 650 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 651 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 652 | true})); |
| 653 | |
| 654 | TEST_P(TestReportWithReportUpdatesAndLimit, |
| 655 | readingsAreUpdatedAfterIntervalExpires) |
| 656 | { |
| 657 | for (int i = 0; i < 4; i++) |
| 658 | { |
| 659 | sut->updateReadings(); |
| 660 | } |
| 661 | |
| 662 | const auto [timestamp, readings] = |
| 663 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 664 | const auto enabled = getProperty<bool>(sut->getPath(), "Enabled"); |
| 665 | |
| 666 | EXPECT_THAT(readings, ElementsAreArray(GetParam().expectedReadings)); |
| 667 | EXPECT_EQ(enabled, GetParam().expectedEnabled); |
| 668 | } |
| 669 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 670 | class TestReportInitialization : public TestReport |
| 671 | { |
| 672 | public: |
| 673 | void SetUp() override |
| 674 | {} |
| 675 | |
| 676 | void monitorProc(sdbusplus::message::message& msg) |
| 677 | { |
| 678 | std::string iface; |
| 679 | std::vector<std::pair<std::string, std::variant<Readings>>> |
| 680 | changed_properties; |
| 681 | std::vector<std::string> invalidated_properties; |
| 682 | |
| 683 | msg.read(iface, changed_properties, invalidated_properties); |
| 684 | |
| 685 | if (iface == Report::reportIfaceName) |
| 686 | { |
| 687 | for (const auto& [name, value] : changed_properties) |
| 688 | { |
| 689 | if (name == "Readings") |
| 690 | { |
| 691 | readingsUpdated.Call(); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | void makeMonitor() |
| 698 | { |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 699 | monitor = std::make_unique<sdbusplus::bus::match_t>( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 700 | *DbusEnvironment::getBus(), |
| 701 | sdbusplus::bus::match::rules::propertiesChanged( |
| 702 | sut->getPath(), Report::reportIfaceName), |
| 703 | [this](auto& msg) { monitorProc(msg); }); |
| 704 | } |
| 705 | |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 706 | std::unique_ptr<sdbusplus::bus::match_t> monitor; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 707 | MockFunction<void()> readingsUpdated; |
| 708 | }; |
| 709 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 710 | TEST_F(TestReportInitialization, |
| 711 | metricsAreInitializedWhenEnabledReportConstructed) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 712 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 713 | initMetricMocks(defaultParams.metricParameters()); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 714 | for (auto& metric : metricMocks) |
| 715 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 716 | EXPECT_CALL(*metric, initialize()).Times(1); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 717 | } |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 718 | sut = makeReport(defaultParams.enabled(true)); |
| 719 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 720 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 721 | TEST_F(TestReportInitialization, |
| 722 | metricsAreNotInitializedWhenDisabledReportConstructed) |
| 723 | { |
| 724 | initMetricMocks(defaultParams.metricParameters()); |
| 725 | for (auto& metric : metricMocks) |
| 726 | { |
| 727 | EXPECT_CALL(*metric, initialize()).Times(0); |
| 728 | } |
| 729 | sut = makeReport(defaultParams.enabled(false)); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 732 | TEST_F(TestReportInitialization, |
| 733 | emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 734 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 735 | EXPECT_CALL(readingsUpdated, Call()) |
| 736 | .WillOnce( |
| 737 | InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated"))); |
| 738 | |
| 739 | const auto elapsed = DbusEnvironment::measureTime([this] { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 740 | sut = |
| 741 | makeReport(defaultParams.reportingType(ReportingType::periodic) |
| 742 | .reportActions({ReportAction::emitsReadingsUpdate})); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 743 | makeMonitor(); |
| 744 | EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated")); |
| 745 | }); |
| 746 | |
| 747 | EXPECT_THAT(elapsed, AllOf(Ge(defaultParams.interval()), |
| 748 | Lt(defaultParams.interval() * 2))); |
| 749 | } |
| 750 | |
| 751 | TEST_F(TestReportInitialization, |
| 752 | emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits) |
| 753 | { |
| 754 | EXPECT_CALL(readingsUpdated, Call()).Times(0); |
| 755 | |
| 756 | sut = makeReport( |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 757 | defaultParams.reportingType(ReportingType::periodic).reportActions({})); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 758 | makeMonitor(); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 759 | DbusEnvironment::sleepFor(defaultParams.interval() * 2); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 760 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 761 | |
| 762 | TEST_F(TestReportInitialization, appendLimitDeducedProperly) |
| 763 | { |
| 764 | sut = makeReport( |
| 765 | ReportParams().appendLimit(std::numeric_limits<uint64_t>::max())); |
| 766 | auto appendLimit = getProperty<uint64_t>(sut->getPath(), "AppendLimit"); |
| 767 | EXPECT_EQ(appendLimit, 2ull); |
| 768 | } |