blob: 2bd94e0ea43d01b4b05aaafe5e93e15f190ff265 [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 McCarneycb3f6a62021-04-30 10:54:30 -050046static const std::string chassisInvPath{
47 "/xyz/openbmc_project/inventory/system/chassis"};
48
Shawn McCarneyc3991f12020-04-05 13:16:06 -050049TEST(SystemTests, Constructor)
50{
51 // Create Rules
52 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050053 rules.emplace_back(createRule("set_voltage_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050054
55 // Create Chassis
56 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050057 std::vector<std::unique_ptr<Device>> devices{};
58 devices.emplace_back(createDevice("reg1", {"rail1"}));
Shawn McCarneycb3f6a62021-04-30 10:54:30 -050059 chassis.emplace_back(
60 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices)));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050061
62 // Create System
63 System system{std::move(rules), std::move(chassis)};
64 EXPECT_EQ(system.getChassis().size(), 1);
65 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
Shawn McCarneydb0b8332020-04-06 14:13:04 -050066 EXPECT_NO_THROW(system.getIDMap().getRule("set_voltage_rule"));
67 EXPECT_NO_THROW(system.getIDMap().getDevice("reg1"));
68 EXPECT_NO_THROW(system.getIDMap().getRail("rail1"));
69 EXPECT_THROW(system.getIDMap().getRail("rail2"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -050070 EXPECT_EQ(system.getRules().size(), 1);
71 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
72}
73
Shawn McCarney9bd94d32021-01-25 19:40:42 -060074TEST(SystemTests, ClearCache)
75{
76 // Create PresenceDetection
77 std::vector<std::unique_ptr<Action>> actions{};
78 std::unique_ptr<PresenceDetection> presenceDetection =
79 std::make_unique<PresenceDetection>(std::move(actions));
80 PresenceDetection* presenceDetectionPtr = presenceDetection.get();
81
82 // Create Device that contains PresenceDetection
83 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
84 std::unique_ptr<Device> device = std::make_unique<Device>(
85 "reg1", true,
86 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
87 std::move(i2cInterface), std::move(presenceDetection));
88 Device* devicePtr = device.get();
89
90 // Create Chassis that contains Device
91 std::vector<std::unique_ptr<Device>> devices{};
92 devices.emplace_back(std::move(device));
93 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -050094 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney9bd94d32021-01-25 19:40:42 -060095 Chassis* chassisPtr = chassis.get();
96
97 // Create System that contains Chassis
98 std::vector<std::unique_ptr<Rule>> rules{};
99 std::vector<std::unique_ptr<Chassis>> chassisVec{};
100 chassisVec.emplace_back(std::move(chassis));
101 System system{std::move(rules), std::move(chassisVec)};
102
103 // Cache presence value in PresenceDetection
104 MockServices services{};
105 presenceDetectionPtr->execute(services, system, *chassisPtr, *devicePtr);
106 EXPECT_TRUE(presenceDetectionPtr->getCachedPresence().has_value());
107
108 // Clear cached data in System
109 system.clearCache();
110
111 // Verify presence value no longer cached in PresenceDetection
112 EXPECT_FALSE(presenceDetectionPtr->getCachedPresence().has_value());
113}
114
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500115TEST(SystemTests, CloseDevices)
116{
117 // Specify an empty rules vector
118 std::vector<std::unique_ptr<Rule>> rules{};
119
Bob Kingd692d6d2020-09-14 13:42:57 +0800120 // Create mock services. Expect logDebug() to be called.
121 MockServices services{};
122 MockJournal& journal = services.getMockJournal();
123 EXPECT_CALL(journal, logDebug("Closing devices in chassis 1")).Times(1);
124 EXPECT_CALL(journal, logDebug("Closing devices in chassis 3")).Times(1);
125 EXPECT_CALL(journal, logInfo(A<const std::string&>())).Times(0);
126 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
127
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500128 // Create Chassis
129 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500130 chassis.emplace_back(std::make_unique<Chassis>(1, chassisInvPath + '1'));
131 chassis.emplace_back(std::make_unique<Chassis>(3, chassisInvPath + '3'));
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500132
133 // Create System
134 System system{std::move(rules), std::move(chassis)};
135
136 // Call closeDevices()
Bob Kingd692d6d2020-09-14 13:42:57 +0800137 system.closeDevices(services);
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500138}
139
Shawn McCarney2af52892020-04-14 11:54:45 -0500140TEST(SystemTests, Configure)
141{
Bob King5cfe5102020-07-30 16:26:18 +0800142 // Create mock services. Expect logInfo() to be called.
Bob King23243f82020-07-29 10:38:57 +0800143 MockServices services{};
Bob King5cfe5102020-07-30 16:26:18 +0800144 MockJournal& journal = services.getMockJournal();
145 EXPECT_CALL(journal, logInfo("Configuring chassis 1")).Times(1);
146 EXPECT_CALL(journal, logInfo("Configuring chassis 3")).Times(1);
147 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
148 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
Bob King23243f82020-07-29 10:38:57 +0800149
Shawn McCarney2af52892020-04-14 11:54:45 -0500150 // Specify an empty rules vector
151 std::vector<std::unique_ptr<Rule>> rules{};
152
153 // Create Chassis
154 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500155 chassis.emplace_back(std::make_unique<Chassis>(1, chassisInvPath + '1'));
156 chassis.emplace_back(std::make_unique<Chassis>(3, chassisInvPath + '3'));
Shawn McCarney2af52892020-04-14 11:54:45 -0500157
158 // Create System
159 System system{std::move(rules), std::move(chassis)};
160
161 // Call configure()
Bob King23243f82020-07-29 10:38:57 +0800162 system.configure(services);
Shawn McCarney2af52892020-04-14 11:54:45 -0500163}
164
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500165TEST(SystemTests, GetChassis)
166{
167 // Specify an empty rules vector
168 std::vector<std::unique_ptr<Rule>> rules{};
169
170 // Create Chassis
171 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500172 chassis.emplace_back(std::make_unique<Chassis>(1, chassisInvPath + '1'));
173 chassis.emplace_back(std::make_unique<Chassis>(3, chassisInvPath + '3'));
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500174
175 // Create System
176 System system{std::move(rules), std::move(chassis)};
177 EXPECT_EQ(system.getChassis().size(), 2);
178 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
179 EXPECT_EQ(system.getChassis()[1]->getNumber(), 3);
180}
181
182TEST(SystemTests, GetIDMap)
183{
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500184 // Create Rules
185 std::vector<std::unique_ptr<Rule>> rules{};
186 rules.emplace_back(createRule("set_voltage_rule"));
187 rules.emplace_back(createRule("read_sensors_rule"));
188
189 // Create Chassis
190 std::vector<std::unique_ptr<Chassis>> chassis{};
191 {
192 // Chassis 1
193 std::vector<std::unique_ptr<Device>> devices{};
194 devices.emplace_back(createDevice("reg1", {"rail1"}));
195 devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"}));
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500196 chassis.emplace_back(std::make_unique<Chassis>(1, chassisInvPath + '1',
197 std::move(devices)));
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500198 }
199 {
200 // Chassis 2
201 std::vector<std::unique_ptr<Device>> devices{};
202 devices.emplace_back(createDevice("reg3", {"rail3a", "rail3b"}));
203 devices.emplace_back(createDevice("reg4"));
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500204 chassis.emplace_back(std::make_unique<Chassis>(2, chassisInvPath + '2',
205 std::move(devices)));
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500206 }
207
208 // Create System
209 System system{std::move(rules), std::move(chassis)};
210 const IDMap& idMap = system.getIDMap();
211
212 // Verify all Rules are in the IDMap
213 EXPECT_NO_THROW(idMap.getRule("set_voltage_rule"));
214 EXPECT_NO_THROW(idMap.getRule("read_sensors_rule"));
215 EXPECT_THROW(idMap.getRule("set_voltage_rule2"), std::invalid_argument);
216
217 // Verify all Devices are in the IDMap
218 EXPECT_NO_THROW(idMap.getDevice("reg1"));
219 EXPECT_NO_THROW(idMap.getDevice("reg2"));
220 EXPECT_NO_THROW(idMap.getDevice("reg3"));
221 EXPECT_NO_THROW(idMap.getDevice("reg4"));
222 EXPECT_THROW(idMap.getDevice("reg5"), std::invalid_argument);
223
224 // Verify all Rails are in the IDMap
225 EXPECT_NO_THROW(idMap.getRail("rail1"));
226 EXPECT_NO_THROW(idMap.getRail("rail2a"));
227 EXPECT_NO_THROW(idMap.getRail("rail2b"));
228 EXPECT_NO_THROW(idMap.getRail("rail3a"));
229 EXPECT_NO_THROW(idMap.getRail("rail3b"));
230 EXPECT_THROW(idMap.getRail("rail4"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500231}
232
233TEST(SystemTests, GetRules)
234{
235 // Create Rules
236 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500237 rules.emplace_back(createRule("set_voltage_rule"));
238 rules.emplace_back(createRule("read_sensors_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500239
240 // Create Chassis
241 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500242 chassis.emplace_back(std::make_unique<Chassis>(1, chassisInvPath));
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500243
244 // Create System
245 System system{std::move(rules), std::move(chassis)};
246 EXPECT_EQ(system.getRules().size(), 2);
247 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
248 EXPECT_EQ(system.getRules()[1]->getID(), "read_sensors_rule");
249}
Bob King8e2294d2020-07-14 17:41:31 +0800250
251TEST(SystemTests, MonitorSensors)
252{
Bob King8a552922020-08-05 17:02:31 +0800253 // Create mock services. No logging should occur.
254 MockServices services{};
255 MockJournal& journal = services.getMockJournal();
256 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
257 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
258
Bob King8e2294d2020-07-14 17:41:31 +0800259 // Create PMBusReadSensorAction
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500260 SensorType type{SensorType::iout};
Bob King8e2294d2020-07-14 17:41:31 +0800261 uint8_t command = 0x8C;
262 pmbus_utils::SensorDataFormat format{
263 pmbus_utils::SensorDataFormat::linear_11};
264 std::optional<int8_t> exponent{};
265 std::unique_ptr<PMBusReadSensorAction> action =
266 std::make_unique<PMBusReadSensorAction>(type, command, format,
267 exponent);
268
269 // Create mock I2CInterface. A two-byte read should occur.
270 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
271 std::make_unique<i2c::MockedI2CInterface>();
272 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
273 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
274 .Times(1);
275
276 // Create SensorMonitoring
277 std::vector<std::unique_ptr<Action>> actions{};
278 actions.emplace_back(std::move(action));
279 std::unique_ptr<SensorMonitoring> sensorMonitoring =
280 std::make_unique<SensorMonitoring>(std::move(actions));
281
282 // Create Rail
283 std::vector<std::unique_ptr<Rail>> rails{};
284 std::unique_ptr<Configuration> configuration{};
285 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
286 "vdd0", std::move(configuration), std::move(sensorMonitoring));
287 rails.emplace_back(std::move(rail));
288
289 // Create Device
290 std::unique_ptr<PresenceDetection> presenceDetection{};
291 std::unique_ptr<Configuration> deviceConfiguration{};
292 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800293 "reg1", true,
294 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King8e2294d2020-07-14 17:41:31 +0800295 std::move(i2cInterface), std::move(presenceDetection),
296 std::move(deviceConfiguration), std::move(rails));
297
298 // Create Chassis
299 std::vector<std::unique_ptr<Device>> devices{};
300 devices.emplace_back(std::move(device));
301 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500302 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Bob King8e2294d2020-07-14 17:41:31 +0800303
304 // Create System that contains Chassis
305 std::vector<std::unique_ptr<Rule>> rules{};
306 std::vector<std::unique_ptr<Chassis>> chassisVec{};
307 chassisVec.emplace_back(std::move(chassis));
308 System system{std::move(rules), std::move(chassisVec)};
309
310 // Call monitorSensors()
Bob King8a552922020-08-05 17:02:31 +0800311 system.monitorSensors(services);
Bob King8e2294d2020-07-14 17:41:31 +0800312}