blob: d8524dc5b5c419a15ed5e62b044c2443287432d9 [file] [log] [blame]
Shawn McCarneya2461b32019-10-24 18:53:01 -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 McCarney4bf310e2020-03-10 17:48:11 -050016#include "action.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050017#include "chassis.hpp"
Shawn McCarney4bf310e2020-03-10 17:48:11 -050018#include "configuration.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050019#include "device.hpp"
20#include "i2c_interface.hpp"
21#include "journal.hpp"
Shawn McCarney4bf310e2020-03-10 17:48:11 -050022#include "mock_action.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050023#include "mock_journal.hpp"
Bob King23243f82020-07-29 10:38:57 +080024#include "mock_services.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050025#include "mocked_i2c_interface.hpp"
Bob King7b743432020-06-22 17:35:04 +080026#include "pmbus_read_sensor_action.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050027#include "presence_detection.hpp"
Shawn McCarneya2461b32019-10-24 18:53:01 -050028#include "rail.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050029#include "rule.hpp"
Shawn McCarney4bf310e2020-03-10 17:48:11 -050030#include "sensor_monitoring.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050031#include "system.hpp"
Shawn McCarney4bf310e2020-03-10 17:48:11 -050032
33#include <memory>
34#include <optional>
35#include <utility>
36#include <vector>
Shawn McCarneya2461b32019-10-24 18:53:01 -050037
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050038#include <gmock/gmock.h>
Shawn McCarneya2461b32019-10-24 18:53:01 -050039#include <gtest/gtest.h>
40
41using namespace phosphor::power::regulators;
42
Bob King7b743432020-06-22 17:35:04 +080043using ::testing::A;
Shawn McCarney779b9562020-04-13 17:05:45 -050044using ::testing::Return;
Bob King7b743432020-06-22 17:35:04 +080045using ::testing::TypedEq;
Shawn McCarney779b9562020-04-13 17:05:45 -050046
Shawn McCarneya2461b32019-10-24 18:53:01 -050047TEST(RailTests, Constructor)
48{
Shawn McCarney4bf310e2020-03-10 17:48:11 -050049 // Test where only required parameters are specified
50 {
51 Rail rail{"vdd0"};
52 EXPECT_EQ(rail.getID(), "vdd0");
53 EXPECT_EQ(rail.getConfiguration(), nullptr);
54 EXPECT_EQ(rail.getSensorMonitoring(), nullptr);
55 }
56
57 // Test where all parameters are specified
58 {
59 // Create Configuration
60 std::optional<double> volts{1.3};
61 std::vector<std::unique_ptr<Action>> actions{};
62 actions.push_back(std::make_unique<MockAction>());
63 actions.push_back(std::make_unique<MockAction>());
64 std::unique_ptr<Configuration> configuration =
65 std::make_unique<Configuration>(volts, std::move(actions));
66
67 // Create SensorMonitoring
68 actions.clear();
69 actions.push_back(std::make_unique<MockAction>());
70 std::unique_ptr<SensorMonitoring> sensorMonitoring =
71 std::make_unique<SensorMonitoring>(std::move(actions));
72
73 // Create Rail
74 Rail rail{"vddr1", std::move(configuration),
75 std::move(sensorMonitoring)};
76 EXPECT_EQ(rail.getID(), "vddr1");
77 EXPECT_NE(rail.getConfiguration(), nullptr);
78 EXPECT_EQ(rail.getConfiguration()->getVolts().has_value(), true);
79 EXPECT_EQ(rail.getConfiguration()->getVolts().value(), 1.3);
80 EXPECT_EQ(rail.getConfiguration()->getActions().size(), 2);
81 EXPECT_NE(rail.getSensorMonitoring(), nullptr);
82 EXPECT_EQ(rail.getSensorMonitoring()->getActions().size(), 1);
83 }
84}
85
Shawn McCarney779b9562020-04-13 17:05:45 -050086TEST(RailTests, Configure)
87{
88 // Test where Configuration was not specified in constructor
89 {
Bob King23243f82020-07-29 10:38:57 +080090 // Create mock services.
91 MockServices services{};
92
Shawn McCarney779b9562020-04-13 17:05:45 -050093 // Create Rail
94 std::unique_ptr<Rail> rail = std::make_unique<Rail>("vdd0");
95 Rail* railPtr = rail.get();
96
97 // Create Device that contains Rail
98 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
99 std::make_unique<i2c::MockedI2CInterface>();
100 std::unique_ptr<PresenceDetection> presenceDetection{};
101 std::unique_ptr<Configuration> deviceConfiguration{};
102 std::vector<std::unique_ptr<Rail>> rails{};
103 rails.emplace_back(std::move(rail));
104 std::unique_ptr<Device> device = std::make_unique<Device>(
105 "reg1", true, "/system/chassis/motherboard/reg1",
106 std::move(i2cInterface), std::move(presenceDetection),
107 std::move(deviceConfiguration), std::move(rails));
108 Device* devicePtr = device.get();
109
110 // Create Chassis that contains Device
111 std::vector<std::unique_ptr<Device>> devices{};
112 devices.emplace_back(std::move(device));
113 std::unique_ptr<Chassis> chassis =
114 std::make_unique<Chassis>(1, std::move(devices));
115 Chassis* chassisPtr = chassis.get();
116
117 // Create System that contains Chassis
118 std::vector<std::unique_ptr<Rule>> rules{};
119 std::vector<std::unique_ptr<Chassis>> chassisVec{};
120 chassisVec.emplace_back(std::move(chassis));
121 System system{std::move(rules), std::move(chassisVec)};
122
123 // Call configure(). Should do nothing.
124 journal::clear();
Bob King23243f82020-07-29 10:38:57 +0800125 railPtr->configure(services, system, *chassisPtr, *devicePtr);
Shawn McCarney779b9562020-04-13 17:05:45 -0500126 EXPECT_EQ(journal::getDebugMessages().size(), 0);
127 EXPECT_EQ(journal::getErrMessages().size(), 0);
128 }
129
130 // Test where Configuration was specified in constructor
131 {
Bob King23243f82020-07-29 10:38:57 +0800132 // Create mock services.
133 MockServices services{};
134
Shawn McCarney779b9562020-04-13 17:05:45 -0500135 // Create Configuration
136 std::optional<double> volts{1.3};
137 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
138 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
139 std::vector<std::unique_ptr<Action>> actions{};
140 actions.emplace_back(std::move(action));
141 std::unique_ptr<Configuration> configuration =
142 std::make_unique<Configuration>(volts, std::move(actions));
143
144 // Create Rail
145 std::unique_ptr<Rail> rail =
146 std::make_unique<Rail>("vddr1", std::move(configuration));
147 Rail* railPtr = rail.get();
148
149 // Create Device that contains Rail
150 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
151 std::make_unique<i2c::MockedI2CInterface>();
152 std::unique_ptr<PresenceDetection> presenceDetection{};
153 std::unique_ptr<Configuration> deviceConfiguration{};
154 std::vector<std::unique_ptr<Rail>> rails{};
155 rails.emplace_back(std::move(rail));
156 std::unique_ptr<Device> device = std::make_unique<Device>(
157 "reg1", true, "/system/chassis/motherboard/reg1",
158 std::move(i2cInterface), std::move(presenceDetection),
159 std::move(deviceConfiguration), std::move(rails));
160 Device* devicePtr = device.get();
161
162 // Create Chassis that contains Device
163 std::vector<std::unique_ptr<Device>> devices{};
164 devices.emplace_back(std::move(device));
165 std::unique_ptr<Chassis> chassis =
166 std::make_unique<Chassis>(1, std::move(devices));
167 Chassis* chassisPtr = chassis.get();
168
169 // Create System that contains Chassis
170 std::vector<std::unique_ptr<Rule>> rules{};
171 std::vector<std::unique_ptr<Chassis>> chassisVec{};
172 chassisVec.emplace_back(std::move(chassis));
173 System system{std::move(rules), std::move(chassisVec)};
174
175 // Call configure(). Should execute Configuration and log debug message
176 // to journal.
177 journal::clear();
Bob King23243f82020-07-29 10:38:57 +0800178 railPtr->configure(services, system, *chassisPtr, *devicePtr);
Shawn McCarney779b9562020-04-13 17:05:45 -0500179 std::vector<std::string> expectedDebugMessages{
180 "Configuring vddr1: volts=1.300000"};
181 EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages);
182 EXPECT_EQ(journal::getErrMessages().size(), 0);
183 }
184}
185
Shawn McCarney4bf310e2020-03-10 17:48:11 -0500186TEST(RailTests, GetConfiguration)
187{
188 // Test where Configuration was not specified in constructor
189 {
190 Rail rail{"vdd0"};
191 EXPECT_EQ(rail.getConfiguration(), nullptr);
192 }
193
194 // Test where Configuration was specified in constructor
195 {
196 // Create Configuration
197 std::optional<double> volts{3.2};
198 std::vector<std::unique_ptr<Action>> actions{};
199 actions.push_back(std::make_unique<MockAction>());
200 std::unique_ptr<Configuration> configuration =
201 std::make_unique<Configuration>(volts, std::move(actions));
202
203 // Create Rail
204 Rail rail{"vddr1", std::move(configuration)};
205 EXPECT_NE(rail.getConfiguration(), nullptr);
206 EXPECT_EQ(rail.getConfiguration()->getVolts().has_value(), true);
207 EXPECT_EQ(rail.getConfiguration()->getVolts().value(), 3.2);
208 EXPECT_EQ(rail.getConfiguration()->getActions().size(), 1);
209 }
Shawn McCarneya2461b32019-10-24 18:53:01 -0500210}
211
Shawn McCarney4afb2852019-10-27 18:28:53 -0500212TEST(RailTests, GetID)
Shawn McCarneya2461b32019-10-24 18:53:01 -0500213{
Shawn McCarney4bf310e2020-03-10 17:48:11 -0500214 Rail rail{"vio2"};
Shawn McCarney4afb2852019-10-27 18:28:53 -0500215 EXPECT_EQ(rail.getID(), "vio2");
Shawn McCarneya2461b32019-10-24 18:53:01 -0500216}
Shawn McCarney4bf310e2020-03-10 17:48:11 -0500217
Bob King7b743432020-06-22 17:35:04 +0800218TEST(RailTests, MonitorSensors)
219{
220 // Test where SensorMonitoring was not specified in constructor
221 {
222 // Create mock I2CInterface. A two-byte read should NOT occur.
223 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
224 std::make_unique<i2c::MockedI2CInterface>();
225 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint16_t&>())).Times(0);
226
227 // Create Rail
228 std::unique_ptr<Rail> rail = std::make_unique<Rail>("vdd0");
229 Rail* railPtr = rail.get();
230
231 // Create Device that contains Rail
232 std::unique_ptr<PresenceDetection> presenceDetection{};
233 std::unique_ptr<Configuration> deviceConfiguration{};
234 std::vector<std::unique_ptr<Rail>> rails{};
235 rails.emplace_back(std::move(rail));
236 std::unique_ptr<Device> device = std::make_unique<Device>(
237 "reg1", true, "/system/chassis/motherboard/reg1",
238 std::move(i2cInterface), std::move(presenceDetection),
239 std::move(deviceConfiguration), std::move(rails));
240 Device* devicePtr = device.get();
241
242 // Create Chassis that contains Device
243 std::vector<std::unique_ptr<Device>> devices{};
244 devices.emplace_back(std::move(device));
245 std::unique_ptr<Chassis> chassis =
246 std::make_unique<Chassis>(1, std::move(devices));
247 Chassis* chassisPtr = chassis.get();
248
249 // Create System that contains Chassis
250 std::vector<std::unique_ptr<Rule>> rules{};
251 std::vector<std::unique_ptr<Chassis>> chassisVec{};
252 chassisVec.emplace_back(std::move(chassis));
253 System system{std::move(rules), std::move(chassisVec)};
254
255 // Call monitorSensors(). Should do nothing.
256 journal::clear();
257 railPtr->monitorSensors(system, *chassisPtr, *devicePtr);
258 EXPECT_EQ(journal::getDebugMessages().size(), 0);
259 EXPECT_EQ(journal::getErrMessages().size(), 0);
260 }
261
262 // Test where SensorMonitoring was specified in constructor
263 {
264 // Create PMBusReadSensorAction
265 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
266 uint8_t command = 0x8C;
267 pmbus_utils::SensorDataFormat format{
268 pmbus_utils::SensorDataFormat::linear_11};
269 std::optional<int8_t> exponent{};
270 std::unique_ptr<PMBusReadSensorAction> action =
271 std::make_unique<PMBusReadSensorAction>(type, command, format,
272 exponent);
273
274 // Create mock I2CInterface. A two-byte read should occur.
275 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
276 std::make_unique<i2c::MockedI2CInterface>();
277 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
278 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
279 .Times(1);
280
281 // Create SensorMonitoring
282 std::vector<std::unique_ptr<Action>> actions{};
283 actions.emplace_back(std::move(action));
284 std::unique_ptr<SensorMonitoring> sensorMonitoring =
285 std::make_unique<SensorMonitoring>(std::move(actions));
286
287 // Create Rail
288 std::unique_ptr<Configuration> configuration{};
289 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
290 "vddr1", std::move(configuration), std::move(sensorMonitoring));
291 Rail* railPtr = rail.get();
292
293 // Create Device that contains Rail
294 std::unique_ptr<PresenceDetection> presenceDetection{};
295 std::unique_ptr<Configuration> deviceConfiguration{};
296 std::vector<std::unique_ptr<Rail>> rails{};
297 rails.emplace_back(std::move(rail));
298 std::unique_ptr<Device> device = std::make_unique<Device>(
299 "reg1", true, "/system/chassis/motherboard/reg1",
300 std::move(i2cInterface), std::move(presenceDetection),
301 std::move(deviceConfiguration), std::move(rails));
302 Device* devicePtr = device.get();
303
304 // Create Chassis that contains Device
305 std::vector<std::unique_ptr<Device>> devices{};
306 devices.emplace_back(std::move(device));
307 std::unique_ptr<Chassis> chassis =
308 std::make_unique<Chassis>(1, std::move(devices));
309 Chassis* chassisPtr = chassis.get();
310
311 // Create System that contains Chassis
312 std::vector<std::unique_ptr<Rule>> rules{};
313 std::vector<std::unique_ptr<Chassis>> chassisVec{};
314 chassisVec.emplace_back(std::move(chassis));
315 System system{std::move(rules), std::move(chassisVec)};
316
317 // Call monitorSensors().
318 journal::clear();
319 railPtr->monitorSensors(system, *chassisPtr, *devicePtr);
320 EXPECT_EQ(journal::getDebugMessages().size(), 0);
321 EXPECT_EQ(journal::getErrMessages().size(), 0);
322 }
323}
324
Shawn McCarney4bf310e2020-03-10 17:48:11 -0500325TEST(RailTests, GetSensorMonitoring)
326{
327 // Test where SensorMonitoring was not specified in constructor
328 {
329 Rail rail{"vdd0", nullptr, nullptr};
330 EXPECT_EQ(rail.getSensorMonitoring(), nullptr);
331 }
332
333 // Test where SensorMonitoring was specified in constructor
334 {
335 std::unique_ptr<Configuration> configuration{};
336
337 // Create SensorMonitoring
338 std::vector<std::unique_ptr<Action>> actions{};
339 actions.push_back(std::make_unique<MockAction>());
340 actions.push_back(std::make_unique<MockAction>());
341 std::unique_ptr<SensorMonitoring> sensorMonitoring =
342 std::make_unique<SensorMonitoring>(std::move(actions));
343
344 // Create Rail
345 Rail rail{"vddr1", std::move(configuration),
346 std::move(sensorMonitoring)};
347 EXPECT_NE(rail.getSensorMonitoring(), nullptr);
348 EXPECT_EQ(rail.getSensorMonitoring()->getActions().size(), 2);
349 }
350}