blob: 54afc2ec7b2455f14120226bcc2624c8e34bd42f [file] [log] [blame]
Shawn McCarneyc69a2752019-10-30 17:37:30 -05001/**
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 McCarneyafb7fc32019-12-11 19:42:03 -060018#include "i2c_interface.hpp"
Shawn McCarneyc69a2752019-10-30 17:37:30 -050019#include "id_map.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060020#include "mocked_i2c_interface.hpp"
Bob Kingb1ab1cf2020-05-18 18:26:26 +080021#include "pmbus_utils.hpp"
Shawn McCarneyc69a2752019-10-30 17:37:30 -050022#include "rule.hpp"
23
24#include <cstddef> // for size_t
25#include <exception>
Shawn McCarney2134ca62019-11-11 13:06:18 -060026#include <memory>
Shawn McCarneyc69a2752019-10-30 17:37:30 -050027#include <stdexcept>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060028#include <utility>
Shawn McCarney2134ca62019-11-11 13:06:18 -060029#include <vector>
Shawn McCarneyc69a2752019-10-30 17:37:30 -050030
31#include <gtest/gtest.h>
32
33using namespace phosphor::power::regulators;
34
35TEST(ActionEnvironmentTests, Constructor)
36{
37 // Create IDMap
38 IDMap idMap{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060039
40 // Create Device and add to IDMap
41 std::unique_ptr<i2c::I2CInterface> i2cInterface =
42 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
Bob Kinga76898f2020-10-13 15:08:33 +080043 Device reg1{
44 "regulator1", true,
45 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
46 std::move(i2cInterface)};
Shawn McCarneyc69a2752019-10-30 17:37:30 -050047 idMap.addDevice(reg1);
48
49 // Verify object state after constructor
50 try
51 {
52 ActionEnvironment env{idMap, "regulator1"};
53 EXPECT_EQ(env.getDevice().getID(), "regulator1");
54 EXPECT_EQ(env.getDeviceID(), "regulator1");
55 EXPECT_EQ(env.getRuleDepth(), 0);
Shawn McCarney0fd07d72020-03-06 17:16:24 -060056 EXPECT_EQ(env.getVolts().has_value(), false);
Shawn McCarneyc69a2752019-10-30 17:37:30 -050057 }
58 catch (const std::exception& error)
59 {
60 ADD_FAILURE() << "Should not have caught exception.";
61 }
62}
63
Bob Kingb1ab1cf2020-05-18 18:26:26 +080064TEST(ActionEnvironmentTests, AddSensorReading)
65{
66 IDMap idMap{};
67 ActionEnvironment env{idMap, ""};
68 pmbus_utils::SensorReading reading;
69 reading.type = pmbus_utils::SensorValueType::iout;
70 reading.value = 1;
71 EXPECT_EQ(env.getSensorReadings().size(), 0);
72 env.addSensorReading(reading);
73 EXPECT_EQ(env.getSensorReadings().size(), 1);
74 EXPECT_EQ(env.getSensorReadings()[0].type,
75 pmbus_utils::SensorValueType::iout);
76 EXPECT_EQ(env.getSensorReadings()[0].value, 1);
77 reading.type = pmbus_utils::SensorValueType::vout;
78 reading.value = 2;
79 env.addSensorReading(reading);
80 EXPECT_EQ(env.getSensorReadings().size(), 2);
81 EXPECT_EQ(env.getSensorReadings()[1].type,
82 pmbus_utils::SensorValueType::vout);
83 EXPECT_EQ(env.getSensorReadings()[1].value, 2);
84}
85
Shawn McCarneyc69a2752019-10-30 17:37:30 -050086TEST(ActionEnvironmentTests, DecrementRuleDepth)
87{
88 IDMap idMap{};
89 ActionEnvironment env{idMap, ""};
90 EXPECT_EQ(env.getRuleDepth(), 0);
Shawn McCarney2134ca62019-11-11 13:06:18 -060091 env.incrementRuleDepth("set_voltage_rule");
92 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -050093 EXPECT_EQ(env.getRuleDepth(), 2);
94 env.decrementRuleDepth();
95 EXPECT_EQ(env.getRuleDepth(), 1);
96 env.decrementRuleDepth();
97 EXPECT_EQ(env.getRuleDepth(), 0);
98 env.decrementRuleDepth();
99 EXPECT_EQ(env.getRuleDepth(), 0);
100}
101
102TEST(ActionEnvironmentTests, GetDevice)
103{
104 // Create IDMap
105 IDMap idMap{};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600106
107 // Create Device and add to IDMap
108 std::unique_ptr<i2c::I2CInterface> i2cInterface =
109 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
Bob Kinga76898f2020-10-13 15:08:33 +0800110 Device reg1{
111 "regulator1", true,
112 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
113 std::move(i2cInterface)};
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500114 idMap.addDevice(reg1);
115
116 ActionEnvironment env{idMap, "regulator1"};
117
118 // Test where current device ID is in the IDMap
119 try
120 {
121 Device& device = env.getDevice();
122 EXPECT_EQ(device.getID(), "regulator1");
123 EXPECT_EQ(&device, &reg1);
124 }
125 catch (const std::exception& error)
126 {
127 ADD_FAILURE() << "Should not have caught exception.";
128 }
129
130 // Test where current device ID is not in the IDMap
131 env.setDeviceID("regulator2");
132 try
133 {
134 env.getDevice();
135 ADD_FAILURE() << "Should not have reached this line.";
136 }
137 catch (const std::invalid_argument& ia_error)
138 {
139 EXPECT_STREQ(ia_error.what(),
140 "Unable to find device with ID \"regulator2\"");
141 }
142 catch (const std::exception& error)
143 {
144 ADD_FAILURE() << "Should not have caught exception.";
145 }
146}
147
148TEST(ActionEnvironmentTests, GetDeviceID)
149{
150 IDMap idMap{};
151 ActionEnvironment env{idMap, ""};
152 EXPECT_EQ(env.getDeviceID(), "");
153 env.setDeviceID("regulator1");
154 EXPECT_EQ(env.getDeviceID(), "regulator1");
155}
156
157TEST(ActionEnvironmentTests, GetRule)
158{
159 // Create IDMap
160 IDMap idMap{};
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600161 Rule setVoltageRule{"set_voltage_rule",
162 std::vector<std::unique_ptr<Action>>{}};
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500163 idMap.addRule(setVoltageRule);
164
165 ActionEnvironment env{idMap, ""};
166
167 // Test where rule ID is in the IDMap
168 try
169 {
170 Rule& rule = env.getRule("set_voltage_rule");
171 EXPECT_EQ(rule.getID(), "set_voltage_rule");
172 EXPECT_EQ(&rule, &setVoltageRule);
173 }
174 catch (const std::exception& error)
175 {
176 ADD_FAILURE() << "Should not have caught exception.";
177 }
178
179 // Test where rule ID is not in the IDMap
180 try
181 {
182 env.getRule("set_voltage_rule2");
183 ADD_FAILURE() << "Should not have reached this line.";
184 }
185 catch (const std::invalid_argument& ia_error)
186 {
187 EXPECT_STREQ(ia_error.what(),
188 "Unable to find rule with ID \"set_voltage_rule2\"");
189 }
190 catch (const std::exception& error)
191 {
192 ADD_FAILURE() << "Should not have caught exception.";
193 }
194}
195
196TEST(ActionEnvironmentTests, GetRuleDepth)
197{
198 IDMap idMap{};
199 ActionEnvironment env{idMap, ""};
200 EXPECT_EQ(env.getRuleDepth(), 0);
Shawn McCarney2134ca62019-11-11 13:06:18 -0600201 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500202 EXPECT_EQ(env.getRuleDepth(), 1);
Shawn McCarney2134ca62019-11-11 13:06:18 -0600203 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500204 EXPECT_EQ(env.getRuleDepth(), 2);
205 env.decrementRuleDepth();
206 EXPECT_EQ(env.getRuleDepth(), 1);
207 env.decrementRuleDepth();
208 EXPECT_EQ(env.getRuleDepth(), 0);
209}
210
Bob Kingb1ab1cf2020-05-18 18:26:26 +0800211TEST(ActionEnvironmentTests, GetSensorReadings)
212{
213 IDMap idMap{};
214 ActionEnvironment env{idMap, ""};
215 pmbus_utils::SensorReading reading;
216 reading.type = pmbus_utils::SensorValueType::pout;
217 reading.value = 1.3;
218 EXPECT_EQ(env.getSensorReadings().size(), 0);
219 env.addSensorReading(reading);
220 EXPECT_EQ(env.getSensorReadings().size(), 1);
221 EXPECT_EQ(env.getSensorReadings()[0].type,
222 pmbus_utils::SensorValueType::pout);
223 EXPECT_EQ(env.getSensorReadings()[0].value, 1.3);
224 reading.type = pmbus_utils::SensorValueType::temperature;
225 reading.value = -1;
226 env.addSensorReading(reading);
227 EXPECT_EQ(env.getSensorReadings().size(), 2);
228 EXPECT_EQ(env.getSensorReadings()[1].type,
229 pmbus_utils::SensorValueType::temperature);
230 EXPECT_EQ(env.getSensorReadings()[1].value, -1);
231}
232
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500233TEST(ActionEnvironmentTests, GetVolts)
234{
235 IDMap idMap{};
236 ActionEnvironment env{idMap, ""};
Shawn McCarney0fd07d72020-03-06 17:16:24 -0600237 EXPECT_EQ(env.getVolts().has_value(), false);
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500238 env.setVolts(1.31);
Shawn McCarney0fd07d72020-03-06 17:16:24 -0600239 EXPECT_EQ(env.getVolts().has_value(), true);
240 EXPECT_EQ(env.getVolts().value(), 1.31);
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500241}
242
243TEST(ActionEnvironmentTests, IncrementRuleDepth)
244{
245 IDMap idMap{};
246 ActionEnvironment env{idMap, ""};
247 EXPECT_EQ(env.getRuleDepth(), 0);
248
249 // Test where rule depth has not exceeded maximum
250 try
251 {
252 for (size_t i = 1; i <= env.maxRuleDepth; ++i)
253 {
Shawn McCarney2134ca62019-11-11 13:06:18 -0600254 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500255 EXPECT_EQ(env.getRuleDepth(), i);
256 }
257 }
258 catch (const std::exception& error)
259 {
260 ADD_FAILURE() << "Should not have caught exception.";
261 }
262
263 // Test where rule depth has exceeded maximum
264 try
265 {
Shawn McCarney2134ca62019-11-11 13:06:18 -0600266 env.incrementRuleDepth("set_voltage_rule");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500267 }
268 catch (const std::runtime_error& r_error)
269 {
Shawn McCarney2134ca62019-11-11 13:06:18 -0600270 EXPECT_STREQ(r_error.what(),
271 "Maximum rule depth exceeded by rule set_voltage_rule.");
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500272 }
273 catch (const std::exception& error)
274 {
275 ADD_FAILURE() << "Should not have caught exception.";
276 }
277}
278
279TEST(ActionEnvironmentTests, SetDeviceID)
280{
281 IDMap idMap{};
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500282 ActionEnvironment env{idMap, "regulator1"};
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500283 EXPECT_EQ(env.getDeviceID(), "regulator1");
284 env.setDeviceID("regulator2");
285 EXPECT_EQ(env.getDeviceID(), "regulator2");
286}
287
288TEST(ActionEnvironmentTests, SetVolts)
289{
290 try
291 {
292 IDMap idMap{};
293 ActionEnvironment env{idMap, ""};
Shawn McCarney0fd07d72020-03-06 17:16:24 -0600294 EXPECT_EQ(env.getVolts().has_value(), false);
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500295 env.setVolts(2.35);
Shawn McCarney0fd07d72020-03-06 17:16:24 -0600296 EXPECT_EQ(env.getVolts().has_value(), true);
297 EXPECT_EQ(env.getVolts().value(), 2.35);
Shawn McCarneyc69a2752019-10-30 17:37:30 -0500298 }
299 catch (const std::exception& error)
300 {
301 ADD_FAILURE() << "Should not have caught exception.";
302 }
303}