Wludzik, Jozef | 2f9f9b8 | 2020-10-13 09:07:45 +0200 | [diff] [blame^] | 1 | #include "dbus_environment.hpp" |
| 2 | #include "mocks/report_manager_mock.hpp" |
| 3 | #include "report.hpp" |
| 4 | #include "report_manager.hpp" |
| 5 | |
| 6 | #include <sdbusplus/exception.hpp> |
| 7 | |
| 8 | using namespace testing; |
| 9 | using namespace std::literals::string_literals; |
| 10 | |
| 11 | class TestReport : public Test |
| 12 | { |
| 13 | public: |
| 14 | std::string defaultReportName = "TestReport"; |
| 15 | std::string defaultReportType = "Periodic"; |
| 16 | bool defaultEmitReadingSignal = true; |
| 17 | bool defaultLogToMetricReportCollection = true; |
| 18 | uint64_t defaultInterval = ReportManager::minInterval.count(); |
| 19 | ReadingParameters defaultReadingParams = {}; |
| 20 | |
| 21 | std::unique_ptr<ReportManagerMock> reportManagerMock = |
| 22 | std::make_unique<StrictMock<ReportManagerMock>>(); |
| 23 | Report sut = |
| 24 | Report(DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(), |
| 25 | defaultReportName, defaultReportType, defaultEmitReadingSignal, |
| 26 | defaultLogToMetricReportCollection, |
| 27 | std::chrono::milliseconds{defaultInterval}, defaultReadingParams, |
| 28 | *reportManagerMock); |
| 29 | |
| 30 | template <class T> |
| 31 | static T getProperty(const std::string& path, const std::string& property) |
| 32 | { |
| 33 | std::promise<T> propertyPromise; |
| 34 | sdbusplus::asio::getProperty<T>( |
| 35 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path, |
| 36 | Report::reportIfaceName, property, |
| 37 | [&propertyPromise](boost::system::error_code ec) { |
| 38 | EXPECT_THAT(static_cast<bool>(ec), ::testing::Eq(false)); |
| 39 | propertyPromise.set_value(T{}); |
| 40 | }, |
| 41 | [&propertyPromise](T t) { propertyPromise.set_value(t); }); |
| 42 | return DbusEnvironment::waitForFuture(propertyPromise.get_future()) |
| 43 | .value_or(T{}); |
| 44 | } |
| 45 | |
| 46 | template <class T> |
| 47 | static boost::system::error_code setProperty(const std::string& path, |
| 48 | const std::string& property, |
| 49 | const T& newValue) |
| 50 | { |
| 51 | std::promise<boost::system::error_code> setPromise; |
| 52 | sdbusplus::asio::setProperty( |
| 53 | *DbusEnvironment::getBus(), DbusEnvironment::serviceName(), path, |
| 54 | Report::reportIfaceName, property, std::move(newValue), |
| 55 | [&setPromise](boost::system::error_code ec) { |
| 56 | setPromise.set_value(ec); |
| 57 | }, |
| 58 | [&setPromise]() { |
| 59 | setPromise.set_value(boost::system::error_code{}); |
| 60 | }); |
| 61 | return DbusEnvironment::waitForFuture(setPromise.get_future()) |
| 62 | .value_or(boost::system::error_code{}); |
| 63 | } |
| 64 | |
| 65 | boost::system::error_code deleteReport(const std::string& path) |
| 66 | { |
| 67 | std::promise<boost::system::error_code> deleteReportPromise; |
| 68 | DbusEnvironment::getBus()->async_method_call( |
| 69 | [&deleteReportPromise](boost::system::error_code ec) { |
| 70 | deleteReportPromise.set_value(ec); |
| 71 | }, |
| 72 | DbusEnvironment::serviceName(), path, Report::deleteIfaceName, |
| 73 | "Delete"); |
| 74 | return DbusEnvironment::waitForFuture(deleteReportPromise.get_future()) |
| 75 | .value_or(boost::system::error_code{}); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | TEST_F(TestReport, verifyIfPropertiesHaveValidValue) |
| 80 | { |
| 81 | EXPECT_THAT(getProperty<uint64_t>(sut.getPath(), "Interval"), |
| 82 | Eq(defaultInterval)); |
| 83 | EXPECT_THAT(getProperty<bool>(sut.getPath(), "Persistency"), Eq(false)); |
| 84 | EXPECT_THAT(getProperty<std::string>(sut.getPath(), "ReportingType"), |
| 85 | Eq(defaultReportType)); |
| 86 | EXPECT_THAT(getProperty<bool>(sut.getPath(), "EmitsReadingsUpdate"), |
| 87 | Eq(defaultEmitReadingSignal)); |
| 88 | EXPECT_THAT( |
| 89 | getProperty<bool>(sut.getPath(), "LogToMetricReportsCollection"), |
| 90 | Eq(defaultLogToMetricReportCollection)); |
| 91 | EXPECT_THAT( |
| 92 | getProperty<ReadingParameters>(sut.getPath(), "ReadingParameters"), |
| 93 | Eq(defaultReadingParams)); |
| 94 | } |
| 95 | |
| 96 | TEST_F(TestReport, setIntervalWithValidValue) |
| 97 | { |
| 98 | uint64_t newValue = defaultInterval + 1; |
| 99 | EXPECT_THAT(setProperty(sut.getPath(), "Interval", newValue).value(), |
| 100 | Eq(boost::system::errc::success)); |
| 101 | EXPECT_THAT(getProperty<uint64_t>(sut.getPath(), "Interval"), Eq(newValue)); |
| 102 | } |
| 103 | |
| 104 | TEST_F(TestReport, settingIntervalWithInvalidValueDoesNotChangeProperty) |
| 105 | { |
| 106 | uint64_t newValue = defaultInterval - 1; |
| 107 | EXPECT_THAT(setProperty(sut.getPath(), "Interval", newValue).value(), |
| 108 | Eq(boost::system::errc::success)); |
| 109 | EXPECT_THAT(getProperty<uint64_t>(sut.getPath(), "Interval"), |
| 110 | Eq(defaultInterval)); |
| 111 | } |
| 112 | |
| 113 | TEST_F(TestReport, deleteReport) |
| 114 | { |
| 115 | EXPECT_CALL(*reportManagerMock, removeReport(&sut)); |
| 116 | auto ec = deleteReport(sut.getPath()); |
| 117 | EXPECT_THAT(ec, Eq(boost::system::errc::success)); |
| 118 | } |
| 119 | |
| 120 | TEST_F(TestReport, deletingNonExistingReportReturnInvalidRequestDescriptor) |
| 121 | { |
| 122 | auto ec = deleteReport(Report::reportDir + "NonExisting"s); |
| 123 | EXPECT_THAT(ec.value(), Eq(EBADR)); |
| 124 | } |
| 125 | |
| 126 | class TestReportCreation : public Test |
| 127 | { |
| 128 | public: |
| 129 | std::unique_ptr<ReportManagerMock> reportManagerMock = |
| 130 | std::make_unique<StrictMock<ReportManagerMock>>(); |
| 131 | |
| 132 | std::unique_ptr<Report> createReportWithName(std::string name) |
| 133 | { |
| 134 | return std::make_unique<Report>( |
| 135 | DbusEnvironment::getIoc(), DbusEnvironment::getObjServer(), name, |
| 136 | "", true, true, |
| 137 | std::chrono::milliseconds{ReportManager::minInterval.count()}, |
| 138 | ReadingParameters{}, *reportManagerMock); |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | class TestReportValidNames : |
| 143 | public TestReportCreation, |
| 144 | public WithParamInterface<const char*> |
| 145 | {}; |
| 146 | |
| 147 | INSTANTIATE_TEST_SUITE_P(ValidNames, TestReportValidNames, |
| 148 | ValuesIn({"Valid_1", "Valid_1/Valid_2", |
| 149 | "Valid_1/Valid_2/Valid_3"})); |
| 150 | |
| 151 | TEST_P(TestReportValidNames, reportCtorDoesNotThrowOnValidName) |
| 152 | { |
| 153 | EXPECT_NO_THROW(createReportWithName(GetParam())); |
| 154 | } |
| 155 | |
| 156 | class TestReportInvalidNames : |
| 157 | public TestReportCreation, |
| 158 | public WithParamInterface<const char*> |
| 159 | {}; |
| 160 | |
| 161 | INSTANTIATE_TEST_SUITE_P(InvalidNames, TestReportInvalidNames, |
| 162 | ValuesIn({"/", "/Invalid", "Invalid/", |
| 163 | "Invalid/Invalid/", "Invalid?"})); |
| 164 | |
| 165 | TEST_P(TestReportInvalidNames, reportCtorThrowOnInvalidName) |
| 166 | { |
| 167 | EXPECT_THROW(createReportWithName(GetParam()), |
| 168 | sdbusplus::exception::SdBusError); |
| 169 | } |