blob: 5d1d7204b822f65ad85fd982e29fa87dd100fd2c [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"
Bob King5cfe5102020-07-30 16:26:18 +080025#include "services.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050026#include "system.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050027#include "test_utils.hpp"
Shawn McCarneyc3991f12020-04-05 13:16:06 -050028
29#include <memory>
Shawn McCarneydb0b8332020-04-06 14:13:04 -050030#include <stdexcept>
Shawn McCarney2af52892020-04-14 11:54:45 -050031#include <string>
Shawn McCarneyc3991f12020-04-05 13:16:06 -050032#include <utility>
33#include <vector>
34
Bob King8e2294d2020-07-14 17:41:31 +080035#include <gmock/gmock.h>
Shawn McCarneyc3991f12020-04-05 13:16:06 -050036#include <gtest/gtest.h>
37
38using namespace phosphor::power::regulators;
Shawn McCarneydb0b8332020-04-06 14:13:04 -050039using namespace phosphor::power::regulators::test_utils;
Shawn McCarneyc3991f12020-04-05 13:16:06 -050040
Bob King8e2294d2020-07-14 17:41:31 +080041using ::testing::A;
42using ::testing::Return;
43using ::testing::TypedEq;
44
Shawn McCarneyc3991f12020-04-05 13:16:06 -050045TEST(SystemTests, Constructor)
46{
47 // Create Rules
48 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050049 rules.emplace_back(createRule("set_voltage_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050050
51 // Create Chassis
52 std::vector<std::unique_ptr<Chassis>> chassis{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050053 std::vector<std::unique_ptr<Device>> devices{};
54 devices.emplace_back(createDevice("reg1", {"rail1"}));
55 chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
Shawn McCarneyc3991f12020-04-05 13:16:06 -050056
57 // Create System
58 System system{std::move(rules), std::move(chassis)};
59 EXPECT_EQ(system.getChassis().size(), 1);
60 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
Shawn McCarneydb0b8332020-04-06 14:13:04 -050061 EXPECT_NO_THROW(system.getIDMap().getRule("set_voltage_rule"));
62 EXPECT_NO_THROW(system.getIDMap().getDevice("reg1"));
63 EXPECT_NO_THROW(system.getIDMap().getRail("rail1"));
64 EXPECT_THROW(system.getIDMap().getRail("rail2"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -050065 EXPECT_EQ(system.getRules().size(), 1);
66 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
67}
68
Shawn McCarney5b19ea52020-06-02 18:52:56 -050069TEST(SystemTests, CloseDevices)
70{
71 // Specify an empty rules vector
72 std::vector<std::unique_ptr<Rule>> rules{};
73
74 // Create Chassis
75 std::vector<std::unique_ptr<Chassis>> chassis{};
76 chassis.emplace_back(std::make_unique<Chassis>(1));
77 chassis.emplace_back(std::make_unique<Chassis>(3));
78
79 // Create System
80 System system{std::move(rules), std::move(chassis)};
81
82 // Call closeDevices()
83 journal::clear();
84 system.closeDevices();
85 EXPECT_EQ(journal::getErrMessages().size(), 0);
86 EXPECT_EQ(journal::getInfoMessages().size(), 0);
87 std::vector<std::string> expectedDebugMessages{
88 "Closing devices in chassis 1", "Closing devices in chassis 3"};
89 EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages);
90}
91
Shawn McCarney2af52892020-04-14 11:54:45 -050092TEST(SystemTests, Configure)
93{
Bob King5cfe5102020-07-30 16:26:18 +080094 // Create mock services. Expect logInfo() to be called.
Bob King23243f82020-07-29 10:38:57 +080095 MockServices services{};
Bob King5cfe5102020-07-30 16:26:18 +080096 MockJournal& journal = services.getMockJournal();
97 EXPECT_CALL(journal, logInfo("Configuring chassis 1")).Times(1);
98 EXPECT_CALL(journal, logInfo("Configuring chassis 3")).Times(1);
99 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
100 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
Bob King23243f82020-07-29 10:38:57 +0800101
Shawn McCarney2af52892020-04-14 11:54:45 -0500102 // Specify an empty rules vector
103 std::vector<std::unique_ptr<Rule>> rules{};
104
105 // Create Chassis
106 std::vector<std::unique_ptr<Chassis>> chassis{};
107 chassis.emplace_back(std::make_unique<Chassis>(1));
108 chassis.emplace_back(std::make_unique<Chassis>(3));
109
110 // Create System
111 System system{std::move(rules), std::move(chassis)};
112
113 // Call configure()
Bob King23243f82020-07-29 10:38:57 +0800114 system.configure(services);
Shawn McCarney2af52892020-04-14 11:54:45 -0500115}
116
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500117TEST(SystemTests, GetChassis)
118{
119 // Specify an empty rules vector
120 std::vector<std::unique_ptr<Rule>> rules{};
121
122 // Create Chassis
123 std::vector<std::unique_ptr<Chassis>> chassis{};
124 chassis.emplace_back(std::make_unique<Chassis>(1));
125 chassis.emplace_back(std::make_unique<Chassis>(3));
126
127 // Create System
128 System system{std::move(rules), std::move(chassis)};
129 EXPECT_EQ(system.getChassis().size(), 2);
130 EXPECT_EQ(system.getChassis()[0]->getNumber(), 1);
131 EXPECT_EQ(system.getChassis()[1]->getNumber(), 3);
132}
133
134TEST(SystemTests, GetIDMap)
135{
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500136 // Create Rules
137 std::vector<std::unique_ptr<Rule>> rules{};
138 rules.emplace_back(createRule("set_voltage_rule"));
139 rules.emplace_back(createRule("read_sensors_rule"));
140
141 // Create Chassis
142 std::vector<std::unique_ptr<Chassis>> chassis{};
143 {
144 // Chassis 1
145 std::vector<std::unique_ptr<Device>> devices{};
146 devices.emplace_back(createDevice("reg1", {"rail1"}));
147 devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"}));
148 chassis.emplace_back(std::make_unique<Chassis>(1, std::move(devices)));
149 }
150 {
151 // Chassis 2
152 std::vector<std::unique_ptr<Device>> devices{};
153 devices.emplace_back(createDevice("reg3", {"rail3a", "rail3b"}));
154 devices.emplace_back(createDevice("reg4"));
155 chassis.emplace_back(std::make_unique<Chassis>(2, std::move(devices)));
156 }
157
158 // Create System
159 System system{std::move(rules), std::move(chassis)};
160 const IDMap& idMap = system.getIDMap();
161
162 // Verify all Rules are in the IDMap
163 EXPECT_NO_THROW(idMap.getRule("set_voltage_rule"));
164 EXPECT_NO_THROW(idMap.getRule("read_sensors_rule"));
165 EXPECT_THROW(idMap.getRule("set_voltage_rule2"), std::invalid_argument);
166
167 // Verify all Devices are in the IDMap
168 EXPECT_NO_THROW(idMap.getDevice("reg1"));
169 EXPECT_NO_THROW(idMap.getDevice("reg2"));
170 EXPECT_NO_THROW(idMap.getDevice("reg3"));
171 EXPECT_NO_THROW(idMap.getDevice("reg4"));
172 EXPECT_THROW(idMap.getDevice("reg5"), std::invalid_argument);
173
174 // Verify all Rails are in the IDMap
175 EXPECT_NO_THROW(idMap.getRail("rail1"));
176 EXPECT_NO_THROW(idMap.getRail("rail2a"));
177 EXPECT_NO_THROW(idMap.getRail("rail2b"));
178 EXPECT_NO_THROW(idMap.getRail("rail3a"));
179 EXPECT_NO_THROW(idMap.getRail("rail3b"));
180 EXPECT_THROW(idMap.getRail("rail4"), std::invalid_argument);
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500181}
182
183TEST(SystemTests, GetRules)
184{
185 // Create Rules
186 std::vector<std::unique_ptr<Rule>> rules{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500187 rules.emplace_back(createRule("set_voltage_rule"));
188 rules.emplace_back(createRule("read_sensors_rule"));
Shawn McCarneyc3991f12020-04-05 13:16:06 -0500189
190 // Create Chassis
191 std::vector<std::unique_ptr<Chassis>> chassis{};
192 chassis.emplace_back(std::make_unique<Chassis>(1));
193
194 // Create System
195 System system{std::move(rules), std::move(chassis)};
196 EXPECT_EQ(system.getRules().size(), 2);
197 EXPECT_EQ(system.getRules()[0]->getID(), "set_voltage_rule");
198 EXPECT_EQ(system.getRules()[1]->getID(), "read_sensors_rule");
199}
Bob King8e2294d2020-07-14 17:41:31 +0800200
201TEST(SystemTests, MonitorSensors)
202{
Bob King8a552922020-08-05 17:02:31 +0800203 // Create mock services. No logging should occur.
204 MockServices services{};
205 MockJournal& journal = services.getMockJournal();
206 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
207 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
208
Bob King8e2294d2020-07-14 17:41:31 +0800209 // Create PMBusReadSensorAction
210 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
211 uint8_t command = 0x8C;
212 pmbus_utils::SensorDataFormat format{
213 pmbus_utils::SensorDataFormat::linear_11};
214 std::optional<int8_t> exponent{};
215 std::unique_ptr<PMBusReadSensorAction> action =
216 std::make_unique<PMBusReadSensorAction>(type, command, format,
217 exponent);
218
219 // Create mock I2CInterface. A two-byte read should occur.
220 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
221 std::make_unique<i2c::MockedI2CInterface>();
222 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
223 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
224 .Times(1);
225
226 // Create SensorMonitoring
227 std::vector<std::unique_ptr<Action>> actions{};
228 actions.emplace_back(std::move(action));
229 std::unique_ptr<SensorMonitoring> sensorMonitoring =
230 std::make_unique<SensorMonitoring>(std::move(actions));
231
232 // Create Rail
233 std::vector<std::unique_ptr<Rail>> rails{};
234 std::unique_ptr<Configuration> configuration{};
235 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
236 "vdd0", std::move(configuration), std::move(sensorMonitoring));
237 rails.emplace_back(std::move(rail));
238
239 // Create Device
240 std::unique_ptr<PresenceDetection> presenceDetection{};
241 std::unique_ptr<Configuration> deviceConfiguration{};
242 std::unique_ptr<Device> device = std::make_unique<Device>(
243 "reg1", true, "/system/chassis/motherboard/reg1",
244 std::move(i2cInterface), std::move(presenceDetection),
245 std::move(deviceConfiguration), std::move(rails));
246
247 // Create Chassis
248 std::vector<std::unique_ptr<Device>> devices{};
249 devices.emplace_back(std::move(device));
250 std::unique_ptr<Chassis> chassis =
251 std::make_unique<Chassis>(1, std::move(devices));
252
253 // Create System that contains Chassis
254 std::vector<std::unique_ptr<Rule>> rules{};
255 std::vector<std::unique_ptr<Chassis>> chassisVec{};
256 chassisVec.emplace_back(std::move(chassis));
257 System system{std::move(rules), std::move(chassisVec)};
258
259 // Call monitorSensors()
Bob King8a552922020-08-05 17:02:31 +0800260 system.monitorSensors(services);
Bob King8e2294d2020-07-14 17:41:31 +0800261}