blob: 5ce76ed33868ea033ad1b8d5825440b1eab85e06 [file] [log] [blame]
Shawn McCarneyc3991f12020-04-05 13:16:06 -05001/**
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 */
Shawn McCarneyc3991f12020-04-05 13:16:06 -050016#include "chassis.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050017#include "device.hpp"
18#include "id_map.hpp"
Shawn McCarney2af52892020-04-14 11:54:45 -050019#include "mock_journal.hpp"
Bob King23243f82020-07-29 10:38:57 +080020#include "mock_services.hpp"
Bob King8e2294d2020-07-14 17:41:31 +080021#include "mocked_i2c_interface.hpp"
22#include "pmbus_read_sensor_action.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050023#include "rail.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050024#include "rule.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050025#include "sensors.hpp"
Bob King5cfe5102020-07-30 16:26:18 +080026#include "services.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050027#include "system.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050028#include "test_utils.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050029
30#include <memory>
Shawn McCarneydb0b8332020-04-06 14:13:04 -050031#include <stdexcept>
Shawn McCarney2af52892020-04-14 11:54:45 -050032#include <string>
Shawn McCarneyc3991f12020-04-05 13:16:06 -050033#include <utility>
34#include <vector>
35
Bob King8e2294d2020-07-14 17:41:31 +080036#include <gmock/gmock.h>
Shawn McCarneyc3991f12020-04-05 13:16:06 -050037#include <gtest/gtest.h>
38
39using namespace phosphor::power::regulators;
Shawn McCarneydb0b8332020-04-06 14:13:04 -050040using namespace phosphor::power::regulators::test_utils;
Shawn McCarneyc3991f12020-04-05 13:16:06 -050041
Bob King8e2294d2020-07-14 17:41:31 +080042using ::testing::A;
43using ::testing::Return;
44using ::testing::TypedEq;
45
Shawn McCarneyc3991f12020-04-05 13:16:06 -050046TEST(SystemTests, Constructor)
47{
48 // Create Rules
49 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050050 rules.emplace_back(createRule("set_voltage_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050051
52 // Create Chassis
53 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050054 std::vector<std::unique_ptr<Device>> devices{};
55 devices.emplace_back(createDevice("reg1", {"rail1"}));
56 chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050057
58 // Create System
59 System system{std::move(rules), std::move(chassis)};
60 EXPECT_EQ(system.getChassis().size(), 1);
61 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
Shawn McCarneydb0b8332020-04-06 14:13:04 -050062 EXPECT_NO_THROW(system.getIDMap().getRule("set_voltage_rule"));
63 EXPECT_NO_THROW(system.getIDMap().getDevice("reg1"));
64 EXPECT_NO_THROW(system.getIDMap().getRail("rail1"));
65 EXPECT_THROW(system.getIDMap().getRail("rail2"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -050066 EXPECT_EQ(system.getRules().size(), 1);
67 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
68}
69
Shawn McCarney9bd94d32021-01-25 19:40:42 -060070TEST(SystemTests, ClearCache)
71{
72 // Create PresenceDetection
73 std::vector<std::unique_ptr<Action>> actions{};
74 std::unique_ptr<PresenceDetection> presenceDetection =
75 std::make_unique<PresenceDetection>(std::move(actions));
76 PresenceDetection* presenceDetectionPtr = presenceDetection.get();
77
78 // Create Device that contains PresenceDetection
79 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
80 std::unique_ptr<Device> device = std::make_unique<Device>(
81 "reg1", true,
82 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
83 std::move(i2cInterface), std::move(presenceDetection));
84 Device* devicePtr = device.get();
85
86 // Create Chassis that contains Device
87 std::vector<std::unique_ptr<Device>> devices{};
88 devices.emplace_back(std::move(device));
89 std::unique_ptr<Chassis> chassis =
90 std::make_unique<Chassis>(1, std::move(devices));
91 Chassis* chassisPtr = chassis.get();
92
93 // Create System that contains Chassis
94 std::vector<std::unique_ptr<Rule>> rules{};
95 std::vector<std::unique_ptr<Chassis>> chassisVec{};
96 chassisVec.emplace_back(std::move(chassis));
97 System system{std::move(rules), std::move(chassisVec)};
98
99 // Cache presence value in PresenceDetection
100 MockServices services{};
101 presenceDetectionPtr->execute(services, system, *chassisPtr, *devicePtr);
102 EXPECT_TRUE(presenceDetectionPtr->getCachedPresence().has_value());
103
104 // Clear cached data in System
105 system.clearCache();
106
107 // Verify presence value no longer cached in PresenceDetection
108 EXPECT_FALSE(presenceDetectionPtr->getCachedPresence().has_value());
109}
110
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500111TEST(SystemTests, CloseDevices)
112{
113 // Specify an empty rules vector
114 std::vector<std::unique_ptr<Rule>> rules{};
115
Bob Kingd692d6d2020-09-14 13:42:57 +0800116 // Create mock services. Expect logDebug() to be called.
117 MockServices services{};
118 MockJournal& journal = services.getMockJournal();
119 EXPECT_CALL(journal, logDebug("Closing devices in chassis 1")).Times(1);
120 EXPECT_CALL(journal, logDebug("Closing devices in chassis 3")).Times(1);
121 EXPECT_CALL(journal, logInfo(A<const std::string&>())).Times(0);
122 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
123
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500124 // Create Chassis
125 std::vector<std::unique_ptr<Chassis>> chassis{};
126 chassis.emplace_back(std::make_unique<Chassis>(1));
127 chassis.emplace_back(std::make_unique<Chassis>(3));
128
129 // Create System
130 System system{std::move(rules), std::move(chassis)};
131
132 // Call closeDevices()
Bob Kingd692d6d2020-09-14 13:42:57 +0800133 system.closeDevices(services);
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500134}
135
Shawn McCarney2af52892020-04-14 11:54:45 -0500136TEST(SystemTests, Configure)
137{
Bob King5cfe5102020-07-30 16:26:18 +0800138 // Create mock services. Expect logInfo() to be called.
Bob King23243f82020-07-29 10:38:57 +0800139 MockServices services{};
Bob King5cfe5102020-07-30 16:26:18 +0800140 MockJournal& journal = services.getMockJournal();
141 EXPECT_CALL(journal, logInfo("Configuring chassis 1")).Times(1);
142 EXPECT_CALL(journal, logInfo("Configuring chassis 3")).Times(1);
143 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
144 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
Bob King23243f82020-07-29 10:38:57 +0800145
Shawn McCarney2af52892020-04-14 11:54:45 -0500146 // Specify an empty rules vector
147 std::vector<std::unique_ptr<Rule>> rules{};
148
149 // Create Chassis
150 std::vector<std::unique_ptr<Chassis>> chassis{};
151 chassis.emplace_back(std::make_unique<Chassis>(1));
152 chassis.emplace_back(std::make_unique<Chassis>(3));
153
154 // Create System
155 System system{std::move(rules), std::move(chassis)};
156
157 // Call configure()
Bob King23243f82020-07-29 10:38:57 +0800158 system.configure(services);
Shawn McCarney2af52892020-04-14 11:54:45 -0500159}
160
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500161TEST(SystemTests, GetChassis)
162{
163 // Specify an empty rules vector
164 std::vector<std::unique_ptr<Rule>> rules{};
165
166 // Create Chassis
167 std::vector<std::unique_ptr<Chassis>> chassis{};
168 chassis.emplace_back(std::make_unique<Chassis>(1));
169 chassis.emplace_back(std::make_unique<Chassis>(3));
170
171 // Create System
172 System system{std::move(rules), std::move(chassis)};
173 EXPECT_EQ(system.getChassis().size(), 2);
174 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
175 EXPECT_EQ(system.getChassis()[1]->getNumber(), 3);
176}
177
178TEST(SystemTests, GetIDMap)
179{
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500180 // Create Rules
181 std::vector<std::unique_ptr<Rule>> rules{};
182 rules.emplace_back(createRule("set_voltage_rule"));
183 rules.emplace_back(createRule("read_sensors_rule"));
184
185 // Create Chassis
186 std::vector<std::unique_ptr<Chassis>> chassis{};
187 {
188 // Chassis 1
189 std::vector<std::unique_ptr<Device>> devices{};
190 devices.emplace_back(createDevice("reg1", {"rail1"}));
191 devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"}));
192 chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
193 }
194 {
195 // Chassis 2
196 std::vector<std::unique_ptr<Device>> devices{};
197 devices.emplace_back(createDevice("reg3", {"rail3a", "rail3b"}));
198 devices.emplace_back(createDevice("reg4"));
199 chassis.emplace_back(std::make_unique<Chassis>(2, std::move(devices)));
200 }
201
202 // Create System
203 System system{std::move(rules), std::move(chassis)};
204 const IDMap& idMap = system.getIDMap();
205
206 // Verify all Rules are in the IDMap
207 EXPECT_NO_THROW(idMap.getRule("set_voltage_rule"));
208 EXPECT_NO_THROW(idMap.getRule("read_sensors_rule"));
209 EXPECT_THROW(idMap.getRule("set_voltage_rule2"), std::invalid_argument);
210
211 // Verify all Devices are in the IDMap
212 EXPECT_NO_THROW(idMap.getDevice("reg1"));
213 EXPECT_NO_THROW(idMap.getDevice("reg2"));
214 EXPECT_NO_THROW(idMap.getDevice("reg3"));
215 EXPECT_NO_THROW(idMap.getDevice("reg4"));
216 EXPECT_THROW(idMap.getDevice("reg5"), std::invalid_argument);
217
218 // Verify all Rails are in the IDMap
219 EXPECT_NO_THROW(idMap.getRail("rail1"));
220 EXPECT_NO_THROW(idMap.getRail("rail2a"));
221 EXPECT_NO_THROW(idMap.getRail("rail2b"));
222 EXPECT_NO_THROW(idMap.getRail("rail3a"));
223 EXPECT_NO_THROW(idMap.getRail("rail3b"));
224 EXPECT_THROW(idMap.getRail("rail4"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500225}
226
227TEST(SystemTests, GetRules)
228{
229 // Create Rules
230 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500231 rules.emplace_back(createRule("set_voltage_rule"));
232 rules.emplace_back(createRule("read_sensors_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500233
234 // Create Chassis
235 std::vector<std::unique_ptr<Chassis>> chassis{};
236 chassis.emplace_back(std::make_unique<Chassis>(1));
237
238 // Create System
239 System system{std::move(rules), std::move(chassis)};
240 EXPECT_EQ(system.getRules().size(), 2);
241 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
242 EXPECT_EQ(system.getRules()[1]->getID(), "read_sensors_rule");
243}
Bob King8e2294d2020-07-14 17:41:31 +0800244
245TEST(SystemTests, MonitorSensors)
246{
Bob King8a552922020-08-05 17:02:31 +0800247 // Create mock services. No logging should occur.
248 MockServices services{};
249 MockJournal& journal = services.getMockJournal();
250 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
251 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
252
Bob King8e2294d2020-07-14 17:41:31 +0800253 // Create PMBusReadSensorAction
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500254 SensorType type{SensorType::iout};
Bob King8e2294d2020-07-14 17:41:31 +0800255 uint8_t command = 0x8C;
256 pmbus_utils::SensorDataFormat format{
257 pmbus_utils::SensorDataFormat::linear_11};
258 std::optional<int8_t> exponent{};
259 std::unique_ptr<PMBusReadSensorAction> action =
260 std::make_unique<PMBusReadSensorAction>(type, command, format,
261 exponent);
262
263 // Create mock I2CInterface. A two-byte read should occur.
264 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
265 std::make_unique<i2c::MockedI2CInterface>();
266 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
267 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
268 .Times(1);
269
270 // Create SensorMonitoring
271 std::vector<std::unique_ptr<Action>> actions{};
272 actions.emplace_back(std::move(action));
273 std::unique_ptr<SensorMonitoring> sensorMonitoring =
274 std::make_unique<SensorMonitoring>(std::move(actions));
275
276 // Create Rail
277 std::vector<std::unique_ptr<Rail>> rails{};
278 std::unique_ptr<Configuration> configuration{};
279 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
280 "vdd0", std::move(configuration), std::move(sensorMonitoring));
281 rails.emplace_back(std::move(rail));
282
283 // Create Device
284 std::unique_ptr<PresenceDetection> presenceDetection{};
285 std::unique_ptr<Configuration> deviceConfiguration{};
286 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800287 "reg1", true,
288 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King8e2294d2020-07-14 17:41:31 +0800289 std::move(i2cInterface), std::move(presenceDetection),
290 std::move(deviceConfiguration), std::move(rails));
291
292 // Create Chassis
293 std::vector<std::unique_ptr<Device>> devices{};
294 devices.emplace_back(std::move(device));
295 std::unique_ptr<Chassis> chassis =
296 std::make_unique<Chassis>(1, std::move(devices));
297
298 // Create System that contains Chassis
299 std::vector<std::unique_ptr<Rule>> rules{};
300 std::vector<std::unique_ptr<Chassis>> chassisVec{};
301 chassisVec.emplace_back(std::move(chassis));
302 System system{std::move(rules), std::move(chassisVec)};
303
304 // Call monitorSensors()
Bob King8a552922020-08-05 17:02:31 +0800305 system.monitorSensors(services);
Bob King8e2294d2020-07-14 17:41:31 +0800306}