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); |
| 125 | EXPECT_EQ(env.getSensorReadings().size(), 1); |
| 126 | EXPECT_EQ(env.getSensorReadings()[0].type, |
| 127 | pmbus_utils::SensorValueType::iout); |
| 128 | EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 11.5); |
| 129 | } |
| 130 | catch (...) |
| 131 | { |
| 132 | ADD_FAILURE() << "Should not have caught exception."; |
| 133 | } |
| 134 | |
| 135 | // Test where works: linear_16 with exponent defined in action |
| 136 | try |
| 137 | { |
| 138 | // Create mock I2CInterface. |
| 139 | // * will read 0x0002 from READ_VOUT (command/register 0x8B) |
| 140 | // * will not read from VOUT_MODE (command/register 0x20) |
| 141 | // assume output voltage is 16 volts, |
| 142 | // exponent = 3 |
| 143 | // linear data format = 0000 0000 0000 0010 = 0x0002 = 2 |
| 144 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 145 | std::make_unique<i2c::MockedI2CInterface>(); |
| 146 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 147 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8B), A<uint16_t&>())) |
| 148 | .Times(1) |
| 149 | .WillOnce(SetArgReferee<1>(0x0002)); |
| 150 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 151 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 152 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 153 | Device device{ |
| 154 | "reg1", true, |
| 155 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 156 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 157 | IDMap idMap{}; |
| 158 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 159 | MockServices services{}; |
| 160 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 161 | |
| 162 | // Create and execute action |
| 163 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout}; |
| 164 | uint8_t command = 0x8B; |
| 165 | pmbus_utils::SensorDataFormat format{ |
| 166 | pmbus_utils::SensorDataFormat::linear_16}; |
| 167 | std::optional<int8_t> exponent{3}; |
| 168 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 169 | EXPECT_EQ(action.execute(env), true); |
| 170 | EXPECT_EQ(env.getSensorReadings().size(), 1); |
| 171 | EXPECT_EQ(env.getSensorReadings()[0].type, |
| 172 | pmbus_utils::SensorValueType::vout); |
| 173 | EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 16); |
| 174 | } |
| 175 | catch (...) |
| 176 | { |
| 177 | ADD_FAILURE() << "Should not have caught exception."; |
| 178 | } |
| 179 | |
| 180 | // Test where works: linear_16 with no exponent defined in action |
| 181 | try |
| 182 | { |
| 183 | // Create mock I2CInterface. |
| 184 | // * will read 0xB877 from vout_peak (command/register 0xC6) |
| 185 | // * will read 0b0001'0111 (linear format, -9 exponent) from VOUT_MODE |
| 186 | // assume output voltage is 0.232421875 volts, |
| 187 | // linear data format = 0000 0000 0111 0111 = 0x0077 |
| 188 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 189 | std::make_unique<i2c::MockedI2CInterface>(); |
| 190 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 191 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0xC6), A<uint16_t&>())) |
| 192 | .Times(1) |
| 193 | .WillOnce(SetArgReferee<1>(0x0077)); |
| 194 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 195 | .Times(1) |
| 196 | .WillOnce(SetArgReferee<1>(0b0001'0111)); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 197 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 198 | Device device{ |
| 199 | "reg1", true, |
| 200 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 201 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 202 | IDMap idMap{}; |
| 203 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 204 | MockServices services{}; |
| 205 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 206 | |
| 207 | // Create and execute action |
| 208 | pmbus_utils::SensorValueType type{ |
| 209 | pmbus_utils::SensorValueType::vout_peak}; |
| 210 | uint8_t command = 0xC6; |
| 211 | pmbus_utils::SensorDataFormat format{ |
| 212 | pmbus_utils::SensorDataFormat::linear_16}; |
| 213 | std::optional<int8_t> exponent{}; |
| 214 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 215 | EXPECT_EQ(action.execute(env), true); |
| 216 | EXPECT_EQ(env.getSensorReadings().size(), 1); |
| 217 | EXPECT_EQ(env.getSensorReadings()[0].type, |
| 218 | pmbus_utils::SensorValueType::vout_peak); |
| 219 | EXPECT_DOUBLE_EQ(env.getSensorReadings()[0].value, 0.232421875); |
| 220 | } |
| 221 | catch (...) |
| 222 | { |
| 223 | ADD_FAILURE() << "Should not have caught exception."; |
| 224 | } |
| 225 | |
| 226 | // Test where fails: Unable to get I2C interface to current device |
| 227 | try |
| 228 | { |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 229 | // Create IDMap, MockServices, and ActionEnvironment |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 230 | IDMap idMap{}; |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 231 | MockServices services{}; |
| 232 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 233 | |
| 234 | // Create and execute action |
| 235 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout}; |
| 236 | uint8_t command = 0x96; |
| 237 | pmbus_utils::SensorDataFormat format{ |
| 238 | pmbus_utils::SensorDataFormat::linear_11}; |
| 239 | std::optional<int8_t> exponent{}; |
| 240 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 241 | action.execute(env); |
| 242 | ADD_FAILURE() << "Should not have reached this line."; |
| 243 | } |
| 244 | catch (const std::invalid_argument& e) |
| 245 | { |
| 246 | EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\""); |
| 247 | } |
| 248 | catch (...) |
| 249 | { |
| 250 | ADD_FAILURE() << "Should not have caught exception."; |
| 251 | } |
| 252 | |
| 253 | // Test where fails: VOUT_MODE data format is not linear |
| 254 | try |
| 255 | { |
| 256 | // Create mock I2CInterface. Expect action to do the following: |
| 257 | // * will read 0b0010'0000 (vid data format) from VOUT_MODE |
| 258 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 259 | std::make_unique<i2c::MockedI2CInterface>(); |
| 260 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
Bob King | ab7d6cb | 2020-07-17 10:24:07 +0800 | [diff] [blame] | 261 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8B), A<uint16_t&>())) |
| 262 | .Times(1); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 263 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 264 | .Times(1) |
| 265 | .WillOnce(SetArgReferee<1>(0b0010'0000)); |
| 266 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 267 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 268 | Device device{ |
| 269 | "reg1", true, |
| 270 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 271 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 272 | IDMap idMap{}; |
| 273 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 274 | MockServices services{}; |
| 275 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 276 | |
| 277 | // Create and execute action |
| 278 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout}; |
| 279 | uint8_t command = 0x8B; |
| 280 | pmbus_utils::SensorDataFormat format{ |
| 281 | pmbus_utils::SensorDataFormat::linear_16}; |
| 282 | std::optional<int8_t> exponent{}; |
| 283 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 284 | action.execute(env); |
| 285 | ADD_FAILURE() << "Should not have reached this line."; |
| 286 | } |
| 287 | catch (const ActionError& e) |
| 288 | { |
| 289 | EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: vout, " |
| 290 | "command: 0x8B, format: linear_16 }"); |
| 291 | try |
| 292 | { |
| 293 | // Re-throw inner PMBusError |
| 294 | std::rethrow_if_nested(e); |
| 295 | ADD_FAILURE() << "Should not have reached this line."; |
| 296 | } |
| 297 | catch (const PMBusError& pe) |
| 298 | { |
| 299 | EXPECT_STREQ( |
| 300 | pe.what(), |
| 301 | "PMBusError: VOUT_MODE contains unsupported data format"); |
| 302 | } |
| 303 | catch (...) |
| 304 | { |
| 305 | ADD_FAILURE() << "Should not have caught exception."; |
| 306 | } |
| 307 | } |
| 308 | catch (...) |
| 309 | { |
| 310 | ADD_FAILURE() << "Should not have caught exception."; |
| 311 | } |
| 312 | |
| 313 | // Test where fails: Reading VOUT_MODE fails |
| 314 | try |
| 315 | { |
| 316 | // Create mock I2CInterface. Expect action to do the following: |
| 317 | // * will try to read VOUT_MODE; exception will be thrown |
| 318 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 319 | std::make_unique<i2c::MockedI2CInterface>(); |
| 320 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
Bob King | ab7d6cb | 2020-07-17 10:24:07 +0800 | [diff] [blame] | 321 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0xC6), A<uint16_t&>())) |
| 322 | .Times(1); |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 323 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 324 | .Times(1) |
| 325 | .WillOnce(Throw( |
| 326 | i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70})); |
| 327 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 328 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 329 | Device device{ |
| 330 | "reg1", true, |
| 331 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 332 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 333 | IDMap idMap{}; |
| 334 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 335 | MockServices services{}; |
| 336 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 337 | |
| 338 | // Create and execute action |
| 339 | pmbus_utils::SensorValueType type{ |
| 340 | pmbus_utils::SensorValueType::vout_peak}; |
| 341 | uint8_t command = 0xC6; |
| 342 | pmbus_utils::SensorDataFormat format{ |
| 343 | pmbus_utils::SensorDataFormat::linear_16}; |
| 344 | std::optional<int8_t> exponent{}; |
| 345 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 346 | action.execute(env); |
| 347 | ADD_FAILURE() << "Should not have reached this line."; |
| 348 | } |
| 349 | catch (const ActionError& e) |
| 350 | { |
| 351 | EXPECT_STREQ(e.what(), |
| 352 | "ActionError: pmbus_read_sensor: { type: vout_peak, " |
| 353 | "command: 0xC6, format: linear_16 }"); |
| 354 | try |
| 355 | { |
| 356 | // Re-throw inner I2CException |
| 357 | std::rethrow_if_nested(e); |
| 358 | ADD_FAILURE() << "Should not have reached this line."; |
| 359 | } |
| 360 | catch (const i2c::I2CException& ie) |
| 361 | { |
| 362 | EXPECT_STREQ( |
| 363 | ie.what(), |
| 364 | "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70"); |
| 365 | } |
| 366 | catch (...) |
| 367 | { |
| 368 | ADD_FAILURE() << "Should not have caught exception."; |
| 369 | } |
| 370 | } |
| 371 | catch (...) |
| 372 | { |
| 373 | ADD_FAILURE() << "Should not have caught exception."; |
| 374 | } |
| 375 | |
| 376 | // Test where fails: Reading PMBus command code with sensor value fails |
| 377 | try |
| 378 | { |
| 379 | // Create mock I2CInterface. Expect action to do the following: |
| 380 | // * will try to read PMBus command(0x96); exception will be thrown |
| 381 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 382 | std::make_unique<i2c::MockedI2CInterface>(); |
| 383 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 384 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x96), A<uint16_t&>())) |
| 385 | .Times(1) |
| 386 | .WillOnce(Throw( |
| 387 | i2c::I2CException{"Failed to read word", "/dev/i2c-1", 0x70})); |
| 388 | |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 389 | // Create Device, IDMap, MockServices, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 390 | Device device{ |
| 391 | "reg1", true, |
| 392 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 393 | std::move(i2cInterface)}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 394 | IDMap idMap{}; |
| 395 | idMap.addDevice(device); |
Bob King | 73eacee | 2020-10-23 13:58:02 +0800 | [diff] [blame] | 396 | MockServices services{}; |
| 397 | ActionEnvironment env{idMap, "reg1", services}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 398 | |
| 399 | // Create and execute action |
| 400 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout}; |
| 401 | uint8_t command = 0x96; |
| 402 | pmbus_utils::SensorDataFormat format{ |
| 403 | pmbus_utils::SensorDataFormat::linear_11}; |
| 404 | std::optional<int8_t> exponent{}; |
| 405 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 406 | action.execute(env); |
| 407 | ADD_FAILURE() << "Should not have reached this line."; |
| 408 | } |
| 409 | catch (const ActionError& e) |
| 410 | { |
| 411 | EXPECT_STREQ(e.what(), "ActionError: pmbus_read_sensor: { type: pout, " |
| 412 | "command: 0x96, format: linear_11 }"); |
| 413 | try |
| 414 | { |
| 415 | // Re-throw inner I2CException |
| 416 | std::rethrow_if_nested(e); |
| 417 | ADD_FAILURE() << "Should not have reached this line."; |
| 418 | } |
| 419 | catch (const i2c::I2CException& ie) |
| 420 | { |
| 421 | EXPECT_STREQ( |
| 422 | ie.what(), |
| 423 | "I2CException: Failed to read word: bus /dev/i2c-1, addr 0x70"); |
| 424 | } |
| 425 | catch (...) |
| 426 | { |
| 427 | ADD_FAILURE() << "Should not have caught exception."; |
| 428 | } |
| 429 | } |
| 430 | catch (...) |
| 431 | { |
| 432 | ADD_FAILURE() << "Should not have caught exception."; |
| 433 | } |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | TEST(PMBusReadSensorActionTests, GetCommand) |
| 437 | { |
| 438 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 439 | uint8_t command = 0x8C; |
| 440 | pmbus_utils::SensorDataFormat format{ |
| 441 | pmbus_utils::SensorDataFormat::linear_16}; |
| 442 | std::optional<int8_t> exponent{-8}; |
| 443 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 444 | EXPECT_EQ(action.getCommand(), 0x8C); |
| 445 | } |
| 446 | |
| 447 | TEST(PMBusReadSensorActionTests, GetExponent) |
| 448 | { |
| 449 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 450 | uint8_t command = 0x8C; |
| 451 | pmbus_utils::SensorDataFormat format{ |
| 452 | pmbus_utils::SensorDataFormat::linear_16}; |
| 453 | |
| 454 | // Exponent value is specified |
| 455 | { |
| 456 | std::optional<int8_t> exponent{-9}; |
| 457 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 458 | EXPECT_EQ(action.getExponent().has_value(), true); |
| 459 | EXPECT_EQ(action.getExponent().value(), -9); |
| 460 | } |
| 461 | |
| 462 | // Exponent value is not specified |
| 463 | { |
| 464 | std::optional<int8_t> exponent{}; |
| 465 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 466 | EXPECT_EQ(action.getExponent().has_value(), false); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | TEST(PMBusReadSensorActionTests, GetFormat) |
| 471 | { |
| 472 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
| 473 | uint8_t command = 0x8C; |
| 474 | pmbus_utils::SensorDataFormat format{ |
| 475 | pmbus_utils::SensorDataFormat::linear_16}; |
| 476 | std::optional<int8_t> exponent{-8}; |
| 477 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 478 | EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16); |
| 479 | } |
| 480 | |
| 481 | TEST(PMBusReadSensorActionTests, GetType) |
| 482 | { |
| 483 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout}; |
| 484 | uint8_t command = 0x8C; |
| 485 | pmbus_utils::SensorDataFormat format{ |
| 486 | pmbus_utils::SensorDataFormat::linear_16}; |
| 487 | std::optional<int8_t> exponent{-8}; |
| 488 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 489 | EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::pout); |
| 490 | } |
| 491 | |
| 492 | TEST(PMBusReadSensorActionTests, ToString) |
| 493 | { |
| 494 | // Test where exponent value is specified |
| 495 | { |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 496 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout}; |
| 497 | uint8_t command = 0x8B; |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 498 | pmbus_utils::SensorDataFormat format{ |
| 499 | pmbus_utils::SensorDataFormat::linear_16}; |
| 500 | std::optional<int8_t> exponent{-8}; |
| 501 | PMBusReadSensorAction action{type, command, format, exponent}; |
| 502 | EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: " |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 503 | "vout, command: 0x8B, format: " |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 504 | "linear_16, exponent: -8 }"); |
| 505 | } |
| 506 | |
| 507 | // Test where exponent value is not specified |
| 508 | { |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 509 | pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout}; |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 510 | uint8_t command = 0x8C; |
| 511 | pmbus_utils::SensorDataFormat format{ |
| 512 | pmbus_utils::SensorDataFormat::linear_11}; |
| 513 | std::optional<int8_t> exponent{}; |
| 514 | PMBusReadSensorAction action{type, command, format, exponent}; |
Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [diff] [blame] | 515 | EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: iout, " |
Bob King | d6820bb | 2020-04-28 15:37:02 +0800 | [diff] [blame] | 516 | "command: 0x8C, format: linear_11 }"); |
| 517 | } |
| 518 | } |