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 | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 22 | #include "mock_action.hpp" |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 23 | #include "mock_error_logging.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 24 | #include "mock_journal.hpp" |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 25 | #include "mock_sensors.hpp" |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 26 | #include "mock_services.hpp" |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 27 | #include "mocked_i2c_interface.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 | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 31 | #include "sensor_monitoring.hpp" |
Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 32 | #include "sensors.hpp" |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 33 | #include "system.hpp" |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 34 | #include "test_utils.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 35 | |
| 36 | #include <memory> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 37 | #include <optional> |
Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 38 | #include <string> |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 39 | #include <utility> |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 40 | #include <vector> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 41 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 42 | #include <gmock/gmock.h> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 43 | #include <gtest/gtest.h> |
| 44 | |
| 45 | using namespace phosphor::power::regulators; |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 46 | using namespace phosphor::power::regulators::test_utils; |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 47 | |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 48 | using ::testing::A; |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 49 | using ::testing::Ref; |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 50 | using ::testing::Return; |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 51 | using ::testing::Throw; |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 52 | using ::testing::TypedEq; |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 53 | |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 54 | static const std::string chassisInvPath{ |
| 55 | "/xyz/openbmc_project/inventory/system/chassis"}; |
| 56 | |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 57 | TEST(DeviceTests, Constructor) |
| 58 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 59 | // Test where only required parameters are specified |
| 60 | { |
| 61 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 62 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 63 | Device device{ |
| 64 | "vdd_reg", true, |
| 65 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 66 | std::move(i2cInterface)}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 67 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 68 | EXPECT_EQ(device.isRegulator(), true); |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 69 | EXPECT_EQ( |
| 70 | device.getFRU(), |
| 71 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2"); |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 72 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 73 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 74 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 75 | EXPECT_EQ(device.getRails().size(), 0); |
| 76 | } |
| 77 | |
| 78 | // Test where all parameters are specified |
| 79 | { |
| 80 | // Create I2CInterface |
| 81 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 82 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 83 | |
| 84 | // Create PresenceDetection |
| 85 | std::vector<std::unique_ptr<Action>> actions{}; |
| 86 | actions.push_back(std::make_unique<MockAction>()); |
| 87 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 88 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 89 | |
| 90 | // Create Configuration |
| 91 | std::optional<double> volts{}; |
| 92 | actions.clear(); |
| 93 | actions.push_back(std::make_unique<MockAction>()); |
| 94 | actions.push_back(std::make_unique<MockAction>()); |
| 95 | std::unique_ptr<Configuration> configuration = |
| 96 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 97 | |
| 98 | // Create vector of Rail objects |
| 99 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 100 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 101 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 102 | |
| 103 | // Create Device |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 104 | Device device{ |
| 105 | "vdd_reg", |
| 106 | false, |
| 107 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 108 | std::move(i2cInterface), |
| 109 | std::move(presenceDetection), |
| 110 | std::move(configuration), |
| 111 | std::move(rails)}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 112 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 113 | EXPECT_EQ(device.isRegulator(), false); |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 114 | EXPECT_EQ( |
| 115 | device.getFRU(), |
| 116 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1"); |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 117 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
| 118 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 119 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 120 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 121 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), false); |
| 122 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 123 | EXPECT_EQ(device.getRails().size(), 2); |
| 124 | } |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 127 | TEST(DeviceTests, AddToIDMap) |
| 128 | { |
| 129 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 130 | std::unique_ptr<Configuration> configuration{}; |
| 131 | |
| 132 | // Create vector of Rail objects |
| 133 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 134 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 135 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 136 | |
| 137 | // Create Device |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 138 | Device device{ |
| 139 | "vdd_reg", |
| 140 | false, |
| 141 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 142 | std::move(createI2CInterface()), |
| 143 | std::move(presenceDetection), |
| 144 | std::move(configuration), |
| 145 | std::move(rails)}; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 146 | |
| 147 | // Add Device and Rail objects to an IDMap |
| 148 | IDMap idMap{}; |
| 149 | device.addToIDMap(idMap); |
| 150 | |
| 151 | // Verify Device is in the IDMap |
| 152 | EXPECT_NO_THROW(idMap.getDevice("vdd_reg")); |
| 153 | EXPECT_THROW(idMap.getDevice("vio_reg"), std::invalid_argument); |
| 154 | |
| 155 | // Verify all Rails are in the IDMap |
| 156 | EXPECT_NO_THROW(idMap.getRail("vdd0")); |
| 157 | EXPECT_NO_THROW(idMap.getRail("vdd1")); |
| 158 | EXPECT_THROW(idMap.getRail("vdd2"), std::invalid_argument); |
| 159 | } |
| 160 | |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 161 | TEST(DeviceTests, ClearCache) |
| 162 | { |
| 163 | // Test where Device does not contain a PresenceDetection object |
| 164 | try |
| 165 | { |
| 166 | Device device{ |
| 167 | "vdd_reg", false, |
| 168 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 169 | std::move(createI2CInterface())}; |
| 170 | device.clearCache(); |
| 171 | } |
| 172 | catch (...) |
| 173 | { |
| 174 | ADD_FAILURE() << "Should not have caught exception."; |
| 175 | } |
| 176 | |
| 177 | // Test where Device contains a PresenceDetection object |
| 178 | { |
| 179 | // Create PresenceDetection |
| 180 | std::vector<std::unique_ptr<Action>> actions{}; |
| 181 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 182 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 183 | PresenceDetection* presenceDetectionPtr = presenceDetection.get(); |
| 184 | |
| 185 | // Create Device |
| 186 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 187 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 188 | "reg1", true, |
| 189 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 190 | std::move(i2cInterface), std::move(presenceDetection)); |
| 191 | Device* devicePtr = device.get(); |
| 192 | |
| 193 | // Create Chassis that contains Device |
| 194 | std::vector<std::unique_ptr<Device>> devices{}; |
| 195 | devices.emplace_back(std::move(device)); |
| 196 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 197 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 198 | Chassis* chassisPtr = chassis.get(); |
| 199 | |
| 200 | // Create System that contains Chassis |
| 201 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 202 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 203 | chassisVec.emplace_back(std::move(chassis)); |
| 204 | System system{std::move(rules), std::move(chassisVec)}; |
| 205 | |
| 206 | // Cache presence value in PresenceDetection |
| 207 | MockServices services{}; |
| 208 | presenceDetectionPtr->execute(services, system, *chassisPtr, |
| 209 | *devicePtr); |
| 210 | EXPECT_TRUE(presenceDetectionPtr->getCachedPresence().has_value()); |
| 211 | |
| 212 | // Clear cached data in Device |
| 213 | devicePtr->clearCache(); |
| 214 | |
| 215 | // Verify presence value no longer cached in PresenceDetection |
| 216 | EXPECT_FALSE(presenceDetectionPtr->getCachedPresence().has_value()); |
| 217 | } |
| 218 | } |
| 219 | |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 220 | TEST(DeviceTests, Close) |
| 221 | { |
| 222 | // Test where works: I2C interface is not open |
| 223 | { |
| 224 | // Create mock I2CInterface |
| 225 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 226 | std::make_unique<i2c::MockedI2CInterface>(); |
| 227 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(false)); |
| 228 | EXPECT_CALL(*i2cInterface, close).Times(0); |
| 229 | |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 230 | // Create mock services. No logError should occur. |
| 231 | MockServices services{}; |
| 232 | MockJournal& journal = services.getMockJournal(); |
| 233 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
| 234 | EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>())) |
| 235 | .Times(0); |
| 236 | |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 237 | // Create Device |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 238 | Device device{ |
| 239 | "vdd_reg", true, |
| 240 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 241 | std::move(i2cInterface)}; |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 242 | |
| 243 | // Close Device |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 244 | device.close(services); |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Test where works: I2C interface is open |
| 248 | { |
| 249 | // Create mock I2CInterface |
| 250 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 251 | std::make_unique<i2c::MockedI2CInterface>(); |
| 252 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 253 | EXPECT_CALL(*i2cInterface, close).Times(1); |
| 254 | |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 255 | // Create mock services. No logError should occur. |
| 256 | MockServices services{}; |
| 257 | MockJournal& journal = services.getMockJournal(); |
| 258 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
| 259 | EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>())) |
| 260 | .Times(0); |
| 261 | |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 262 | // Create Device |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 263 | Device device{ |
| 264 | "vdd_reg", true, |
| 265 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 266 | std::move(i2cInterface)}; |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 267 | |
| 268 | // Close Device |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 269 | device.close(services); |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Test where fails: closing I2C interface fails |
| 273 | { |
| 274 | // Create mock I2CInterface |
| 275 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 276 | std::make_unique<i2c::MockedI2CInterface>(); |
| 277 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 278 | EXPECT_CALL(*i2cInterface, close) |
| 279 | .Times(1) |
| 280 | .WillOnce(Throw( |
| 281 | i2c::I2CException{"Failed to close", "/dev/i2c-1", 0x70})); |
| 282 | |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 283 | // Create mock services. Expect logError() and logI2CError() to be |
| 284 | // called. |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 285 | MockServices services{}; |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 286 | MockErrorLogging& errorLogging = services.getMockErrorLogging(); |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 287 | MockJournal& journal = services.getMockJournal(); |
| 288 | std::vector<std::string> expectedErrMessagesException{ |
| 289 | "I2CException: Failed to close: bus /dev/i2c-1, addr 0x70"}; |
| 290 | EXPECT_CALL(journal, logError("Unable to close device vdd_reg")) |
| 291 | .Times(1); |
| 292 | EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1); |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 293 | EXPECT_CALL(errorLogging, |
| 294 | logI2CError(Entry::Level::Notice, Ref(journal), |
| 295 | "/dev/i2c-1", 0x70, 0)) |
| 296 | .Times(1); |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 297 | |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 298 | // Create Device |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 299 | Device device{ |
| 300 | "vdd_reg", true, |
| 301 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 302 | std::move(i2cInterface)}; |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 303 | |
| 304 | // Close Device |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 305 | device.close(services); |
Shawn McCarney | b4d18a4 | 2020-06-02 10:27:05 -0500 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 309 | TEST(DeviceTests, Configure) |
| 310 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 311 | // Test where device is not present |
| 312 | { |
| 313 | // Create mock services. No logging should occur. |
| 314 | MockServices services{}; |
| 315 | MockJournal& journal = services.getMockJournal(); |
| 316 | EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0); |
| 317 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
| 318 | |
| 319 | // Create PresenceDetection. Indicates device is not present. |
| 320 | std::unique_ptr<MockAction> presAction = std::make_unique<MockAction>(); |
| 321 | EXPECT_CALL(*presAction, execute).Times(1).WillOnce(Return(false)); |
| 322 | std::vector<std::unique_ptr<Action>> presActions{}; |
| 323 | presActions.emplace_back(std::move(presAction)); |
| 324 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 325 | std::make_unique<PresenceDetection>(std::move(presActions)); |
| 326 | |
| 327 | // Create Configuration. Action inside it should not be executed. |
| 328 | std::optional<double> volts{}; |
| 329 | std::unique_ptr<MockAction> confAction = std::make_unique<MockAction>(); |
| 330 | EXPECT_CALL(*confAction, execute).Times(0); |
| 331 | std::vector<std::unique_ptr<Action>> confActions{}; |
| 332 | confActions.emplace_back(std::move(confAction)); |
| 333 | std::unique_ptr<Configuration> configuration = |
| 334 | std::make_unique<Configuration>(volts, std::move(confActions)); |
| 335 | |
| 336 | // Create Device |
| 337 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 338 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 339 | "reg1", true, |
| 340 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 341 | std::move(i2cInterface), std::move(presenceDetection), |
| 342 | std::move(configuration)); |
| 343 | Device* devicePtr = device.get(); |
| 344 | |
| 345 | // Create Chassis that contains Device |
| 346 | std::vector<std::unique_ptr<Device>> devices{}; |
| 347 | devices.emplace_back(std::move(device)); |
| 348 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 349 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 350 | Chassis* chassisPtr = chassis.get(); |
| 351 | |
| 352 | // Create System that contains Chassis |
| 353 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 354 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 355 | chassisVec.emplace_back(std::move(chassis)); |
| 356 | System system{std::move(rules), std::move(chassisVec)}; |
| 357 | |
| 358 | // Call configure(). Should do nothing. |
| 359 | devicePtr->configure(services, system, *chassisPtr); |
| 360 | } |
| 361 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 362 | // Test where Configuration and Rails were not specified in constructor |
| 363 | { |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 364 | // Create mock services. No logging should occur. |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 365 | MockServices services{}; |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 366 | MockJournal& journal = services.getMockJournal(); |
| 367 | EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0); |
| 368 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 369 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 370 | // Create Device |
| 371 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 372 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 373 | "reg1", true, |
| 374 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 375 | std::move(i2cInterface)); |
| 376 | Device* devicePtr = device.get(); |
| 377 | |
| 378 | // Create Chassis that contains Device |
| 379 | std::vector<std::unique_ptr<Device>> devices{}; |
| 380 | devices.emplace_back(std::move(device)); |
| 381 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 382 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 383 | Chassis* chassisPtr = chassis.get(); |
| 384 | |
| 385 | // Create System that contains Chassis |
| 386 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 387 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 388 | chassisVec.emplace_back(std::move(chassis)); |
| 389 | System system{std::move(rules), std::move(chassisVec)}; |
| 390 | |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 391 | // Call configure(). |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 392 | devicePtr->configure(services, system, *chassisPtr); |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Test where Configuration and Rails were specified in constructor |
| 396 | { |
| 397 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 398 | |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 399 | // Create mock services. Expect logDebug() to be called. |
| 400 | // For the Device and both Rails, should execute the Configuration |
| 401 | // and log a debug message. |
| 402 | MockServices services{}; |
| 403 | MockJournal& journal = services.getMockJournal(); |
| 404 | EXPECT_CALL(journal, logDebug("Configuring reg1")).Times(1); |
| 405 | EXPECT_CALL(journal, logDebug("Configuring vdd0: volts=1.300000")) |
| 406 | .Times(1); |
| 407 | EXPECT_CALL(journal, logDebug("Configuring vio0: volts=3.200000")) |
| 408 | .Times(1); |
| 409 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
| 410 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 411 | // Create Rail vdd0 |
| 412 | { |
| 413 | // Create Configuration for Rail |
| 414 | std::optional<double> volts{1.3}; |
| 415 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 416 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 417 | std::vector<std::unique_ptr<Action>> actions{}; |
| 418 | actions.emplace_back(std::move(action)); |
| 419 | std::unique_ptr<Configuration> configuration = |
| 420 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 421 | |
| 422 | // Create Rail |
| 423 | std::unique_ptr<Rail> rail = |
| 424 | std::make_unique<Rail>("vdd0", std::move(configuration)); |
| 425 | rails.emplace_back(std::move(rail)); |
| 426 | } |
| 427 | |
| 428 | // Create Rail vio0 |
| 429 | { |
| 430 | // Create Configuration for Rail |
| 431 | std::optional<double> volts{3.2}; |
| 432 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 433 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 434 | std::vector<std::unique_ptr<Action>> actions{}; |
| 435 | actions.emplace_back(std::move(action)); |
| 436 | std::unique_ptr<Configuration> configuration = |
| 437 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 438 | |
| 439 | // Create Rail |
| 440 | std::unique_ptr<Rail> rail = |
| 441 | std::make_unique<Rail>("vio0", std::move(configuration)); |
| 442 | rails.emplace_back(std::move(rail)); |
| 443 | } |
| 444 | |
| 445 | // Create Configuration for Device |
| 446 | std::optional<double> volts{}; |
| 447 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 448 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 449 | std::vector<std::unique_ptr<Action>> actions{}; |
| 450 | actions.emplace_back(std::move(action)); |
| 451 | std::unique_ptr<Configuration> configuration = |
| 452 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 453 | |
| 454 | // Create Device |
| 455 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 456 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 457 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 458 | "reg1", true, |
| 459 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 460 | std::move(i2cInterface), std::move(presenceDetection), |
| 461 | std::move(configuration), std::move(rails)); |
| 462 | Device* devicePtr = device.get(); |
| 463 | |
| 464 | // Create Chassis that contains Device |
| 465 | std::vector<std::unique_ptr<Device>> devices{}; |
| 466 | devices.emplace_back(std::move(device)); |
| 467 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 468 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 469 | Chassis* chassisPtr = chassis.get(); |
| 470 | |
| 471 | // Create System that contains Chassis |
| 472 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 473 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 474 | chassisVec.emplace_back(std::move(chassis)); |
| 475 | System system{std::move(rules), std::move(chassisVec)}; |
| 476 | |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 477 | // Call configure(). |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 478 | devicePtr->configure(services, system, *chassisPtr); |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 482 | TEST(DeviceTests, GetConfiguration) |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 483 | { |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 484 | // Test where Configuration was not specified in constructor |
| 485 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 486 | Device device{ |
| 487 | "vdd_reg", true, |
| 488 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 489 | std::move(createI2CInterface())}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 490 | EXPECT_EQ(device.getConfiguration(), nullptr); |
| 491 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 492 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 493 | // Test where Configuration was specified in constructor |
| 494 | { |
| 495 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 496 | |
| 497 | // Create Configuration |
| 498 | std::optional<double> volts{3.2}; |
| 499 | std::vector<std::unique_ptr<Action>> actions{}; |
| 500 | actions.push_back(std::make_unique<MockAction>()); |
| 501 | actions.push_back(std::make_unique<MockAction>()); |
| 502 | std::unique_ptr<Configuration> configuration = |
| 503 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 504 | |
| 505 | // Create Device |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 506 | Device device{ |
| 507 | "vdd_reg", |
| 508 | true, |
| 509 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 510 | std::move(createI2CInterface()), |
| 511 | std::move(presenceDetection), |
| 512 | std::move(configuration)}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 513 | EXPECT_NE(device.getConfiguration(), nullptr); |
| 514 | EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true); |
| 515 | EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2); |
| 516 | EXPECT_EQ(device.getConfiguration()->getActions().size(), 2); |
| 517 | } |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | TEST(DeviceTests, GetFRU) |
| 521 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 522 | Device device{ |
| 523 | "vdd_reg", true, |
| 524 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 525 | std::move(createI2CInterface())}; |
| 526 | EXPECT_EQ(device.getFRU(), |
| 527 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2"); |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | TEST(DeviceTests, GetI2CInterface) |
| 531 | { |
| 532 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 533 | i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get(); |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 534 | Device device{ |
| 535 | "vdd_reg", true, |
| 536 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 537 | std::move(i2cInterface)}; |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 538 | EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr); |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 539 | } |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 540 | |
| 541 | TEST(DeviceTests, GetID) |
| 542 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 543 | Device device{ |
| 544 | "vdd_reg", false, |
| 545 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 546 | std::move(createI2CInterface())}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 547 | EXPECT_EQ(device.getID(), "vdd_reg"); |
| 548 | } |
| 549 | |
| 550 | TEST(DeviceTests, GetPresenceDetection) |
| 551 | { |
| 552 | // Test where PresenceDetection was not specified in constructor |
| 553 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 554 | Device device{ |
| 555 | "vdd_reg", true, |
| 556 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 557 | std::move(createI2CInterface())}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 558 | EXPECT_EQ(device.getPresenceDetection(), nullptr); |
| 559 | } |
| 560 | |
| 561 | // Test where PresenceDetection was specified in constructor |
| 562 | { |
| 563 | // Create PresenceDetection |
| 564 | std::vector<std::unique_ptr<Action>> actions{}; |
| 565 | actions.push_back(std::make_unique<MockAction>()); |
| 566 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 567 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 568 | |
| 569 | // Create Device |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 570 | Device device{ |
| 571 | "vdd_reg", false, |
| 572 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 573 | std::move(createI2CInterface()), std::move(presenceDetection)}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 574 | EXPECT_NE(device.getPresenceDetection(), nullptr); |
| 575 | EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | TEST(DeviceTests, GetRails) |
| 580 | { |
| 581 | // Test where no rails were specified in constructor |
| 582 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 583 | Device device{ |
| 584 | "vdd_reg", true, |
| 585 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 586 | std::move(createI2CInterface())}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 587 | EXPECT_EQ(device.getRails().size(), 0); |
| 588 | } |
| 589 | |
| 590 | // Test where rails were specified in constructor |
| 591 | { |
| 592 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 593 | std::unique_ptr<Configuration> configuration{}; |
| 594 | |
| 595 | // Create vector of Rail objects |
| 596 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 597 | rails.push_back(std::make_unique<Rail>("vdd0")); |
| 598 | rails.push_back(std::make_unique<Rail>("vdd1")); |
| 599 | |
| 600 | // Create Device |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 601 | Device device{ |
| 602 | "vdd_reg", |
| 603 | false, |
| 604 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 605 | std::move(createI2CInterface()), |
| 606 | std::move(presenceDetection), |
| 607 | std::move(configuration), |
| 608 | std::move(rails)}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 609 | EXPECT_EQ(device.getRails().size(), 2); |
| 610 | EXPECT_EQ(device.getRails()[0]->getID(), "vdd0"); |
| 611 | EXPECT_EQ(device.getRails()[1]->getID(), "vdd1"); |
| 612 | } |
| 613 | } |
| 614 | |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 615 | TEST(DeviceTests, IsPresent) |
| 616 | { |
| 617 | // Test where PresenceDetection not specified in constructor |
| 618 | { |
| 619 | // Create Device |
| 620 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 621 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 622 | "reg1", true, |
| 623 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 624 | std::move(i2cInterface)); |
| 625 | Device* devicePtr = device.get(); |
| 626 | |
| 627 | // Create Chassis that contains Device |
| 628 | std::vector<std::unique_ptr<Device>> devices{}; |
| 629 | devices.emplace_back(std::move(device)); |
| 630 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 631 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 632 | Chassis* chassisPtr = chassis.get(); |
| 633 | |
| 634 | // Create System that contains Chassis |
| 635 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 636 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 637 | chassisVec.emplace_back(std::move(chassis)); |
| 638 | System system{std::move(rules), std::move(chassisVec)}; |
| 639 | |
| 640 | // Create MockServices |
| 641 | MockServices services{}; |
| 642 | |
| 643 | // Since no PresenceDetection defined, isPresent() should return true |
| 644 | EXPECT_TRUE(devicePtr->isPresent(services, system, *chassisPtr)); |
| 645 | } |
| 646 | |
| 647 | // Test where PresenceDetection was specified in constructor: Is present |
| 648 | { |
| 649 | // Create PresenceDetection. Indicates device is present. |
| 650 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 651 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 652 | std::vector<std::unique_ptr<Action>> actions{}; |
| 653 | actions.emplace_back(std::move(action)); |
| 654 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 655 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 656 | |
| 657 | // Create Device |
| 658 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 659 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 660 | "reg1", true, |
| 661 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 662 | std::move(i2cInterface), std::move(presenceDetection)); |
| 663 | Device* devicePtr = device.get(); |
| 664 | |
| 665 | // Create Chassis that contains Device |
| 666 | std::vector<std::unique_ptr<Device>> devices{}; |
| 667 | devices.emplace_back(std::move(device)); |
| 668 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 669 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 670 | Chassis* chassisPtr = chassis.get(); |
| 671 | |
| 672 | // Create System that contains Chassis |
| 673 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 674 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 675 | chassisVec.emplace_back(std::move(chassis)); |
| 676 | System system{std::move(rules), std::move(chassisVec)}; |
| 677 | |
| 678 | // Create MockServices |
| 679 | MockServices services{}; |
| 680 | |
| 681 | // PresenceDetection::execute() and isPresent() should return true |
| 682 | EXPECT_TRUE(devicePtr->isPresent(services, system, *chassisPtr)); |
| 683 | } |
| 684 | |
| 685 | // Test where PresenceDetection was specified in constructor: Is not present |
| 686 | { |
| 687 | // Create PresenceDetection. Indicates device is not present. |
| 688 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 689 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false)); |
| 690 | std::vector<std::unique_ptr<Action>> actions{}; |
| 691 | actions.emplace_back(std::move(action)); |
| 692 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 693 | std::make_unique<PresenceDetection>(std::move(actions)); |
| 694 | |
| 695 | // Create Device |
| 696 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 697 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 698 | "reg1", true, |
| 699 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 700 | std::move(i2cInterface), std::move(presenceDetection)); |
| 701 | Device* devicePtr = device.get(); |
| 702 | |
| 703 | // Create Chassis that contains Device |
| 704 | std::vector<std::unique_ptr<Device>> devices{}; |
| 705 | devices.emplace_back(std::move(device)); |
| 706 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 707 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 708 | Chassis* chassisPtr = chassis.get(); |
| 709 | |
| 710 | // Create System that contains Chassis |
| 711 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 712 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 713 | chassisVec.emplace_back(std::move(chassis)); |
| 714 | System system{std::move(rules), std::move(chassisVec)}; |
| 715 | |
| 716 | // Create MockServices |
| 717 | MockServices services{}; |
| 718 | |
| 719 | // PresenceDetection::execute() and isPresent() should return false |
| 720 | EXPECT_FALSE(devicePtr->isPresent(services, system, *chassisPtr)); |
| 721 | } |
| 722 | } |
| 723 | |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 724 | TEST(DeviceTests, IsRegulator) |
| 725 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 726 | Device device{ |
| 727 | "vdd_reg", false, |
| 728 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 729 | std::move(createI2CInterface())}; |
Shawn McCarney | 0b1a0e7 | 2020-03-11 18:01:44 -0500 | [diff] [blame] | 730 | EXPECT_EQ(device.isRegulator(), false); |
| 731 | } |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 732 | |
| 733 | TEST(DeviceTests, MonitorSensors) |
| 734 | { |
Shawn McCarney | 48033bf | 2021-01-27 17:56:49 -0600 | [diff] [blame] | 735 | // Test where device is not present |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 736 | { |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 737 | // Create mock services. No Sensors methods should be called. |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 738 | MockServices services{}; |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 739 | MockSensors& sensors = services.getMockSensors(); |
| 740 | EXPECT_CALL(sensors, startRail).Times(0); |
| 741 | EXPECT_CALL(sensors, setValue).Times(0); |
| 742 | EXPECT_CALL(sensors, endRail).Times(0); |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 743 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 744 | // Create SensorMonitoring. Action inside it should not be executed. |
| 745 | std::unique_ptr<MockAction> sensAction = std::make_unique<MockAction>(); |
| 746 | EXPECT_CALL(*sensAction, execute).Times(0); |
| 747 | std::vector<std::unique_ptr<Action>> sensActions{}; |
| 748 | sensActions.emplace_back(std::move(sensAction)); |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 749 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 750 | std::make_unique<SensorMonitoring>(std::move(sensActions)); |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 751 | |
| 752 | // Create Rail |
| 753 | std::unique_ptr<Configuration> configuration{}; |
| 754 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 755 | "vddr1", std::move(configuration), std::move(sensorMonitoring)); |
| 756 | |
| 757 | // Create PresenceDetection. Indicates device is not present. |
| 758 | std::unique_ptr<MockAction> presAction = std::make_unique<MockAction>(); |
| 759 | EXPECT_CALL(*presAction, execute).Times(1).WillOnce(Return(false)); |
| 760 | std::vector<std::unique_ptr<Action>> presActions{}; |
| 761 | presActions.emplace_back(std::move(presAction)); |
| 762 | std::unique_ptr<PresenceDetection> presenceDetection = |
| 763 | std::make_unique<PresenceDetection>(std::move(presActions)); |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 764 | |
| 765 | // Create Device |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 766 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 767 | std::unique_ptr<Configuration> deviceConfiguration{}; |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 768 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 769 | rails.emplace_back(std::move(rail)); |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 770 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 771 | "reg1", true, |
| 772 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 773 | std::move(i2cInterface), std::move(presenceDetection), |
| 774 | std::move(deviceConfiguration), std::move(rails)); |
| 775 | Device* devicePtr = device.get(); |
| 776 | |
| 777 | // Create Chassis that contains Device |
| 778 | std::vector<std::unique_ptr<Device>> devices{}; |
| 779 | devices.emplace_back(std::move(device)); |
| 780 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 781 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 782 | Chassis* chassisPtr = chassis.get(); |
| 783 | |
| 784 | // Create System that contains Chassis |
| 785 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 786 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 787 | chassisVec.emplace_back(std::move(chassis)); |
| 788 | System system{std::move(rules), std::move(chassisVec)}; |
| 789 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 790 | // Call monitorSensors(). Should do nothing. |
| 791 | devicePtr->monitorSensors(services, system, *chassisPtr); |
| 792 | } |
| 793 | |
| 794 | // Test where Rails were not specified in constructor |
| 795 | { |
| 796 | // Create mock services. No Sensors methods should be called. |
| 797 | MockServices services{}; |
| 798 | MockSensors& sensors = services.getMockSensors(); |
| 799 | EXPECT_CALL(sensors, startRail).Times(0); |
| 800 | EXPECT_CALL(sensors, setValue).Times(0); |
| 801 | EXPECT_CALL(sensors, endRail).Times(0); |
| 802 | |
| 803 | // Create Device |
| 804 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 805 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 806 | "reg1", true, |
| 807 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 808 | std::move(i2cInterface)); |
| 809 | Device* devicePtr = device.get(); |
| 810 | |
| 811 | // Create Chassis that contains Device |
| 812 | std::vector<std::unique_ptr<Device>> devices{}; |
| 813 | devices.emplace_back(std::move(device)); |
| 814 | std::unique_ptr<Chassis> chassis = |
| 815 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
| 816 | Chassis* chassisPtr = chassis.get(); |
| 817 | |
| 818 | // Create System that contains Chassis |
| 819 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 820 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 821 | chassisVec.emplace_back(std::move(chassis)); |
| 822 | System system{std::move(rules), std::move(chassisVec)}; |
| 823 | |
| 824 | // Call monitorSensors(). Should do nothing. |
| 825 | devicePtr->monitorSensors(services, system, *chassisPtr); |
| 826 | } |
| 827 | |
| 828 | // Test where Rails were specified in constructor |
| 829 | { |
| 830 | // Create mock services. Set Sensors service expectations. |
| 831 | MockServices services{}; |
| 832 | MockSensors& sensors = services.getMockSensors(); |
| 833 | EXPECT_CALL(sensors, startRail("vdd0", |
| 834 | "/xyz/openbmc_project/inventory/system/" |
| 835 | "chassis/motherboard/reg1", |
| 836 | chassisInvPath)) |
| 837 | .Times(1); |
| 838 | EXPECT_CALL(sensors, startRail("vio0", |
| 839 | "/xyz/openbmc_project/inventory/system/" |
| 840 | "chassis/motherboard/reg1", |
| 841 | chassisInvPath)) |
| 842 | .Times(1); |
| 843 | EXPECT_CALL(sensors, setValue).Times(0); |
| 844 | EXPECT_CALL(sensors, endRail(false)).Times(2); |
| 845 | |
| 846 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 847 | |
| 848 | // Create Rail vdd0 |
| 849 | { |
| 850 | // Create SensorMonitoring for Rail |
| 851 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 852 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 853 | std::vector<std::unique_ptr<Action>> actions{}; |
| 854 | actions.emplace_back(std::move(action)); |
| 855 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 856 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 857 | |
| 858 | // Create Rail |
| 859 | std::unique_ptr<Configuration> configuration{}; |
| 860 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 861 | "vdd0", std::move(configuration), std::move(sensorMonitoring)); |
| 862 | rails.emplace_back(std::move(rail)); |
| 863 | } |
| 864 | |
| 865 | // Create Rail vio0 |
| 866 | { |
| 867 | // Create SensorMonitoring for Rail |
| 868 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 869 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 870 | std::vector<std::unique_ptr<Action>> actions{}; |
| 871 | actions.emplace_back(std::move(action)); |
| 872 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 873 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 874 | |
| 875 | // Create Rail |
| 876 | std::unique_ptr<Configuration> configuration{}; |
| 877 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 878 | "vio0", std::move(configuration), std::move(sensorMonitoring)); |
| 879 | rails.emplace_back(std::move(rail)); |
| 880 | } |
| 881 | |
| 882 | // Create Device that contains Rails |
| 883 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); |
| 884 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 885 | std::unique_ptr<Configuration> configuration{}; |
| 886 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 887 | "reg1", true, |
| 888 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 889 | std::move(i2cInterface), std::move(presenceDetection), |
| 890 | std::move(configuration), std::move(rails)); |
| 891 | Device* devicePtr = device.get(); |
| 892 | |
| 893 | // Create Chassis that contains Device |
| 894 | std::vector<std::unique_ptr<Device>> devices{}; |
| 895 | devices.emplace_back(std::move(device)); |
| 896 | std::unique_ptr<Chassis> chassis = |
| 897 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
| 898 | Chassis* chassisPtr = chassis.get(); |
| 899 | |
| 900 | // Create System that contains Chassis |
| 901 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 902 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 903 | chassisVec.emplace_back(std::move(chassis)); |
| 904 | System system{std::move(rules), std::move(chassisVec)}; |
| 905 | |
| 906 | // Call monitorSensors(). Should monitor sensors in both rails. |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 907 | devicePtr->monitorSensors(services, system, *chassisPtr); |
Bob King | 8e1cd0b | 2020-07-08 13:30:27 +0800 | [diff] [blame] | 908 | } |
| 909 | } |