blob: 6c8d1ca532c6e929e28812a3273458f9d804f63e [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"};
Bob Kinga76898f2020-10-13 15:08:33 +080042 Device device{
43 id, true,
44 "/xyz/openbmc_project/inventory/system/chassis/motherboard/vio_reg",
45 std::move(i2cInterface)};
Shawn McCarney3daeb912019-10-28 15:04:17 -050046
47 // Verify device is not initially in map
48 EXPECT_THROW(idMap.getDevice(id), std::invalid_argument);
49
50 // Add device to map
51 idMap.addDevice(device);
52
53 // Verify device is now in map
54 try
55 {
56 Device& deviceFound = idMap.getDevice(id);
57 EXPECT_EQ(deviceFound.getID(), id);
58 EXPECT_EQ(&deviceFound, &device);
59 }
60 catch (const std::exception& error)
61 {
62 ADD_FAILURE() << "Should not have caught exception.";
63 }
64
65 // Verify different device is not in map
66 EXPECT_THROW(idMap.getDevice("vio_reg2"), std::invalid_argument);
Shawn McCarneye38f8a22020-04-05 15:39:14 -050067
68 // Test where device ID already exists in map
69 try
70 {
Patrick Williams48781ae2023-05-10 07:50:50 -050071 i2cInterface = i2c::create(1, 0x72,
72 i2c::I2CInterface::InitialState::CLOSED);
Bob Kinga76898f2020-10-13 15:08:33 +080073 Device device2{"vio_reg", true,
74 "/xyz/openbmc_project/inventory/system/chassis/"
75 "motherboard/vio_reg2",
Shawn McCarneye38f8a22020-04-05 15:39:14 -050076 std::move(i2cInterface)};
77 idMap.addDevice(device2);
Shawn McCarneye22e0c42020-04-07 18:41:46 -050078 ADD_FAILURE() << "Should not have reached this line.";
Shawn McCarneye38f8a22020-04-05 15:39:14 -050079 }
80 catch (const std::invalid_argument& error)
81 {
82 EXPECT_STREQ(error.what(),
83 "Unable to add device: Duplicate ID \"vio_reg\"");
84 }
Shawn McCarney3daeb912019-10-28 15:04:17 -050085}
86
87TEST(IDMapTests, AddRail)
88{
89 IDMap idMap{};
90
91 // Create rail
92 std::string id{"vio0"};
93 Rail rail{id};
94
95 // Verify rail is not initially in map
96 EXPECT_THROW(idMap.getRail(id), std::invalid_argument);
97
98 // Add rail to map
99 idMap.addRail(rail);
100
101 // Verify rail is now in map
102 try
103 {
104 Rail& railFound = idMap.getRail(id);
105 EXPECT_EQ(railFound.getID(), id);
106 EXPECT_EQ(&railFound, &rail);
107 }
108 catch (const std::exception& error)
109 {
110 ADD_FAILURE() << "Should not have caught exception.";
111 }
112
113 // Verify different rail is not in map
114 EXPECT_THROW(idMap.getRail("vcs0"), std::invalid_argument);
Shawn McCarneye38f8a22020-04-05 15:39:14 -0500115
116 // Test where rail ID already exists in map
117 try
118 {
119 Rail rail2{"vio0"};
120 idMap.addRail(rail2);
Shawn McCarneye22e0c42020-04-07 18:41:46 -0500121 ADD_FAILURE() << "Should not have reached this line.";
Shawn McCarneye38f8a22020-04-05 15:39:14 -0500122 }
123 catch (const std::invalid_argument& error)
124 {
125 EXPECT_STREQ(error.what(), "Unable to add rail: Duplicate ID \"vio0\"");
126 }
Shawn McCarney3daeb912019-10-28 15:04:17 -0500127}
128
129TEST(IDMapTests, AddRule)
130{
131 IDMap idMap{};
132
133 // Create rule
134 std::string id{"set_voltage_rule"};
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600135 Rule rule{id, std::vector<std::unique_ptr<Action>>{}};
Shawn McCarney3daeb912019-10-28 15:04:17 -0500136
137 // Verify rule is not initially in map
138 EXPECT_THROW(idMap.getRule(id), std::invalid_argument);
139
140 // Add rule to map
141 idMap.addRule(rule);
142
143 // Verify rule is now in map
144 try
145 {
146 Rule& ruleFound = idMap.getRule(id);
147 EXPECT_EQ(ruleFound.getID(), id);
148 EXPECT_EQ(&ruleFound, &rule);
149 }
150 catch (const std::exception& error)
151 {
152 ADD_FAILURE() << "Should not have caught exception.";
153 }
154
155 // Verify different rule is not in map
156 EXPECT_THROW(idMap.getRule("set_voltage_rule_page0"),
157 std::invalid_argument);
Shawn McCarneye38f8a22020-04-05 15:39:14 -0500158
159 // Test where rule ID already exists in map
160 try
161 {
162 Rule rule2{"set_voltage_rule", std::vector<std::unique_ptr<Action>>{}};
163 idMap.addRule(rule2);
Shawn McCarneye22e0c42020-04-07 18:41:46 -0500164 ADD_FAILURE() << "Should not have reached this line.";
Shawn McCarneye38f8a22020-04-05 15:39:14 -0500165 }
166 catch (const std::invalid_argument& error)
167 {
168 EXPECT_STREQ(error.what(),
169 "Unable to add rule: Duplicate ID \"set_voltage_rule\"");
170 }
Shawn McCarney3daeb912019-10-28 15:04:17 -0500171}
172
173TEST(IDMapTests, GetDevice)
174{
175 IDMap idMap{};
176
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600177 // Create device
178 std::unique_ptr<i2c::I2CInterface> i2cInterface =
179 i2c::create(1, 0x70, i2c::I2CInterface::InitialState::CLOSED);
Shawn McCarney3daeb912019-10-28 15:04:17 -0500180 std::string id{"vio_reg"};
Bob Kinga76898f2020-10-13 15:08:33 +0800181 Device device{
182 id, true,
183 "/xyz/openbmc_project/inventory/system/chassis/motherboard/vio_reg",
184 std::move(i2cInterface)};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600185
186 // Add a device to the map
Shawn McCarney3daeb912019-10-28 15:04:17 -0500187 idMap.addDevice(device);
188
189 // Test where ID found in map
190 try
191 {
192 Device& deviceFound = idMap.getDevice(id);
193 EXPECT_EQ(deviceFound.getID(), id);
194 EXPECT_EQ(&deviceFound, &device);
195 }
196 catch (const std::exception& error)
197 {
198 ADD_FAILURE() << "Should not have caught exception.";
199 }
200
201 // Test where ID not found in map
202 try
203 {
204 idMap.getDevice("vcs_reg");
205 ADD_FAILURE() << "Should not have reached this line.";
206 }
207 catch (const std::invalid_argument& ia_error)
208 {
209 EXPECT_STREQ(ia_error.what(),
210 "Unable to find device with ID \"vcs_reg\"");
211 }
212 catch (const std::exception& error)
213 {
214 ADD_FAILURE() << "Should not have caught exception.";
215 }
216}
217
218TEST(IDMapTests, GetRail)
219{
220 IDMap idMap{};
221
222 // Add a rail to the map
223 std::string id{"vio0"};
224 Rail rail{id};
225 idMap.addRail(rail);
226
227 // Test where ID found in map
228 try
229 {
230 Rail& railFound = idMap.getRail(id);
231 EXPECT_EQ(railFound.getID(), id);
232 EXPECT_EQ(&railFound, &rail);
233 }
234 catch (const std::exception& error)
235 {
236 ADD_FAILURE() << "Should not have caught exception.";
237 }
238
239 // Test where ID not found in map
240 try
241 {
242 idMap.getRail("vcs0");
243 ADD_FAILURE() << "Should not have reached this line.";
244 }
245 catch (const std::invalid_argument& ia_error)
246 {
247 EXPECT_STREQ(ia_error.what(), "Unable to find rail with ID \"vcs0\"");
248 }
249 catch (const std::exception& error)
250 {
251 ADD_FAILURE() << "Should not have caught exception.";
252 }
253}
254
255TEST(IDMapTests, GetRule)
256{
257 IDMap idMap{};
258
259 // Add a rule to the map
260 std::string id{"set_voltage_rule"};
Shawn McCarney9fc08e72019-11-07 13:18:55 -0600261 Rule rule{id, std::vector<std::unique_ptr<Action>>{}};
Shawn McCarney3daeb912019-10-28 15:04:17 -0500262 idMap.addRule(rule);
263
264 // Test where ID found in map
265 try
266 {
267 Rule& ruleFound = idMap.getRule(id);
268 EXPECT_EQ(ruleFound.getID(), id);
269 EXPECT_EQ(&ruleFound, &rule);
270 }
271 catch (const std::exception& error)
272 {
273 ADD_FAILURE() << "Should not have caught exception.";
274 }
275
276 // Test where ID not found in map
277 try
278 {
279 idMap.getRule("read_sensors_rule");
280 ADD_FAILURE() << "Should not have reached this line.";
281 }
282 catch (const std::invalid_argument& ia_error)
283 {
284 EXPECT_STREQ(ia_error.what(),
285 "Unable to find rule with ID \"read_sensors_rule\"");
286 }
287 catch (const std::exception& error)
288 {
289 ADD_FAILURE() << "Should not have caught exception.";
290 }
291}