Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -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 | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 16 | #include "chassis.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 17 | #include "device.hpp" |
| 18 | #include "id_map.hpp" |
Shawn McCarney | 2af5289 | 2020-04-14 11:54:45 -0500 | [diff] [blame] | 19 | #include "mock_journal.hpp" |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 20 | #include "mock_services.hpp" |
Bob King | 8e2294d | 2020-07-14 17:41:31 +0800 | [diff] [blame] | 21 | #include "mocked_i2c_interface.hpp" |
| 22 | #include "pmbus_read_sensor_action.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 23 | #include "rail.hpp" |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 24 | #include "rule.hpp" |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 25 | #include "services.hpp" |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 26 | #include "system.hpp" |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 27 | #include "test_utils.hpp" |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 28 | |
| 29 | #include <memory> |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 30 | #include <stdexcept> |
Shawn McCarney | 2af5289 | 2020-04-14 11:54:45 -0500 | [diff] [blame] | 31 | #include <string> |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 32 | #include <utility> |
| 33 | #include <vector> |
| 34 | |
Bob King | 8e2294d | 2020-07-14 17:41:31 +0800 | [diff] [blame] | 35 | #include <gmock/gmock.h> |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 36 | #include <gtest/gtest.h> |
| 37 | |
| 38 | using namespace phosphor::power::regulators; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 39 | using namespace phosphor::power::regulators::test_utils; |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 40 | |
Bob King | 8e2294d | 2020-07-14 17:41:31 +0800 | [diff] [blame] | 41 | using ::testing::A; |
| 42 | using ::testing::Return; |
| 43 | using ::testing::TypedEq; |
| 44 | |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 45 | TEST(SystemTests, Constructor) |
| 46 | { |
| 47 | // Create Rules |
| 48 | std::vector<std::unique_ptr<Rule>> rules{}; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 49 | rules.emplace_back(createRule("set_voltage_rule")); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 50 | |
| 51 | // Create Chassis |
| 52 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 53 | std::vector<std::unique_ptr<Device>> devices{}; |
| 54 | devices.emplace_back(createDevice("reg1", {"rail1"})); |
| 55 | chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices))); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 56 | |
| 57 | // Create System |
| 58 | System system{std::move(rules), std::move(chassis)}; |
| 59 | EXPECT_EQ(system.getChassis().size(), 1); |
| 60 | EXPECT_EQ(system.getChassis()[0]->getNumber(), 1); |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 61 | EXPECT_NO_THROW(system.getIDMap().getRule("set_voltage_rule")); |
| 62 | EXPECT_NO_THROW(system.getIDMap().getDevice("reg1")); |
| 63 | EXPECT_NO_THROW(system.getIDMap().getRail("rail1")); |
| 64 | EXPECT_THROW(system.getIDMap().getRail("rail2"), std::invalid_argument); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 65 | EXPECT_EQ(system.getRules().size(), 1); |
| 66 | EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule"); |
| 67 | } |
| 68 | |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 69 | TEST(SystemTests, CloseDevices) |
| 70 | { |
| 71 | // Specify an empty rules vector |
| 72 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 73 | |
| 74 | // Create Chassis |
| 75 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 76 | chassis.emplace_back(std::make_unique<Chassis>(1)); |
| 77 | chassis.emplace_back(std::make_unique<Chassis>(3)); |
| 78 | |
| 79 | // Create System |
| 80 | System system{std::move(rules), std::move(chassis)}; |
| 81 | |
| 82 | // Call closeDevices() |
| 83 | journal::clear(); |
| 84 | system.closeDevices(); |
| 85 | EXPECT_EQ(journal::getErrMessages().size(), 0); |
| 86 | EXPECT_EQ(journal::getInfoMessages().size(), 0); |
| 87 | std::vector<std::string> expectedDebugMessages{ |
| 88 | "Closing devices in chassis 1", "Closing devices in chassis 3"}; |
| 89 | EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages); |
| 90 | } |
| 91 | |
Shawn McCarney | 2af5289 | 2020-04-14 11:54:45 -0500 | [diff] [blame] | 92 | TEST(SystemTests, Configure) |
| 93 | { |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 94 | // Create mock services. Expect logInfo() to be called. |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 95 | MockServices services{}; |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 96 | MockJournal& journal = services.getMockJournal(); |
| 97 | EXPECT_CALL(journal, logInfo("Configuring chassis 1")).Times(1); |
| 98 | EXPECT_CALL(journal, logInfo("Configuring chassis 3")).Times(1); |
| 99 | EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0); |
| 100 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 101 | |
Shawn McCarney | 2af5289 | 2020-04-14 11:54:45 -0500 | [diff] [blame] | 102 | // Specify an empty rules vector |
| 103 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 104 | |
| 105 | // Create Chassis |
| 106 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 107 | chassis.emplace_back(std::make_unique<Chassis>(1)); |
| 108 | chassis.emplace_back(std::make_unique<Chassis>(3)); |
| 109 | |
| 110 | // Create System |
| 111 | System system{std::move(rules), std::move(chassis)}; |
| 112 | |
| 113 | // Call configure() |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 114 | system.configure(services); |
Shawn McCarney | 2af5289 | 2020-04-14 11:54:45 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 117 | TEST(SystemTests, GetChassis) |
| 118 | { |
| 119 | // Specify an empty rules vector |
| 120 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 121 | |
| 122 | // Create Chassis |
| 123 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 124 | chassis.emplace_back(std::make_unique<Chassis>(1)); |
| 125 | chassis.emplace_back(std::make_unique<Chassis>(3)); |
| 126 | |
| 127 | // Create System |
| 128 | System system{std::move(rules), std::move(chassis)}; |
| 129 | EXPECT_EQ(system.getChassis().size(), 2); |
| 130 | EXPECT_EQ(system.getChassis()[0]->getNumber(), 1); |
| 131 | EXPECT_EQ(system.getChassis()[1]->getNumber(), 3); |
| 132 | } |
| 133 | |
| 134 | TEST(SystemTests, GetIDMap) |
| 135 | { |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 136 | // Create Rules |
| 137 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 138 | rules.emplace_back(createRule("set_voltage_rule")); |
| 139 | rules.emplace_back(createRule("read_sensors_rule")); |
| 140 | |
| 141 | // Create Chassis |
| 142 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 143 | { |
| 144 | // Chassis 1 |
| 145 | std::vector<std::unique_ptr<Device>> devices{}; |
| 146 | devices.emplace_back(createDevice("reg1", {"rail1"})); |
| 147 | devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"})); |
| 148 | chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices))); |
| 149 | } |
| 150 | { |
| 151 | // Chassis 2 |
| 152 | std::vector<std::unique_ptr<Device>> devices{}; |
| 153 | devices.emplace_back(createDevice("reg3", {"rail3a", "rail3b"})); |
| 154 | devices.emplace_back(createDevice("reg4")); |
| 155 | chassis.emplace_back(std::make_unique<Chassis>(2, std::move(devices))); |
| 156 | } |
| 157 | |
| 158 | // Create System |
| 159 | System system{std::move(rules), std::move(chassis)}; |
| 160 | const IDMap& idMap = system.getIDMap(); |
| 161 | |
| 162 | // Verify all Rules are in the IDMap |
| 163 | EXPECT_NO_THROW(idMap.getRule("set_voltage_rule")); |
| 164 | EXPECT_NO_THROW(idMap.getRule("read_sensors_rule")); |
| 165 | EXPECT_THROW(idMap.getRule("set_voltage_rule2"), std::invalid_argument); |
| 166 | |
| 167 | // Verify all Devices are in the IDMap |
| 168 | EXPECT_NO_THROW(idMap.getDevice("reg1")); |
| 169 | EXPECT_NO_THROW(idMap.getDevice("reg2")); |
| 170 | EXPECT_NO_THROW(idMap.getDevice("reg3")); |
| 171 | EXPECT_NO_THROW(idMap.getDevice("reg4")); |
| 172 | EXPECT_THROW(idMap.getDevice("reg5"), std::invalid_argument); |
| 173 | |
| 174 | // Verify all Rails are in the IDMap |
| 175 | EXPECT_NO_THROW(idMap.getRail("rail1")); |
| 176 | EXPECT_NO_THROW(idMap.getRail("rail2a")); |
| 177 | EXPECT_NO_THROW(idMap.getRail("rail2b")); |
| 178 | EXPECT_NO_THROW(idMap.getRail("rail3a")); |
| 179 | EXPECT_NO_THROW(idMap.getRail("rail3b")); |
| 180 | EXPECT_THROW(idMap.getRail("rail4"), std::invalid_argument); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | TEST(SystemTests, GetRules) |
| 184 | { |
| 185 | // Create Rules |
| 186 | std::vector<std::unique_ptr<Rule>> rules{}; |
Shawn McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 187 | rules.emplace_back(createRule("set_voltage_rule")); |
| 188 | rules.emplace_back(createRule("read_sensors_rule")); |
Shawn McCarney | c3991f1 | 2020-04-05 13:16:06 -0500 | [diff] [blame] | 189 | |
| 190 | // Create Chassis |
| 191 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 192 | chassis.emplace_back(std::make_unique<Chassis>(1)); |
| 193 | |
| 194 | // Create System |
| 195 | System system{std::move(rules), std::move(chassis)}; |
| 196 | EXPECT_EQ(system.getRules().size(), 2); |
| 197 | EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule"); |
| 198 | EXPECT_EQ(system.getRules()[1]->getID(), "read_sensors_rule"); |
| 199 | } |
Bob King | 8e2294d | 2020-07-14 17:41:31 +0800 | [diff] [blame] | 200 | |
| 201 | TEST(SystemTests, MonitorSensors) |
| 202 | { |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 203 | // Create mock services. No logging should occur. |
| 204 | MockServices services{}; |
| 205 | MockJournal& journal = services.getMockJournal(); |
| 206 | EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0); |
| 207 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
| 208 | |
Bob King | 8e2294d | 2020-07-14 17:41:31 +0800 | [diff] [blame] | 209 | // Create PMBusReadSensorAction |
| 210 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 211 | uint8_t command = 0x8C; |
| 212 | pmbus_utils::SensorDataFormat format{ |
| 213 | pmbus_utils::SensorDataFormat::linear_11}; |
| 214 | std::optional<int8_t> exponent{}; |
| 215 | std::unique_ptr<PMBusReadSensorAction> action = |
| 216 | std::make_unique<PMBusReadSensorAction>(type, command, format, |
| 217 | exponent); |
| 218 | |
| 219 | // Create mock I2CInterface. A two-byte read should occur. |
| 220 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 221 | std::make_unique<i2c::MockedI2CInterface>(); |
| 222 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 223 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>())) |
| 224 | .Times(1); |
| 225 | |
| 226 | // Create SensorMonitoring |
| 227 | std::vector<std::unique_ptr<Action>> actions{}; |
| 228 | actions.emplace_back(std::move(action)); |
| 229 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 230 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 231 | |
| 232 | // Create Rail |
| 233 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 234 | std::unique_ptr<Configuration> configuration{}; |
| 235 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 236 | "vdd0", std::move(configuration), std::move(sensorMonitoring)); |
| 237 | rails.emplace_back(std::move(rail)); |
| 238 | |
| 239 | // Create Device |
| 240 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 241 | std::unique_ptr<Configuration> deviceConfiguration{}; |
| 242 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 243 | "reg1", true, "/system/chassis/motherboard/reg1", |
| 244 | std::move(i2cInterface), std::move(presenceDetection), |
| 245 | std::move(deviceConfiguration), std::move(rails)); |
| 246 | |
| 247 | // Create Chassis |
| 248 | std::vector<std::unique_ptr<Device>> devices{}; |
| 249 | devices.emplace_back(std::move(device)); |
| 250 | std::unique_ptr<Chassis> chassis = |
| 251 | std::make_unique<Chassis>(1, std::move(devices)); |
| 252 | |
| 253 | // Create System that contains Chassis |
| 254 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 255 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 256 | chassisVec.emplace_back(std::move(chassis)); |
| 257 | System system{std::move(rules), std::move(chassisVec)}; |
| 258 | |
| 259 | // Call monitorSensors() |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 260 | system.monitorSensors(services); |
Bob King | 8e2294d | 2020-07-14 17:41:31 +0800 | [diff] [blame] | 261 | } |