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