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" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 4 | #include "messages/collect_trigger_id.hpp" |
| 5 | #include "messages/trigger_presence_changed_ind.hpp" |
| 6 | #include "messages/update_report_ind.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 7 | #include "mocks/json_storage_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 8 | #include "mocks/metric_mock.hpp" |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 9 | #include "mocks/report_factory_mock.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 10 | #include "mocks/report_manager_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 11 | #include "params/report_params.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 12 | #include "report.hpp" |
| 13 | #include "report_manager.hpp" |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 14 | #include "utils/clock.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 15 | #include "utils/contains.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 16 | #include "utils/conv_container.hpp" |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 17 | #include "utils/messanger.hpp" |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 18 | #include "utils/transform.hpp" |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 19 | #include "utils/tstring.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 20 | |
| 21 | #include <sdbusplus/exception.hpp> |
| 22 | |
| 23 | using namespace testing; |
| 24 | using namespace std::literals::string_literals; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 25 | using namespace std::chrono_literals; |
Krzysztof Grobelny | e8fc575 | 2021-02-05 14:30:45 +0000 | [diff] [blame] | 26 | namespace tstring = utils::tstring; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 27 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 28 | constexpr Milliseconds systemTimestamp = 55ms; |
| 29 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 30 | namespace |
| 31 | { |
| 32 | |
| 33 | ReportParams defaultParams() |
| 34 | { |
| 35 | return ReportParams(); |
| 36 | } |
| 37 | |
| 38 | ReportParams defaultOnChangeParams() |
| 39 | { |
| 40 | return defaultParams().reportingType(ReportingType::onChange); |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 45 | class TestReport : public Test |
| 46 | { |
| 47 | public: |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 48 | std::unique_ptr<ReportManagerMock> reportManagerMock = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 49 | std::make_unique<NiceMock<ReportManagerMock>>(); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 50 | std::unique_ptr<ReportFactoryMock> reportFactoryMock = |
| 51 | std::make_unique<NiceMock<ReportFactoryMock>>(); |
| 52 | NiceMock<StorageMock> storageMock; |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 53 | std::vector<std::shared_ptr<MetricMock>> metricMocks; |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 54 | std::unique_ptr<ClockFake> clockFakePtr = std::make_unique<ClockFake>(); |
| 55 | ClockFake& clockFake = *clockFakePtr; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 56 | std::unique_ptr<Report> sut; |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 57 | utils::Messanger messanger; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 58 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 59 | MockFunction<void()> checkPoint; |
| 60 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 61 | TestReport() : messanger(DbusEnvironment::getIoc()) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 62 | { |
| 63 | clockFake.system.set(systemTimestamp); |
| 64 | } |
| 65 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 66 | void initMetricMocks( |
| 67 | const std::vector<LabeledMetricParameters>& metricParameters) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 68 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 69 | for (auto i = metricMocks.size(); i < metricParameters.size(); ++i) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 70 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 71 | metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>()); |
| 72 | } |
| 73 | metricMocks.resize(metricParameters.size()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 74 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 75 | std::vector<MetricValue> readings{{MetricValue{"a", "b", 17.1, 114}, |
| 76 | MetricValue{"aa", "bb", 42.0, 74}}}; |
| 77 | readings.resize(metricParameters.size()); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 78 | |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 79 | for (size_t i = 0; i < metricParameters.size(); ++i) |
| 80 | { |
| 81 | ON_CALL(*metricMocks[i], getReadings()) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 82 | .WillByDefault(Return(std::vector({readings[i]}))); |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 83 | ON_CALL(*metricMocks[i], dumpConfiguration()) |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 84 | .WillByDefault(Return(metricParameters[i])); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 88 | std::vector<std::shared_ptr<interfaces::Metric>> |
| 89 | getMetricsFromReadingParams(const ReadingParameters& params) |
| 90 | { |
| 91 | const auto metricParameters = |
| 92 | reportFactoryMock->convertMetricParams(params); |
| 93 | std::vector<std::shared_ptr<MetricMock>> metricMocks; |
| 94 | |
| 95 | for (size_t i = 0; i < metricParameters.size(); ++i) |
| 96 | { |
| 97 | metricMocks.emplace_back(std::make_shared<NiceMock<MetricMock>>()); |
| 98 | ON_CALL(*metricMocks[i], dumpConfiguration()) |
| 99 | .WillByDefault(Return(metricParameters[i])); |
| 100 | } |
| 101 | |
| 102 | return utils::convContainer<std::shared_ptr<interfaces::Metric>>( |
| 103 | metricMocks); |
| 104 | } |
| 105 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 106 | void SetUp() override |
| 107 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 108 | sut = makeReport(defaultParams()); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 111 | static interfaces::JsonStorage::FilePath to_file_path(std::string id) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 112 | { |
| 113 | return interfaces::JsonStorage::FilePath( |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 114 | std::to_string(std::hash<std::string>{}(id))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 115 | } |
| 116 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 117 | std::unique_ptr<Report> makeReport(const ReportParams& params) |
| 118 | { |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 119 | initMetricMocks(params.metricParameters()); |
| 120 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 121 | return std::make_unique<Report>( |
| 122 | DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(), |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 123 | params.reportId(), params.reportName(), params.reportingType(), |
| 124 | params.reportActions(), params.interval(), params.appendLimit(), |
| 125 | params.reportUpdates(), *reportManagerMock, storageMock, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 126 | utils::convContainer<std::shared_ptr<interfaces::Metric>>( |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 127 | metricMocks), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 128 | *reportFactoryMock, params.enabled(), std::move(clockFakePtr)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 129 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 130 | |
| 131 | template <class T> |
| 132 | static T getProperty(const std::string& path, const std::string& property) |
| 133 | { |
Szymon Dompke | e28aa53 | 2021-10-27 12:33:12 +0200 | [diff] [blame] | 134 | return DbusEnvironment::getProperty<T>(path, Report::reportIfaceName, |
| 135 | property); |
| 136 | } |
| 137 | |
| 138 | template <class T> |
| 139 | static boost::system::error_code setProperty(const std::string& path, |
| 140 | const std::string& property, |
| 141 | const T& newValue) |
| 142 | { |
| 143 | return DbusEnvironment::setProperty<T>(path, Report::reportIfaceName, |
| 144 | property, newValue); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | boost::system::error_code call(const std::string& path, |
| 148 | const std::string& interface, |
| 149 | const std::string& method) |
| 150 | { |
| 151 | std::promise<boost::system::error_code> methodPromise; |
| 152 | DbusEnvironment::getBus()->async_method_call( |
| 153 | [&methodPromise](boost::system::error_code ec) { |
| 154 | methodPromise.set_value(ec); |
| 155 | }, |
| 156 | DbusEnvironment::serviceName(), path, interface, method); |
| 157 | return DbusEnvironment::waitForFuture(methodPromise.get_future()); |
| 158 | } |
| 159 | |
| 160 | boost::system::error_code update(const std::string& path) |
| 161 | { |
| 162 | return call(path, Report::reportIfaceName, "Update"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 165 | boost::system::error_code deleteReport(const std::string& path) |
| 166 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 167 | return call(path, Report::deleteIfaceName, "Delete"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 168 | } |
| 169 | }; |
| 170 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 171 | TEST_F(TestReport, returnsId) |
| 172 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 173 | EXPECT_THAT(sut->getId(), Eq(defaultParams().reportId())); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 176 | TEST_F(TestReport, verifyIfPropertiesHaveValidValue) |
| 177 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 178 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 179 | Eq(defaultParams().enabled())); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 180 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 181 | Eq(defaultParams().interval().count())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 182 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true)); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 183 | EXPECT_THAT( |
| 184 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 185 | Eq(utils::transform(defaultParams().reportActions(), [](const auto v) { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 186 | return utils::enumToString(v); |
| 187 | }))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 188 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 189 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 190 | ReportAction::emitsReadingsUpdate))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 191 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "AppendLimit"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 192 | Eq(defaultParams().appendLimit())); |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 193 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 194 | Eq(utils::enumToString(defaultParams().reportingType()))); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 195 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportUpdates"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 196 | Eq(utils::enumToString(defaultParams().reportUpdates()))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 197 | EXPECT_THAT( |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 198 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 199 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 200 | ReportAction::logToMetricReportsCollection))); |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 201 | EXPECT_THAT(getProperty<ReadingParameters>( |
| 202 | sut->getPath(), "ReadingParametersFutureVersion"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 203 | Eq(toReadingParameters(defaultParams().metricParameters()))); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 204 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "Name"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 205 | Eq(defaultParams().reportName())); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 206 | EXPECT_THAT( |
| 207 | getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"), |
| 208 | Eq(std::vector<std::string>())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 211 | TEST_F(TestReport, readingsAreInitialyEmpty) |
| 212 | { |
| 213 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 214 | Eq(Readings{})); |
| 215 | } |
| 216 | |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 217 | TEST_F(TestReport, setReadingParametersWithNewParams) |
| 218 | { |
| 219 | ReadingParameters newParams = toReadingParameters( |
| 220 | std::vector<LabeledMetricParameters>{{LabeledMetricParameters{ |
| 221 | {LabeledSensorInfo{"Service", |
| 222 | "/xyz/openbmc_project/sensors/power/psu", |
| 223 | "NewMetadata123"}}, |
| 224 | OperationType::avg, |
| 225 | "NewMetricId123", |
| 226 | CollectionTimeScope::startup, |
| 227 | CollectionDuration(250ms)}}}); |
| 228 | auto metrics = getMetricsFromReadingParams(newParams); |
| 229 | |
| 230 | EXPECT_CALL(*reportFactoryMock, updateMetrics(_, _, _)) |
| 231 | .WillOnce(SetArgReferee<0>(metrics)); |
| 232 | EXPECT_THAT( |
| 233 | setProperty(sut->getPath(), "ReadingParametersFutureVersion", newParams) |
| 234 | .value(), |
| 235 | Eq(boost::system::errc::success)); |
| 236 | EXPECT_THAT(getProperty<ReadingParameters>( |
| 237 | sut->getPath(), "ReadingParametersFutureVersion"), |
| 238 | Eq(newParams)); |
| 239 | } |
| 240 | |
| 241 | TEST_F(TestReport, setReportingTypeWithValidNewType) |
| 242 | { |
| 243 | std::string newType = "Periodic"; |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 244 | std::string currType = utils::enumToString(defaultParams().reportingType()); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 245 | |
| 246 | EXPECT_THAT(newType, Ne(currType)); |
| 247 | EXPECT_THAT(setProperty(sut->getPath(), "ReportingType", newType).value(), |
| 248 | Eq(boost::system::errc::success)); |
| 249 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"), |
| 250 | Eq(newType)); |
| 251 | } |
| 252 | |
| 253 | TEST_F(TestReport, setReportingTypeWithInvalidType) |
| 254 | { |
| 255 | std::string newType = "Periodic_ABC"; |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 256 | std::string prevType = utils::enumToString(defaultParams().reportingType()); |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 257 | |
| 258 | EXPECT_THAT(setProperty(sut->getPath(), "ReportingType", newType).value(), |
| 259 | Eq(boost::system::errc::invalid_argument)); |
| 260 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"), |
| 261 | Eq(prevType)); |
| 262 | } |
| 263 | |
| 264 | TEST_F(TestReport, setReportActionsWithValidNewActions) |
| 265 | { |
| 266 | std::vector<std::string> newActions = {"EmitsReadingsUpdate"}; |
| 267 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 268 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 269 | [](const auto v) { return utils::enumToString(v); }); |
| 270 | |
| 271 | EXPECT_THAT(newActions, Ne(currActions)); |
| 272 | EXPECT_THAT( |
| 273 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 274 | Eq(boost::system::errc::success)); |
| 275 | EXPECT_THAT( |
| 276 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 277 | UnorderedElementsAre("EmitsReadingsUpdate", |
| 278 | "LogToMetricReportsCollection")); |
| 279 | } |
| 280 | |
| 281 | TEST_F(TestReport, setReportActionsWithValidUnsortedActions) |
| 282 | { |
| 283 | std::vector<std::string> newActions = {"LogToMetricReportsCollection", |
| 284 | "EmitsReadingsUpdate"}; |
| 285 | std::vector<std::string> expectedActions = {"EmitsReadingsUpdate", |
| 286 | "LogToMetricReportsCollection"}; |
| 287 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 288 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 289 | [](const auto v) { return utils::enumToString(v); }); |
| 290 | |
| 291 | EXPECT_THAT(newActions, Ne(currActions)); |
| 292 | EXPECT_THAT( |
| 293 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 294 | Eq(boost::system::errc::success)); |
| 295 | EXPECT_THAT( |
| 296 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 297 | Eq(expectedActions)); |
| 298 | } |
| 299 | |
| 300 | TEST_F(TestReport, setReportActionsWithEmptyActions) |
| 301 | { |
| 302 | std::vector<std::string> newActions = {}; |
| 303 | std::vector<std::string> expectedActions = {"LogToMetricReportsCollection"}; |
| 304 | std::vector<std::string> currActions = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 305 | utils::transform(defaultParams().reportActions(), |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 306 | [](const auto v) { return utils::enumToString(v); }); |
| 307 | |
| 308 | EXPECT_THAT(newActions, Ne(currActions)); |
| 309 | EXPECT_THAT( |
| 310 | setProperty(sut->getPath(), "ReportActions", newActions).value(), |
| 311 | Eq(boost::system::errc::success)); |
| 312 | EXPECT_THAT( |
| 313 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 314 | Eq(expectedActions)); |
| 315 | } |
| 316 | |
| 317 | TEST_F(TestReport, setReportActionsWithInvalidActions) |
| 318 | { |
| 319 | std::vector<std::string> invalidActions = {"EmitsReadingsUpdate_1"}; |
| 320 | EXPECT_THAT( |
| 321 | setProperty(sut->getPath(), "ReportActions", invalidActions).value(), |
| 322 | Eq(boost::system::errc::invalid_argument)); |
| 323 | EXPECT_THAT( |
| 324 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 325 | Eq(utils::transform(defaultParams().reportActions(), [](const auto v) { |
Szymon Dompke | fdb06a1 | 2022-02-11 11:04:44 +0100 | [diff] [blame] | 326 | return utils::enumToString(v); |
| 327 | }))); |
| 328 | } |
| 329 | |
| 330 | TEST_F(TestReport, createReportWithEmptyActions) |
| 331 | { |
| 332 | std::vector<std::string> expectedActions = {"LogToMetricReportsCollection"}; |
| 333 | |
| 334 | sut = makeReport(ReportParams().reportId("TestId_1").reportActions({})); |
| 335 | EXPECT_THAT( |
| 336 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 337 | Eq(expectedActions)); |
| 338 | } |
| 339 | |
| 340 | TEST_F(TestReport, createReportWithValidUnsortedActions) |
| 341 | { |
| 342 | std::vector<std::string> newActions = {"LogToMetricReportsCollection", |
| 343 | "EmitsReadingsUpdate"}; |
| 344 | std::vector<std::string> expectedActions = {"EmitsReadingsUpdate", |
| 345 | "LogToMetricReportsCollection"}; |
| 346 | |
| 347 | sut = makeReport( |
| 348 | ReportParams() |
| 349 | .reportId("TestId_1") |
| 350 | .reportActions(utils::transform(newActions, [](const auto& action) { |
| 351 | return utils::toReportAction(action); |
| 352 | }))); |
| 353 | EXPECT_THAT( |
| 354 | getProperty<std::vector<std::string>>(sut->getPath(), "ReportActions"), |
| 355 | Eq(expectedActions)); |
| 356 | } |
| 357 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 358 | TEST_F(TestReport, setEnabledWithNewValue) |
| 359 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 360 | bool newValue = !defaultParams().enabled(); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 361 | EXPECT_THAT(setProperty(sut->getPath(), "Enabled", newValue).value(), |
| 362 | Eq(boost::system::errc::success)); |
| 363 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Enabled"), Eq(newValue)); |
| 364 | } |
| 365 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 366 | TEST_F(TestReport, setIntervalWithValidValue) |
| 367 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 368 | uint64_t newValue = defaultParams().interval().count() + 1; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 369 | EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 370 | Eq(boost::system::errc::success)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 371 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
| 372 | Eq(newValue)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 375 | TEST_F( |
| 376 | TestReport, |
| 377 | settingIntervalWithInvalidValueDoesNotChangePropertyAndReturnsInvalidArgument) |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 378 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 379 | uint64_t newValue = defaultParams().interval().count() - 1; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 380 | EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(), |
Szymon Dompke | 3e2cc9d | 2021-12-14 12:00:52 +0100 | [diff] [blame] | 381 | Eq(boost::system::errc::invalid_argument)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 382 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 383 | Eq(defaultParams().interval().count())); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 386 | TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect) |
| 387 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 388 | EXPECT_THAT( |
| 389 | setProperty(sut->getPath(), "EmitsReadingsUpdate", true).value(), |
| 390 | Eq(boost::system::errc::read_only_file_system)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 391 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 392 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 393 | ReportAction::emitsReadingsUpdate))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect) |
| 397 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 398 | EXPECT_THAT( |
| 399 | setProperty(sut->getPath(), "LogToMetricReportsCollection", true) |
| 400 | .value(), |
| 401 | Eq(boost::system::errc::read_only_file_system)); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 402 | EXPECT_THAT( |
| 403 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 404 | Eq(utils::contains(defaultParams().reportActions(), |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 405 | ReportAction::logToMetricReportsCollection))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 408 | TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage) |
| 409 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 410 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 411 | |
| 412 | bool persistency = false; |
| 413 | EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(), |
| 414 | Eq(boost::system::errc::success)); |
| 415 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), |
| 416 | Eq(persistency)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | TEST_F(TestReport, deleteReport) |
| 420 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 421 | EXPECT_CALL(*reportManagerMock, removeReport(sut.get())); |
| 422 | auto ec = deleteReport(sut->getPath()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 423 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 424 | } |
| 425 | |
| 426 | TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor) |
| 427 | { |
| 428 | auto ec = deleteReport(Report::reportDir + "NonExisting"s); |
| 429 | EXPECT_THAT(ec.value(), Eq(EBADR)); |
| 430 | } |
| 431 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 432 | TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage) |
| 433 | { |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 434 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 435 | auto ec = deleteReport(sut->getPath()); |
| 436 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 437 | } |
| 438 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 439 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsAdded) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 440 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 441 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 442 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 443 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 444 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 445 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 446 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 447 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 448 | messages::Presence::Exist, "trigger2", {"someOtherReport"}}); |
| 449 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 450 | messages::Presence::Exist, |
| 451 | "trigger3", |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 452 | {"someOtherReport", defaultParams().reportId()}}); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 453 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 454 | EXPECT_THAT( |
| 455 | getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"), |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 456 | UnorderedElementsAre("trigger1", "trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 459 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsRemoved) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 460 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 461 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
| 462 | |
| 463 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 464 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 465 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 466 | messages::Presence::Exist, "trigger2", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 467 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 468 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 469 | |
| 470 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 471 | messages::Presence::Removed, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 472 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 473 | messages::Presence::Removed, "trigger2", {}}); |
| 474 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 475 | messages::Presence::Removed, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 476 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 477 | EXPECT_THAT( |
| 478 | getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"), |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 479 | UnorderedElementsAre("trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 480 | } |
| 481 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 482 | TEST_F(TestReport, updatesTriggerIdWhenTriggerIsModified) |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 483 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 484 | utils::Messanger messanger(DbusEnvironment::getIoc()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 485 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 486 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 487 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 488 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 489 | messages::Presence::Exist, "trigger2", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 490 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 491 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 492 | |
| 493 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 494 | messages::Presence::Exist, "trigger1", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 495 | messanger.send(messages::TriggerPresenceChangedInd{ |
| 496 | messages::Presence::Exist, "trigger2", {}}); |
| 497 | messanger.send(messages::TriggerPresenceChangedInd{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 498 | messages::Presence::Exist, "trigger3", {defaultParams().reportId()}}); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 499 | |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 500 | EXPECT_THAT( |
| 501 | getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"), |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 502 | UnorderedElementsAre("trigger1", "trigger3")); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 505 | class TestReportStore : |
| 506 | public TestReport, |
| 507 | public WithParamInterface<std::pair<std::string, nlohmann::json>> |
| 508 | { |
| 509 | public: |
| 510 | void SetUp() override |
| 511 | {} |
| 512 | |
| 513 | nlohmann::json storedConfiguration; |
| 514 | }; |
| 515 | |
| 516 | INSTANTIATE_TEST_SUITE_P( |
| 517 | _, TestReportStore, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 518 | Values( |
| 519 | std::make_pair("Enabled"s, nlohmann::json(defaultParams().enabled())), |
| 520 | std::make_pair("Version"s, nlohmann::json(6)), |
| 521 | std::make_pair("Id"s, nlohmann::json(defaultParams().reportId())), |
| 522 | std::make_pair("Name"s, nlohmann::json(defaultParams().reportName())), |
| 523 | std::make_pair("ReportingType", |
| 524 | nlohmann::json(defaultParams().reportingType())), |
| 525 | std::make_pair("ReportActions", nlohmann::json(utils::transform( |
| 526 | defaultParams().reportActions(), |
| 527 | [](const auto v) { |
| 528 | return utils::toUnderlying(v); |
| 529 | }))), |
| 530 | std::make_pair("Interval", |
| 531 | nlohmann::json(defaultParams().interval().count())), |
| 532 | std::make_pair("AppendLimit", |
| 533 | nlohmann::json(ReportParams().appendLimit())), |
| 534 | std::make_pair( |
| 535 | "ReadingParameters", |
| 536 | nlohmann::json( |
| 537 | {{{tstring::SensorPath::str(), |
| 538 | {{{tstring::Service::str(), "Service"}, |
| 539 | {tstring::Path::str(), |
| 540 | "/xyz/openbmc_project/sensors/power/p1"}, |
| 541 | {tstring::Metadata::str(), "metadata1"}}}}, |
| 542 | {tstring::OperationType::str(), OperationType::avg}, |
| 543 | {tstring::Id::str(), "MetricId1"}, |
| 544 | {tstring::CollectionTimeScope::str(), |
| 545 | CollectionTimeScope::point}, |
| 546 | {tstring::CollectionDuration::str(), 0}}, |
| 547 | {{tstring::SensorPath::str(), |
| 548 | {{{tstring::Service::str(), "Service"}, |
| 549 | {tstring::Path::str(), |
| 550 | "/xyz/openbmc_project/sensors/power/p2"}, |
| 551 | {tstring::Metadata::str(), "metadata2"}}}}, |
| 552 | {tstring::OperationType::str(), OperationType::avg}, |
| 553 | {tstring::Id::str(), "MetricId2"}, |
| 554 | {tstring::CollectionTimeScope::str(), |
| 555 | CollectionTimeScope::point}, |
| 556 | {tstring::CollectionDuration::str(), 0}}})))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 557 | |
| 558 | TEST_P(TestReportStore, settingPersistencyToTrueStoresReport) |
| 559 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 560 | sut = makeReport(defaultParams()); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 561 | |
| 562 | { |
| 563 | InSequence seq; |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 564 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getId()))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 565 | EXPECT_CALL(checkPoint, Call()); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 566 | EXPECT_CALL(storageMock, store(to_file_path(sut->getId()), _)) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 567 | .WillOnce(SaveArg<1>(&storedConfiguration)); |
| 568 | } |
| 569 | |
| 570 | setProperty(sut->getPath(), "Persistency", false); |
| 571 | checkPoint.Call(); |
| 572 | setProperty(sut->getPath(), "Persistency", true); |
| 573 | |
| 574 | const auto& [key, value] = GetParam(); |
| 575 | |
| 576 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 577 | } |
| 578 | |
| 579 | TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated) |
| 580 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 581 | EXPECT_CALL(storageMock, store(to_file_path(defaultParams().reportId()), _)) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 582 | .WillOnce(SaveArg<1>(&storedConfiguration)); |
| 583 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 584 | sut = makeReport(defaultParams()); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 585 | |
| 586 | const auto& [key, value] = GetParam(); |
| 587 | |
| 588 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 589 | } |
| 590 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 591 | class TestReportValidNames : |
| 592 | public TestReport, |
| 593 | public WithParamInterface<ReportParams> |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 594 | { |
| 595 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 596 | void SetUp() override |
| 597 | {} |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 598 | }; |
| 599 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 600 | INSTANTIATE_TEST_SUITE_P( |
| 601 | ValidNames, TestReportValidNames, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 602 | Values(defaultParams().reportName("Valid_1"), |
| 603 | defaultParams().reportName("Valid_1/Valid_2"), |
| 604 | defaultParams().reportName("Valid_1/Valid_2/Valid_3"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 605 | |
| 606 | TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName) |
| 607 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 608 | EXPECT_NO_THROW(makeReport(GetParam())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 609 | } |
| 610 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 611 | class TestReportInvalidIds : |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 612 | public TestReport, |
| 613 | public WithParamInterface<ReportParams> |
| 614 | { |
| 615 | public: |
| 616 | void SetUp() override |
| 617 | {} |
| 618 | }; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 619 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 620 | INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidIds, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 621 | Values(defaultParams().reportId("/"), |
| 622 | defaultParams().reportId("/Invalid"), |
| 623 | defaultParams().reportId("Invalid/"), |
| 624 | defaultParams().reportId("Invalid/Invalid/"), |
| 625 | defaultParams().reportId("Invalid?"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 626 | |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 627 | TEST_P(TestReportInvalidIds, failsToCreateReportWithInvalidName) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 628 | { |
| 629 | EXPECT_CALL(storageMock, store).Times(0); |
Krzysztof Grobelny | b8cc78d | 2021-11-29 15:54:53 +0100 | [diff] [blame] | 630 | |
| 631 | EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 634 | class TestReportAllReportTypes : |
| 635 | public TestReport, |
| 636 | public WithParamInterface<ReportParams> |
| 637 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 638 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 639 | void SetUp() override |
| 640 | { |
| 641 | sut = makeReport(GetParam()); |
| 642 | } |
| 643 | }; |
| 644 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 645 | INSTANTIATE_TEST_SUITE_P( |
| 646 | _, TestReportAllReportTypes, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 647 | Values(defaultParams().reportingType(ReportingType::onRequest), |
| 648 | defaultParams().reportingType(ReportingType::onChange), |
| 649 | defaultParams().reportingType(ReportingType::periodic))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 650 | |
| 651 | TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType) |
| 652 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 653 | EXPECT_THAT(utils::toReportingType( |
| 654 | getProperty<std::string>(sut->getPath(), "ReportingType")), |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 655 | Eq(GetParam().reportingType())); |
| 656 | } |
| 657 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 658 | TEST_P(TestReportAllReportTypes, readingsAreUpdated) |
| 659 | { |
| 660 | clockFake.system.advance(10ms); |
| 661 | |
| 662 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
| 663 | const auto [timestamp, readings] = |
| 664 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 665 | |
| 666 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
| 667 | } |
| 668 | |
| 669 | TEST_P(TestReportAllReportTypes, readingsAreNotUpdatedWhenReportIsDisabled) |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 670 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 671 | clockFake.system.advance(10ms); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 672 | |
| 673 | setProperty(sut->getPath(), "Enabled", false); |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 674 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 675 | const auto [timestamp, readings] = |
| 676 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 677 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 678 | EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms)); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 681 | TEST_P(TestReportAllReportTypes, readingsAreNotUpdatedWhenReportIdDiffers) |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 682 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 683 | clockFake.system.advance(10ms); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 684 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 685 | messanger.send(messages::UpdateReportInd{{sut->getId() + "x"s}}); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 686 | const auto [timestamp, readings] = |
| 687 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 688 | |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 689 | EXPECT_THAT(Milliseconds{timestamp}, Eq(0ms)); |
Wludzik, Jozef | d960e1f | 2021-01-08 09:25:59 +0100 | [diff] [blame] | 690 | } |
| 691 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 692 | class TestReportOnRequestType : public TestReport |
| 693 | { |
| 694 | void SetUp() override |
| 695 | { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 696 | sut = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 697 | makeReport(defaultParams().reportingType(ReportingType::onRequest)); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 698 | } |
| 699 | }; |
| 700 | |
| 701 | TEST_F(TestReportOnRequestType, updatesReadingTimestamp) |
| 702 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 703 | clockFake.system.advance(10ms); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 704 | |
| 705 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 706 | |
| 707 | const auto [timestamp, readings] = |
| 708 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 709 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 710 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled) |
| 714 | { |
| 715 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 716 | |
| 717 | const auto [timestamp, readings] = |
| 718 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 719 | |
| 720 | EXPECT_THAT(readings, |
| 721 | ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 722 | std::make_tuple("aa"s, "bb"s, 42.0, 74u))); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | class TestReportNonOnRequestType : |
| 726 | public TestReport, |
| 727 | public WithParamInterface<ReportParams> |
| 728 | { |
| 729 | void SetUp() override |
| 730 | { |
| 731 | sut = makeReport(GetParam()); |
| 732 | } |
| 733 | }; |
| 734 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 735 | INSTANTIATE_TEST_SUITE_P( |
| 736 | _, TestReportNonOnRequestType, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 737 | Values(defaultParams().reportingType(ReportingType::periodic), |
| 738 | defaultParams().reportingType(ReportingType::onChange))); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 739 | |
| 740 | TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall) |
| 741 | { |
| 742 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 743 | |
| 744 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 745 | Eq(Readings{})); |
| 746 | } |
| 747 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 748 | class TestReportNonPeriodicReport : |
| 749 | public TestReport, |
| 750 | public WithParamInterface<ReportParams> |
| 751 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 752 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 753 | void SetUp() override |
| 754 | { |
| 755 | sut = makeReport(GetParam()); |
| 756 | } |
| 757 | }; |
| 758 | |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 759 | INSTANTIATE_TEST_SUITE_P( |
| 760 | _, TestReportNonPeriodicReport, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 761 | Values(defaultParams().reportingType(ReportingType::onRequest), |
| 762 | defaultParams().reportingType(ReportingType::onChange))); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 763 | |
| 764 | TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires) |
| 765 | { |
| 766 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 767 | |
| 768 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 769 | Eq(Readings{})); |
| 770 | } |
| 771 | |
| 772 | class TestReportPeriodicReport : public TestReport |
| 773 | { |
| 774 | void SetUp() override |
| 775 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 776 | sut = |
| 777 | makeReport(defaultParams().reportingType(ReportingType::periodic)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 778 | } |
| 779 | }; |
| 780 | |
| 781 | TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires) |
| 782 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 783 | clockFake.system.advance(10ms); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 784 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 785 | |
| 786 | const auto [timestamp, readings] = |
| 787 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 788 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 789 | EXPECT_THAT(Milliseconds{timestamp}, Eq(systemTimestamp + 10ms)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires) |
| 793 | { |
| 794 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 795 | |
| 796 | const auto [timestamp, readings] = |
| 797 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 798 | |
| 799 | EXPECT_THAT(readings, |
| 800 | ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u), |
Krzysztof Grobelny | dcc4e19 | 2021-03-08 09:09:34 +0000 | [diff] [blame] | 801 | std::make_tuple("aa"s, "bb"s, 42.0, 74u))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 802 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 803 | |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 804 | struct ReportUpdatesReportParams |
| 805 | { |
| 806 | ReportParams reportParams; |
| 807 | std::vector<ReadingData> expectedReadings; |
| 808 | bool expectedEnabled; |
| 809 | }; |
| 810 | |
| 811 | class TestReportWithReportUpdatesAndLimit : |
| 812 | public TestReport, |
| 813 | public WithParamInterface<ReportUpdatesReportParams> |
| 814 | { |
| 815 | void SetUp() override |
| 816 | { |
| 817 | sut = makeReport(ReportParams(GetParam().reportParams) |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 818 | .reportingType(ReportingType::periodic) |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 819 | .interval(std::chrono::hours(1000))); |
| 820 | } |
| 821 | }; |
| 822 | |
| 823 | INSTANTIATE_TEST_SUITE_P( |
| 824 | _, TestReportWithReportUpdatesAndLimit, |
| 825 | Values( |
| 826 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 827 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 828 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 829 | .appendLimit(5), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 830 | std::vector<ReadingData>{{std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 831 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 832 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 833 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 834 | std::make_tuple("a"s, "b"s, 17.1, 114u)}}, |
| 835 | true}, |
| 836 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 837 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 838 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 839 | .appendLimit(4), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 840 | std::vector<ReadingData>{ |
| 841 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 842 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 843 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 844 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 845 | true}, |
| 846 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 847 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 848 | .reportUpdates(ReportUpdates::appendWrapsWhenFull) |
| 849 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 850 | std::vector<ReadingData>{}, true}, |
| 851 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 852 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 853 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 854 | .appendLimit(10), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 855 | std::vector<ReadingData>{ |
| 856 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 857 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 858 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 859 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 860 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 861 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 862 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 863 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 864 | true}, |
| 865 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 866 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 867 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 868 | .appendLimit(5), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 869 | std::vector<ReadingData>{{std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 870 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 871 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 872 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 873 | std::make_tuple("a"s, "b"s, 17.1, 114u)}}, |
| 874 | false}, |
| 875 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 876 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 877 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 878 | .appendLimit(4), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 879 | std::vector<ReadingData>{ |
| 880 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 881 | std::make_tuple("aa"s, "bb"s, 42.0, 74u), |
| 882 | std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 883 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 884 | false}, |
| 885 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 886 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 887 | .reportUpdates(ReportUpdates::appendStopsWhenFull) |
| 888 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 889 | std::vector<ReadingData>{}, false}, |
| 890 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 891 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 892 | .reportUpdates(ReportUpdates::overwrite) |
| 893 | .appendLimit(500), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 894 | std::vector<ReadingData>{ |
| 895 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 896 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 897 | true}, |
| 898 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 899 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 900 | .reportUpdates(ReportUpdates::overwrite) |
| 901 | .appendLimit(1), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 902 | std::vector<ReadingData>{ |
| 903 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 904 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 905 | true}, |
| 906 | ReportUpdatesReportParams{ |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 907 | defaultParams() |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 908 | .reportUpdates(ReportUpdates::overwrite) |
| 909 | .appendLimit(0), |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 910 | std::vector<ReadingData>{ |
| 911 | {std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 912 | std::make_tuple("aa"s, "bb"s, 42.0, 74u)}}, |
| 913 | true})); |
| 914 | |
| 915 | TEST_P(TestReportWithReportUpdatesAndLimit, |
| 916 | readingsAreUpdatedAfterIntervalExpires) |
| 917 | { |
| 918 | for (int i = 0; i < 4; i++) |
| 919 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 920 | messanger.send(messages::UpdateReportInd{{sut->getId()}}); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | const auto [timestamp, readings] = |
| 924 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 925 | const auto enabled = getProperty<bool>(sut->getPath(), "Enabled"); |
| 926 | |
| 927 | EXPECT_THAT(readings, ElementsAreArray(GetParam().expectedReadings)); |
| 928 | EXPECT_EQ(enabled, GetParam().expectedEnabled); |
| 929 | } |
| 930 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 931 | class TestReportInitialization : public TestReport |
| 932 | { |
| 933 | public: |
| 934 | void SetUp() override |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 935 | { |
| 936 | initMetricMocks(defaultParams().metricParameters()); |
| 937 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 938 | |
| 939 | void monitorProc(sdbusplus::message::message& msg) |
| 940 | { |
| 941 | std::string iface; |
| 942 | std::vector<std::pair<std::string, std::variant<Readings>>> |
| 943 | changed_properties; |
| 944 | std::vector<std::string> invalidated_properties; |
| 945 | |
| 946 | msg.read(iface, changed_properties, invalidated_properties); |
| 947 | |
| 948 | if (iface == Report::reportIfaceName) |
| 949 | { |
| 950 | for (const auto& [name, value] : changed_properties) |
| 951 | { |
| 952 | if (name == "Readings") |
| 953 | { |
| 954 | readingsUpdated.Call(); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | void makeMonitor() |
| 961 | { |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 962 | monitor = std::make_unique<sdbusplus::bus::match_t>( |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 963 | *DbusEnvironment::getBus(), |
| 964 | sdbusplus::bus::match::rules::propertiesChanged( |
| 965 | sut->getPath(), Report::reportIfaceName), |
| 966 | [this](auto& msg) { monitorProc(msg); }); |
| 967 | } |
| 968 | |
Patrick Williams | 3a62ee1 | 2021-12-03 10:13:25 -0600 | [diff] [blame] | 969 | std::unique_ptr<sdbusplus::bus::match_t> monitor; |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 970 | MockFunction<void()> readingsUpdated; |
| 971 | }; |
| 972 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 973 | TEST_F(TestReportInitialization, |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 974 | registersForMetricUpdatesWhenOnChangeReportCreated) |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 975 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 976 | std::vector<const interfaces::MetricListener*> args; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 977 | for (auto& metric : metricMocks) |
| 978 | { |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 979 | EXPECT_CALL(*metric, registerForUpdates(_)) |
| 980 | .WillOnce(Invoke([&args](const interfaces::MetricListener& report) { |
| 981 | args.emplace_back(&report); |
| 982 | })); |
| 983 | ; |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 984 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 985 | |
| 986 | sut = makeReport(defaultParams().reportingType(ReportingType::onChange)); |
| 987 | |
| 988 | EXPECT_THAT(args, SizeIs(metricMocks.size())); |
| 989 | for (const auto* reportPtr : args) |
| 990 | { |
| 991 | EXPECT_THAT(reportPtr, Eq(sut.get())); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | TEST_F(TestReportInitialization, |
| 996 | deregistersForMetricUpdatesWhenOnChangeReportDestroyed) |
| 997 | { |
| 998 | sut = makeReport(defaultParams().reportingType(ReportingType::onChange)); |
| 999 | |
| 1000 | for (auto& metric : metricMocks) |
| 1001 | { |
| 1002 | EXPECT_CALL(*metric, |
| 1003 | unregisterFromUpdates(Ref( |
| 1004 | static_cast<interfaces::MetricListener&>(*sut.get())))); |
| 1005 | } |
| 1006 | |
| 1007 | sut = nullptr; |
| 1008 | } |
| 1009 | |
| 1010 | TEST_F(TestReportInitialization, |
| 1011 | metricsAreInitializedWhenEnabledReportConstructed) |
| 1012 | { |
| 1013 | for (auto& metric : metricMocks) |
| 1014 | { |
| 1015 | EXPECT_CALL(*metric, initialize()); |
| 1016 | } |
| 1017 | sut = makeReport(defaultParams().enabled(true)); |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1018 | } |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1019 | |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1020 | TEST_F(TestReportInitialization, |
| 1021 | metricsAreNotInitializedWhenDisabledReportConstructed) |
| 1022 | { |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 1023 | for (auto& metric : metricMocks) |
| 1024 | { |
| 1025 | EXPECT_CALL(*metric, initialize()).Times(0); |
| 1026 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 1027 | sut = makeReport(defaultParams().enabled(false)); |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 1028 | } |
| 1029 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1030 | TEST_F(TestReportInitialization, |
| 1031 | emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1032 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1033 | EXPECT_CALL(readingsUpdated, Call()) |
| 1034 | .WillOnce( |
| 1035 | InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated"))); |
| 1036 | |
| 1037 | const auto elapsed = DbusEnvironment::measureTime([this] { |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1038 | sut = |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 1039 | makeReport(defaultParams() |
| 1040 | .reportingType(ReportingType::periodic) |
Krzysztof Grobelny | 51497a0 | 2021-11-09 14:56:22 +0100 | [diff] [blame] | 1041 | .reportActions({ReportAction::emitsReadingsUpdate})); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1042 | makeMonitor(); |
| 1043 | EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated")); |
| 1044 | }); |
| 1045 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 1046 | EXPECT_THAT(elapsed, AllOf(Ge(defaultParams().interval()), |
| 1047 | Lt(defaultParams().interval() * 2))); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | TEST_F(TestReportInitialization, |
| 1051 | emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits) |
| 1052 | { |
| 1053 | EXPECT_CALL(readingsUpdated, Call()).Times(0); |
| 1054 | |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 1055 | sut = makeReport(defaultParams() |
| 1056 | .reportingType(ReportingType::periodic) |
| 1057 | .reportActions({})); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1058 | makeMonitor(); |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 1059 | DbusEnvironment::sleepFor(defaultParams().interval() * 2); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 1060 | } |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1061 | |
| 1062 | TEST_F(TestReportInitialization, appendLimitDeducedProperly) |
| 1063 | { |
| 1064 | sut = makeReport( |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 1065 | defaultParams().appendLimit(std::numeric_limits<uint64_t>::max())); |
Szymon Dompke | 3eb5686 | 2021-09-20 15:32:04 +0200 | [diff] [blame] | 1066 | auto appendLimit = getProperty<uint64_t>(sut->getPath(), "AppendLimit"); |
| 1067 | EXPECT_EQ(appendLimit, 2ull); |
| 1068 | } |
Krzysztof Grobelny | e6c417c | 2022-02-02 17:25:53 +0100 | [diff] [blame] | 1069 | |
| 1070 | TEST_F(TestReportInitialization, appendLimitSetToUintMaxIsStoredCorrectly) |
| 1071 | { |
| 1072 | nlohmann::json storedConfiguration; |
| 1073 | |
| 1074 | EXPECT_CALL(storageMock, store(to_file_path(ReportParams().reportId()), _)) |
| 1075 | .WillOnce(SaveArg<1>(&storedConfiguration)); |
| 1076 | |
| 1077 | sut = makeReport( |
| 1078 | ReportParams().appendLimit(std::numeric_limits<uint64_t>::max())); |
| 1079 | |
| 1080 | ASSERT_THAT(storedConfiguration.at("AppendLimit"), |
| 1081 | Eq(std::numeric_limits<uint64_t>::max())); |
| 1082 | } |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 1083 | |
| 1084 | TEST_F(TestReportInitialization, triggerIdsPropertyIsInitialzed) |
| 1085 | { |
Krzysztof Grobelny | e6d4887 | 2022-02-08 13:41:30 +0100 | [diff] [blame] | 1086 | for (const auto& triggerId : {"trigger1", "trigger2"}) |
| 1087 | { |
| 1088 | messanger.on_receive<messages::CollectTriggerIdReq>( |
| 1089 | [&](const auto& msg) { |
| 1090 | messanger.send(messages::CollectTriggerIdResp{triggerId}); |
| 1091 | }); |
| 1092 | } |
| 1093 | |
| 1094 | sut = makeReport(ReportParams()); |
Szymon Dompke | b4ef22e | 2022-02-07 15:15:12 +0100 | [diff] [blame] | 1095 | |
| 1096 | EXPECT_THAT( |
| 1097 | getProperty<std::vector<std::string>>(sut->getPath(), "TriggerIds"), |
| 1098 | UnorderedElementsAre("trigger1", "trigger2")); |
| 1099 | } |
Krzysztof Grobelny | f7ea299 | 2022-01-27 11:04:58 +0100 | [diff] [blame^] | 1100 | |
| 1101 | class TestReportInitializationOnChangeReport : public TestReportInitialization |
| 1102 | { |
| 1103 | public: |
| 1104 | void SetUp() override |
| 1105 | { |
| 1106 | initMetricMocks(params.metricParameters()); |
| 1107 | } |
| 1108 | |
| 1109 | ReportParams params = defaultOnChangeParams(); |
| 1110 | }; |
| 1111 | |
| 1112 | TEST_F(TestReportInitializationOnChangeReport, |
| 1113 | doesntUpdateReadingsWhenNotRequired) |
| 1114 | { |
| 1115 | EXPECT_CALL(*metricMocks[0], updateReadings(_)).Times(0); |
| 1116 | |
| 1117 | ON_CALL(*metricMocks[0], isTimerRequired()).WillByDefault(Return(false)); |
| 1118 | |
| 1119 | sut = makeReport(params); |
| 1120 | |
| 1121 | DbusEnvironment::sleepFor(500ms); |
| 1122 | } |
| 1123 | |
| 1124 | TEST_F(TestReportInitializationOnChangeReport, updatesReadingsWhenRequired) |
| 1125 | { |
| 1126 | EXPECT_CALL(*metricMocks[0], updateReadings(_)) |
| 1127 | .WillOnce(Return()) |
| 1128 | .WillOnce( |
| 1129 | InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated"))) |
| 1130 | .WillRepeatedly(Return()); |
| 1131 | |
| 1132 | ON_CALL(*metricMocks[0], isTimerRequired()).WillByDefault(Return(true)); |
| 1133 | |
| 1134 | sut = makeReport(params); |
| 1135 | |
| 1136 | DbusEnvironment::waitForFuture("readingsUpdated"); |
| 1137 | } |