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