Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 2 | #include "helpers.hpp" |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 3 | #include "mocks/json_storage_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 4 | #include "mocks/metric_mock.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 5 | #include "mocks/report_manager_mock.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 6 | #include "params/report_params.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 7 | #include "report.hpp" |
| 8 | #include "report_manager.hpp" |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 9 | #include "utils/conv_container.hpp" |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 10 | #include "utils/set_exception.hpp" |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 11 | |
| 12 | #include <sdbusplus/exception.hpp> |
| 13 | |
| 14 | using namespace testing; |
| 15 | using namespace std::literals::string_literals; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 16 | using namespace std::chrono_literals; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 17 | |
| 18 | class TestReport : public Test |
| 19 | { |
| 20 | public: |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 21 | ReportParams defaultParams; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 22 | |
| 23 | std::unique_ptr<ReportManagerMock> reportManagerMock = |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 24 | std::make_unique<NiceMock<ReportManagerMock>>(); |
| 25 | testing::NiceMock<StorageMock> storageMock; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 26 | std::vector<std::shared_ptr<MetricMock>> metricMocks = { |
| 27 | std::make_shared<NiceMock<MetricMock>>(), |
| 28 | std::make_shared<NiceMock<MetricMock>>(), |
| 29 | std::make_shared<NiceMock<MetricMock>>()}; |
| 30 | std::unique_ptr<Report> sut; |
| 31 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 32 | MockFunction<void()> checkPoint; |
| 33 | |
| 34 | TestReport() |
| 35 | { |
| 36 | ON_CALL(*metricMocks[0], getReadings()) |
| 37 | .WillByDefault(ReturnRefOfCopy(std::vector<MetricValue>( |
| 38 | {MetricValue{"a", "b", 17.1, 114}, |
| 39 | MetricValue{"aaa", "bbb", 21.7, 100}}))); |
| 40 | ON_CALL(*metricMocks[1], getReadings()) |
| 41 | .WillByDefault(ReturnRefOfCopy( |
| 42 | std::vector<MetricValue>({MetricValue{"aa", "bb", 42.0, 74}}))); |
| 43 | |
| 44 | for (size_t i = 0; i < metricMocks.size(); ++i) |
| 45 | { |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 46 | using namespace std::string_literals; |
| 47 | |
| 48 | auto id = std::to_string(i); |
| 49 | |
| 50 | auto sensorParameters = std::vector<LabeledSensorParameters>( |
| 51 | {LabeledSensorParameters("service"s + id, "path"s + id)}); |
| 52 | auto metricParameters = |
| 53 | LabeledMetricParameters(std::move(sensorParameters), "op"s + id, |
| 54 | "id"s + id, "metadata"s + id); |
| 55 | |
| 56 | ON_CALL(*metricMocks[i], dumpConfiguration()) |
| 57 | .WillByDefault(Return(std::move(metricParameters))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 61 | void SetUp() override |
| 62 | { |
| 63 | sut = makeReport(ReportParams()); |
| 64 | } |
| 65 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 66 | static interfaces::JsonStorage::FilePath to_file_path(std::string name) |
| 67 | { |
| 68 | return interfaces::JsonStorage::FilePath( |
| 69 | std::to_string(std::hash<std::string>{}(name))); |
| 70 | } |
| 71 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 72 | std::unique_ptr<Report> makeReport(const ReportParams& params) |
| 73 | { |
| 74 | return std::make_unique<Report>( |
| 75 | DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(), |
| 76 | params.reportName(), params.reportingType(), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 77 | params.emitReadingUpdate(), params.logToMetricReportCollection(), |
| 78 | params.interval(), params.readingParameters(), *reportManagerMock, |
| 79 | storageMock, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 80 | utils::convContainer<std::shared_ptr<interfaces::Metric>>( |
| 81 | metricMocks)); |
| 82 | } |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 83 | |
| 84 | template <class T> |
| 85 | static T getProperty(const std::string& path, const std::string& property) |
| 86 | { |
| 87 | std::promise<T> propertyPromise; |
| 88 | sdbusplus::asio::getProperty<T>( |
| 89 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path, |
| 90 | Report::reportIfaceName, property, |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 91 | [&propertyPromise](boost::system::error_code) { |
| 92 | utils::setException(propertyPromise, "GetProperty failed"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 93 | }, |
| 94 | [&propertyPromise](T t) { propertyPromise.set_value(t); }); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 95 | return DbusEnvironment::waitForFuture(propertyPromise.get_future()); |
| 96 | } |
| 97 | |
| 98 | boost::system::error_code call(const std::string& path, |
| 99 | const std::string& interface, |
| 100 | const std::string& method) |
| 101 | { |
| 102 | std::promise<boost::system::error_code> methodPromise; |
| 103 | DbusEnvironment::getBus()->async_method_call( |
| 104 | [&methodPromise](boost::system::error_code ec) { |
| 105 | methodPromise.set_value(ec); |
| 106 | }, |
| 107 | DbusEnvironment::serviceName(), path, interface, method); |
| 108 | return DbusEnvironment::waitForFuture(methodPromise.get_future()); |
| 109 | } |
| 110 | |
| 111 | boost::system::error_code update(const std::string& path) |
| 112 | { |
| 113 | return call(path, Report::reportIfaceName, "Update"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | template <class T> |
| 117 | static boost::system::error_code setProperty(const std::string& path, |
| 118 | const std::string& property, |
| 119 | const T& newValue) |
| 120 | { |
| 121 | std::promise<boost::system::error_code> setPromise; |
| 122 | sdbusplus::asio::setProperty( |
| 123 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path, |
| 124 | Report::reportIfaceName, property, std::move(newValue), |
| 125 | [&setPromise](boost::system::error_code ec) { |
| 126 | setPromise.set_value(ec); |
| 127 | }, |
| 128 | [&setPromise]() { |
| 129 | setPromise.set_value(boost::system::error_code{}); |
| 130 | }); |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 131 | return DbusEnvironment::waitForFuture(setPromise.get_future()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | boost::system::error_code deleteReport(const std::string& path) |
| 135 | { |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 136 | return call(path, Report::deleteIfaceName, "Delete"); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 137 | } |
| 138 | }; |
| 139 | |
| 140 | TEST_F(TestReport, verifyIfPropertiesHaveValidValue) |
| 141 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 142 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 143 | Eq(defaultParams.interval().count())); |
| 144 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), Eq(true)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 145 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 146 | Eq(defaultParams.emitReadingUpdate())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 147 | EXPECT_THAT( |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 148 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 149 | Eq(defaultParams.logToMetricReportCollection())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 150 | EXPECT_THAT( |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 151 | getProperty<ReadingParameters>(sut->getPath(), "ReadingParameters"), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 152 | Eq(defaultParams.readingParameters())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 155 | TEST_F(TestReport, readingsAreInitialyEmpty) |
| 156 | { |
| 157 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 158 | Eq(Readings{})); |
| 159 | } |
| 160 | |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 161 | TEST_F(TestReport, setIntervalWithValidValue) |
| 162 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 163 | uint64_t newValue = defaultParams.interval().count() + 1; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 164 | EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 165 | Eq(boost::system::errc::success)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 166 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
| 167 | Eq(newValue)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST_F(TestReport, settingIntervalWithInvalidValueDoesNotChangeProperty) |
| 171 | { |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 172 | uint64_t newValue = defaultParams.interval().count() - 1; |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 173 | EXPECT_THAT(setProperty(sut->getPath(), "Interval", newValue).value(), |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 174 | Eq(boost::system::errc::success)); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 175 | EXPECT_THAT(getProperty<uint64_t>(sut->getPath(), "Interval"), |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 176 | Eq(defaultParams.interval().count())); |
| 177 | } |
| 178 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 179 | TEST_F(TestReport, settingEmitsReadingsUpdateHaveNoEffect) |
| 180 | { |
| 181 | EXPECT_THAT(setProperty(sut->getPath(), "EmitsReadingsUpdate", |
| 182 | !defaultParams.emitReadingUpdate()) |
| 183 | .value(), |
| 184 | Eq(boost::system::errc::read_only_file_system)); |
| 185 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "EmitsReadingsUpdate"), |
| 186 | Eq(defaultParams.emitReadingUpdate())); |
| 187 | } |
| 188 | |
| 189 | TEST_F(TestReport, settingLogToMetricReportCollectionHaveNoEffect) |
| 190 | { |
| 191 | EXPECT_THAT(setProperty(sut->getPath(), "LogToMetricReportsCollection", |
| 192 | !defaultParams.logToMetricReportCollection()) |
| 193 | .value(), |
| 194 | Eq(boost::system::errc::read_only_file_system)); |
| 195 | EXPECT_THAT( |
| 196 | getProperty<bool>(sut->getPath(), "LogToMetricReportsCollection"), |
| 197 | Eq(defaultParams.logToMetricReportCollection())); |
| 198 | } |
| 199 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 200 | TEST_F(TestReport, settingPersistencyToFalseRemovesReportFromStorage) |
| 201 | { |
| 202 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getName()))); |
| 203 | |
| 204 | bool persistency = false; |
| 205 | EXPECT_THAT(setProperty(sut->getPath(), "Persistency", persistency).value(), |
| 206 | Eq(boost::system::errc::success)); |
| 207 | EXPECT_THAT(getProperty<bool>(sut->getPath(), "Persistency"), |
| 208 | Eq(persistency)); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | TEST_F(TestReport, deleteReport) |
| 212 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 213 | EXPECT_CALL(*reportManagerMock, removeReport(sut.get())); |
| 214 | auto ec = deleteReport(sut->getPath()); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 215 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 216 | } |
| 217 | |
| 218 | TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor) |
| 219 | { |
| 220 | auto ec = deleteReport(Report::reportDir + "NonExisting"s); |
| 221 | EXPECT_THAT(ec.value(), Eq(EBADR)); |
| 222 | } |
| 223 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 224 | TEST_F(TestReport, deleteReportExpectThatFileIsRemoveFromStorage) |
| 225 | { |
| 226 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getName()))); |
| 227 | auto ec = deleteReport(sut->getPath()); |
| 228 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 229 | } |
| 230 | |
| 231 | class TestReportStore : |
| 232 | public TestReport, |
| 233 | public WithParamInterface<std::pair<std::string, nlohmann::json>> |
| 234 | { |
| 235 | public: |
| 236 | void SetUp() override |
| 237 | {} |
| 238 | |
| 239 | nlohmann::json storedConfiguration; |
| 240 | }; |
| 241 | |
| 242 | INSTANTIATE_TEST_SUITE_P( |
| 243 | _, TestReportStore, |
Krzysztof Grobelny | d223819 | 2020-12-02 09:27:28 +0000 | [diff] [blame] | 244 | Values( |
| 245 | std::make_pair("Version"s, nlohmann::json(2)), |
| 246 | std::make_pair("Name"s, nlohmann::json(ReportParams().reportName())), |
| 247 | std::make_pair("ReportingType", |
| 248 | nlohmann::json(ReportParams().reportingType())), |
| 249 | std::make_pair("EmitsReadingsUpdate", |
| 250 | nlohmann::json(ReportParams().emitReadingUpdate())), |
| 251 | std::make_pair( |
| 252 | "LogToMetricReportsCollection", |
| 253 | nlohmann::json(ReportParams().logToMetricReportCollection())), |
| 254 | std::make_pair("Interval", |
| 255 | nlohmann::json(ReportParams().interval().count())), |
| 256 | std::make_pair( |
| 257 | "ReadingParameters", |
| 258 | nlohmann::json({{{"sensorPaths", |
| 259 | {{{"service", "service0"}, {"path", "path0"}}}}, |
| 260 | {"operationType", "op0"}, |
| 261 | {"id", "id0"}, |
| 262 | {"metricMetadata", "metadata0"}}, |
| 263 | {{"sensorPaths", |
| 264 | {{{"service", "service1"}, {"path", "path1"}}}}, |
| 265 | {"operationType", "op1"}, |
| 266 | {"id", "id1"}, |
| 267 | {"metricMetadata", "metadata1"}}, |
| 268 | {{"sensorPaths", |
| 269 | {{{"service", "service2"}, {"path", "path2"}}}}, |
| 270 | {"operationType", "op2"}, |
| 271 | {"id", "id2"}, |
| 272 | {"metricMetadata", "metadata2"}}})))); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 273 | |
| 274 | TEST_P(TestReportStore, settingPersistencyToTrueStoresReport) |
| 275 | { |
| 276 | sut = makeReport(ReportParams()); |
| 277 | |
| 278 | { |
| 279 | InSequence seq; |
| 280 | EXPECT_CALL(storageMock, remove(to_file_path(sut->getName()))); |
| 281 | EXPECT_CALL(checkPoint, Call()); |
| 282 | EXPECT_CALL(storageMock, store(to_file_path(sut->getName()), _)) |
| 283 | .WillOnce(SaveArg<1>(&storedConfiguration)); |
| 284 | } |
| 285 | |
| 286 | setProperty(sut->getPath(), "Persistency", false); |
| 287 | checkPoint.Call(); |
| 288 | setProperty(sut->getPath(), "Persistency", true); |
| 289 | |
| 290 | const auto& [key, value] = GetParam(); |
| 291 | |
| 292 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 293 | } |
| 294 | |
| 295 | TEST_P(TestReportStore, reportIsSavedToStorageAfterCreated) |
| 296 | { |
| 297 | EXPECT_CALL(storageMock, |
| 298 | store(to_file_path(ReportParams().reportName()), _)) |
| 299 | .WillOnce(SaveArg<1>(&storedConfiguration)); |
| 300 | |
| 301 | sut = makeReport(ReportParams()); |
| 302 | |
| 303 | const auto& [key, value] = GetParam(); |
| 304 | |
| 305 | ASSERT_THAT(storedConfiguration.at(key), Eq(value)); |
| 306 | } |
| 307 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 308 | class TestReportValidNames : |
| 309 | public TestReport, |
| 310 | public WithParamInterface<ReportParams> |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 311 | { |
| 312 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 313 | void SetUp() override |
| 314 | {} |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 315 | }; |
| 316 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 317 | INSTANTIATE_TEST_SUITE_P( |
| 318 | ValidNames, TestReportValidNames, |
| 319 | Values(ReportParams().reportName("Valid_1"), |
| 320 | ReportParams().reportName("Valid_1/Valid_2"), |
| 321 | ReportParams().reportName("Valid_1/Valid_2/Valid_3"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 322 | |
| 323 | TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName) |
| 324 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 325 | EXPECT_NO_THROW(makeReport(GetParam())); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | class TestReportInvalidNames : |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 329 | public TestReport, |
| 330 | public WithParamInterface<ReportParams> |
| 331 | { |
| 332 | public: |
| 333 | void SetUp() override |
| 334 | {} |
| 335 | }; |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 336 | |
| 337 | INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidNames, |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 338 | Values(ReportParams().reportName("/"), |
| 339 | ReportParams().reportName("/Invalid"), |
| 340 | ReportParams().reportName("Invalid/"), |
| 341 | ReportParams().reportName("Invalid/Invalid/"), |
| 342 | ReportParams().reportName("Invalid?"))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 343 | |
| 344 | TEST_P(TestReportInvalidNames, reportCtorThrowOnInvalidName) |
| 345 | { |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 346 | EXPECT_THROW(makeReport(GetParam()), sdbusplus::exception::SdBusError); |
| 347 | } |
| 348 | |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 349 | TEST_F(TestReportInvalidNames, reportCtorThrowOnInvalidNameAndNoStoreIsCalled) |
| 350 | { |
| 351 | EXPECT_CALL(storageMock, store).Times(0); |
| 352 | EXPECT_THROW(makeReport(ReportParams().reportName("/Invalid")), |
| 353 | sdbusplus::exception::SdBusError); |
| 354 | } |
| 355 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 356 | class TestReportAllReportTypes : |
| 357 | public TestReport, |
| 358 | public WithParamInterface<ReportParams> |
| 359 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 360 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 361 | void SetUp() override |
| 362 | { |
| 363 | sut = makeReport(GetParam()); |
| 364 | } |
| 365 | }; |
| 366 | |
| 367 | INSTANTIATE_TEST_SUITE_P(_, TestReportAllReportTypes, |
| 368 | Values(ReportParams().reportingType("OnRequest"), |
| 369 | ReportParams().reportingType("OnChange"), |
| 370 | ReportParams().reportingType("Periodic"))); |
| 371 | |
| 372 | TEST_P(TestReportAllReportTypes, returnPropertValueOfReportType) |
| 373 | { |
| 374 | EXPECT_THAT(getProperty<std::string>(sut->getPath(), "ReportingType"), |
| 375 | Eq(GetParam().reportingType())); |
| 376 | } |
| 377 | |
Krzysztof Grobelny | f32f6fe | 2020-10-30 13:51:58 +0100 | [diff] [blame] | 378 | class TestReportOnRequestType : public TestReport |
| 379 | { |
| 380 | void SetUp() override |
| 381 | { |
| 382 | sut = makeReport(ReportParams().reportingType("OnRequest")); |
| 383 | } |
| 384 | }; |
| 385 | |
| 386 | TEST_F(TestReportOnRequestType, updatesReadingTimestamp) |
| 387 | { |
| 388 | const uint64_t expectedTime = std::time(0); |
| 389 | |
| 390 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 391 | |
| 392 | const auto [timestamp, readings] = |
| 393 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 394 | |
| 395 | EXPECT_THAT(timestamp, Ge(expectedTime)); |
| 396 | } |
| 397 | |
| 398 | TEST_F(TestReportOnRequestType, updatesReadingWhenUpdateIsCalled) |
| 399 | { |
| 400 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 401 | |
| 402 | const auto [timestamp, readings] = |
| 403 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 404 | |
| 405 | EXPECT_THAT(readings, |
| 406 | ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 407 | std::make_tuple("aaa"s, "bbb"s, 21.7, 100u), |
| 408 | std::make_tuple("aa"s, "bb"s, 42.0, 74u))); |
| 409 | } |
| 410 | |
| 411 | class TestReportNonOnRequestType : |
| 412 | public TestReport, |
| 413 | public WithParamInterface<ReportParams> |
| 414 | { |
| 415 | void SetUp() override |
| 416 | { |
| 417 | sut = makeReport(GetParam()); |
| 418 | } |
| 419 | }; |
| 420 | |
| 421 | INSTANTIATE_TEST_SUITE_P(_, TestReportNonOnRequestType, |
| 422 | Values(ReportParams().reportingType("Periodic"), |
| 423 | ReportParams().reportingType("OnChange"))); |
| 424 | |
| 425 | TEST_P(TestReportNonOnRequestType, readingsAreNotUpdateOnUpdateCall) |
| 426 | { |
| 427 | ASSERT_THAT(update(sut->getPath()), Eq(boost::system::errc::success)); |
| 428 | |
| 429 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 430 | Eq(Readings{})); |
| 431 | } |
| 432 | |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 433 | class TestReportNonPeriodicReport : |
| 434 | public TestReport, |
| 435 | public WithParamInterface<ReportParams> |
| 436 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 437 | public: |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 438 | void SetUp() override |
| 439 | { |
| 440 | sut = makeReport(GetParam()); |
| 441 | } |
| 442 | }; |
| 443 | |
| 444 | INSTANTIATE_TEST_SUITE_P(_, TestReportNonPeriodicReport, |
| 445 | Values(ReportParams().reportingType("OnRequest"), |
| 446 | ReportParams().reportingType("OnChange"))); |
| 447 | |
| 448 | TEST_P(TestReportNonPeriodicReport, readingsAreNotUpdatedAfterIntervalExpires) |
| 449 | { |
| 450 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 451 | |
| 452 | EXPECT_THAT(getProperty<Readings>(sut->getPath(), "Readings"), |
| 453 | Eq(Readings{})); |
| 454 | } |
| 455 | |
| 456 | class TestReportPeriodicReport : public TestReport |
| 457 | { |
| 458 | void SetUp() override |
| 459 | { |
| 460 | sut = makeReport(ReportParams().reportingType("Periodic")); |
Krzysztof Grobelny | c8e3a64 | 2020-10-23 12:29:16 +0200 | [diff] [blame] | 461 | } |
| 462 | }; |
| 463 | |
| 464 | TEST_F(TestReportPeriodicReport, readingTimestampIsUpdatedAfterIntervalExpires) |
| 465 | { |
| 466 | const uint64_t expectedTime = std::time(0); |
| 467 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 468 | |
| 469 | const auto [timestamp, readings] = |
| 470 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 471 | |
| 472 | EXPECT_THAT(timestamp, Ge(expectedTime)); |
| 473 | } |
| 474 | |
| 475 | TEST_F(TestReportPeriodicReport, readingsAreUpdatedAfterIntervalExpires) |
| 476 | { |
| 477 | DbusEnvironment::sleepFor(ReportManager::minInterval + 1ms); |
| 478 | |
| 479 | const auto [timestamp, readings] = |
| 480 | getProperty<Readings>(sut->getPath(), "Readings"); |
| 481 | |
| 482 | EXPECT_THAT(readings, |
| 483 | ElementsAre(std::make_tuple("a"s, "b"s, 17.1, 114u), |
| 484 | std::make_tuple("aaa"s, "bbb"s, 21.7, 100u), |
| 485 | std::make_tuple("aa"s, "bb"s, 42.0, 74u))); |
Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame] | 486 | } |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 487 | |
| 488 | class TestReportInitialization : public TestReport |
| 489 | { |
| 490 | public: |
| 491 | void SetUp() override |
| 492 | {} |
| 493 | |
| 494 | void monitorProc(sdbusplus::message::message& msg) |
| 495 | { |
| 496 | std::string iface; |
| 497 | std::vector<std::pair<std::string, std::variant<Readings>>> |
| 498 | changed_properties; |
| 499 | std::vector<std::string> invalidated_properties; |
| 500 | |
| 501 | msg.read(iface, changed_properties, invalidated_properties); |
| 502 | |
| 503 | if (iface == Report::reportIfaceName) |
| 504 | { |
| 505 | for (const auto& [name, value] : changed_properties) |
| 506 | { |
| 507 | if (name == "Readings") |
| 508 | { |
| 509 | readingsUpdated.Call(); |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void makeMonitor() |
| 516 | { |
| 517 | monitor = std::make_unique<sdbusplus::bus::match::match>( |
| 518 | *DbusEnvironment::getBus(), |
| 519 | sdbusplus::bus::match::rules::propertiesChanged( |
| 520 | sut->getPath(), Report::reportIfaceName), |
| 521 | [this](auto& msg) { monitorProc(msg); }); |
| 522 | } |
| 523 | |
| 524 | std::unique_ptr<sdbusplus::bus::match::match> monitor; |
| 525 | MockFunction<void()> readingsUpdated; |
| 526 | }; |
| 527 | |
Krzysztof Grobelny | 6ccfcbf | 2020-11-04 09:31:36 +0100 | [diff] [blame] | 528 | TEST_F(TestReportInitialization, metricsAreInitializedWhenConstructed) |
| 529 | { |
| 530 | for (auto& metric : metricMocks) |
| 531 | { |
| 532 | EXPECT_CALL(*metric, initialize()); |
| 533 | } |
| 534 | |
| 535 | sut = makeReport(ReportParams()); |
| 536 | } |
| 537 | |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 538 | TEST_F(TestReportInitialization, |
| 539 | emitReadingsUpdateIsTrueReadingsPropertiesChangedSingalEmits) |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 540 | { |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 541 | EXPECT_CALL(readingsUpdated, Call()) |
| 542 | .WillOnce( |
| 543 | InvokeWithoutArgs(DbusEnvironment::setPromise("readingsUpdated"))); |
| 544 | |
| 545 | const auto elapsed = DbusEnvironment::measureTime([this] { |
| 546 | sut = makeReport( |
| 547 | defaultParams.reportingType("Periodic").emitReadingUpdate(true)); |
| 548 | makeMonitor(); |
| 549 | EXPECT_TRUE(DbusEnvironment::waitForFuture("readingsUpdated")); |
| 550 | }); |
| 551 | |
| 552 | EXPECT_THAT(elapsed, AllOf(Ge(defaultParams.interval()), |
| 553 | Lt(defaultParams.interval() * 2))); |
| 554 | } |
| 555 | |
| 556 | TEST_F(TestReportInitialization, |
| 557 | emitReadingsUpdateIsFalseReadingsPropertiesChangesSigalDoesNotEmits) |
| 558 | { |
| 559 | EXPECT_CALL(readingsUpdated, Call()).Times(0); |
| 560 | |
| 561 | sut = makeReport( |
| 562 | defaultParams.reportingType("Periodic").emitReadingUpdate(false)); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 563 | makeMonitor(); |
Wludzik, Jozef | b1ff1f6 | 2020-10-23 13:20:52 +0200 | [diff] [blame] | 564 | DbusEnvironment::sleepFor(defaultParams.interval() * 2); |
Wludzik, Jozef | e236279 | 2020-10-27 17:23:55 +0100 | [diff] [blame] | 565 | } |