Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 16 | #include "action.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 17 | #include "chassis.hpp" |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 18 | #include "configuration.hpp" |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 19 | #include "device.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 20 | #include "i2c_interface.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 21 | #include "id_map.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 22 | #include "journal.hpp" |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 23 | #include "mock_action.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 24 | #include "mock_journal.hpp" |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame^] | 25 | #include "mock_services.hpp" |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 26 | #include "mocked_i2c_interface.hpp" |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 27 | #include "pmbus_read_sensor_action.hpp" |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 28 | #include "presence_detection.hpp" |
| 29 | #include "rail.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 30 | #include "rule.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 31 | #include "system.hpp" |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 32 | #include "test_utils.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 33 | |
| 34 | #include <memory> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 35 | #include <optional> |
Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 36 | #include <string> |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 37 | #include <utility> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 38 | #include <vector> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 39 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 40 | #include <gmock/gmock.h> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 41 | #include <gtest/gtest.h> |
| 42 | |
| 43 | using namespace phosphor::power::regulators; |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 44 | using namespace phosphor::power::regulators::test_utils; |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 45 | |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 46 | using ::testing::A; |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 47 | using ::testing::Return; |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 48 | using ::testing::Throw; |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 49 | using ::testing::TypedEq; |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 50 | |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 51 | TEST(DeviceTests, Constructor) |
| 52 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 53 | // Test where only required parameters are specified |
| 54 | { |
| 55 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 56 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 57 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 58 | std::move(i2cInterface)}; |
| 59 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 60 | EXPECT_EQ(device.isRegulator(), true); |
| 61 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); |
| 62 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 63 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 64 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 65 | EXPECT_EQ(device.getRails().size(), 0); |
| 66 | } |
| 67 | |
| 68 | // Test where all parameters are specified |
| 69 | { |
| 70 | // Create I2CInterface |
| 71 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 72 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 73 | |
| 74 | // Create PresenceDetection |
| 75 | std::vector<std::unique_ptr<Action>> actions{}; |
| 76 | actions.push_back(std::make_unique<MockAction>()); |
| 77 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 78 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 79 | |
| 80 | // Create Configuration |
| 81 | std::optional<double> volts{}; |
| 82 | actions.clear(); |
| 83 | actions.push_back(std::make_unique<MockAction>()); |
| 84 | actions.push_back(std::make_unique<MockAction>()); |
| 85 | std::unique_ptr<Configuration> configuration = |
| 86 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 87 | |
| 88 | // Create vector of Rail objects |
| 89 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 90 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 91 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 92 | |
| 93 | // Create Device |
| 94 | Device device{"vdd_reg", |
| 95 | false, |
| 96 | "/system/chassis/motherboard/reg1", |
| 97 | std::move(i2cInterface), |
| 98 | std::move(presenceDetection), |
| 99 | std::move(configuration), |
| 100 | std::move(rails)}; |
| 101 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 102 | EXPECT_EQ(device.isRegulator(), false); |
| 103 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg1"); |
| 104 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 105 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 106 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 107 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 108 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), false); |
| 109 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 110 | EXPECT_EQ(device.getRails().size(), 2); |
| 111 | } |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 114 | TEST(DeviceTests, AddToIDMap) |
| 115 | { |
| 116 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 117 | std::unique_ptr<Configuration> configuration{}; |
| 118 | |
| 119 | // Create vector of Rail objects |
| 120 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 121 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 122 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 123 | |
| 124 | // Create Device |
| 125 | Device device{"vdd_reg", |
| 126 | false, |
| 127 | "/system/chassis/motherboard/reg2", |
| 128 | std::move(createI2CInterface()), |
| 129 | std::move(presenceDetection), |
| 130 | std::move(configuration), |
| 131 | std::move(rails)}; |
| 132 | |
| 133 | // Add Device and Rail objects to an IDMap |
| 134 | IDMap idMap{}; |
| 135 | device.addToIDMap(idMap); |
| 136 | |
| 137 | // Verify Device is in the IDMap |
| 138 | EXPECT_NO_THROW(idMap.getDevice("vdd_reg")); |
| 139 | EXPECT_THROW(idMap.getDevice("vio_reg"), std::invalid_argument); |
| 140 | |
| 141 | // Verify all Rails are in the IDMap |
| 142 | EXPECT_NO_THROW(idMap.getRail("vdd0")); |
| 143 | EXPECT_NO_THROW(idMap.getRail("vdd1")); |
| 144 | EXPECT_THROW(idMap.getRail("vdd2"), std::invalid_argument); |
| 145 | } |
| 146 | |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 147 | TEST(DeviceTests, Close) |
| 148 | { |
| 149 | // Test where works: I2C interface is not open |
| 150 | { |
| 151 | // Create mock I2CInterface |
| 152 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 153 | std::make_unique<i2c::MockedI2CInterface>(); |
| 154 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(false)); |
| 155 | EXPECT_CALL(*i2cInterface, close).Times(0); |
| 156 | |
| 157 | // Create Device |
| 158 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 159 | std::move(i2cInterface)}; |
| 160 | |
| 161 | // Close Device |
| 162 | journal::clear(); |
| 163 | device.close(); |
| 164 | EXPECT_EQ(journal::getErrMessages().size(), 0); |
| 165 | } |
| 166 | |
| 167 | // Test where works: I2C interface is open |
| 168 | { |
| 169 | // Create mock I2CInterface |
| 170 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 171 | std::make_unique<i2c::MockedI2CInterface>(); |
| 172 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 173 | EXPECT_CALL(*i2cInterface, close).Times(1); |
| 174 | |
| 175 | // Create Device |
| 176 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 177 | std::move(i2cInterface)}; |
| 178 | |
| 179 | // Close Device |
| 180 | journal::clear(); |
| 181 | device.close(); |
| 182 | EXPECT_EQ(journal::getErrMessages().size(), 0); |
| 183 | } |
| 184 | |
| 185 | // Test where fails: closing I2C interface fails |
| 186 | { |
| 187 | // Create mock I2CInterface |
| 188 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 189 | std::make_unique<i2c::MockedI2CInterface>(); |
| 190 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 191 | EXPECT_CALL(*i2cInterface, close) |
| 192 | .Times(1) |
| 193 | .WillOnce(Throw( |
| 194 | i2c::I2CException{"Failed to close", "/dev/i2c-1", 0x70})); |
| 195 | |
| 196 | // Create Device |
| 197 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 198 | std::move(i2cInterface)}; |
| 199 | |
| 200 | // Close Device |
| 201 | journal::clear(); |
| 202 | device.close(); |
| 203 | std::vector<std::string> expectedErrMessages{ |
| 204 | "I2CException: Failed to close: bus /dev/i2c-1, addr 0x70", |
| 205 | "Unable to close device vdd_reg"}; |
| 206 | EXPECT_EQ(journal::getErrMessages(), expectedErrMessages); |
| 207 | } |
| 208 | } |
| 209 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 210 | TEST(DeviceTests, Configure) |
| 211 | { |
| 212 | // Test where Configuration and Rails were not specified in constructor |
| 213 | { |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame^] | 214 | // Create mock services. |
| 215 | MockServices services{}; |
| 216 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 217 | // Create Device |
| 218 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 219 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 220 | "reg1", true, "/system/chassis/motherboard/reg1", |
| 221 | std::move(i2cInterface)); |
| 222 | Device* devicePtr = device.get(); |
| 223 | |
| 224 | // Create Chassis that contains Device |
| 225 | std::vector<std::unique_ptr<Device>> devices{}; |
| 226 | devices.emplace_back(std::move(device)); |
| 227 | std::unique_ptr<Chassis> chassis = |
| 228 | std::make_unique<Chassis>(1, std::move(devices)); |
| 229 | Chassis* chassisPtr = chassis.get(); |
| 230 | |
| 231 | // Create System that contains Chassis |
| 232 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 233 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 234 | chassisVec.emplace_back(std::move(chassis)); |
| 235 | System system{std::move(rules), std::move(chassisVec)}; |
| 236 | |
| 237 | // Call configure(). Should do nothing. |
| 238 | journal::clear(); |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame^] | 239 | devicePtr->configure(services, system, *chassisPtr); |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 240 | EXPECT_EQ(journal::getDebugMessages().size(), 0); |
| 241 | EXPECT_EQ(journal::getErrMessages().size(), 0); |
| 242 | } |
| 243 | |
| 244 | // Test where Configuration and Rails were specified in constructor |
| 245 | { |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame^] | 246 | // Create mock services. |
| 247 | MockServices services{}; |
| 248 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 249 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 250 | |
| 251 | // Create Rail vdd0 |
| 252 | { |
| 253 | // Create Configuration for Rail |
| 254 | std::optional<double> volts{1.3}; |
| 255 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 256 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 257 | std::vector<std::unique_ptr<Action>> actions{}; |
| 258 | actions.emplace_back(std::move(action)); |
| 259 | std::unique_ptr<Configuration> configuration = |
| 260 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 261 | |
| 262 | // Create Rail |
| 263 | std::unique_ptr<Rail> rail = |
| 264 | std::make_unique<Rail>("vdd0", std::move(configuration)); |
| 265 | rails.emplace_back(std::move(rail)); |
| 266 | } |
| 267 | |
| 268 | // Create Rail vio0 |
| 269 | { |
| 270 | // Create Configuration for Rail |
| 271 | std::optional<double> volts{3.2}; |
| 272 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 273 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 274 | std::vector<std::unique_ptr<Action>> actions{}; |
| 275 | actions.emplace_back(std::move(action)); |
| 276 | std::unique_ptr<Configuration> configuration = |
| 277 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 278 | |
| 279 | // Create Rail |
| 280 | std::unique_ptr<Rail> rail = |
| 281 | std::make_unique<Rail>("vio0", std::move(configuration)); |
| 282 | rails.emplace_back(std::move(rail)); |
| 283 | } |
| 284 | |
| 285 | // Create Configuration for Device |
| 286 | std::optional<double> volts{}; |
| 287 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 288 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 289 | std::vector<std::unique_ptr<Action>> actions{}; |
| 290 | actions.emplace_back(std::move(action)); |
| 291 | std::unique_ptr<Configuration> configuration = |
| 292 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 293 | |
| 294 | // Create Device |
| 295 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 296 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 297 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 298 | "reg1", true, "/system/chassis/motherboard/reg1", |
| 299 | std::move(i2cInterface), std::move(presenceDetection), |
| 300 | std::move(configuration), std::move(rails)); |
| 301 | Device* devicePtr = device.get(); |
| 302 | |
| 303 | // Create Chassis that contains Device |
| 304 | std::vector<std::unique_ptr<Device>> devices{}; |
| 305 | devices.emplace_back(std::move(device)); |
| 306 | std::unique_ptr<Chassis> chassis = |
| 307 | std::make_unique<Chassis>(1, std::move(devices)); |
| 308 | Chassis* chassisPtr = chassis.get(); |
| 309 | |
| 310 | // Create System that contains Chassis |
| 311 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 312 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 313 | chassisVec.emplace_back(std::move(chassis)); |
| 314 | System system{std::move(rules), std::move(chassisVec)}; |
| 315 | |
| 316 | // Call configure(). For the Device and both Rails, should execute the |
| 317 | // Configuration and log a debug message. |
| 318 | journal::clear(); |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame^] | 319 | devicePtr->configure(services, system, *chassisPtr); |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 320 | std::vector<std::string> expectedDebugMessages{ |
Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 321 | "Configuring reg1", "Configuring vdd0: volts=1.300000", |
| 322 | "Configuring vio0: volts=3.200000"}; |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 323 | EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages); |
| 324 | EXPECT_EQ(journal::getErrMessages().size(), 0); |
| 325 | } |
| 326 | } |
| 327 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 328 | TEST(DeviceTests, GetConfiguration) |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 329 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 330 | // Test where Configuration was not specified in constructor |
| 331 | { |
| 332 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 333 | std::move(createI2CInterface())}; |
| 334 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 335 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 336 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 337 | // Test where Configuration was specified in constructor |
| 338 | { |
| 339 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 340 | |
| 341 | // Create Configuration |
| 342 | std::optional<double> volts{3.2}; |
| 343 | std::vector<std::unique_ptr<Action>> actions{}; |
| 344 | actions.push_back(std::make_unique<MockAction>()); |
| 345 | actions.push_back(std::make_unique<MockAction>()); |
| 346 | std::unique_ptr<Configuration> configuration = |
| 347 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 348 | |
| 349 | // Create Device |
| 350 | Device device{"vdd_reg", |
| 351 | true, |
| 352 | "/system/chassis/motherboard/reg2", |
| 353 | std::move(createI2CInterface()), |
| 354 | std::move(presenceDetection), |
| 355 | std::move(configuration)}; |
| 356 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 357 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true); |
| 358 | EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2); |
| 359 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 360 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | TEST(DeviceTests, GetFRU) |
| 364 | { |
| 365 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 366 | std::move(createI2CInterface())}; |
| 367 | EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2"); |
| 368 | } |
| 369 | |
| 370 | TEST(DeviceTests, GetI2CInterface) |
| 371 | { |
| 372 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 373 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 374 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 375 | std::move(i2cInterface)}; |
| 376 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 377 | } |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 378 | |
| 379 | TEST(DeviceTests, GetID) |
| 380 | { |
| 381 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 382 | std::move(createI2CInterface())}; |
| 383 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 384 | } |
| 385 | |
| 386 | TEST(DeviceTests, GetPresenceDetection) |
| 387 | { |
| 388 | // Test where PresenceDetection was not specified in constructor |
| 389 | { |
| 390 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 391 | std::move(createI2CInterface())}; |
| 392 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 393 | } |
| 394 | |
| 395 | // Test where PresenceDetection was specified in constructor |
| 396 | { |
| 397 | // Create PresenceDetection |
| 398 | std::vector<std::unique_ptr<Action>> actions{}; |
| 399 | actions.push_back(std::make_unique<MockAction>()); |
| 400 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 401 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 402 | |
| 403 | // Create Device |
| 404 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 405 | std::move(createI2CInterface()), |
| 406 | std::move(presenceDetection)}; |
| 407 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 408 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | TEST(DeviceTests, GetRails) |
| 413 | { |
| 414 | // Test where no rails were specified in constructor |
| 415 | { |
| 416 | Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2", |
| 417 | std::move(createI2CInterface())}; |
| 418 | EXPECT_EQ(device.getRails().size(), 0); |
| 419 | } |
| 420 | |
| 421 | // Test where rails were specified in constructor |
| 422 | { |
| 423 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 424 | std::unique_ptr<Configuration> configuration{}; |
| 425 | |
| 426 | // Create vector of Rail objects |
| 427 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 428 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 429 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 430 | |
| 431 | // Create Device |
| 432 | Device device{"vdd_reg", |
| 433 | false, |
| 434 | "/system/chassis/motherboard/reg2", |
| 435 | std::move(createI2CInterface()), |
| 436 | std::move(presenceDetection), |
| 437 | std::move(configuration), |
| 438 | std::move(rails)}; |
| 439 | EXPECT_EQ(device.getRails().size(), 2); |
| 440 | EXPECT_EQ(device.getRails()[0]->getID(), "vdd0"); |
| 441 | EXPECT_EQ(device.getRails()[1]->getID(), "vdd1"); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | TEST(DeviceTests, IsRegulator) |
| 446 | { |
| 447 | Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2", |
| 448 | std::move(createI2CInterface())}; |
| 449 | EXPECT_EQ(device.isRegulator(), false); |
| 450 | } |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 451 | |
| 452 | TEST(DeviceTests, MonitorSensors) |
| 453 | { |
| 454 | // Test where Rails were not specified in constructor |
| 455 | { |
| 456 | // Create mock I2CInterface. A two-byte read should NOT occur. |
| 457 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 458 | std::make_unique<i2c::MockedI2CInterface>(); |
| 459 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint16_t&>())).Times(0); |
| 460 | |
| 461 | // Create Device |
| 462 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 463 | "reg1", true, "/system/chassis/motherboard/reg1", |
| 464 | std::move(i2cInterface)); |
| 465 | Device* devicePtr = device.get(); |
| 466 | |
| 467 | // Create Chassis that contains Device |
| 468 | std::vector<std::unique_ptr<Device>> devices{}; |
| 469 | devices.emplace_back(std::move(device)); |
| 470 | std::unique_ptr<Chassis> chassis = |
| 471 | std::make_unique<Chassis>(1, std::move(devices)); |
| 472 | Chassis* chassisPtr = chassis.get(); |
| 473 | |
| 474 | // Create System that contains Chassis |
| 475 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 476 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 477 | chassisVec.emplace_back(std::move(chassis)); |
| 478 | System system{std::move(rules), std::move(chassisVec)}; |
| 479 | |
| 480 | // Call monitorSensors(). Should do nothing. |
| 481 | journal::clear(); |
| 482 | devicePtr->monitorSensors(system, *chassisPtr); |
| 483 | EXPECT_EQ(journal::getDebugMessages().size(), 0); |
| 484 | EXPECT_EQ(journal::getErrMessages().size(), 0); |
| 485 | } |
| 486 | |
| 487 | // Test where Rails were specified in constructor |
| 488 | { |
| 489 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 490 | |
| 491 | // Create PMBusReadSensorAction |
| 492 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 493 | uint8_t command = 0x8C; |
| 494 | pmbus_utils::SensorDataFormat format{ |
| 495 | pmbus_utils::SensorDataFormat::linear_11}; |
| 496 | std::optional<int8_t> exponent{}; |
| 497 | std::unique_ptr<PMBusReadSensorAction> action = |
| 498 | std::make_unique<PMBusReadSensorAction>(type, command, format, |
| 499 | exponent); |
| 500 | |
| 501 | // Create mock I2CInterface. A two-byte read should occur. |
| 502 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 503 | std::make_unique<i2c::MockedI2CInterface>(); |
| 504 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 505 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>())) |
| 506 | .Times(1); |
| 507 | |
| 508 | // Create SensorMonitoring |
| 509 | std::vector<std::unique_ptr<Action>> actions{}; |
| 510 | actions.emplace_back(std::move(action)); |
| 511 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 512 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 513 | |
| 514 | // Create Rail |
| 515 | std::unique_ptr<Configuration> configuration{}; |
| 516 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 517 | "vdd0", std::move(configuration), std::move(sensorMonitoring)); |
| 518 | rails.emplace_back(std::move(rail)); |
| 519 | |
| 520 | // Create Device |
| 521 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 522 | std::unique_ptr<Configuration> deviceConfiguration{}; |
| 523 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 524 | "reg1", true, "/system/chassis/motherboard/reg1", |
| 525 | std::move(i2cInterface), std::move(presenceDetection), |
| 526 | std::move(deviceConfiguration), std::move(rails)); |
| 527 | Device* devicePtr = device.get(); |
| 528 | |
| 529 | // Create Chassis that contains Device |
| 530 | std::vector<std::unique_ptr<Device>> devices{}; |
| 531 | devices.emplace_back(std::move(device)); |
| 532 | std::unique_ptr<Chassis> chassis = |
| 533 | std::make_unique<Chassis>(1, std::move(devices)); |
| 534 | Chassis* chassisPtr = chassis.get(); |
| 535 | |
| 536 | // Create System that contains Chassis |
| 537 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 538 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 539 | chassisVec.emplace_back(std::move(chassis)); |
| 540 | System system{std::move(rules), std::move(chassisVec)}; |
| 541 | |
| 542 | // Call monitorSensors(). |
| 543 | journal::clear(); |
| 544 | devicePtr->monitorSensors(system, *chassisPtr); |
| 545 | EXPECT_EQ(journal::getDebugMessages().size(), 0); |
| 546 | EXPECT_EQ(journal::getErrMessages().size(), 0); |
| 547 | } |
| 548 | } |