Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -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 | */ |
| 16 | #include "action.hpp" |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 17 | #include "chassis.hpp" |
| 18 | #include "configuration.hpp" |
| 19 | #include "device.hpp" |
| 20 | #include "i2c_interface.hpp" |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 21 | #include "mock_action.hpp" |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 22 | #include "mock_error_logging.hpp" |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [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 | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 25 | #include "mock_services.hpp" |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 26 | #include "mocked_i2c_interface.hpp" |
| 27 | #include "pmbus_read_sensor_action.hpp" |
| 28 | #include "pmbus_utils.hpp" |
| 29 | #include "presence_detection.hpp" |
| 30 | #include "rail.hpp" |
| 31 | #include "rule.hpp" |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 32 | #include "sensor_monitoring.hpp" |
Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 33 | #include "sensors.hpp" |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 34 | #include "system.hpp" |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 35 | |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 36 | #include <cstdint> |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 37 | #include <memory> |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 38 | #include <optional> |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 39 | #include <string> |
| 40 | #include <tuple> |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 41 | #include <utility> |
| 42 | #include <vector> |
| 43 | |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 44 | #include <gmock/gmock.h> |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 45 | #include <gtest/gtest.h> |
| 46 | |
| 47 | using namespace phosphor::power::regulators; |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 48 | using namespace phosphor::power::regulators::pmbus_utils; |
| 49 | |
| 50 | using ::testing::A; |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 51 | using ::testing::Ref; |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 52 | using ::testing::Return; |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 53 | using ::testing::SetArgReferee; |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 54 | using ::testing::Throw; |
| 55 | using ::testing::TypedEq; |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 56 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 57 | /** |
| 58 | * Creates the parent objects that normally contain a SensorMonitoring object. |
| 59 | * |
| 60 | * A SensorMonitoring object is normally contained within a hierarchy of System, |
| 61 | * Chassis, Device, and Rail objects. These objects are required in order to |
| 62 | * call the execute() method. |
| 63 | * |
| 64 | * Creates the System, Chassis, Device, and Rail objects. The SensorMonitoring |
| 65 | * object is moved into the Rail object. |
| 66 | * |
| 67 | * @param monitoring SensorMonitoring object to move into object hierarchy |
| 68 | * @return Tuple containing pointers the parent objects and the |
| 69 | * MockedI2CInterface object. They are all contained within the System |
| 70 | * object and will be automatically destructed. |
| 71 | */ |
| 72 | std::tuple<std::unique_ptr<System>, Chassis*, Device*, i2c::MockedI2CInterface*, |
| 73 | Rail*> |
| 74 | createParentObjects(std::unique_ptr<SensorMonitoring> monitoring) |
| 75 | { |
| 76 | // Create Rail that contains SensorMonitoring |
| 77 | std::unique_ptr<Configuration> configuration{}; |
| 78 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 79 | "vdd", std::move(configuration), std::move(monitoring)); |
| 80 | Rail* railPtr = rail.get(); |
| 81 | |
| 82 | // Create mock I2CInterface |
| 83 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 84 | std::make_unique<i2c::MockedI2CInterface>(); |
| 85 | i2c::MockedI2CInterface* i2cInterfacePtr = i2cInterface.get(); |
| 86 | |
| 87 | // Create Device that contains Rail |
| 88 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 89 | std::unique_ptr<Configuration> deviceConfiguration{}; |
| 90 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 91 | rails.emplace_back(std::move(rail)); |
| 92 | std::unique_ptr<Device> device = std::make_unique<Device>( |
| 93 | "vdd_reg", true, |
| 94 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2", |
| 95 | std::move(i2cInterface), std::move(presenceDetection), |
| 96 | std::move(deviceConfiguration), std::move(rails)); |
| 97 | Device* devicePtr = device.get(); |
| 98 | |
| 99 | // Create Chassis that contains Device |
| 100 | std::vector<std::unique_ptr<Device>> devices{}; |
| 101 | devices.emplace_back(std::move(device)); |
| 102 | std::unique_ptr<Chassis> chassis = std::make_unique<Chassis>( |
| 103 | 1, "/xyz/openbmc_project/inventory/system/chassis", std::move(devices)); |
| 104 | Chassis* chassisPtr = chassis.get(); |
| 105 | |
| 106 | // Create System that contains Chassis |
| 107 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 108 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 109 | chassisVec.emplace_back(std::move(chassis)); |
| 110 | std::unique_ptr<System> system = |
| 111 | std::make_unique<System>(std::move(rules), std::move(chassisVec)); |
| 112 | |
| 113 | return std::make_tuple(std::move(system), chassisPtr, devicePtr, |
| 114 | i2cInterfacePtr, railPtr); |
| 115 | } |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 116 | |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 117 | TEST(SensorMonitoringTests, Constructor) |
| 118 | { |
| 119 | std::vector<std::unique_ptr<Action>> actions{}; |
| 120 | actions.push_back(std::make_unique<MockAction>()); |
| 121 | |
| 122 | SensorMonitoring sensorMonitoring(std::move(actions)); |
| 123 | EXPECT_EQ(sensorMonitoring.getActions().size(), 1); |
| 124 | } |
| 125 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 126 | TEST(SensorMonitoringTests, ClearErrorHistory) |
| 127 | { |
| 128 | // Create PMBusReadSensorAction |
| 129 | SensorType type{SensorType::iout}; |
| 130 | uint8_t command{0x8C}; |
| 131 | SensorDataFormat format{SensorDataFormat::linear_11}; |
| 132 | std::optional<int8_t> exponent{}; |
| 133 | std::unique_ptr<PMBusReadSensorAction> action = |
| 134 | std::make_unique<PMBusReadSensorAction>(type, command, format, |
| 135 | exponent); |
| 136 | |
| 137 | // Create SensorMonitoring |
| 138 | std::vector<std::unique_ptr<Action>> actions{}; |
| 139 | actions.emplace_back(std::move(action)); |
| 140 | SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions)); |
| 141 | |
| 142 | // Create parent objects that contain SensorMonitoring |
| 143 | auto [system, chassis, device, i2cInterface, rail] = |
| 144 | createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring}); |
| 145 | |
| 146 | // Set I2CInterface expectations |
| 147 | EXPECT_CALL(*i2cInterface, isOpen).WillRepeatedly(Return(true)); |
| 148 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>())) |
| 149 | .WillRepeatedly(Throw( |
| 150 | i2c::I2CException{"Failed to read word data", "/dev/i2c-1", 0x70})); |
| 151 | |
| 152 | // Perform sensor monitoring 10 times to set error history data members |
| 153 | { |
| 154 | // Create mock services |
| 155 | MockServices services{}; |
| 156 | |
| 157 | // Set Sensors service expectations. SensorMonitoring will be executed |
| 158 | // 10 times. |
| 159 | MockSensors& sensors = services.getMockSensors(); |
| 160 | EXPECT_CALL(sensors, startRail).Times(10); |
| 161 | EXPECT_CALL(sensors, setValue).Times(0); |
| 162 | EXPECT_CALL(sensors, endRail(true)).Times(10); |
| 163 | |
| 164 | // Set Journal service expectations. SensorMonitoring should log error |
| 165 | // messages 3 times and then stop. |
| 166 | MockJournal& journal = services.getMockJournal(); |
| 167 | EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>())) |
| 168 | .Times(3); |
| 169 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(3); |
| 170 | |
| 171 | // Set ErrorLogging service expectations. SensorMonitoring should log |
| 172 | // an error only once. |
| 173 | MockErrorLogging& errorLogging = services.getMockErrorLogging(); |
| 174 | EXPECT_CALL(errorLogging, logI2CError).Times(1); |
| 175 | |
| 176 | // Execute SensorMonitoring 10 times |
| 177 | for (int i = 1; i <= 10; ++i) |
| 178 | { |
| 179 | monitoring->execute(services, *system, *chassis, *device, *rail); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Clear error history |
| 184 | monitoring->clearErrorHistory(); |
| 185 | |
| 186 | // Perform sensor monitoring one more time. Should log errors again. |
| 187 | { |
| 188 | // Create mock services |
| 189 | MockServices services{}; |
| 190 | |
| 191 | // Set Sensors service expectations. SensorMonitoring will be executed |
| 192 | // 1 time. |
| 193 | MockSensors& sensors = services.getMockSensors(); |
| 194 | EXPECT_CALL(sensors, startRail).Times(1); |
| 195 | EXPECT_CALL(sensors, setValue).Times(0); |
| 196 | EXPECT_CALL(sensors, endRail(true)).Times(1); |
| 197 | |
| 198 | // Set Journal service expectations. SensorMonitoring should log error |
| 199 | // messages 1 time. |
| 200 | MockJournal& journal = services.getMockJournal(); |
| 201 | EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>())) |
| 202 | .Times(1); |
| 203 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(1); |
| 204 | |
| 205 | // Set ErrorLogging server expectations. SensorMonitoring should log an |
| 206 | // error. |
| 207 | MockErrorLogging& errorLogging = services.getMockErrorLogging(); |
| 208 | EXPECT_CALL(errorLogging, logI2CError).Times(1); |
| 209 | |
| 210 | // Execute SensorMonitoring |
| 211 | monitoring->execute(services, *system, *chassis, *device, *rail); |
| 212 | } |
| 213 | } |
| 214 | |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 215 | TEST(SensorMonitoringTests, Execute) |
| 216 | { |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 217 | // Test where works |
| 218 | { |
| 219 | // Create PMBusReadSensorAction |
Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 220 | SensorType type{SensorType::iout}; |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 221 | uint8_t command{0x8C}; |
| 222 | SensorDataFormat format{SensorDataFormat::linear_11}; |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 223 | std::optional<int8_t> exponent{}; |
| 224 | std::unique_ptr<PMBusReadSensorAction> action = |
| 225 | std::make_unique<PMBusReadSensorAction>(type, command, format, |
| 226 | exponent); |
| 227 | |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 228 | // Create SensorMonitoring |
| 229 | std::vector<std::unique_ptr<Action>> actions{}; |
| 230 | actions.emplace_back(std::move(action)); |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 231 | SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions)); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 232 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 233 | // Create parent objects that contain SensorMonitoring |
| 234 | auto [system, chassis, device, i2cInterface, rail] = |
| 235 | createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring}); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 236 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 237 | // Set I2CInterface expectations |
| 238 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 239 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>())) |
| 240 | .Times(1) |
| 241 | .WillOnce(SetArgReferee<1>(0xD2E0)); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 242 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 243 | // Create mock services. Set Sensors service expectations. |
| 244 | MockServices services{}; |
| 245 | MockSensors& sensors = services.getMockSensors(); |
| 246 | EXPECT_CALL(sensors, |
| 247 | startRail("vdd", |
| 248 | "/xyz/openbmc_project/inventory/system/chassis/" |
| 249 | "motherboard/reg2", |
| 250 | "/xyz/openbmc_project/inventory/system/chassis")) |
| 251 | .Times(1); |
| 252 | EXPECT_CALL(sensors, setValue(SensorType::iout, 11.5)).Times(1); |
| 253 | EXPECT_CALL(sensors, endRail(false)).Times(1); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 254 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 255 | // Execute SensorMonitoring |
| 256 | monitoring->execute(services, *system, *chassis, *device, *rail); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Test where fails |
| 260 | { |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 261 | // Create PMBusReadSensorAction |
| 262 | SensorType type{SensorType::iout}; |
| 263 | uint8_t command{0x8C}; |
| 264 | SensorDataFormat format{SensorDataFormat::linear_11}; |
| 265 | std::optional<int8_t> exponent{}; |
| 266 | std::unique_ptr<PMBusReadSensorAction> action = |
| 267 | std::make_unique<PMBusReadSensorAction>(type, command, format, |
| 268 | exponent); |
| 269 | |
| 270 | // Create SensorMonitoring |
| 271 | std::vector<std::unique_ptr<Action>> actions{}; |
| 272 | actions.emplace_back(std::move(action)); |
| 273 | SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions)); |
| 274 | |
| 275 | // Create parent objects that contain SensorMonitoring |
| 276 | auto [system, chassis, device, i2cInterface, rail] = |
| 277 | createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring}); |
| 278 | |
| 279 | // Set I2CInterface expectations. Should read register 0x8C 4 times. |
| 280 | EXPECT_CALL(*i2cInterface, isOpen) |
| 281 | .Times(4) |
| 282 | .WillRepeatedly(Return(true)); |
| 283 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>())) |
| 284 | .Times(4) |
| 285 | .WillRepeatedly(Throw(i2c::I2CException{"Failed to read word data", |
| 286 | "/dev/i2c-1", 0x70})); |
| 287 | |
| 288 | // Create mock services |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 289 | MockServices services{}; |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 290 | |
| 291 | // Set Sensors service expectations. SensorMonitoring will be executed |
| 292 | // 4 times, and all should fail. |
| 293 | MockSensors& sensors = services.getMockSensors(); |
| 294 | EXPECT_CALL(sensors, |
| 295 | startRail("vdd", |
| 296 | "/xyz/openbmc_project/inventory/system/chassis/" |
| 297 | "motherboard/reg2", |
| 298 | "/xyz/openbmc_project/inventory/system/chassis")) |
| 299 | .Times(4); |
| 300 | EXPECT_CALL(sensors, setValue).Times(0); |
| 301 | EXPECT_CALL(sensors, endRail(true)).Times(4); |
| 302 | |
| 303 | // Set Journal service expectations. SensorMonitoring should log error |
| 304 | // messages 3 times and then stop. |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 305 | MockJournal& journal = services.getMockJournal(); |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 306 | std::vector<std::string> expectedErrMessagesException{ |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 307 | "I2CException: Failed to read word data: bus /dev/i2c-1, addr 0x70", |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 308 | "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, " |
| 309 | "format: linear_11 }"}; |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 310 | EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(3); |
| 311 | EXPECT_CALL(journal, logError("Unable to monitor sensors for rail vdd")) |
| 312 | .Times(3); |
| 313 | |
| 314 | // Set ErrorLogging service expectations. SensorMonitoring should log |
| 315 | // an error only once. |
| 316 | MockErrorLogging& errorLogging = services.getMockErrorLogging(); |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 317 | EXPECT_CALL(errorLogging, |
| 318 | logI2CError(Entry::Level::Warning, Ref(journal), |
| 319 | "/dev/i2c-1", 0x70, 0)) |
| 320 | .Times(1); |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 321 | |
Shawn McCarney | 17bac89 | 2021-05-08 07:55:52 -0500 | [diff] [blame] | 322 | // Execute SensorMonitoring 4 times |
| 323 | for (int i = 1; i <= 4; ++i) |
| 324 | { |
| 325 | monitoring->execute(services, *system, *chassis, *device, *rail); |
| 326 | } |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 327 | } |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | TEST(SensorMonitoringTests, GetActions) |
| 331 | { |
| 332 | std::vector<std::unique_ptr<Action>> actions{}; |
| 333 | |
| 334 | MockAction* action1 = new MockAction{}; |
| 335 | actions.push_back(std::unique_ptr<MockAction>{action1}); |
| 336 | |
| 337 | MockAction* action2 = new MockAction{}; |
| 338 | actions.push_back(std::unique_ptr<MockAction>{action2}); |
| 339 | |
| 340 | SensorMonitoring sensorMonitoring(std::move(actions)); |
| 341 | EXPECT_EQ(sensorMonitoring.getActions().size(), 2); |
| 342 | EXPECT_EQ(sensorMonitoring.getActions()[0].get(), action1); |
| 343 | EXPECT_EQ(sensorMonitoring.getActions()[1].get(), action2); |
| 344 | } |