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