blob: 991949b706770ac6c53061236d031d2eb824aef3 [file] [log] [blame]
Shawn McCarney3daeb912019-10-28 15:04:17 -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 */
Shawn McCarney494ef032019-11-07 15:56:50 -060016#include "device.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060017#include "i2c_interface.hpp"
Shawn McCarney3daeb912019-10-28 15:04:17 -050018#include "id_map.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060019#include "mocked_i2c_interface.hpp"
Shawn McCarney494ef032019-11-07 15:56:50 -060020#include "rail.hpp"
21#include "rule.hpp"
Shawn McCarney3daeb912019-10-28 15:04:17 -050022
23#include <exception>
Shawn McCarney2134ca62019-11-11 13:06:18 -060024#include <memory>
Shawn McCarney3daeb912019-10-28 15:04:17 -050025#include <stdexcept>
26#include <string>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060027#include <utility>
Shawn McCarney2134ca62019-11-11 13:06:18 -060028#include <vector>
Shawn McCarney3daeb912019-10-28 15:04:17 -050029
30#include <gtest/gtest.h>
31
32using namespace phosphor::power::regulators;
33
34TEST(IDMapTests, AddDevice)
35{
36 IDMap idMap{};
37
38 // Create device
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060039 std::unique_ptr<i2c::I2CInterface> i2cInterface =
40 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
Shawn McCarney3daeb912019-10-28 15:04:17 -050041 std::string id{"vio_reg"};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060042 Device device{id, true, "/system/chassis/motherboard/vio_reg",
43 std::move(i2cInterface)};
Shawn McCarney3daeb912019-10-28 15:04:17 -050044
45 // Verify device is not initially in map
46 EXPECT_THROW(idMap.getDevice(id), std::invalid_argument);
47
48 // Add device to map
49 idMap.addDevice(device);
50
51 // Verify device is now in map
52 try
53 {
54 Device& deviceFound = idMap.getDevice(id);
55 EXPECT_EQ(deviceFound.getID(), id);
56 EXPECT_EQ(&deviceFound, &device);
57 }
58 catch (const std::exception& error)
59 {
60 ADD_FAILURE() << "Should not have caught exception.";
61 }
62
63 // Verify different device is not in map
64 EXPECT_THROW(idMap.getDevice("vio_reg2"), std::invalid_argument);
Shawn McCarneye38f8a22020-04-05 15:39:14 -050065
66 // Test where device ID already exists in map
67 try
68 {
69 i2cInterface =
70 i2c::create(1, 0x72, i2c::I2CInterface::InitialState::CLOSED);
71 Device device2{"vio_reg", true, "/system/chassis/motherboard/vio_reg2",
72 std::move(i2cInterface)};
73 idMap.addDevice(device2);
74 }
75 catch (const std::invalid_argument& error)
76 {
77 EXPECT_STREQ(error.what(),
78 "Unable to add device: Duplicate ID \"vio_reg\"");
79 }
Shawn McCarney3daeb912019-10-28 15:04:17 -050080}
81
82TEST(IDMapTests, AddRail)
83{
84 IDMap idMap{};
85
86 // Create rail
87 std::string id{"vio0"};
88 Rail rail{id};
89
90 // Verify rail is not initially in map
91 EXPECT_THROW(idMap.getRail(id), std::invalid_argument);
92
93 // Add rail to map
94 idMap.addRail(rail);
95
96 // Verify rail is now in map
97 try
98 {
99 Rail& railFound = idMap.getRail(id);
100 EXPECT_EQ(railFound.getID(), id);
101 EXPECT_EQ(&railFound, &rail);
102 }
103 catch (const std::exception& error)
104 {
105 ADD_FAILURE() << "Should not have caught exception.";
106 }
107
108 // Verify different rail is not in map
109 EXPECT_THROW(idMap.getRail("vcs0"), std::invalid_argument);
Shawn McCarneye38f8a22020-04-05 15:39:14 -0500110
111 // Test where rail ID already exists in map
112 try
113 {
114 Rail rail2{"vio0"};
115 idMap.addRail(rail2);
116 }
117 catch (const std::invalid_argument& error)
118 {
119 EXPECT_STREQ(error.what(), "Unable to add rail: Duplicate ID \"vio0\"");
120 }
Shawn McCarney3daeb912019-10-28 15:04:17 -0500121}
122
123TEST(IDMapTests, AddRule)
124{
125 IDMap idMap{};
126
127 // Create rule
128 std::string id{"set_voltage_rule"};
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600129 Rule rule{id, std::vector<std::unique_ptr<Action>>{}};
Shawn McCarney3daeb912019-10-28 15:04:17 -0500130
131 // Verify rule is not initially in map
132 EXPECT_THROW(idMap.getRule(id), std::invalid_argument);
133
134 // Add rule to map
135 idMap.addRule(rule);
136
137 // Verify rule is now in map
138 try
139 {
140 Rule& ruleFound = idMap.getRule(id);
141 EXPECT_EQ(ruleFound.getID(), id);
142 EXPECT_EQ(&ruleFound, &rule);
143 }
144 catch (const std::exception& error)
145 {
146 ADD_FAILURE() << "Should not have caught exception.";
147 }
148
149 // Verify different rule is not in map
150 EXPECT_THROW(idMap.getRule("set_voltage_rule_page0"),
151 std::invalid_argument);
Shawn McCarneye38f8a22020-04-05 15:39:14 -0500152
153 // Test where rule ID already exists in map
154 try
155 {
156 Rule rule2{"set_voltage_rule", std::vector<std::unique_ptr<Action>>{}};
157 idMap.addRule(rule2);
158 }
159 catch (const std::invalid_argument& error)
160 {
161 EXPECT_STREQ(error.what(),
162 "Unable to add rule: Duplicate ID \"set_voltage_rule\"");
163 }
Shawn McCarney3daeb912019-10-28 15:04:17 -0500164}
165
166TEST(IDMapTests, GetDevice)
167{
168 IDMap idMap{};
169
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600170 // Create device
171 std::unique_ptr<i2c::I2CInterface> i2cInterface =
172 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
Shawn McCarney3daeb912019-10-28 15:04:17 -0500173 std::string id{"vio_reg"};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600174 Device device{id, true, "/system/chassis/motherboard/vio_reg",
175 std::move(i2cInterface)};
176
177 // Add a device to the map
Shawn McCarney3daeb912019-10-28 15:04:17 -0500178 idMap.addDevice(device);
179
180 // Test where ID found in map
181 try
182 {
183 Device& deviceFound = idMap.getDevice(id);
184 EXPECT_EQ(deviceFound.getID(), id);
185 EXPECT_EQ(&deviceFound, &device);
186 }
187 catch (const std::exception& error)
188 {
189 ADD_FAILURE() << "Should not have caught exception.";
190 }
191
192 // Test where ID not found in map
193 try
194 {
195 idMap.getDevice("vcs_reg");
196 ADD_FAILURE() << "Should not have reached this line.";
197 }
198 catch (const std::invalid_argument& ia_error)
199 {
200 EXPECT_STREQ(ia_error.what(),
201 "Unable to find device with ID \"vcs_reg\"");
202 }
203 catch (const std::exception& error)
204 {
205 ADD_FAILURE() << "Should not have caught exception.";
206 }
207}
208
209TEST(IDMapTests, GetRail)
210{
211 IDMap idMap{};
212
213 // Add a rail to the map
214 std::string id{"vio0"};
215 Rail rail{id};
216 idMap.addRail(rail);
217
218 // Test where ID found in map
219 try
220 {
221 Rail& railFound = idMap.getRail(id);
222 EXPECT_EQ(railFound.getID(), id);
223 EXPECT_EQ(&railFound, &rail);
224 }
225 catch (const std::exception& error)
226 {
227 ADD_FAILURE() << "Should not have caught exception.";
228 }
229
230 // Test where ID not found in map
231 try
232 {
233 idMap.getRail("vcs0");
234 ADD_FAILURE() << "Should not have reached this line.";
235 }
236 catch (const std::invalid_argument& ia_error)
237 {
238 EXPECT_STREQ(ia_error.what(), "Unable to find rail with ID \"vcs0\"");
239 }
240 catch (const std::exception& error)
241 {
242 ADD_FAILURE() << "Should not have caught exception.";
243 }
244}
245
246TEST(IDMapTests, GetRule)
247{
248 IDMap idMap{};
249
250 // Add a rule to the map
251 std::string id{"set_voltage_rule"};
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600252 Rule rule{id, std::vector<std::unique_ptr<Action>>{}};
Shawn McCarney3daeb912019-10-28 15:04:17 -0500253 idMap.addRule(rule);
254
255 // Test where ID found in map
256 try
257 {
258 Rule& ruleFound = idMap.getRule(id);
259 EXPECT_EQ(ruleFound.getID(), id);
260 EXPECT_EQ(&ruleFound, &rule);
261 }
262 catch (const std::exception& error)
263 {
264 ADD_FAILURE() << "Should not have caught exception.";
265 }
266
267 // Test where ID not found in map
268 try
269 {
270 idMap.getRule("read_sensors_rule");
271 ADD_FAILURE() << "Should not have reached this line.";
272 }
273 catch (const std::invalid_argument& ia_error)
274 {
275 EXPECT_STREQ(ia_error.what(),
276 "Unable to find rule with ID \"read_sensors_rule\"");
277 }
278 catch (const std::exception& error)
279 {
280 ADD_FAILURE() << "Should not have caught exception.";
281 }
282}