| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 1 | /** | 
|  | 2 | * Copyright © 2020 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 | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 16 | #include "action.hpp" | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 17 | #include "chassis.hpp" | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 18 | #include "configuration.hpp" | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 19 | #include "device.hpp" | 
|  | 20 | #include "i2c_interface.hpp" | 
| Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 21 | #include "id_map.hpp" | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 22 | #include "mock_action.hpp" | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 23 | #include "mock_journal.hpp" | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 24 | #include "mock_sensors.hpp" | 
| Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 25 | #include "mock_services.hpp" | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 26 | #include "mocked_i2c_interface.hpp" | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 27 | #include "presence_detection.hpp" | 
|  | 28 | #include "rail.hpp" | 
|  | 29 | #include "rule.hpp" | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 30 | #include "sensor_monitoring.hpp" | 
| Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 31 | #include "sensors.hpp" | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 32 | #include "system.hpp" | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 33 | #include "test_utils.hpp" | 
|  | 34 |  | 
|  | 35 | #include <memory> | 
|  | 36 | #include <stdexcept> | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 37 | #include <string> | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 38 | #include <utility> | 
|  | 39 | #include <vector> | 
|  | 40 |  | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 41 | #include <gmock/gmock.h> | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 42 | #include <gtest/gtest.h> | 
|  | 43 |  | 
|  | 44 | using namespace phosphor::power::regulators; | 
|  | 45 | using namespace phosphor::power::regulators::test_utils; | 
|  | 46 |  | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 47 | using ::testing::A; | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 48 | using ::testing::Return; | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 49 | using ::testing::TypedEq; | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 50 |  | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 51 | // Default chassis inventory path | 
|  | 52 | static const std::string defaultInventoryPath{ | 
|  | 53 | "/xyz/openbmc_project/inventory/system/chassis"}; | 
|  | 54 |  | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 55 | TEST(ChassisTests, Constructor) | 
|  | 56 | { | 
|  | 57 | // Test where works: Only required parameters are specified | 
|  | 58 | { | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 59 | Chassis chassis{2, defaultInventoryPath}; | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 60 | EXPECT_EQ(chassis.getNumber(), 2); | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 61 | EXPECT_EQ(chassis.getInventoryPath(), defaultInventoryPath); | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 62 | EXPECT_EQ(chassis.getDevices().size(), 0); | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | // Test where works: All parameters are specified | 
|  | 66 | { | 
|  | 67 | // Create vector of Device objects | 
|  | 68 | std::vector<std::unique_ptr<Device>> devices{}; | 
| Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 69 | devices.emplace_back(createDevice("vdd_reg1")); | 
|  | 70 | devices.emplace_back(createDevice("vdd_reg2")); | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 71 |  | 
|  | 72 | // Create Chassis | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 73 | Chassis chassis{1, defaultInventoryPath, std::move(devices)}; | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 74 | EXPECT_EQ(chassis.getNumber(), 1); | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 75 | EXPECT_EQ(chassis.getInventoryPath(), defaultInventoryPath); | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 76 | EXPECT_EQ(chassis.getDevices().size(), 2); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | // Test where fails: Invalid chassis number < 1 | 
|  | 80 | try | 
|  | 81 | { | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 82 | Chassis chassis{0, defaultInventoryPath}; | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 83 | ADD_FAILURE() << "Should not have reached this line."; | 
|  | 84 | } | 
|  | 85 | catch (const std::invalid_argument& e) | 
|  | 86 | { | 
|  | 87 | EXPECT_STREQ(e.what(), "Invalid chassis number: 0"); | 
|  | 88 | } | 
|  | 89 | catch (...) | 
|  | 90 | { | 
|  | 91 | ADD_FAILURE() << "Should not have caught exception."; | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 |  | 
| Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 95 | TEST(ChassisTests, AddToIDMap) | 
|  | 96 | { | 
|  | 97 | // Create vector of Device objects | 
|  | 98 | std::vector<std::unique_ptr<Device>> devices{}; | 
|  | 99 | devices.emplace_back(createDevice("reg1", {"rail1"})); | 
|  | 100 | devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"})); | 
|  | 101 | devices.emplace_back(createDevice("reg3")); | 
|  | 102 |  | 
|  | 103 | // Create Chassis | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 104 | Chassis chassis{1, defaultInventoryPath, std::move(devices)}; | 
| Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 105 |  | 
|  | 106 | // Add Device and Rail objects within the Chassis to an IDMap | 
|  | 107 | IDMap idMap{}; | 
|  | 108 | chassis.addToIDMap(idMap); | 
|  | 109 |  | 
|  | 110 | // Verify all Devices are in the IDMap | 
|  | 111 | EXPECT_NO_THROW(idMap.getDevice("reg1")); | 
|  | 112 | EXPECT_NO_THROW(idMap.getDevice("reg2")); | 
|  | 113 | EXPECT_NO_THROW(idMap.getDevice("reg3")); | 
|  | 114 | EXPECT_THROW(idMap.getDevice("reg4"), std::invalid_argument); | 
|  | 115 |  | 
|  | 116 | // Verify all Rails are in the IDMap | 
|  | 117 | EXPECT_NO_THROW(idMap.getRail("rail1")); | 
|  | 118 | EXPECT_NO_THROW(idMap.getRail("rail2a")); | 
|  | 119 | EXPECT_NO_THROW(idMap.getRail("rail2b")); | 
|  | 120 | EXPECT_THROW(idMap.getRail("rail3"), std::invalid_argument); | 
|  | 121 | } | 
|  | 122 |  | 
| Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 123 | TEST(ChassisTests, ClearCache) | 
|  | 124 | { | 
|  | 125 | // Create PresenceDetection | 
|  | 126 | std::vector<std::unique_ptr<Action>> actions{}; | 
|  | 127 | std::unique_ptr<PresenceDetection> presenceDetection = | 
|  | 128 | std::make_unique<PresenceDetection>(std::move(actions)); | 
|  | 129 | PresenceDetection* presenceDetectionPtr = presenceDetection.get(); | 
|  | 130 |  | 
|  | 131 | // Create Device that contains PresenceDetection | 
|  | 132 | std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface(); | 
|  | 133 | std::unique_ptr<Device> device = std::make_unique<Device>( | 
|  | 134 | "reg1", true, | 
|  | 135 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", | 
|  | 136 | std::move(i2cInterface), std::move(presenceDetection)); | 
|  | 137 | Device* devicePtr = device.get(); | 
|  | 138 |  | 
|  | 139 | // Create Chassis that contains Device | 
|  | 140 | std::vector<std::unique_ptr<Device>> devices{}; | 
|  | 141 | devices.emplace_back(std::move(device)); | 
|  | 142 | std::unique_ptr<Chassis> chassis = | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 143 | std::make_unique<Chassis>(1, defaultInventoryPath, std::move(devices)); | 
| Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame] | 144 | Chassis* chassisPtr = chassis.get(); | 
|  | 145 |  | 
|  | 146 | // Create System that contains Chassis | 
|  | 147 | std::vector<std::unique_ptr<Rule>> rules{}; | 
|  | 148 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; | 
|  | 149 | chassisVec.emplace_back(std::move(chassis)); | 
|  | 150 | System system{std::move(rules), std::move(chassisVec)}; | 
|  | 151 |  | 
|  | 152 | // Cache presence value in PresenceDetection | 
|  | 153 | MockServices services{}; | 
|  | 154 | presenceDetectionPtr->execute(services, system, *chassisPtr, *devicePtr); | 
|  | 155 | EXPECT_TRUE(presenceDetectionPtr->getCachedPresence().has_value()); | 
|  | 156 |  | 
|  | 157 | // Clear cached data in Chassis | 
|  | 158 | chassisPtr->clearCache(); | 
|  | 159 |  | 
|  | 160 | // Verify presence value no longer cached in PresenceDetection | 
|  | 161 | EXPECT_FALSE(presenceDetectionPtr->getCachedPresence().has_value()); | 
|  | 162 | } | 
|  | 163 |  | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 164 | TEST(ChassisTests, CloseDevices) | 
|  | 165 | { | 
|  | 166 | // Test where no devices were specified in constructor | 
|  | 167 | { | 
| Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 168 | // Create mock services.  Expect logDebug() to be called. | 
|  | 169 | MockServices services{}; | 
|  | 170 | MockJournal& journal = services.getMockJournal(); | 
|  | 171 | EXPECT_CALL(journal, logDebug("Closing devices in chassis 2")).Times(1); | 
|  | 172 |  | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 173 | // Create Chassis | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 174 | Chassis chassis{2, defaultInventoryPath}; | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 175 |  | 
|  | 176 | // Call closeDevices() | 
| Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 177 | chassis.closeDevices(services); | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
|  | 180 | // Test where devices were specified in constructor | 
|  | 181 | { | 
|  | 182 | std::vector<std::unique_ptr<Device>> devices{}; | 
|  | 183 |  | 
| Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 184 | // Create mock services.  Expect logDebug() to be called. | 
|  | 185 | MockServices services{}; | 
|  | 186 | MockJournal& journal = services.getMockJournal(); | 
|  | 187 | EXPECT_CALL(journal, logDebug("Closing devices in chassis 1")).Times(1); | 
|  | 188 |  | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 189 | // Create Device vdd0_reg | 
|  | 190 | { | 
|  | 191 | // Create mock I2CInterface: isOpen() and close() should be called | 
|  | 192 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = | 
|  | 193 | std::make_unique<i2c::MockedI2CInterface>(); | 
|  | 194 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); | 
|  | 195 | EXPECT_CALL(*i2cInterface, close).Times(1); | 
|  | 196 |  | 
|  | 197 | // Create Device | 
| Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 198 | std::unique_ptr<Device> device = | 
|  | 199 | std::make_unique<Device>("vdd0_reg", true, | 
|  | 200 | "/xyz/openbmc_project/inventory/" | 
|  | 201 | "system/chassis/motherboard/vdd0_reg", | 
|  | 202 | std::move(i2cInterface)); | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 203 | devices.emplace_back(std::move(device)); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | // Create Device vdd1_reg | 
|  | 207 | { | 
|  | 208 | // Create mock I2CInterface: isOpen() and close() should be called | 
|  | 209 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = | 
|  | 210 | std::make_unique<i2c::MockedI2CInterface>(); | 
|  | 211 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); | 
|  | 212 | EXPECT_CALL(*i2cInterface, close).Times(1); | 
|  | 213 |  | 
|  | 214 | // Create Device | 
| Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 215 | std::unique_ptr<Device> device = | 
|  | 216 | std::make_unique<Device>("vdd1_reg", true, | 
|  | 217 | "/xyz/openbmc_project/inventory/" | 
|  | 218 | "system/chassis/motherboard/vdd1_reg", | 
|  | 219 | std::move(i2cInterface)); | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 220 | devices.emplace_back(std::move(device)); | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | // Create Chassis | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 224 | Chassis chassis{1, defaultInventoryPath, std::move(devices)}; | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 225 |  | 
|  | 226 | // Call closeDevices() | 
| Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 227 | chassis.closeDevices(services); | 
| Shawn McCarney | 050531f | 2020-06-02 14:17:12 -0500 | [diff] [blame] | 228 | } | 
|  | 229 | } | 
|  | 230 |  | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 231 | TEST(ChassisTests, Configure) | 
|  | 232 | { | 
|  | 233 | // Test where no devices were specified in constructor | 
|  | 234 | { | 
| Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 235 | // Create mock services.  Expect logInfo() to be called. | 
| Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 236 | MockServices services{}; | 
| Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 237 | MockJournal& journal = services.getMockJournal(); | 
|  | 238 | EXPECT_CALL(journal, logInfo("Configuring chassis 1")).Times(1); | 
|  | 239 | EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0); | 
|  | 240 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); | 
| Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 241 |  | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 242 | // Create Chassis | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 243 | std::unique_ptr<Chassis> chassis = | 
|  | 244 | std::make_unique<Chassis>(1, defaultInventoryPath); | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 245 | Chassis* chassisPtr = chassis.get(); | 
|  | 246 |  | 
|  | 247 | // Create System that contains Chassis | 
|  | 248 | std::vector<std::unique_ptr<Rule>> rules{}; | 
|  | 249 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; | 
|  | 250 | chassisVec.emplace_back(std::move(chassis)); | 
|  | 251 | System system{std::move(rules), std::move(chassisVec)}; | 
|  | 252 |  | 
|  | 253 | // Call configure() | 
| Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 254 | chassisPtr->configure(services, system); | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
|  | 257 | // Test where devices were specified in constructor | 
|  | 258 | { | 
|  | 259 | std::vector<std::unique_ptr<Device>> devices{}; | 
|  | 260 |  | 
| Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 261 | // Create mock services.  Expect logInfo() and logDebug() to be called. | 
|  | 262 | MockServices services{}; | 
|  | 263 | MockJournal& journal = services.getMockJournal(); | 
|  | 264 | EXPECT_CALL(journal, logInfo("Configuring chassis 2")).Times(1); | 
|  | 265 | EXPECT_CALL(journal, logDebug("Configuring vdd0_reg: volts=1.300000")) | 
|  | 266 | .Times(1); | 
|  | 267 | EXPECT_CALL(journal, logDebug("Configuring vdd1_reg: volts=1.200000")) | 
|  | 268 | .Times(1); | 
|  | 269 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); | 
|  | 270 |  | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 271 | // Create Device vdd0_reg | 
|  | 272 | { | 
|  | 273 | // Create Configuration | 
|  | 274 | std::vector<std::unique_ptr<Action>> actions{}; | 
|  | 275 | std::unique_ptr<Configuration> configuration = | 
|  | 276 | std::make_unique<Configuration>(1.3, std::move(actions)); | 
|  | 277 |  | 
|  | 278 | // Create Device | 
|  | 279 | std::unique_ptr<i2c::I2CInterface> i2cInterface = | 
|  | 280 | createI2CInterface(); | 
|  | 281 | std::unique_ptr<PresenceDetection> presenceDetection{}; | 
|  | 282 | std::unique_ptr<Device> device = std::make_unique<Device>( | 
| Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 283 | "vdd0_reg", true, | 
|  | 284 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/" | 
|  | 285 | "vdd0_reg", | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 286 | std::move(i2cInterface), std::move(presenceDetection), | 
|  | 287 | std::move(configuration)); | 
|  | 288 | devices.emplace_back(std::move(device)); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | // Create Device vdd1_reg | 
|  | 292 | { | 
|  | 293 | // Create Configuration | 
|  | 294 | std::vector<std::unique_ptr<Action>> actions{}; | 
|  | 295 | std::unique_ptr<Configuration> configuration = | 
|  | 296 | std::make_unique<Configuration>(1.2, std::move(actions)); | 
|  | 297 |  | 
|  | 298 | // Create Device | 
|  | 299 | std::unique_ptr<i2c::I2CInterface> i2cInterface = | 
|  | 300 | createI2CInterface(); | 
|  | 301 | std::unique_ptr<PresenceDetection> presenceDetection{}; | 
|  | 302 | std::unique_ptr<Device> device = std::make_unique<Device>( | 
| Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 303 | "vdd1_reg", true, | 
|  | 304 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/" | 
|  | 305 | "vdd1_reg", | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 306 | std::move(i2cInterface), std::move(presenceDetection), | 
|  | 307 | std::move(configuration)); | 
|  | 308 | devices.emplace_back(std::move(device)); | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | // Create Chassis | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 312 | std::unique_ptr<Chassis> chassis = std::make_unique<Chassis>( | 
|  | 313 | 2, defaultInventoryPath, std::move(devices)); | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 314 | Chassis* chassisPtr = chassis.get(); | 
|  | 315 |  | 
|  | 316 | // Create System that contains Chassis | 
|  | 317 | std::vector<std::unique_ptr<Rule>> rules{}; | 
|  | 318 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; | 
|  | 319 | chassisVec.emplace_back(std::move(chassis)); | 
|  | 320 | System system{std::move(rules), std::move(chassisVec)}; | 
|  | 321 |  | 
|  | 322 | // Call configure() | 
| Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 323 | chassisPtr->configure(services, system); | 
| Shawn McCarney | 525e20c | 2020-04-14 11:05:39 -0500 | [diff] [blame] | 324 | } | 
|  | 325 | } | 
|  | 326 |  | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 327 | TEST(ChassisTests, GetDevices) | 
|  | 328 | { | 
|  | 329 | // Test where no devices were specified in constructor | 
|  | 330 | { | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 331 | Chassis chassis{2, defaultInventoryPath}; | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 332 | EXPECT_EQ(chassis.getDevices().size(), 0); | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | // Test where devices were specified in constructor | 
|  | 336 | { | 
|  | 337 | // Create vector of Device objects | 
|  | 338 | std::vector<std::unique_ptr<Device>> devices{}; | 
| Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 339 | devices.emplace_back(createDevice("vdd_reg1")); | 
|  | 340 | devices.emplace_back(createDevice("vdd_reg2")); | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 341 |  | 
|  | 342 | // Create Chassis | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 343 | Chassis chassis{1, defaultInventoryPath, std::move(devices)}; | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 344 | EXPECT_EQ(chassis.getDevices().size(), 2); | 
|  | 345 | EXPECT_EQ(chassis.getDevices()[0]->getID(), "vdd_reg1"); | 
|  | 346 | EXPECT_EQ(chassis.getDevices()[1]->getID(), "vdd_reg2"); | 
|  | 347 | } | 
|  | 348 | } | 
|  | 349 |  | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 350 | TEST(ChassisTests, GetInventoryPath) | 
|  | 351 | { | 
|  | 352 | Chassis chassis{3, defaultInventoryPath}; | 
|  | 353 | EXPECT_EQ(chassis.getInventoryPath(), defaultInventoryPath); | 
|  | 354 | } | 
|  | 355 |  | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 356 | TEST(ChassisTests, GetNumber) | 
|  | 357 | { | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 358 | Chassis chassis{3, defaultInventoryPath}; | 
| Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 359 | EXPECT_EQ(chassis.getNumber(), 3); | 
|  | 360 | } | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 361 |  | 
|  | 362 | TEST(ChassisTests, MonitorSensors) | 
|  | 363 | { | 
|  | 364 | // Test where no devices were specified in constructor | 
|  | 365 | { | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 366 | // Create mock services.  No Sensors methods should be called. | 
| Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 367 | MockServices services{}; | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 368 | MockSensors& sensors = services.getMockSensors(); | 
|  | 369 | EXPECT_CALL(sensors, startRail).Times(0); | 
|  | 370 | EXPECT_CALL(sensors, setValue).Times(0); | 
|  | 371 | EXPECT_CALL(sensors, endRail).Times(0); | 
| Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 372 |  | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 373 | // Create Chassis | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 374 | std::unique_ptr<Chassis> chassis = | 
|  | 375 | std::make_unique<Chassis>(1, defaultInventoryPath); | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 376 | Chassis* chassisPtr = chassis.get(); | 
|  | 377 |  | 
|  | 378 | // Create System that contains Chassis | 
|  | 379 | std::vector<std::unique_ptr<Rule>> rules{}; | 
|  | 380 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; | 
|  | 381 | chassisVec.emplace_back(std::move(chassis)); | 
|  | 382 | System system{std::move(rules), std::move(chassisVec)}; | 
|  | 383 |  | 
|  | 384 | // Call monitorSensors().  Should do nothing. | 
| Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 385 | chassisPtr->monitorSensors(services, system); | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 386 | } | 
|  | 387 |  | 
|  | 388 | // Test where devices were specified in constructor | 
|  | 389 | { | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 390 | // Create mock services.  Set Sensors service expectations. | 
| Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 391 | MockServices services{}; | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 392 | MockSensors& sensors = services.getMockSensors(); | 
|  | 393 | EXPECT_CALL(sensors, startRail("vdd0", | 
|  | 394 | "/xyz/openbmc_project/inventory/system/" | 
|  | 395 | "chassis/motherboard/vdd0_reg", | 
|  | 396 | defaultInventoryPath)) | 
|  | 397 | .Times(1); | 
|  | 398 | EXPECT_CALL(sensors, startRail("vdd1", | 
|  | 399 | "/xyz/openbmc_project/inventory/system/" | 
|  | 400 | "chassis/motherboard/vdd1_reg", | 
|  | 401 | defaultInventoryPath)) | 
|  | 402 | .Times(1); | 
|  | 403 | EXPECT_CALL(sensors, setValue).Times(0); | 
|  | 404 | EXPECT_CALL(sensors, endRail(false)).Times(2); | 
| Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 405 |  | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 406 | std::vector<std::unique_ptr<Device>> devices{}; | 
|  | 407 |  | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 408 | // Create Device vdd0_reg | 
|  | 409 | { | 
|  | 410 | // Create SensorMonitoring for Rail | 
|  | 411 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); | 
|  | 412 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); | 
|  | 413 | std::vector<std::unique_ptr<Action>> actions{}; | 
|  | 414 | actions.emplace_back(std::move(action)); | 
|  | 415 | std::unique_ptr<SensorMonitoring> sensorMonitoring = | 
|  | 416 | std::make_unique<SensorMonitoring>(std::move(actions)); | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 417 |  | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 418 | // Create Rail | 
|  | 419 | std::unique_ptr<Configuration> configuration{}; | 
|  | 420 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( | 
|  | 421 | "vdd0", std::move(configuration), std::move(sensorMonitoring)); | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 422 |  | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 423 | // Create Device | 
|  | 424 | std::unique_ptr<i2c::I2CInterface> i2cInterface = | 
|  | 425 | createI2CInterface(); | 
|  | 426 | std::unique_ptr<PresenceDetection> presenceDetection{}; | 
|  | 427 | std::unique_ptr<Configuration> deviceConfiguration{}; | 
|  | 428 | std::vector<std::unique_ptr<Rail>> rails{}; | 
|  | 429 | rails.emplace_back(std::move(rail)); | 
|  | 430 | std::unique_ptr<Device> device = std::make_unique<Device>( | 
|  | 431 | "vdd0_reg", true, | 
|  | 432 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/" | 
|  | 433 | "vdd0_reg", | 
|  | 434 | std::move(i2cInterface), std::move(presenceDetection), | 
|  | 435 | std::move(deviceConfiguration), std::move(rails)); | 
|  | 436 | devices.emplace_back(std::move(device)); | 
|  | 437 | } | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 438 |  | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 439 | // Create Device vdd1_reg | 
|  | 440 | { | 
|  | 441 | // Create SensorMonitoring for Rail | 
|  | 442 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); | 
|  | 443 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); | 
|  | 444 | std::vector<std::unique_ptr<Action>> actions{}; | 
|  | 445 | actions.emplace_back(std::move(action)); | 
|  | 446 | std::unique_ptr<SensorMonitoring> sensorMonitoring = | 
|  | 447 | std::make_unique<SensorMonitoring>(std::move(actions)); | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 448 |  | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 449 | // Create Rail | 
|  | 450 | std::unique_ptr<Configuration> configuration{}; | 
|  | 451 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( | 
|  | 452 | "vdd1", std::move(configuration), std::move(sensorMonitoring)); | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 453 |  | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 454 | // Create Device | 
|  | 455 | std::unique_ptr<i2c::I2CInterface> i2cInterface = | 
|  | 456 | createI2CInterface(); | 
|  | 457 | std::unique_ptr<PresenceDetection> presenceDetection{}; | 
|  | 458 | std::unique_ptr<Configuration> deviceConfiguration{}; | 
|  | 459 | std::vector<std::unique_ptr<Rail>> rails{}; | 
|  | 460 | rails.emplace_back(std::move(rail)); | 
|  | 461 | std::unique_ptr<Device> device = std::make_unique<Device>( | 
|  | 462 | "vdd1_reg", true, | 
|  | 463 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/" | 
|  | 464 | "vdd1_reg", | 
|  | 465 | std::move(i2cInterface), std::move(presenceDetection), | 
|  | 466 | std::move(deviceConfiguration), std::move(rails)); | 
|  | 467 | devices.emplace_back(std::move(device)); | 
|  | 468 | } | 
|  | 469 |  | 
|  | 470 | // Create Chassis that contains Devices | 
| Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 471 | std::unique_ptr<Chassis> chassis = std::make_unique<Chassis>( | 
| Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 472 | 2, defaultInventoryPath, std::move(devices)); | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 473 | Chassis* chassisPtr = chassis.get(); | 
|  | 474 |  | 
|  | 475 | // Create System that contains Chassis | 
|  | 476 | std::vector<std::unique_ptr<Rule>> rules{}; | 
|  | 477 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; | 
|  | 478 | chassisVec.emplace_back(std::move(chassis)); | 
|  | 479 | System system{std::move(rules), std::move(chassisVec)}; | 
|  | 480 |  | 
|  | 481 | // Call monitorSensors() | 
| Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 482 | chassisPtr->monitorSensors(services, system); | 
| Bob King | a2c81a6 | 2020-07-08 13:31:16 +0800 | [diff] [blame] | 483 | } | 
|  | 484 | } |