Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [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_environment.hpp" |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 17 | #include "action_error.hpp" |
| 18 | #include "device.hpp" |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 19 | #include "i2c_action.hpp" |
| 20 | #include "i2c_interface.hpp" |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 21 | #include "id_map.hpp" |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 22 | #include "mock_services.hpp" |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 23 | #include "mocked_i2c_interface.hpp" |
| 24 | #include "pmbus_error.hpp" |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 25 | #include "pmbus_read_sensor_action.hpp" |
| 26 | #include "pmbus_utils.hpp" |
| 27 | |
| 28 | #include <cstdint> |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 29 | #include <memory> |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 30 | #include <optional> |
| 31 | #include <stdexcept> |
| 32 | #include <string> |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 33 | #include <utility> |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 34 | |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 35 | #include <gmock/gmock.h> |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 36 | #include <gtest/gtest.h> |
| 37 | |
| 38 | using namespace phosphor::power::regulators; |
| 39 | |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 40 | using ::testing::A; |
| 41 | using ::testing::Return; |
| 42 | using ::testing::SetArgReferee; |
| 43 | using ::testing::Throw; |
| 44 | using ::testing::TypedEq; |
| 45 | |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 46 | TEST(PMBusReadSensorActionTests, Constructor) |
| 47 | { |
| 48 | // Test where works: exponent value is specified |
| 49 | try |
| 50 | { |
| 51 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 52 | uint8_t command = 0x8C; |
| 53 | pmbus_utils::SensorDataFormat format{ |
| 54 | pmbus_utils::SensorDataFormat::linear_16}; |
| 55 | std::optional<int8_t> exponent{-8}; |
| 56 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 57 | EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::iout); |
| 58 | EXPECT_EQ(action.getCommand(), 0x8C); |
| 59 | EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16); |
| 60 | EXPECT_EQ(action.getExponent().has_value(), true); |
| 61 | EXPECT_EQ(action.getExponent().value(), -8); |
| 62 | } |
| 63 | catch (...) |
| 64 | { |
| 65 | ADD_FAILURE() << "Should not have caught exception."; |
| 66 | } |
| 67 | |
| 68 | // Test where works: exponent value is not specified |
| 69 | try |
| 70 | { |
| 71 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 72 | uint8_t command = 0x8C; |
| 73 | pmbus_utils::SensorDataFormat format{ |
| 74 | pmbus_utils::SensorDataFormat::linear_11}; |
| 75 | std::optional<int8_t> exponent{}; |
| 76 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 77 | EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::iout); |
| 78 | EXPECT_EQ(action.getCommand(), 0x8C); |
| 79 | EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_11); |
| 80 | EXPECT_EQ(action.getExponent().has_value(), false); |
| 81 | } |
| 82 | catch (...) |
| 83 | { |
| 84 | ADD_FAILURE() << "Should not have caught exception."; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | TEST(PMBusReadSensorActionTests, Execute) |
| 89 | { |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 90 | // Test where works: linear_11 defined in action |
| 91 | try |
| 92 | { |
| 93 | // Create mock I2CInterface. |
| 94 | // * will read 0xD2E0 from READ_IOUT (command/register 0x8C) |
| 95 | // * will not read from VOUT_MODE (command/register 0x20) |
| 96 | // assume output current is 11.5 amps, |
| 97 | // exponent = -6 = 11010, mantissa = 736 = 010 1110 0000 |
| 98 | // linear data format = 1101 0010 1110 0000 = 0xD2E0 |
| 99 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 100 | std::make_unique<i2c::MockedI2CInterface>(); |
| 101 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 102 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>())) |
| 103 | .Times(1) |
| 104 | .WillOnce(SetArgReferee<1>(0xD2E0)); |
| 105 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 106 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 107 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 108 | Device device{ |
| 109 | "reg1", true, |
| 110 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 111 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 112 | IDMap idMap{}; |
| 113 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 114 | MockServices services{}; |
| 115 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 116 | |
| 117 | // Create and execute action |
| 118 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 119 | uint8_t command = 0x8C; |
| 120 | pmbus_utils::SensorDataFormat format{ |
| 121 | pmbus_utils::SensorDataFormat::linear_11}; |
| 122 | std::optional<int8_t> exponent{}; |
| 123 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 124 | EXPECT_EQ(action.execute(env), true); |
Shawn McCarney | 5135df6 | 2021-04-28 15:53:24 -0500 | [diff] [blame^] | 125 | /** |
| 126 | * TODO: Replace with EXPECT calls using MockSensors |
| 127 | * |
| 128 | * EXPECT_EQ(env.getSensorReadings().size(), 1); |
| 129 | * EXPECT_EQ(env.getSensorReadings()[0].type, |
| 130 | * pmbus_utils::SensorValueType::iout); |
| 131 | * EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 11.5); |
| 132 | */ |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 133 | } |
| 134 | catch (...) |
| 135 | { |
| 136 | ADD_FAILURE() << "Should not have caught exception."; |
| 137 | } |
| 138 | |
| 139 | // Test where works: linear_16 with exponent defined in action |
| 140 | try |
| 141 | { |
| 142 | // Create mock I2CInterface. |
| 143 | // * will read 0x0002 from READ_VOUT (command/register 0x8B) |
| 144 | // * will not read from VOUT_MODE (command/register 0x20) |
| 145 | // assume output voltage is 16 volts, |
| 146 | // exponent = 3 |
| 147 | // linear data format = 0000 0000 0000 0010 = 0x0002 = 2 |
| 148 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 149 | std::make_unique<i2c::MockedI2CInterface>(); |
| 150 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 151 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8B), A<uint16_t&>())) |
| 152 | .Times(1) |
| 153 | .WillOnce(SetArgReferee<1>(0x0002)); |
| 154 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 155 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 156 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 157 | Device device{ |
| 158 | "reg1", true, |
| 159 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 160 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 161 | IDMap idMap{}; |
| 162 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 163 | MockServices services{}; |
| 164 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 165 | |
| 166 | // Create and execute action |
| 167 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout}; |
| 168 | uint8_t command = 0x8B; |
| 169 | pmbus_utils::SensorDataFormat format{ |
| 170 | pmbus_utils::SensorDataFormat::linear_16}; |
| 171 | std::optional<int8_t> exponent{3}; |
| 172 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 173 | EXPECT_EQ(action.execute(env), true); |
Shawn McCarney | 5135df6 | 2021-04-28 15:53:24 -0500 | [diff] [blame^] | 174 | /** |
| 175 | * TODO: Replace with EXPECT calls using MockSensors |
| 176 | * |
| 177 | * EXPECT_EQ(env.getSensorReadings().size(), 1); |
| 178 | * EXPECT_EQ(env.getSensorReadings()[0].type, |
| 179 | * pmbus_utils::SensorValueType::vout); |
| 180 | * EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 16); |
| 181 | */ |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 182 | } |
| 183 | catch (...) |
| 184 | { |
| 185 | ADD_FAILURE() << "Should not have caught exception."; |
| 186 | } |
| 187 | |
| 188 | // Test where works: linear_16 with no exponent defined in action |
| 189 | try |
| 190 | { |
| 191 | // Create mock I2CInterface. |
| 192 | // * will read 0xB877 from vout_peak (command/register 0xC6) |
| 193 | // * will read 0b0001'0111 (linear format, -9 exponent) from VOUT_MODE |
| 194 | // assume output voltage is 0.232421875 volts, |
| 195 | // linear data format = 0000 0000 0111 0111 = 0x0077 |
| 196 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 197 | std::make_unique<i2c::MockedI2CInterface>(); |
| 198 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 199 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0xC6), A<uint16_t&>())) |
| 200 | .Times(1) |
| 201 | .WillOnce(SetArgReferee<1>(0x0077)); |
| 202 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 203 | .Times(1) |
| 204 | .WillOnce(SetArgReferee<1>(0b0001'0111)); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 205 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 206 | Device device{ |
| 207 | "reg1", true, |
| 208 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 209 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 210 | IDMap idMap{}; |
| 211 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 212 | MockServices services{}; |
| 213 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 214 | |
| 215 | // Create and execute action |
| 216 | pmbus_utils::SensorValueType type{ |
| 217 | pmbus_utils::SensorValueType::vout_peak}; |
| 218 | uint8_t command = 0xC6; |
| 219 | pmbus_utils::SensorDataFormat format{ |
| 220 | pmbus_utils::SensorDataFormat::linear_16}; |
| 221 | std::optional<int8_t> exponent{}; |
| 222 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 223 | EXPECT_EQ(action.execute(env), true); |
Shawn McCarney | 5135df6 | 2021-04-28 15:53:24 -0500 | [diff] [blame^] | 224 | /** |
| 225 | * TODO: Replace with EXPECT calls using MockSensors |
| 226 | * |
| 227 | * EXPECT_EQ(env.getSensorReadings().size(), 1); |
| 228 | * EXPECT_EQ(env.getSensorReadings()[0].type, |
| 229 | * pmbus_utils::SensorValueType::vout_peak); |
| 230 | * EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 0.232421875); |
| 231 | */ |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 232 | } |
| 233 | catch (...) |
| 234 | { |
| 235 | ADD_FAILURE() << "Should not have caught exception."; |
| 236 | } |
| 237 | |
| 238 | // Test where fails: Unable to get I2C interface to current device |
| 239 | try |
| 240 | { |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 241 | // Create IDMap, MockServices, and ActionEnvironment |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 242 | IDMap idMap{}; |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 243 | MockServices services{}; |
| 244 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 245 | |
| 246 | // Create and execute action |
| 247 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout}; |
| 248 | uint8_t command = 0x96; |
| 249 | pmbus_utils::SensorDataFormat format{ |
| 250 | pmbus_utils::SensorDataFormat::linear_11}; |
| 251 | std::optional<int8_t> exponent{}; |
| 252 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 253 | action.execute(env); |
| 254 | ADD_FAILURE() << "Should not have reached this line."; |
| 255 | } |
| 256 | catch (const std::invalid_argument& e) |
| 257 | { |
| 258 | EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\""); |
| 259 | } |
| 260 | catch (...) |
| 261 | { |
| 262 | ADD_FAILURE() << "Should not have caught exception."; |
| 263 | } |
| 264 | |
| 265 | // Test where fails: VOUT_MODE data format is not linear |
| 266 | try |
| 267 | { |
| 268 | // Create mock I2CInterface. Expect action to do the following: |
| 269 | // * will read 0b0010'0000 (vid data format) from VOUT_MODE |
| 270 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 271 | std::make_unique<i2c::MockedI2CInterface>(); |
| 272 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
Bob King | ab7d6cb | 2020-07-17 10:24:07 +0800 | [diff] [blame] | 273 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8B), A<uint16_t&>())) |
| 274 | .Times(1); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 275 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 276 | .Times(1) |
| 277 | .WillOnce(SetArgReferee<1>(0b0010'0000)); |
| 278 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 279 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 280 | Device device{ |
| 281 | "reg1", true, |
| 282 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 283 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 284 | IDMap idMap{}; |
| 285 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 286 | MockServices services{}; |
| 287 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 288 | |
| 289 | // Create and execute action |
| 290 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout}; |
| 291 | uint8_t command = 0x8B; |
| 292 | pmbus_utils::SensorDataFormat format{ |
| 293 | pmbus_utils::SensorDataFormat::linear_16}; |
| 294 | std::optional<int8_t> exponent{}; |
| 295 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 296 | action.execute(env); |
| 297 | ADD_FAILURE() << "Should not have reached this line."; |
| 298 | } |
| 299 | catch (const ActionError& e) |
| 300 | { |
| 301 | EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: vout, " |
| 302 | "command: 0x8B, format: linear_16 }"); |
| 303 | try |
| 304 | { |
| 305 | // Re-throw inner PMBusError |
| 306 | std::rethrow_if_nested(e); |
| 307 | ADD_FAILURE() << "Should not have reached this line."; |
| 308 | } |
| 309 | catch (const PMBusError& pe) |
| 310 | { |
| 311 | EXPECT_STREQ( |
| 312 | pe.what(), |
| 313 | "PMBusError: VOUT_MODE contains unsupported data format"); |
Shawn McCarney | 5b819f4 | 2021-03-16 14:41:15 -0500 | [diff] [blame] | 314 | EXPECT_EQ(pe.getDeviceID(), "reg1"); |
| 315 | EXPECT_EQ(pe.getInventoryPath(), "/xyz/openbmc_project/inventory/" |
| 316 | "system/chassis/motherboard/reg1"); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 317 | } |
| 318 | catch (...) |
| 319 | { |
| 320 | ADD_FAILURE() << "Should not have caught exception."; |
| 321 | } |
| 322 | } |
| 323 | catch (...) |
| 324 | { |
| 325 | ADD_FAILURE() << "Should not have caught exception."; |
| 326 | } |
| 327 | |
| 328 | // Test where fails: Reading VOUT_MODE fails |
| 329 | try |
| 330 | { |
| 331 | // Create mock I2CInterface. Expect action to do the following: |
| 332 | // * will try to read VOUT_MODE; exception will be thrown |
| 333 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 334 | std::make_unique<i2c::MockedI2CInterface>(); |
| 335 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
Bob King | ab7d6cb | 2020-07-17 10:24:07 +0800 | [diff] [blame] | 336 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0xC6), A<uint16_t&>())) |
| 337 | .Times(1); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 338 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 339 | .Times(1) |
| 340 | .WillOnce(Throw( |
| 341 | i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70})); |
| 342 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 343 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 344 | Device device{ |
| 345 | "reg1", true, |
| 346 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 347 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 348 | IDMap idMap{}; |
| 349 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 350 | MockServices services{}; |
| 351 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 352 | |
| 353 | // Create and execute action |
| 354 | pmbus_utils::SensorValueType type{ |
| 355 | pmbus_utils::SensorValueType::vout_peak}; |
| 356 | uint8_t command = 0xC6; |
| 357 | pmbus_utils::SensorDataFormat format{ |
| 358 | pmbus_utils::SensorDataFormat::linear_16}; |
| 359 | std::optional<int8_t> exponent{}; |
| 360 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 361 | action.execute(env); |
| 362 | ADD_FAILURE() << "Should not have reached this line."; |
| 363 | } |
| 364 | catch (const ActionError& e) |
| 365 | { |
| 366 | EXPECT_STREQ(e.what(), |
| 367 | "ActionError: pmbus_read_sensor: { type: vout_peak, " |
| 368 | "command: 0xC6, format: linear_16 }"); |
| 369 | try |
| 370 | { |
| 371 | // Re-throw inner I2CException |
| 372 | std::rethrow_if_nested(e); |
| 373 | ADD_FAILURE() << "Should not have reached this line."; |
| 374 | } |
| 375 | catch (const i2c::I2CException& ie) |
| 376 | { |
| 377 | EXPECT_STREQ( |
| 378 | ie.what(), |
| 379 | "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70"); |
| 380 | } |
| 381 | catch (...) |
| 382 | { |
| 383 | ADD_FAILURE() << "Should not have caught exception."; |
| 384 | } |
| 385 | } |
| 386 | catch (...) |
| 387 | { |
| 388 | ADD_FAILURE() << "Should not have caught exception."; |
| 389 | } |
| 390 | |
| 391 | // Test where fails: Reading PMBus command code with sensor value fails |
| 392 | try |
| 393 | { |
| 394 | // Create mock I2CInterface. Expect action to do the following: |
| 395 | // * will try to read PMBus command(0x96); exception will be thrown |
| 396 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 397 | std::make_unique<i2c::MockedI2CInterface>(); |
| 398 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 399 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x96), A<uint16_t&>())) |
| 400 | .Times(1) |
| 401 | .WillOnce(Throw( |
| 402 | i2c::I2CException{"Failed to read word", "/dev/i2c-1", 0x70})); |
| 403 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 404 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 405 | Device device{ |
| 406 | "reg1", true, |
| 407 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 408 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 409 | IDMap idMap{}; |
| 410 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 411 | MockServices services{}; |
| 412 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 413 | |
| 414 | // Create and execute action |
| 415 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout}; |
| 416 | uint8_t command = 0x96; |
| 417 | pmbus_utils::SensorDataFormat format{ |
| 418 | pmbus_utils::SensorDataFormat::linear_11}; |
| 419 | std::optional<int8_t> exponent{}; |
| 420 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 421 | action.execute(env); |
| 422 | ADD_FAILURE() << "Should not have reached this line."; |
| 423 | } |
| 424 | catch (const ActionError& e) |
| 425 | { |
| 426 | EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: pout, " |
| 427 | "command: 0x96, format: linear_11 }"); |
| 428 | try |
| 429 | { |
| 430 | // Re-throw inner I2CException |
| 431 | std::rethrow_if_nested(e); |
| 432 | ADD_FAILURE() << "Should not have reached this line."; |
| 433 | } |
| 434 | catch (const i2c::I2CException& ie) |
| 435 | { |
| 436 | EXPECT_STREQ( |
| 437 | ie.what(), |
| 438 | "I2CException: Failed to read word: bus /dev/i2c-1, addr 0x70"); |
| 439 | } |
| 440 | catch (...) |
| 441 | { |
| 442 | ADD_FAILURE() << "Should not have caught exception."; |
| 443 | } |
| 444 | } |
| 445 | catch (...) |
| 446 | { |
| 447 | ADD_FAILURE() << "Should not have caught exception."; |
| 448 | } |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | TEST(PMBusReadSensorActionTests, GetCommand) |
| 452 | { |
| 453 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 454 | uint8_t command = 0x8C; |
| 455 | pmbus_utils::SensorDataFormat format{ |
| 456 | pmbus_utils::SensorDataFormat::linear_16}; |
| 457 | std::optional<int8_t> exponent{-8}; |
| 458 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 459 | EXPECT_EQ(action.getCommand(), 0x8C); |
| 460 | } |
| 461 | |
| 462 | TEST(PMBusReadSensorActionTests, GetExponent) |
| 463 | { |
| 464 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 465 | uint8_t command = 0x8C; |
| 466 | pmbus_utils::SensorDataFormat format{ |
| 467 | pmbus_utils::SensorDataFormat::linear_16}; |
| 468 | |
| 469 | // Exponent value is specified |
| 470 | { |
| 471 | std::optional<int8_t> exponent{-9}; |
| 472 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 473 | EXPECT_EQ(action.getExponent().has_value(), true); |
| 474 | EXPECT_EQ(action.getExponent().value(), -9); |
| 475 | } |
| 476 | |
| 477 | // Exponent value is not specified |
| 478 | { |
| 479 | std::optional<int8_t> exponent{}; |
| 480 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 481 | EXPECT_EQ(action.getExponent().has_value(), false); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | TEST(PMBusReadSensorActionTests, GetFormat) |
| 486 | { |
| 487 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 488 | uint8_t command = 0x8C; |
| 489 | pmbus_utils::SensorDataFormat format{ |
| 490 | pmbus_utils::SensorDataFormat::linear_16}; |
| 491 | std::optional<int8_t> exponent{-8}; |
| 492 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 493 | EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16); |
| 494 | } |
| 495 | |
| 496 | TEST(PMBusReadSensorActionTests, GetType) |
| 497 | { |
| 498 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout}; |
| 499 | uint8_t command = 0x8C; |
| 500 | pmbus_utils::SensorDataFormat format{ |
| 501 | pmbus_utils::SensorDataFormat::linear_16}; |
| 502 | std::optional<int8_t> exponent{-8}; |
| 503 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 504 | EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::pout); |
| 505 | } |
| 506 | |
| 507 | TEST(PMBusReadSensorActionTests, ToString) |
| 508 | { |
| 509 | // Test where exponent value is specified |
| 510 | { |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 511 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout}; |
| 512 | uint8_t command = 0x8B; |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 513 | pmbus_utils::SensorDataFormat format{ |
| 514 | pmbus_utils::SensorDataFormat::linear_16}; |
| 515 | std::optional<int8_t> exponent{-8}; |
| 516 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 517 | EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: " |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 518 | "vout, command: 0x8B, format: " |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 519 | "linear_16, exponent: -8 }"); |
| 520 | } |
| 521 | |
| 522 | // Test where exponent value is not specified |
| 523 | { |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 524 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 525 | uint8_t command = 0x8C; |
| 526 | pmbus_utils::SensorDataFormat format{ |
| 527 | pmbus_utils::SensorDataFormat::linear_11}; |
| 528 | std::optional<int8_t> exponent{}; |
| 529 | PMBusReadSensorAction action{type, command, format, exponent}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 530 | EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: iout, " |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 531 | "command: 0x8C, format: linear_11 }"); |
| 532 | } |
| 533 | } |