Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 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 "device.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 18 | #include "i2c_interface.hpp" |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 19 | #include "id_map.hpp" |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 20 | #include "mocked_i2c_interface.hpp" |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 21 | #include "rule.hpp" |
| 22 | |
| 23 | #include <cstddef> // for size_t |
| 24 | #include <exception> |
Shawn McCarney | 2134ca6 | 2019-11-11 13:06:18 -0600 | [diff] [blame] | 25 | #include <memory> |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 26 | #include <stdexcept> |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 27 | #include <utility> |
Shawn McCarney | 2134ca6 | 2019-11-11 13:06:18 -0600 | [diff] [blame] | 28 | #include <vector> |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 29 | |
| 30 | #include <gtest/gtest.h> |
| 31 | |
| 32 | using namespace phosphor::power::regulators; |
| 33 | |
| 34 | TEST(ActionEnvironmentTests, Constructor) |
| 35 | { |
| 36 | // Create IDMap |
| 37 | IDMap idMap{}; |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 38 | |
| 39 | // Create Device and add to IDMap |
| 40 | std::unique_ptr<i2c::I2CInterface> i2cInterface = |
| 41 | i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED); |
| 42 | Device reg1{"regulator1", true, "/system/chassis/motherboard/reg1", |
| 43 | std::move(i2cInterface)}; |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 44 | idMap.addDevice(reg1); |
| 45 | |
| 46 | // Verify object state after constructor |
| 47 | try |
| 48 | { |
| 49 | ActionEnvironment env{idMap, "regulator1"}; |
| 50 | EXPECT_EQ(env.getDevice().getID(), "regulator1"); |
| 51 | EXPECT_EQ(env.getDeviceID(), "regulator1"); |
| 52 | EXPECT_EQ(env.getRuleDepth(), 0); |
| 53 | EXPECT_THROW(env.getVolts(), std::logic_error); |
| 54 | EXPECT_EQ(env.hasVolts(), false); |
| 55 | } |
| 56 | catch (const std::exception& error) |
| 57 | { |
| 58 | ADD_FAILURE() << "Should not have caught exception."; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | TEST(ActionEnvironmentTests, DecrementRuleDepth) |
| 63 | { |
| 64 | IDMap idMap{}; |
| 65 | ActionEnvironment env{idMap, ""}; |
| 66 | EXPECT_EQ(env.getRuleDepth(), 0); |
Shawn McCarney | 2134ca6 | 2019-11-11 13:06:18 -0600 | [diff] [blame] | 67 | env.incrementRuleDepth("set_voltage_rule"); |
| 68 | env.incrementRuleDepth("set_voltage_rule"); |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 69 | EXPECT_EQ(env.getRuleDepth(), 2); |
| 70 | env.decrementRuleDepth(); |
| 71 | EXPECT_EQ(env.getRuleDepth(), 1); |
| 72 | env.decrementRuleDepth(); |
| 73 | EXPECT_EQ(env.getRuleDepth(), 0); |
| 74 | env.decrementRuleDepth(); |
| 75 | EXPECT_EQ(env.getRuleDepth(), 0); |
| 76 | } |
| 77 | |
| 78 | TEST(ActionEnvironmentTests, GetDevice) |
| 79 | { |
| 80 | // Create IDMap |
| 81 | IDMap idMap{}; |
Shawn McCarney | afb7fc3 | 2019-12-11 19:42:03 -0600 | [diff] [blame] | 82 | |
| 83 | // Create Device and add to IDMap |
| 84 | std::unique_ptr<i2c::I2CInterface> i2cInterface = |
| 85 | i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED); |
| 86 | Device reg1{"regulator1", true, "/system/chassis/motherboard/reg1", |
| 87 | std::move(i2cInterface)}; |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 88 | idMap.addDevice(reg1); |
| 89 | |
| 90 | ActionEnvironment env{idMap, "regulator1"}; |
| 91 | |
| 92 | // Test where current device ID is in the IDMap |
| 93 | try |
| 94 | { |
| 95 | Device& device = env.getDevice(); |
| 96 | EXPECT_EQ(device.getID(), "regulator1"); |
| 97 | EXPECT_EQ(&device, ®1); |
| 98 | } |
| 99 | catch (const std::exception& error) |
| 100 | { |
| 101 | ADD_FAILURE() << "Should not have caught exception."; |
| 102 | } |
| 103 | |
| 104 | // Test where current device ID is not in the IDMap |
| 105 | env.setDeviceID("regulator2"); |
| 106 | try |
| 107 | { |
| 108 | env.getDevice(); |
| 109 | ADD_FAILURE() << "Should not have reached this line."; |
| 110 | } |
| 111 | catch (const std::invalid_argument& ia_error) |
| 112 | { |
| 113 | EXPECT_STREQ(ia_error.what(), |
| 114 | "Unable to find device with ID \"regulator2\""); |
| 115 | } |
| 116 | catch (const std::exception& error) |
| 117 | { |
| 118 | ADD_FAILURE() << "Should not have caught exception."; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | TEST(ActionEnvironmentTests, GetDeviceID) |
| 123 | { |
| 124 | IDMap idMap{}; |
| 125 | ActionEnvironment env{idMap, ""}; |
| 126 | EXPECT_EQ(env.getDeviceID(), ""); |
| 127 | env.setDeviceID("regulator1"); |
| 128 | EXPECT_EQ(env.getDeviceID(), "regulator1"); |
| 129 | } |
| 130 | |
| 131 | TEST(ActionEnvironmentTests, GetRule) |
| 132 | { |
| 133 | // Create IDMap |
| 134 | IDMap idMap{}; |
Shawn McCarney | 9fc08e7 | 2019-11-07 13:18:55 -0600 | [diff] [blame] | 135 | Rule setVoltageRule{"set_voltage_rule", |
| 136 | std::vector<std::unique_ptr<Action>>{}}; |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 137 | idMap.addRule(setVoltageRule); |
| 138 | |
| 139 | ActionEnvironment env{idMap, ""}; |
| 140 | |
| 141 | // Test where rule ID is in the IDMap |
| 142 | try |
| 143 | { |
| 144 | Rule& rule = env.getRule("set_voltage_rule"); |
| 145 | EXPECT_EQ(rule.getID(), "set_voltage_rule"); |
| 146 | EXPECT_EQ(&rule, &setVoltageRule); |
| 147 | } |
| 148 | catch (const std::exception& error) |
| 149 | { |
| 150 | ADD_FAILURE() << "Should not have caught exception."; |
| 151 | } |
| 152 | |
| 153 | // Test where rule ID is not in the IDMap |
| 154 | try |
| 155 | { |
| 156 | env.getRule("set_voltage_rule2"); |
| 157 | ADD_FAILURE() << "Should not have reached this line."; |
| 158 | } |
| 159 | catch (const std::invalid_argument& ia_error) |
| 160 | { |
| 161 | EXPECT_STREQ(ia_error.what(), |
| 162 | "Unable to find rule with ID \"set_voltage_rule2\""); |
| 163 | } |
| 164 | catch (const std::exception& error) |
| 165 | { |
| 166 | ADD_FAILURE() << "Should not have caught exception."; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | TEST(ActionEnvironmentTests, GetRuleDepth) |
| 171 | { |
| 172 | IDMap idMap{}; |
| 173 | ActionEnvironment env{idMap, ""}; |
| 174 | EXPECT_EQ(env.getRuleDepth(), 0); |
Shawn McCarney | 2134ca6 | 2019-11-11 13:06:18 -0600 | [diff] [blame] | 175 | env.incrementRuleDepth("set_voltage_rule"); |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 176 | EXPECT_EQ(env.getRuleDepth(), 1); |
Shawn McCarney | 2134ca6 | 2019-11-11 13:06:18 -0600 | [diff] [blame] | 177 | env.incrementRuleDepth("set_voltage_rule"); |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 178 | EXPECT_EQ(env.getRuleDepth(), 2); |
| 179 | env.decrementRuleDepth(); |
| 180 | EXPECT_EQ(env.getRuleDepth(), 1); |
| 181 | env.decrementRuleDepth(); |
| 182 | EXPECT_EQ(env.getRuleDepth(), 0); |
| 183 | } |
| 184 | |
| 185 | TEST(ActionEnvironmentTests, GetVolts) |
| 186 | { |
| 187 | IDMap idMap{}; |
| 188 | ActionEnvironment env{idMap, ""}; |
| 189 | EXPECT_EQ(env.hasVolts(), false); |
| 190 | |
| 191 | // Test where a volts value has not been set |
| 192 | try |
| 193 | { |
| 194 | env.getVolts(); |
| 195 | } |
| 196 | catch (const std::logic_error& l_error) |
| 197 | { |
| 198 | EXPECT_STREQ(l_error.what(), "No volts value has been set."); |
| 199 | } |
| 200 | catch (const std::exception& error) |
| 201 | { |
| 202 | ADD_FAILURE() << "Should not have caught exception."; |
| 203 | } |
| 204 | |
| 205 | // Test where a volts value has been set |
| 206 | env.setVolts(1.31); |
| 207 | try |
| 208 | { |
| 209 | double volts = env.getVolts(); |
| 210 | EXPECT_EQ(volts, 1.31); |
| 211 | } |
| 212 | catch (const std::exception& error) |
| 213 | { |
| 214 | ADD_FAILURE() << "Should not have caught exception."; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | TEST(ActionEnvironmentTests, HasVolts) |
| 219 | { |
| 220 | IDMap idMap{}; |
| 221 | ActionEnvironment env{idMap, ""}; |
| 222 | EXPECT_EQ(env.hasVolts(), false); |
| 223 | env.setVolts(1.31); |
| 224 | EXPECT_EQ(env.hasVolts(), true); |
| 225 | } |
| 226 | |
| 227 | TEST(ActionEnvironmentTests, IncrementRuleDepth) |
| 228 | { |
| 229 | IDMap idMap{}; |
| 230 | ActionEnvironment env{idMap, ""}; |
| 231 | EXPECT_EQ(env.getRuleDepth(), 0); |
| 232 | |
| 233 | // Test where rule depth has not exceeded maximum |
| 234 | try |
| 235 | { |
| 236 | for (size_t i = 1; i <= env.maxRuleDepth; ++i) |
| 237 | { |
Shawn McCarney | 2134ca6 | 2019-11-11 13:06:18 -0600 | [diff] [blame] | 238 | env.incrementRuleDepth("set_voltage_rule"); |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 239 | EXPECT_EQ(env.getRuleDepth(), i); |
| 240 | } |
| 241 | } |
| 242 | catch (const std::exception& error) |
| 243 | { |
| 244 | ADD_FAILURE() << "Should not have caught exception."; |
| 245 | } |
| 246 | |
| 247 | // Test where rule depth has exceeded maximum |
| 248 | try |
| 249 | { |
Shawn McCarney | 2134ca6 | 2019-11-11 13:06:18 -0600 | [diff] [blame] | 250 | env.incrementRuleDepth("set_voltage_rule"); |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 251 | } |
| 252 | catch (const std::runtime_error& r_error) |
| 253 | { |
Shawn McCarney | 2134ca6 | 2019-11-11 13:06:18 -0600 | [diff] [blame] | 254 | EXPECT_STREQ(r_error.what(), |
| 255 | "Maximum rule depth exceeded by rule set_voltage_rule."); |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 256 | } |
| 257 | catch (const std::exception& error) |
| 258 | { |
| 259 | ADD_FAILURE() << "Should not have caught exception."; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | TEST(ActionEnvironmentTests, SetDeviceID) |
| 264 | { |
| 265 | IDMap idMap{}; |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 266 | ActionEnvironment env{idMap, "regulator1"}; |
Shawn McCarney | c69a275 | 2019-10-30 17:37:30 -0500 | [diff] [blame] | 267 | EXPECT_EQ(env.getDeviceID(), "regulator1"); |
| 268 | env.setDeviceID("regulator2"); |
| 269 | EXPECT_EQ(env.getDeviceID(), "regulator2"); |
| 270 | } |
| 271 | |
| 272 | TEST(ActionEnvironmentTests, SetVolts) |
| 273 | { |
| 274 | try |
| 275 | { |
| 276 | IDMap idMap{}; |
| 277 | ActionEnvironment env{idMap, ""}; |
| 278 | EXPECT_EQ(env.hasVolts(), false); |
| 279 | env.setVolts(2.35); |
| 280 | EXPECT_EQ(env.hasVolts(), true); |
| 281 | EXPECT_EQ(env.getVolts(), 2.35); |
| 282 | } |
| 283 | catch (const std::exception& error) |
| 284 | { |
| 285 | ADD_FAILURE() << "Should not have caught exception."; |
| 286 | } |
| 287 | } |