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" |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 24 | #include "mock_services.hpp" |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 25 | #include "mocked_i2c_interface.hpp" |
| 26 | #include "pmbus_read_sensor_action.hpp" |
| 27 | #include "pmbus_utils.hpp" |
| 28 | #include "presence_detection.hpp" |
| 29 | #include "rail.hpp" |
| 30 | #include "rule.hpp" |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 31 | #include "sensor_monitoring.hpp" |
Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 32 | #include "sensors.hpp" |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 33 | #include "system.hpp" |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 34 | |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 35 | #include <cstdint> |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 36 | #include <memory> |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 37 | #include <optional> |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 38 | #include <utility> |
| 39 | #include <vector> |
| 40 | |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 41 | #include <gmock/gmock.h> |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 42 | #include <gtest/gtest.h> |
| 43 | |
| 44 | using namespace phosphor::power::regulators; |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 45 | using namespace phosphor::power::regulators::pmbus_utils; |
| 46 | |
| 47 | using ::testing::A; |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 48 | using ::testing::Ref; |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 49 | using ::testing::Return; |
| 50 | using ::testing::Throw; |
| 51 | using ::testing::TypedEq; |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 52 | |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 53 | static const std::string chassisInvPath{ |
| 54 | "/xyz/openbmc_project/inventory/system/chassis"}; |
| 55 | |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 56 | TEST(SensorMonitoringTests, Constructor) |
| 57 | { |
| 58 | std::vector<std::unique_ptr<Action>> actions{}; |
| 59 | actions.push_back(std::make_unique<MockAction>()); |
| 60 | |
| 61 | SensorMonitoring sensorMonitoring(std::move(actions)); |
| 62 | EXPECT_EQ(sensorMonitoring.getActions().size(), 1); |
| 63 | } |
| 64 | |
| 65 | TEST(SensorMonitoringTests, Execute) |
| 66 | { |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 67 | // Test where works |
| 68 | { |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 69 | // Create mock services. No logging should occur. |
| 70 | MockServices services{}; |
| 71 | MockJournal& journal = services.getMockJournal(); |
| 72 | EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0); |
| 73 | EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0); |
| 74 | |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 75 | // Create PMBusReadSensorAction |
Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 76 | SensorType type{SensorType::iout}; |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 77 | uint8_t command = 0x8C; |
| 78 | pmbus_utils::SensorDataFormat format{ |
| 79 | pmbus_utils::SensorDataFormat::linear_11}; |
| 80 | std::optional<int8_t> exponent{}; |
| 81 | std::unique_ptr<PMBusReadSensorAction> action = |
| 82 | std::make_unique<PMBusReadSensorAction>(type, command, format, |
| 83 | exponent); |
| 84 | |
| 85 | // Create mock I2CInterface. |
| 86 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 87 | std::make_unique<i2c::MockedI2CInterface>(); |
| 88 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 89 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>())) |
| 90 | .Times(1); |
| 91 | |
| 92 | // Create SensorMonitoring |
| 93 | std::vector<std::unique_ptr<Action>> actions{}; |
| 94 | actions.emplace_back(std::move(action)); |
| 95 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 96 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 97 | SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get(); |
| 98 | |
| 99 | // Create Rail that contains sensorMonitoring |
| 100 | std::unique_ptr<Configuration> configuration{}; |
| 101 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 102 | "vio2", std::move(configuration), std::move(sensorMonitoring)); |
| 103 | Rail* railPtr = rail.get(); |
| 104 | |
| 105 | // Create Device that contains Rail |
| 106 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 107 | std::unique_ptr<Configuration> deviceConfiguration{}; |
| 108 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 109 | rails.emplace_back(std::move(rail)); |
| 110 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 111 | "reg1", true, |
| 112 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 113 | std::move(i2cInterface), std::move(presenceDetection), |
| 114 | std::move(deviceConfiguration), std::move(rails)); |
| 115 | Device* devicePtr = device.get(); |
| 116 | |
| 117 | // Create Chassis that contains Device |
| 118 | std::vector<std::unique_ptr<Device>> devices{}; |
| 119 | devices.emplace_back(std::move(device)); |
| 120 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 121 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 122 | Chassis* chassisPtr = chassis.get(); |
| 123 | |
| 124 | // Create System that contains Chassis |
| 125 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 126 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 127 | chassisVec.emplace_back(std::move(chassis)); |
| 128 | System system{std::move(rules), std::move(chassisVec)}; |
| 129 | |
| 130 | // Execute sensorMonitoring |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 131 | sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr, |
| 132 | *railPtr); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Test where fails |
| 136 | { |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 137 | // Create mock services. Expect logError() and logI2CError() to be |
| 138 | // called. |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 139 | MockServices services{}; |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 140 | MockErrorLogging& errorLogging = services.getMockErrorLogging(); |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 141 | MockJournal& journal = services.getMockJournal(); |
| 142 | EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0); |
| 143 | std::vector<std::string> expectedErrMessagesException{ |
| 144 | "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70", |
| 145 | "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, " |
| 146 | "format: linear_11 }"}; |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 147 | EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1); |
| 148 | EXPECT_CALL(journal, |
| 149 | logError("Unable to monitor sensors for rail vio2")) |
| 150 | .Times(1); |
Shawn McCarney | 81a2f90 | 2021-03-23 21:41:34 -0500 | [diff] [blame] | 151 | EXPECT_CALL(errorLogging, |
| 152 | logI2CError(Entry::Level::Warning, Ref(journal), |
| 153 | "/dev/i2c-1", 0x70, 0)) |
| 154 | .Times(1); |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 155 | |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 156 | // Create PMBusReadSensorAction |
Shawn McCarney | 2f9e14f | 2021-04-29 02:45:18 -0500 | [diff] [blame] | 157 | SensorType type{SensorType::iout}; |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 158 | uint8_t command = 0x8C; |
| 159 | pmbus_utils::SensorDataFormat format{ |
| 160 | pmbus_utils::SensorDataFormat::linear_11}; |
| 161 | std::optional<int8_t> exponent{}; |
| 162 | std::unique_ptr<PMBusReadSensorAction> action = |
| 163 | std::make_unique<PMBusReadSensorAction>(type, command, format, |
| 164 | exponent); |
| 165 | |
| 166 | // Create mock I2CInterface. |
| 167 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 168 | std::make_unique<i2c::MockedI2CInterface>(); |
| 169 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 170 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>())) |
| 171 | .Times(1) |
| 172 | .WillOnce(Throw( |
| 173 | i2c::I2CException{"Failed to write byte", "/dev/i2c-1", 0x70})); |
| 174 | |
| 175 | // Create SensorMonitoring |
| 176 | std::vector<std::unique_ptr<Action>> actions{}; |
| 177 | actions.emplace_back(std::move(action)); |
| 178 | std::unique_ptr<SensorMonitoring> sensorMonitoring = |
| 179 | std::make_unique<SensorMonitoring>(std::move(actions)); |
| 180 | SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get(); |
| 181 | |
| 182 | // Create Rail that contains sensorMonitoring |
| 183 | std::unique_ptr<Configuration> configuration{}; |
| 184 | std::unique_ptr<Rail> rail = std::make_unique<Rail>( |
| 185 | "vio2", std::move(configuration), std::move(sensorMonitoring)); |
| 186 | Rail* railPtr = rail.get(); |
| 187 | |
| 188 | // Create Device that contains Rail |
| 189 | std::unique_ptr<PresenceDetection> presenceDetection{}; |
| 190 | std::unique_ptr<Configuration> deviceConfiguration{}; |
| 191 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 192 | rails.emplace_back(std::move(rail)); |
| 193 | std::unique_ptr<Device> device = std::make_unique<Device>( |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 194 | "reg1", true, |
| 195 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 196 | std::move(i2cInterface), std::move(presenceDetection), |
| 197 | std::move(deviceConfiguration), std::move(rails)); |
| 198 | Device* devicePtr = device.get(); |
| 199 | |
| 200 | // Create Chassis that contains Device |
| 201 | std::vector<std::unique_ptr<Device>> devices{}; |
| 202 | devices.emplace_back(std::move(device)); |
| 203 | std::unique_ptr<Chassis> chassis = |
Shawn McCarney | cb3f6a6 | 2021-04-30 10:54:30 -0500 | [diff] [blame] | 204 | std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 205 | Chassis* chassisPtr = chassis.get(); |
| 206 | |
| 207 | // Create System that contains Chassis |
| 208 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 209 | std::vector<std::unique_ptr<Chassis>> chassisVec{}; |
| 210 | chassisVec.emplace_back(std::move(chassis)); |
| 211 | System system{std::move(rules), std::move(chassisVec)}; |
| 212 | |
| 213 | // Execute sensorMonitoring |
Bob King | 8a55292 | 2020-08-05 17:02:31 +0800 | [diff] [blame] | 214 | sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr, |
| 215 | *railPtr); |
Bob King | 833b8e0 | 2020-06-11 14:56:38 +0800 | [diff] [blame] | 216 | } |
Shawn McCarney | bc47c1b | 2020-03-10 15:38:07 -0500 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | TEST(SensorMonitoringTests, GetActions) |
| 220 | { |
| 221 | std::vector<std::unique_ptr<Action>> actions{}; |
| 222 | |
| 223 | MockAction* action1 = new MockAction{}; |
| 224 | actions.push_back(std::unique_ptr<MockAction>{action1}); |
| 225 | |
| 226 | MockAction* action2 = new MockAction{}; |
| 227 | actions.push_back(std::unique_ptr<MockAction>{action2}); |
| 228 | |
| 229 | SensorMonitoring sensorMonitoring(std::move(actions)); |
| 230 | EXPECT_EQ(sensorMonitoring.getActions().size(), 2); |
| 231 | EXPECT_EQ(sensorMonitoring.getActions()[0].get(), action1); |
| 232 | EXPECT_EQ(sensorMonitoring.getActions()[1].get(), action2); |
| 233 | } |