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