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 | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 16 | #include "action.hpp" |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 17 | #include "chassis.hpp" |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 18 | #include "configuration.hpp" |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 19 | #include "device.hpp" |
| 20 | #include "i2c_interface.hpp" |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 21 | #include "mock_action.hpp" |
Shawn McCarney | 2ccf961 | 2021-05-14 11:09:17 -0500 | [diff] [blame] | 22 | #include "mock_error_logging.hpp" |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -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 | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 26 | #include "mocked_i2c_interface.hpp" |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 27 | #include "phase_fault_detection.hpp" |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 28 | #include "presence_detection.hpp" |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 29 | #include "rail.hpp" |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 30 | #include "rule.hpp" |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -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 | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 33 | #include "system.hpp" |
Shawn McCarney | 2ccf961 | 2021-05-14 11:09:17 -0500 | [diff] [blame] | 34 | #include "test_sdbus_error.hpp" |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 35 | |
| 36 | #include <memory> |
| 37 | #include <optional> |
| 38 | #include <utility> |
| 39 | #include <vector> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 40 | |
Shawn McCarney | eb7bec4 | 2020-04-14 09:38:15 -0500 | [diff] [blame] | 41 | #include <gmock/gmock.h> |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 42 | #include <gtest/gtest.h> |
| 43 | |
| 44 | using namespace phosphor::power::regulators; |
| 45 | |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 46 | using ::testing::A; |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 47 | using ::testing::Return; |
Shawn McCarney | 2ccf961 | 2021-05-14 11:09:17 -0500 | [diff] [blame] | 48 | using ::testing::Throw; |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 49 | using ::testing::TypedEq; |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 50 | |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 51 | static const std::string chassisInvPath{ |
| 52 | "/xyz/openbmc_project/inventory/system/chassis"}; |
| 53 | |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 54 | TEST(RailTests, Constructor) |
| 55 | { |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 56 | // Test where only required parameters are specified |
| 57 | { |
| 58 | Rail rail{"vdd0"}; |
| 59 | EXPECT_EQ(rail.getID(), "vdd0"); |
| 60 | EXPECT_EQ(rail.getConfiguration(), nullptr); |
| 61 | EXPECT_EQ(rail.getSensorMonitoring(), nullptr); |
| 62 | } |
| 63 | |
| 64 | // Test where all parameters are specified |
| 65 | { |
| 66 | // Create Configuration |
| 67 | std::optional<double> volts{1.3}; |
| 68 | std::vector<std::unique_ptr<Action>> actions{}; |
| 69 | actions.push_back(std::make_unique<MockAction>()); |
| 70 | actions.push_back(std::make_unique<MockAction>()); |
| 71 | std::unique_ptr<Configuration> configuration = |
| 72 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 73 | |
| 74 | // Create SensorMonitoring |
| 75 | actions.clear(); |
| 76 | actions.push_back(std::make_unique<MockAction>()); |
| 77 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 78 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 79 | |
| 80 | // Create Rail |
| 81 | Rail rail{"vddr1", std::move(configuration), |
| 82 | std::move(sensorMonitoring)}; |
| 83 | EXPECT_EQ(rail.getID(), "vddr1"); |
| 84 | EXPECT_NE(rail.getConfiguration(), nullptr); |
| 85 | EXPECT_EQ(rail.getConfiguration()->getVolts().has_value(), true); |
| 86 | EXPECT_EQ(rail.getConfiguration()->getVolts().value(), 1.3); |
| 87 | EXPECT_EQ(rail.getConfiguration()->getActions().size(), 2); |
| 88 | EXPECT_NE(rail.getSensorMonitoring(), nullptr); |
| 89 | EXPECT_EQ(rail.getSensorMonitoring()->getActions().size(), 1); |
| 90 | } |
| 91 | } |
| 92 | |
Shawn McCarney | 2ccf961 | 2021-05-14 11:09:17 -0500 | [diff] [blame] | 93 | TEST(RailTests, ClearErrorHistory) |
| 94 | { |
| 95 | // Create SensorMonitoring. Will fail with a DBus exception. |
| 96 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 97 | EXPECT_CALL(*action, execute) |
| 98 | .WillRepeatedly(Throw(TestSDBusError{"Unable to set sensor value"})); |
| 99 | std::vector<std::unique_ptr<Action>> actions{}; |
| 100 | actions.emplace_back(std::move(action)); |
| 101 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 102 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 103 | |
| 104 | // Create Rail |
| 105 | std::unique_ptr<Configuration> configuration{}; |
| 106 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 107 | "vddr1", std::move(configuration), std::move(sensorMonitoring)); |
| 108 | Rail* railPtr = rail.get(); |
| 109 | |
| 110 | // Create Device that contains Rail |
| 111 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 112 | std::make_unique<i2c::MockedI2CInterface>(); |
| 113 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 114 | std::unique_ptr<Configuration> deviceConfiguration{}; |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 115 | std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{}; |
Shawn McCarney | 2ccf961 | 2021-05-14 11:09:17 -0500 | [diff] [blame] | 116 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 117 | rails.emplace_back(std::move(rail)); |
| 118 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 119 | "reg1", true, |
| 120 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 121 | std::move(i2cInterface), std::move(presenceDetection), |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 122 | std::move(deviceConfiguration), std::move(phaseFaultDetection), |
| 123 | std::move(rails)); |
Shawn McCarney | 2ccf961 | 2021-05-14 11:09:17 -0500 | [diff] [blame] | 124 | Device* devicePtr = device.get(); |
| 125 | |
| 126 | // Create Chassis that contains Device |
| 127 | std::vector<std::unique_ptr<Device>> devices{}; |
| 128 | devices.emplace_back(std::move(device)); |
| 129 | std::unique_ptr<Chassis> chassis = |
| 130 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
| 131 | Chassis* chassisPtr = chassis.get(); |
| 132 | |
| 133 | // Create System that contains Chassis |
| 134 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 135 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 136 | chassisVec.emplace_back(std::move(chassis)); |
| 137 | System system{std::move(rules), std::move(chassisVec)}; |
| 138 | |
| 139 | // Create mock services |
| 140 | MockServices services{}; |
| 141 | |
| 142 | // Expect Sensors service to be called 5+5=10 times |
| 143 | MockSensors& sensors = services.getMockSensors(); |
| 144 | EXPECT_CALL(sensors, startRail).Times(10); |
| 145 | EXPECT_CALL(sensors, setValue).Times(0); |
| 146 | EXPECT_CALL(sensors, endRail).Times(10); |
| 147 | |
| 148 | // Expect Journal service to be called 3+3=6 times to log error messages |
| 149 | MockJournal& journal = services.getMockJournal(); |
| 150 | EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>())) |
| 151 | .Times(6); |
| 152 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(6); |
| 153 | |
| 154 | // Expect ErrorLogging service to be called 1+1=2 times to log a DBus error |
| 155 | MockErrorLogging& errorLogging = services.getMockErrorLogging(); |
| 156 | EXPECT_CALL(errorLogging, logDBusError).Times(2); |
| 157 | |
| 158 | // Monitor sensors 5 times. Should fail every time, write to journal 3 |
| 159 | // times, and log one error. |
| 160 | for (int i = 1; i <= 5; ++i) |
| 161 | { |
| 162 | railPtr->monitorSensors(services, system, *chassisPtr, *devicePtr); |
| 163 | } |
| 164 | |
| 165 | // Clear error history |
| 166 | railPtr->clearErrorHistory(); |
| 167 | |
| 168 | // Monitor sensors 5 times again. Should fail every time, write to journal |
| 169 | // 3 times, and log one error. |
| 170 | for (int i = 1; i <= 5; ++i) |
| 171 | { |
| 172 | railPtr->monitorSensors(services, system, *chassisPtr, *devicePtr); |
| 173 | } |
| 174 | } |
| 175 | |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 176 | TEST(RailTests, Configure) |
| 177 | { |
| 178 | // Test where Configuration was not specified in constructor |
| 179 | { |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 180 | // Create mock services. No logging should occur. |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 181 | MockServices services{}; |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 182 | MockJournal& journal = services.getMockJournal(); |
| 183 | EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0); |
| 184 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 185 | |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 186 | // Create Rail |
| 187 | std::unique_ptr<Rail> rail = std::make_unique<Rail>("vdd0"); |
| 188 | Rail* railPtr = rail.get(); |
| 189 | |
| 190 | // Create Device that contains Rail |
| 191 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 192 | std::make_unique<i2c::MockedI2CInterface>(); |
| 193 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 194 | std::unique_ptr<Configuration> deviceConfiguration{}; |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 195 | std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{}; |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 196 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 197 | rails.emplace_back(std::move(rail)); |
| 198 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 199 | "reg1", true, |
| 200 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 201 | std::move(i2cInterface), std::move(presenceDetection), |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 202 | std::move(deviceConfiguration), std::move(phaseFaultDetection), |
| 203 | std::move(rails)); |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 204 | Device* devicePtr = device.get(); |
| 205 | |
| 206 | // Create Chassis that contains Device |
| 207 | std::vector<std::unique_ptr<Device>> devices{}; |
| 208 | devices.emplace_back(std::move(device)); |
| 209 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 210 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 211 | Chassis* chassisPtr = chassis.get(); |
| 212 | |
| 213 | // Create System that contains Chassis |
| 214 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 215 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 216 | chassisVec.emplace_back(std::move(chassis)); |
| 217 | System system{std::move(rules), std::move(chassisVec)}; |
| 218 | |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 219 | // Call configure(). |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 220 | railPtr->configure(services, system, *chassisPtr, *devicePtr); |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Test where Configuration was specified in constructor |
| 224 | { |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 225 | // Create mock services. Expect logDebug() to be called. |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 226 | MockServices services{}; |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 227 | MockJournal& journal = services.getMockJournal(); |
| 228 | EXPECT_CALL(journal, logDebug("Configuring vddr1: volts=1.300000")) |
| 229 | .Times(1); |
| 230 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 231 | |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 232 | // Create Configuration |
| 233 | std::optional<double> volts{1.3}; |
| 234 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 235 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
| 236 | std::vector<std::unique_ptr<Action>> actions{}; |
| 237 | actions.emplace_back(std::move(action)); |
| 238 | std::unique_ptr<Configuration> configuration = |
| 239 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 240 | |
| 241 | // Create Rail |
| 242 | std::unique_ptr<Rail> rail = |
| 243 | std::make_unique<Rail>("vddr1", std::move(configuration)); |
| 244 | Rail* railPtr = rail.get(); |
| 245 | |
| 246 | // Create Device that contains Rail |
| 247 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 248 | std::make_unique<i2c::MockedI2CInterface>(); |
| 249 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 250 | std::unique_ptr<Configuration> deviceConfiguration{}; |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 251 | std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{}; |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 252 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 253 | rails.emplace_back(std::move(rail)); |
| 254 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 255 | "reg1", true, |
| 256 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 257 | std::move(i2cInterface), std::move(presenceDetection), |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 258 | std::move(deviceConfiguration), std::move(phaseFaultDetection), |
| 259 | std::move(rails)); |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 260 | Device* devicePtr = device.get(); |
| 261 | |
| 262 | // Create Chassis that contains Device |
| 263 | std::vector<std::unique_ptr<Device>> devices{}; |
| 264 | devices.emplace_back(std::move(device)); |
| 265 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 266 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 267 | Chassis* chassisPtr = chassis.get(); |
| 268 | |
| 269 | // Create System that contains Chassis |
| 270 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 271 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 272 | chassisVec.emplace_back(std::move(chassis)); |
| 273 | System system{std::move(rules), std::move(chassisVec)}; |
| 274 | |
Bob King | 5cfe510 | 2020-07-30 16:26:18 +0800 | [diff] [blame] | 275 | // Call configure(). |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 276 | railPtr->configure(services, system, *chassisPtr, *devicePtr); |
Shawn McCarney | 779b956 | 2020-04-13 17:05:45 -0500 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 280 | TEST(RailTests, GetConfiguration) |
| 281 | { |
| 282 | // Test where Configuration was not specified in constructor |
| 283 | { |
| 284 | Rail rail{"vdd0"}; |
| 285 | EXPECT_EQ(rail.getConfiguration(), nullptr); |
| 286 | } |
| 287 | |
| 288 | // Test where Configuration was specified in constructor |
| 289 | { |
| 290 | // Create Configuration |
| 291 | std::optional<double> volts{3.2}; |
| 292 | std::vector<std::unique_ptr<Action>> actions{}; |
| 293 | actions.push_back(std::make_unique<MockAction>()); |
| 294 | std::unique_ptr<Configuration> configuration = |
| 295 | std::make_unique<Configuration>(volts, std::move(actions)); |
| 296 | |
| 297 | // Create Rail |
| 298 | Rail rail{"vddr1", std::move(configuration)}; |
| 299 | EXPECT_NE(rail.getConfiguration(), nullptr); |
| 300 | EXPECT_EQ(rail.getConfiguration()->getVolts().has_value(), true); |
| 301 | EXPECT_EQ(rail.getConfiguration()->getVolts().value(), 3.2); |
| 302 | EXPECT_EQ(rail.getConfiguration()->getActions().size(), 1); |
| 303 | } |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 304 | } |
| 305 | |
Shawn McCarney | 4afb285 | 2019-10-27 18:28:53 -0500 | [diff] [blame] | 306 | TEST(RailTests, GetID) |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 307 | { |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 308 | Rail rail{"vio2"}; |
Shawn McCarney | 4afb285 | 2019-10-27 18:28:53 -0500 | [diff] [blame] | 309 | EXPECT_EQ(rail.getID(), "vio2"); |
Shawn McCarney | a2461b3 | 2019-10-24 18:53:01 -0500 | [diff] [blame] | 310 | } |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 311 | |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 312 | TEST(RailTests, MonitorSensors) |
| 313 | { |
| 314 | // Test where SensorMonitoring was not specified in constructor |
| 315 | { |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 316 | // Create mock services. No Sensors methods should be called. |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 317 | MockServices services{}; |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 318 | MockSensors& sensors = services.getMockSensors(); |
| 319 | EXPECT_CALL(sensors, startRail).Times(0); |
| 320 | EXPECT_CALL(sensors, setValue).Times(0); |
| 321 | EXPECT_CALL(sensors, endRail).Times(0); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 322 | |
| 323 | // Create Rail |
| 324 | std::unique_ptr<Rail> rail = std::make_unique<Rail>("vdd0"); |
| 325 | Rail* railPtr = rail.get(); |
| 326 | |
| 327 | // Create Device that contains Rail |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 328 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 329 | std::make_unique<i2c::MockedI2CInterface>(); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 330 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 331 | std::unique_ptr<Configuration> deviceConfiguration{}; |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 332 | std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{}; |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 333 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 334 | rails.emplace_back(std::move(rail)); |
| 335 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 336 | "reg1", true, |
| 337 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 338 | std::move(i2cInterface), std::move(presenceDetection), |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 339 | std::move(deviceConfiguration), std::move(phaseFaultDetection), |
| 340 | std::move(rails)); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 341 | Device* devicePtr = device.get(); |
| 342 | |
| 343 | // Create Chassis that contains Device |
| 344 | std::vector<std::unique_ptr<Device>> devices{}; |
| 345 | devices.emplace_back(std::move(device)); |
| 346 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 347 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 348 | Chassis* chassisPtr = chassis.get(); |
| 349 | |
| 350 | // Create System that contains Chassis |
| 351 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 352 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 353 | chassisVec.emplace_back(std::move(chassis)); |
| 354 | System system{std::move(rules), std::move(chassisVec)}; |
| 355 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 356 | // Call monitorSensors() |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 357 | railPtr->monitorSensors(services, system, *chassisPtr, *devicePtr); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // Test where SensorMonitoring was specified in constructor |
| 361 | { |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 362 | // Create mock services. Set Sensors service expectations. |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 363 | MockServices services{}; |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 364 | MockSensors& sensors = services.getMockSensors(); |
| 365 | EXPECT_CALL(sensors, |
| 366 | startRail("vddr1", |
| 367 | "/xyz/openbmc_project/inventory/system/chassis/" |
| 368 | "motherboard/reg1", |
| 369 | chassisInvPath)) |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 370 | .Times(1); |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 371 | EXPECT_CALL(sensors, setValue).Times(0); |
| 372 | EXPECT_CALL(sensors, endRail(false)).Times(1); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 373 | |
| 374 | // Create SensorMonitoring |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 375 | std::unique_ptr<MockAction> action = std::make_unique<MockAction>(); |
| 376 | EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true)); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 377 | std::vector<std::unique_ptr<Action>> actions{}; |
| 378 | actions.emplace_back(std::move(action)); |
| 379 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 380 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 381 | |
| 382 | // Create Rail |
| 383 | std::unique_ptr<Configuration> configuration{}; |
| 384 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 385 | "vddr1", std::move(configuration), std::move(sensorMonitoring)); |
| 386 | Rail* railPtr = rail.get(); |
| 387 | |
| 388 | // Create Device that contains Rail |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 389 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 390 | std::make_unique<i2c::MockedI2CInterface>(); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 391 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 392 | std::unique_ptr<Configuration> deviceConfiguration{}; |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 393 | std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{}; |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 394 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 395 | rails.emplace_back(std::move(rail)); |
| 396 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 397 | "reg1", true, |
| 398 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 399 | std::move(i2cInterface), std::move(presenceDetection), |
Shawn McCarney | 3225259 | 2021-09-08 15:29:36 -0500 | [diff] [blame] | 400 | std::move(deviceConfiguration), std::move(phaseFaultDetection), |
| 401 | std::move(rails)); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 402 | Device* devicePtr = device.get(); |
| 403 | |
| 404 | // Create Chassis that contains Device |
| 405 | std::vector<std::unique_ptr<Device>> devices{}; |
| 406 | devices.emplace_back(std::move(device)); |
| 407 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 408 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 409 | Chassis* chassisPtr = chassis.get(); |
| 410 | |
| 411 | // Create System that contains Chassis |
| 412 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 413 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 414 | chassisVec.emplace_back(std::move(chassis)); |
| 415 | System system{std::move(rules), std::move(chassisVec)}; |
| 416 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 417 | // Call monitorSensors() |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 418 | railPtr->monitorSensors(services, system, *chassisPtr, *devicePtr); |
Bob King | 7b74343 | 2020-06-22 17:35:04 +0800 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Shawn McCarney | 4bf310e | 2020-03-10 17:48:11 -0500 | [diff] [blame] | 422 | TEST(RailTests, GetSensorMonitoring) |
| 423 | { |
| 424 | // Test where SensorMonitoring was not specified in constructor |
| 425 | { |
| 426 | Rail rail{"vdd0", nullptr, nullptr}; |
| 427 | EXPECT_EQ(rail.getSensorMonitoring(), nullptr); |
| 428 | } |
| 429 | |
| 430 | // Test where SensorMonitoring was specified in constructor |
| 431 | { |
| 432 | std::unique_ptr<Configuration> configuration{}; |
| 433 | |
| 434 | // Create SensorMonitoring |
| 435 | std::vector<std::unique_ptr<Action>> actions{}; |
| 436 | actions.push_back(std::make_unique<MockAction>()); |
| 437 | actions.push_back(std::make_unique<MockAction>()); |
| 438 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 439 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 440 | |
| 441 | // Create Rail |
| 442 | Rail rail{"vddr1", std::move(configuration), |
| 443 | std::move(sensorMonitoring)}; |
| 444 | EXPECT_NE(rail.getSensorMonitoring(), nullptr); |
| 445 | EXPECT_EQ(rail.getSensorMonitoring()->getActions().size(), 2); |
| 446 | } |
| 447 | } |