Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame^] | 1 | #include "fan_pwm.hpp" |
| 2 | |
| 3 | #include "hwmonio_mock.hpp" |
| 4 | |
| 5 | #include <gmock/gmock.h> |
| 6 | #include <gtest/gtest.h> |
| 7 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 8 | #include <string> |
| 9 | |
| 10 | using ::testing::_; |
| 11 | using ::testing::Invoke; |
| 12 | using ::testing::IsNull; |
| 13 | using ::testing::NotNull; |
| 14 | using ::testing::Return; |
| 15 | using ::testing::StrEq; |
| 16 | |
| 17 | static auto FanPwmIntf = "xyz.openbmc_project.Control.FanPwm"; |
| 18 | static auto FanPwmProp = "Target"; |
| 19 | |
| 20 | // Handle basic expectations we'll need for all these tests, if it's found that |
| 21 | // this is helpful for more tests, it can be promoted in scope. |
| 22 | void SetupDbusObject( |
| 23 | sdbusplus::SdBusMock *sdbus_mock, |
| 24 | const std::string& path, |
| 25 | const std::string& intf, |
| 26 | const std::string property = "") |
| 27 | { |
| 28 | EXPECT_CALL(*sdbus_mock, |
| 29 | sd_bus_add_object_vtable( |
| 30 | IsNull(), |
| 31 | NotNull(), |
| 32 | StrEq(path), |
| 33 | StrEq(intf), |
| 34 | NotNull(), |
| 35 | NotNull())) |
| 36 | .WillOnce(Return(0)); |
| 37 | |
| 38 | if (property.empty()) |
| 39 | { |
| 40 | EXPECT_CALL(*sdbus_mock, |
| 41 | sd_bus_emit_properties_changed_strv( |
| 42 | IsNull(), |
| 43 | StrEq(path), |
| 44 | StrEq(intf), |
| 45 | NotNull())) |
| 46 | .WillOnce(Return(0)); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | EXPECT_CALL(*sdbus_mock, |
| 51 | sd_bus_emit_properties_changed_strv( |
| 52 | IsNull(), |
| 53 | StrEq(path), |
| 54 | StrEq(intf), |
| 55 | NotNull())) |
| 56 | .WillOnce( |
| 57 | Invoke([=](sd_bus *bus, |
| 58 | const char *path, |
| 59 | const char *interface, |
| 60 | char **names) { |
| 61 | EXPECT_STREQ(property.c_str(), names[0]); |
| 62 | return 0; |
| 63 | }) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | TEST(FanPwmTest, BasicConstructorDeferredTest) { |
| 71 | // Attempt to just instantiate one. |
| 72 | |
| 73 | // NOTE: This test's goal is to figure out what's minimally required to |
| 74 | // mock to instantiate this object. |
| 75 | sdbusplus::SdBusMock sdbus_mock; |
| 76 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 77 | |
| 78 | std::string instancePath = ""; |
| 79 | std::string devPath = ""; |
| 80 | std::string id = ""; |
| 81 | std::string objPath = "asdf"; |
| 82 | bool defer = true; |
| 83 | uint64_t target = 0x01; |
| 84 | |
| 85 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 86 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 87 | |
| 88 | SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp); |
| 89 | |
| 90 | hwmon::FanPwm f(std::move(hwmonio_mock), |
| 91 | devPath, |
| 92 | id, |
| 93 | bus_mock, |
| 94 | objPath.c_str(), |
| 95 | defer, |
| 96 | target); |
| 97 | } |
| 98 | |
| 99 | TEST(FanPwmTest, BasicConstructorNotDeferredTest) { |
| 100 | // Attempt to just instantiate one. |
| 101 | |
| 102 | // NOTE: This test's goal is to figure out what's minimally required to |
| 103 | // mock to instantiate this object. |
| 104 | sdbusplus::SdBusMock sdbus_mock; |
| 105 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 106 | |
| 107 | std::string instancePath = ""; |
| 108 | std::string devPath = ""; |
| 109 | std::string id = ""; |
| 110 | std::string objPath = "asdf"; |
| 111 | bool defer = false; |
| 112 | uint64_t target = 0x01; |
| 113 | |
| 114 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 115 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 116 | |
| 117 | SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp); |
| 118 | |
| 119 | EXPECT_CALL(sdbus_mock, |
| 120 | sd_bus_emit_object_added(IsNull(), StrEq("asdf"))) |
| 121 | .WillOnce(Return(0)); |
| 122 | |
| 123 | EXPECT_CALL(sdbus_mock, |
| 124 | sd_bus_emit_object_removed(IsNull(), StrEq("asdf"))) |
| 125 | .WillOnce(Return(0)); |
| 126 | |
| 127 | hwmon::FanPwm f(std::move(hwmonio_mock), |
| 128 | devPath, |
| 129 | id, |
| 130 | bus_mock, |
| 131 | objPath.c_str(), |
| 132 | defer, |
| 133 | target); |
| 134 | } |
| 135 | |
| 136 | TEST(FanPwmTest, WriteTargetValue) { |
| 137 | // Create a FanPwm and write a value to the object. |
| 138 | |
| 139 | sdbusplus::SdBusMock sdbus_mock; |
| 140 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 141 | |
| 142 | std::string instancePath = ""; |
| 143 | std::string devPath = "devp"; |
| 144 | std::string id = "the_id"; |
| 145 | std::string objPath = "asdf"; |
| 146 | bool defer = true; |
| 147 | uint64_t target = 0x01; |
| 148 | |
| 149 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 150 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 151 | |
| 152 | SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp); |
| 153 | |
| 154 | hwmonio::HwmonIOMock *hwmonio = |
| 155 | reinterpret_cast<hwmonio::HwmonIOMock *>(hwmonio_mock.get()); |
| 156 | |
| 157 | hwmon::FanPwm f(std::move(hwmonio_mock), |
| 158 | devPath, |
| 159 | id, |
| 160 | bus_mock, |
| 161 | objPath.c_str(), |
| 162 | defer, |
| 163 | target); |
| 164 | |
| 165 | target = 0x64; |
| 166 | |
| 167 | EXPECT_CALL(*hwmonio, write(static_cast<uint32_t>(target), |
| 168 | StrEq("pwm"), |
| 169 | StrEq("the_id"), |
| 170 | _, |
| 171 | hwmonio::retries, |
| 172 | hwmonio::delay)); |
| 173 | |
| 174 | EXPECT_CALL(sdbus_mock, |
| 175 | sd_bus_emit_properties_changed_strv( |
| 176 | IsNull(), |
| 177 | StrEq("asdf"), |
| 178 | StrEq(FanPwmIntf), |
| 179 | NotNull())) |
| 180 | .WillOnce( |
| 181 | Invoke([&](sd_bus *bus, |
| 182 | const char *path, |
| 183 | const char *interface, |
| 184 | char **names) { |
| 185 | EXPECT_EQ(0, strncmp("Target", names[0], 6)); |
| 186 | return 0; |
| 187 | }) |
| 188 | ); |
| 189 | |
| 190 | EXPECT_EQ(target, f.target(target)); |
| 191 | } |
| 192 | |
| 193 | TEST(FanPwmTest, WriteTargetValueNoUpdate) { |
| 194 | // Create a FanPwm and write a value to the object that was the previous |
| 195 | // value. |
| 196 | |
| 197 | sdbusplus::SdBusMock sdbus_mock; |
| 198 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 199 | |
| 200 | std::string instancePath = ""; |
| 201 | std::string devPath = "devp"; |
| 202 | std::string id = "the_id"; |
| 203 | std::string objPath = "asdf"; |
| 204 | bool defer = true; |
| 205 | uint64_t target = 0x01; |
| 206 | |
| 207 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 208 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 209 | |
| 210 | SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp); |
| 211 | |
| 212 | hwmon::FanPwm f(std::move(hwmonio_mock), |
| 213 | devPath, |
| 214 | id, |
| 215 | bus_mock, |
| 216 | objPath.c_str(), |
| 217 | defer, |
| 218 | target); |
| 219 | |
| 220 | EXPECT_EQ(target, f.target(target)); |
| 221 | } |