Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [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" |
| 17 | #include "action_error.hpp" |
| 18 | #include "device.hpp" |
| 19 | #include "i2c_interface.hpp" |
| 20 | #include "id_map.hpp" |
| 21 | #include "mocked_i2c_interface.hpp" |
| 22 | #include "pmbus_error.hpp" |
| 23 | #include "pmbus_utils.hpp" |
| 24 | #include "pmbus_write_vout_command_action.hpp" |
| 25 | #include "write_verification_error.hpp" |
| 26 | |
| 27 | #include <cstdint> |
| 28 | #include <memory> |
| 29 | #include <optional> |
| 30 | #include <stdexcept> |
| 31 | #include <string> |
| 32 | #include <utility> |
| 33 | |
| 34 | #include <gmock/gmock.h> |
| 35 | #include <gtest/gtest.h> |
| 36 | |
| 37 | using namespace phosphor::power::regulators; |
| 38 | |
| 39 | using ::testing::A; |
| 40 | using ::testing::Return; |
| 41 | using ::testing::SetArgReferee; |
| 42 | using ::testing::Throw; |
| 43 | using ::testing::TypedEq; |
| 44 | |
| 45 | TEST(PMBusWriteVoutCommandActionTests, Constructor) |
| 46 | { |
| 47 | // Test where works: Volts value and exponent value are specified |
| 48 | try |
| 49 | { |
| 50 | std::optional<double> volts{1.3}; |
| 51 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 52 | std::optional<int8_t> exponent{-8}; |
| 53 | bool isVerified{true}; |
| 54 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 55 | EXPECT_EQ(action.getVolts().has_value(), true); |
| 56 | EXPECT_EQ(action.getVolts().value(), 1.3); |
| 57 | EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear); |
| 58 | EXPECT_EQ(action.getExponent().has_value(), true); |
| 59 | EXPECT_EQ(action.getExponent().value(), -8); |
| 60 | EXPECT_EQ(action.isVerified(), true); |
| 61 | } |
| 62 | catch (...) |
| 63 | { |
| 64 | ADD_FAILURE() << "Should not have caught exception."; |
| 65 | } |
| 66 | |
| 67 | // Test where works: Volts value and exponent value are not specified |
| 68 | try |
| 69 | { |
| 70 | std::optional<double> volts{}; |
| 71 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 72 | std::optional<int8_t> exponent{}; |
| 73 | bool isVerified{false}; |
| 74 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 75 | EXPECT_EQ(action.getVolts().has_value(), false); |
| 76 | EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear); |
| 77 | EXPECT_EQ(action.getExponent().has_value(), false); |
| 78 | EXPECT_EQ(action.isVerified(), false); |
| 79 | } |
| 80 | catch (...) |
| 81 | { |
| 82 | ADD_FAILURE() << "Should not have caught exception."; |
| 83 | } |
| 84 | |
| 85 | // Test where fails: Data format is not linear |
| 86 | try |
| 87 | { |
| 88 | std::optional<double> volts{}; |
| 89 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::direct}; |
| 90 | std::optional<int8_t> exponent{}; |
| 91 | bool isVerified{false}; |
| 92 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 93 | ADD_FAILURE() << "Should not have reached this line."; |
| 94 | } |
| 95 | catch (const std::invalid_argument& e) |
| 96 | { |
| 97 | EXPECT_STREQ(e.what(), "Unsupported data format specified"); |
| 98 | } |
| 99 | catch (...) |
| 100 | { |
| 101 | ADD_FAILURE() << "Should not have caught exception."; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | TEST(PMBusWriteVoutCommandActionTests, Execute) |
| 106 | { |
| 107 | // Test where works: Volts value and exponent value defined in action; |
| 108 | // write is verified. |
| 109 | try |
| 110 | { |
| 111 | // Create mock I2CInterface. Expect action to do the following: |
| 112 | // * will not read from VOUT_MODE (command/register 0x20) |
| 113 | // * will write 0x014D to VOUT_COMMAND (command/register 0x21) |
| 114 | // * will read 0x014D from VOUT_COMMAND |
| 115 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 116 | std::make_unique<i2c::MockedI2CInterface>(); |
| 117 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 118 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 119 | EXPECT_CALL(*i2cInterface, |
| 120 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D))) |
| 121 | .Times(1); |
| 122 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>())) |
| 123 | .Times(1) |
| 124 | .WillOnce(SetArgReferee<1>(0x014D)); |
| 125 | |
| 126 | // Create Device, IDMap, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame^] | 127 | Device device{ |
| 128 | "reg1", true, |
| 129 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 130 | std::move(i2cInterface)}; |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 131 | IDMap idMap{}; |
| 132 | idMap.addDevice(device); |
| 133 | ActionEnvironment env{idMap, "reg1"}; |
| 134 | |
| 135 | // Create and execute action |
| 136 | // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D |
| 137 | std::optional<double> volts{1.3}; |
| 138 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 139 | std::optional<int8_t> exponent{-8}; |
| 140 | bool isVerified{true}; |
| 141 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 142 | EXPECT_EQ(action.execute(env), true); |
| 143 | } |
| 144 | catch (...) |
| 145 | { |
| 146 | ADD_FAILURE() << "Should not have caught exception."; |
| 147 | } |
| 148 | |
| 149 | // Test where works: Volts value defined in ActionEnvironment; exponent |
| 150 | // value defined in VOUT_MODE; write is not verified. |
| 151 | try |
| 152 | { |
| 153 | // Create mock I2CInterface. Expect action to do the following: |
| 154 | // * will read 0b0001'0111 (linear format, -9 exponent) from VOUT_MODE |
| 155 | // * will write 0x069A to VOUT_COMMAND |
| 156 | // * will not read from VOUT_COMMAND |
| 157 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 158 | std::make_unique<i2c::MockedI2CInterface>(); |
| 159 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 160 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 161 | .Times(1) |
| 162 | .WillOnce(SetArgReferee<1>(0b0001'0111)); |
| 163 | EXPECT_CALL(*i2cInterface, |
| 164 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x069A))) |
| 165 | .Times(1); |
| 166 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint16_t&>())).Times(0); |
| 167 | |
| 168 | // Create Device, IDMap, and ActionEnvironment. Set volts value to 3.3 |
| 169 | // in ActionEnvironment. |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame^] | 170 | Device device{ |
| 171 | "reg1", true, |
| 172 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 173 | std::move(i2cInterface)}; |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 174 | IDMap idMap{}; |
| 175 | idMap.addDevice(device); |
| 176 | ActionEnvironment env{idMap, "reg1"}; |
| 177 | env.setVolts(3.3); |
| 178 | |
| 179 | // Create and execute action |
| 180 | // Linear format volts value = (3.3 / 2^(-9)) = 1689.6 = 1690 = 0x069A |
| 181 | std::optional<double> volts{}; |
| 182 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 183 | std::optional<int8_t> exponent{}; |
| 184 | bool isVerified{false}; |
| 185 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 186 | EXPECT_EQ(action.execute(env), true); |
| 187 | } |
| 188 | catch (...) |
| 189 | { |
| 190 | ADD_FAILURE() << "Should not have caught exception."; |
| 191 | } |
| 192 | |
| 193 | // Test where fails: No volts value defined |
| 194 | try |
| 195 | { |
| 196 | // Create IDMap and ActionEnvironment |
| 197 | IDMap idMap{}; |
| 198 | ActionEnvironment env{idMap, "reg1"}; |
| 199 | |
| 200 | // Create and execute action |
| 201 | std::optional<double> volts{}; |
| 202 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 203 | std::optional<int8_t> exponent{-8}; |
| 204 | bool isVerified{false}; |
| 205 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 206 | action.execute(env); |
| 207 | ADD_FAILURE() << "Should not have reached this line."; |
| 208 | } |
| 209 | catch (const ActionError& e) |
| 210 | { |
| 211 | EXPECT_STREQ( |
| 212 | e.what(), |
| 213 | "ActionError: pmbus_write_vout_command: { format: linear, " |
| 214 | "exponent: -8, is_verified: false }: No volts value defined"); |
| 215 | } |
| 216 | catch (...) |
| 217 | { |
| 218 | ADD_FAILURE() << "Should not have caught exception."; |
| 219 | } |
| 220 | |
| 221 | // Test where fails: Unable to get I2C interface to current device |
| 222 | try |
| 223 | { |
| 224 | // Create IDMap and ActionEnvironment |
| 225 | IDMap idMap{}; |
| 226 | ActionEnvironment env{idMap, "reg1"}; |
| 227 | |
| 228 | // Create and execute action |
| 229 | std::optional<double> volts{1.3}; |
| 230 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 231 | std::optional<int8_t> exponent{-8}; |
| 232 | bool isVerified{false}; |
| 233 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 234 | action.execute(env); |
| 235 | ADD_FAILURE() << "Should not have reached this line."; |
| 236 | } |
| 237 | catch (const std::invalid_argument& e) |
| 238 | { |
| 239 | EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\""); |
| 240 | } |
| 241 | catch (...) |
| 242 | { |
| 243 | ADD_FAILURE() << "Should not have caught exception."; |
| 244 | } |
| 245 | |
| 246 | // Test where fails: Unable to read VOUT_MODE to get exponent |
| 247 | try |
| 248 | { |
| 249 | // Create mock I2CInterface. Expect action to do the following: |
| 250 | // * will try to read VOUT_MODE; exception will be thrown |
| 251 | // * will not write to VOUT_COMMAND due to exception |
| 252 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 253 | std::make_unique<i2c::MockedI2CInterface>(); |
| 254 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 255 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 256 | .Times(1) |
| 257 | .WillOnce(Throw( |
| 258 | i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70})); |
| 259 | EXPECT_CALL(*i2cInterface, write(A<uint8_t>(), A<uint16_t>())).Times(0); |
| 260 | |
| 261 | // Create Device, IDMap, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame^] | 262 | Device device{ |
| 263 | "reg1", true, |
| 264 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 265 | std::move(i2cInterface)}; |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 266 | IDMap idMap{}; |
| 267 | idMap.addDevice(device); |
| 268 | ActionEnvironment env{idMap, "reg1"}; |
| 269 | |
| 270 | // Create and execute action |
| 271 | std::optional<double> volts{3.3}; |
| 272 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 273 | std::optional<int8_t> exponent{}; |
| 274 | bool isVerified{false}; |
| 275 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 276 | action.execute(env); |
| 277 | ADD_FAILURE() << "Should not have reached this line."; |
| 278 | } |
| 279 | catch (const ActionError& e) |
| 280 | { |
| 281 | EXPECT_STREQ(e.what(), |
| 282 | "ActionError: pmbus_write_vout_command: { volts: 3.3, " |
| 283 | "format: linear, is_verified: false }"); |
| 284 | try |
| 285 | { |
| 286 | // Re-throw inner I2CException |
| 287 | std::rethrow_if_nested(e); |
| 288 | ADD_FAILURE() << "Should not have reached this line."; |
| 289 | } |
| 290 | catch (const i2c::I2CException& ie) |
| 291 | { |
| 292 | EXPECT_STREQ( |
| 293 | ie.what(), |
| 294 | "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70"); |
| 295 | } |
| 296 | catch (...) |
| 297 | { |
| 298 | ADD_FAILURE() << "Should not have caught exception."; |
| 299 | } |
| 300 | } |
| 301 | catch (...) |
| 302 | { |
| 303 | ADD_FAILURE() << "Should not have caught exception."; |
| 304 | } |
| 305 | |
| 306 | // Test where fails: VOUT_MODE data format is not linear |
| 307 | try |
| 308 | { |
| 309 | // Create mock I2CInterface. Expect action to do the following: |
| 310 | // * will read 0b0010'0000 (vid data format) from VOUT_MODE |
| 311 | // * will not write to VOUT_COMMAND due to data format error |
| 312 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 313 | std::make_unique<i2c::MockedI2CInterface>(); |
| 314 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 315 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 316 | .Times(1) |
| 317 | .WillOnce(SetArgReferee<1>(0b0010'0000)); |
| 318 | EXPECT_CALL(*i2cInterface, write(A<uint8_t>(), A<uint16_t>())).Times(0); |
| 319 | |
| 320 | // Create Device, IDMap, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame^] | 321 | Device device{ |
| 322 | "reg1", true, |
| 323 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 324 | std::move(i2cInterface)}; |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 325 | IDMap idMap{}; |
| 326 | idMap.addDevice(device); |
| 327 | ActionEnvironment env{idMap, "reg1"}; |
| 328 | |
| 329 | // Create and execute action |
| 330 | std::optional<double> volts{3.3}; |
| 331 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 332 | std::optional<int8_t> exponent{}; |
| 333 | bool isVerified{false}; |
| 334 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 335 | action.execute(env); |
| 336 | ADD_FAILURE() << "Should not have reached this line."; |
| 337 | } |
| 338 | catch (const ActionError& e) |
| 339 | { |
| 340 | EXPECT_STREQ(e.what(), |
| 341 | "ActionError: pmbus_write_vout_command: { volts: 3.3, " |
| 342 | "format: linear, is_verified: false }"); |
| 343 | try |
| 344 | { |
| 345 | // Re-throw inner PMBusError |
| 346 | std::rethrow_if_nested(e); |
| 347 | ADD_FAILURE() << "Should not have reached this line."; |
| 348 | } |
| 349 | catch (const PMBusError& pe) |
| 350 | { |
| 351 | EXPECT_STREQ( |
| 352 | pe.what(), |
| 353 | "PMBusError: VOUT_MODE contains unsupported data format"); |
| 354 | } |
| 355 | catch (...) |
| 356 | { |
| 357 | ADD_FAILURE() << "Should not have caught exception."; |
| 358 | } |
| 359 | } |
| 360 | catch (...) |
| 361 | { |
| 362 | ADD_FAILURE() << "Should not have caught exception."; |
| 363 | } |
| 364 | |
| 365 | // Test where fails: Unable to write VOUT_COMMAND |
| 366 | try |
| 367 | { |
| 368 | // Create mock I2CInterface. Expect action to do the following: |
| 369 | // * will not read from VOUT_MODE |
| 370 | // * will try to write 0x014D to VOUT_COMMAND; exception will be thrown |
| 371 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 372 | std::make_unique<i2c::MockedI2CInterface>(); |
| 373 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 374 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 375 | EXPECT_CALL(*i2cInterface, |
| 376 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D))) |
| 377 | .Times(1) |
| 378 | .WillOnce(Throw(i2c::I2CException{"Failed to write word data", |
| 379 | "/dev/i2c-1", 0x70})); |
| 380 | |
| 381 | // Create Device, IDMap, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame^] | 382 | Device device{ |
| 383 | "reg1", true, |
| 384 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 385 | std::move(i2cInterface)}; |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 386 | IDMap idMap{}; |
| 387 | idMap.addDevice(device); |
| 388 | ActionEnvironment env{idMap, "reg1"}; |
| 389 | |
| 390 | // Create and execute action |
| 391 | // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D |
| 392 | std::optional<double> volts{1.3}; |
| 393 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 394 | std::optional<int8_t> exponent{-8}; |
| 395 | bool isVerified{false}; |
| 396 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 397 | action.execute(env); |
| 398 | ADD_FAILURE() << "Should not have reached this line."; |
| 399 | } |
| 400 | catch (const ActionError& e) |
| 401 | { |
| 402 | EXPECT_STREQ(e.what(), |
| 403 | "ActionError: pmbus_write_vout_command: { volts: 1.3, " |
| 404 | "format: linear, exponent: -8, is_verified: false }"); |
| 405 | try |
| 406 | { |
| 407 | // Re-throw inner I2CException |
| 408 | std::rethrow_if_nested(e); |
| 409 | ADD_FAILURE() << "Should not have reached this line."; |
| 410 | } |
| 411 | catch (const i2c::I2CException& ie) |
| 412 | { |
| 413 | EXPECT_STREQ(ie.what(), "I2CException: Failed to write word data: " |
| 414 | "bus /dev/i2c-1, addr 0x70"); |
| 415 | } |
| 416 | catch (...) |
| 417 | { |
| 418 | ADD_FAILURE() << "Should not have caught exception."; |
| 419 | } |
| 420 | } |
| 421 | catch (...) |
| 422 | { |
| 423 | ADD_FAILURE() << "Should not have caught exception."; |
| 424 | } |
| 425 | |
| 426 | // Test where fails: Unable to read VOUT_COMMAND |
| 427 | try |
| 428 | { |
| 429 | // Create mock I2CInterface. Expect action to do the following: |
| 430 | // * will not read from VOUT_MODE |
| 431 | // * will write 0x014D to VOUT_COMMAND |
| 432 | // * will try to read from VOUT_COMMAND; exception will be thrown |
| 433 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 434 | std::make_unique<i2c::MockedI2CInterface>(); |
| 435 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 436 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 437 | EXPECT_CALL(*i2cInterface, |
| 438 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D))) |
| 439 | .Times(1); |
| 440 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>())) |
| 441 | .Times(1) |
| 442 | .WillOnce(Throw(i2c::I2CException{"Failed to read word data", |
| 443 | "/dev/i2c-1", 0x70})); |
| 444 | |
| 445 | // Create Device, IDMap, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame^] | 446 | Device device{ |
| 447 | "reg1", true, |
| 448 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 449 | std::move(i2cInterface)}; |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 450 | IDMap idMap{}; |
| 451 | idMap.addDevice(device); |
| 452 | ActionEnvironment env{idMap, "reg1"}; |
| 453 | |
| 454 | // Create and execute action |
| 455 | // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D |
| 456 | std::optional<double> volts{1.3}; |
| 457 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 458 | std::optional<int8_t> exponent{-8}; |
| 459 | bool isVerified{true}; |
| 460 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 461 | action.execute(env); |
| 462 | ADD_FAILURE() << "Should not have reached this line."; |
| 463 | } |
| 464 | catch (const ActionError& e) |
| 465 | { |
| 466 | EXPECT_STREQ(e.what(), |
| 467 | "ActionError: pmbus_write_vout_command: { volts: 1.3, " |
| 468 | "format: linear, exponent: -8, is_verified: true }"); |
| 469 | try |
| 470 | { |
| 471 | // Re-throw inner I2CException |
| 472 | std::rethrow_if_nested(e); |
| 473 | ADD_FAILURE() << "Should not have reached this line."; |
| 474 | } |
| 475 | catch (const i2c::I2CException& ie) |
| 476 | { |
| 477 | EXPECT_STREQ(ie.what(), "I2CException: Failed to read word data: " |
| 478 | "bus /dev/i2c-1, addr 0x70"); |
| 479 | } |
| 480 | catch (...) |
| 481 | { |
| 482 | ADD_FAILURE() << "Should not have caught exception."; |
| 483 | } |
| 484 | } |
| 485 | catch (...) |
| 486 | { |
| 487 | ADD_FAILURE() << "Should not have caught exception."; |
| 488 | } |
| 489 | |
| 490 | // Test where fails: Write verification error |
| 491 | try |
| 492 | { |
| 493 | // Create mock I2CInterface. Expect action to do the following: |
| 494 | // * will not read from VOUT_MODE |
| 495 | // * will write 0x014D to VOUT_COMMAND |
| 496 | // * will read 0x014C from VOUT_COMMAND (not equal to 0x014D) |
| 497 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 498 | std::make_unique<i2c::MockedI2CInterface>(); |
| 499 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 500 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 501 | EXPECT_CALL(*i2cInterface, |
| 502 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D))) |
| 503 | .Times(1); |
| 504 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>())) |
| 505 | .Times(1) |
| 506 | .WillOnce(SetArgReferee<1>(0x014C)); |
| 507 | |
| 508 | // Create Device, IDMap, and ActionEnvironment |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame^] | 509 | Device device{ |
| 510 | "reg1", true, |
| 511 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1", |
| 512 | std::move(i2cInterface)}; |
Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 513 | IDMap idMap{}; |
| 514 | idMap.addDevice(device); |
| 515 | ActionEnvironment env{idMap, "reg1"}; |
| 516 | |
| 517 | // Create and execute action |
| 518 | // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D |
| 519 | std::optional<double> volts{1.3}; |
| 520 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 521 | std::optional<int8_t> exponent{-8}; |
| 522 | bool isVerified{true}; |
| 523 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 524 | action.execute(env); |
| 525 | ADD_FAILURE() << "Should not have reached this line."; |
| 526 | } |
| 527 | catch (const ActionError& e) |
| 528 | { |
| 529 | EXPECT_STREQ(e.what(), |
| 530 | "ActionError: pmbus_write_vout_command: { volts: 1.3, " |
| 531 | "format: linear, exponent: -8, is_verified: true }"); |
| 532 | try |
| 533 | { |
| 534 | // Re-throw inner WriteVerificationError |
| 535 | std::rethrow_if_nested(e); |
| 536 | ADD_FAILURE() << "Should not have reached this line."; |
| 537 | } |
| 538 | catch (const WriteVerificationError& we) |
| 539 | { |
| 540 | EXPECT_STREQ( |
| 541 | we.what(), |
| 542 | "WriteVerificationError: device: reg1, register: VOUT_COMMAND, " |
| 543 | "value_written: 0x14D, value_read: 0x14C"); |
| 544 | } |
| 545 | catch (...) |
| 546 | { |
| 547 | ADD_FAILURE() << "Should not have caught exception."; |
| 548 | } |
| 549 | } |
| 550 | catch (...) |
| 551 | { |
| 552 | ADD_FAILURE() << "Should not have caught exception."; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | TEST(PMBusWriteVoutCommandActionTests, GetExponent) |
| 557 | { |
| 558 | std::optional<double> volts{1.3}; |
| 559 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 560 | bool isVerified{true}; |
| 561 | |
| 562 | // Exponent value was specified |
| 563 | { |
| 564 | std::optional<int8_t> exponent{-9}; |
| 565 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 566 | EXPECT_EQ(action.getExponent().has_value(), true); |
| 567 | EXPECT_EQ(action.getExponent().value(), -9); |
| 568 | } |
| 569 | |
| 570 | // Exponent value was not specified |
| 571 | { |
| 572 | std::optional<int8_t> exponent{}; |
| 573 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 574 | EXPECT_EQ(action.getExponent().has_value(), false); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | TEST(PMBusWriteVoutCommandActionTests, GetFormat) |
| 579 | { |
| 580 | std::optional<double> volts{}; |
| 581 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 582 | std::optional<int8_t> exponent{}; |
| 583 | bool isVerified{false}; |
| 584 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 585 | EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear); |
| 586 | } |
| 587 | |
| 588 | TEST(PMBusWriteVoutCommandActionTests, GetVolts) |
| 589 | { |
| 590 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 591 | std::optional<int8_t> exponent{-8}; |
| 592 | bool isVerified{true}; |
| 593 | |
| 594 | // Volts value was specified |
| 595 | { |
| 596 | std::optional<double> volts{1.3}; |
| 597 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 598 | EXPECT_EQ(action.getVolts().has_value(), true); |
| 599 | EXPECT_EQ(action.getVolts().value(), 1.3); |
| 600 | } |
| 601 | |
| 602 | // Volts value was not specified |
| 603 | { |
| 604 | std::optional<double> volts{}; |
| 605 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 606 | EXPECT_EQ(action.getVolts().has_value(), false); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | TEST(PMBusWriteVoutCommandActionTests, IsVerified) |
| 611 | { |
| 612 | std::optional<double> volts{1.3}; |
| 613 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 614 | std::optional<int8_t> exponent{-8}; |
| 615 | bool isVerified{true}; |
| 616 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 617 | EXPECT_EQ(action.isVerified(), true); |
| 618 | } |
| 619 | |
| 620 | TEST(PMBusWriteVoutCommandActionTests, ToString) |
| 621 | { |
| 622 | // Test where volts value and exponent value are specified |
| 623 | { |
| 624 | std::optional<double> volts{1.3}; |
| 625 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 626 | std::optional<int8_t> exponent{-8}; |
| 627 | bool isVerified{true}; |
| 628 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 629 | EXPECT_EQ(action.toString(), |
| 630 | "pmbus_write_vout_command: { volts: 1.3, format: linear, " |
| 631 | "exponent: -8, is_verified: true }"); |
| 632 | } |
| 633 | |
| 634 | // Test where volts value and exponent value are not specified |
| 635 | { |
| 636 | std::optional<double> volts{}; |
| 637 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 638 | std::optional<int8_t> exponent{}; |
| 639 | bool isVerified{false}; |
| 640 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 641 | EXPECT_EQ( |
| 642 | action.toString(), |
| 643 | "pmbus_write_vout_command: { format: linear, is_verified: false }"); |
| 644 | } |
| 645 | } |