blob: 49a8a8d1166bde92977cec5cd94aaf66366a8335 [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 McCarney0b1a0e72020-03-11 18:01:44 -050016#include "action.hpp"
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050017#include "chassis.hpp"
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050018#include "configuration.hpp"
Shawn McCarneya2461b32019-10-24 18:53:01 -050019#include "device.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060020#include "i2c_interface.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050021#include "id_map.hpp"
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050022#include "mock_action.hpp"
Shawn McCarney81a2f902021-03-23 21:41:34 -050023#include "mock_error_logging.hpp"
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050024#include "mock_journal.hpp"
Shawn McCarney17bac892021-05-08 07:55:52 -050025#include "mock_sensors.hpp"
Bob King23243f82020-07-29 10:38:57 +080026#include "mock_services.hpp"
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050027#include "mocked_i2c_interface.hpp"
Shawn McCarney32252592021-09-08 15:29:36 -050028#include "phase_fault_detection.hpp"
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050029#include "presence_detection.hpp"
30#include "rail.hpp"
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050031#include "rule.hpp"
Shawn McCarney17bac892021-05-08 07:55:52 -050032#include "sensor_monitoring.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050033#include "sensors.hpp"
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050034#include "system.hpp"
Shawn McCarney371e2442021-05-14 14:18:07 -050035#include "test_sdbus_error.hpp"
Shawn McCarney8a3afd72020-03-12 14:28:44 -050036#include "test_utils.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060037
38#include <memory>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050039#include <optional>
Shawn McCarney525e20c2020-04-14 11:05:39 -050040#include <string>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060041#include <utility>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050042#include <vector>
Shawn McCarneya2461b32019-10-24 18:53:01 -050043
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050044#include <gmock/gmock.h>
Shawn McCarneya2461b32019-10-24 18:53:01 -050045#include <gtest/gtest.h>
46
47using namespace phosphor::power::regulators;
Shawn McCarney8a3afd72020-03-12 14:28:44 -050048using namespace phosphor::power::regulators::test_utils;
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060049
Bob King8e1cd0b2020-07-08 13:30:27 +080050using ::testing::A;
Shawn McCarney81a2f902021-03-23 21:41:34 -050051using ::testing::Ref;
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050052using ::testing::Return;
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050053using ::testing::Throw;
Bob King8e1cd0b2020-07-08 13:30:27 +080054using ::testing::TypedEq;
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050055
Shawn McCarneycb3f6a62021-04-30 10:54:30 -050056static const std::string chassisInvPath{
57 "/xyz/openbmc_project/inventory/system/chassis"};
58
Shawn McCarneya2461b32019-10-24 18:53:01 -050059TEST(DeviceTests, Constructor)
60{
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050061 // Test where only required parameters are specified
62 {
63 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
64 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
Bob Kinga76898f2020-10-13 15:08:33 +080065 Device device{
66 "vdd_reg", true,
67 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
68 std::move(i2cInterface)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050069 EXPECT_EQ(device.getID(), "vdd_reg");
70 EXPECT_EQ(device.isRegulator(), true);
Bob Kinga76898f2020-10-13 15:08:33 +080071 EXPECT_EQ(
72 device.getFRU(),
73 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050074 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
75 EXPECT_EQ(device.getPresenceDetection(), nullptr);
76 EXPECT_EQ(device.getConfiguration(), nullptr);
Shawn McCarney32252592021-09-08 15:29:36 -050077 EXPECT_EQ(device.getPhaseFaultDetection(), nullptr);
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050078 EXPECT_EQ(device.getRails().size(), 0);
79 }
80
81 // Test where all parameters are specified
82 {
83 // Create I2CInterface
84 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
85 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
86
87 // Create PresenceDetection
88 std::vector<std::unique_ptr<Action>> actions{};
89 actions.push_back(std::make_unique<MockAction>());
90 std::unique_ptr<PresenceDetection> presenceDetection =
91 std::make_unique<PresenceDetection>(std::move(actions));
92
93 // Create Configuration
94 std::optional<double> volts{};
95 actions.clear();
96 actions.push_back(std::make_unique<MockAction>());
97 actions.push_back(std::make_unique<MockAction>());
98 std::unique_ptr<Configuration> configuration =
99 std::make_unique<Configuration>(volts, std::move(actions));
100
Shawn McCarney32252592021-09-08 15:29:36 -0500101 // Create PhaseFaultDetection
102 actions.clear();
103 actions.push_back(std::make_unique<MockAction>());
104 actions.push_back(std::make_unique<MockAction>());
105 actions.push_back(std::make_unique<MockAction>());
106 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection =
107 std::make_unique<PhaseFaultDetection>(std::move(actions));
108
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500109 // Create vector of Rail objects
110 std::vector<std::unique_ptr<Rail>> rails{};
111 rails.push_back(std::make_unique<Rail>("vdd0"));
112 rails.push_back(std::make_unique<Rail>("vdd1"));
113
114 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800115 Device device{
116 "vdd_reg",
117 false,
118 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
119 std::move(i2cInterface),
120 std::move(presenceDetection),
121 std::move(configuration),
Shawn McCarney32252592021-09-08 15:29:36 -0500122 std::move(phaseFaultDetection),
Bob Kinga76898f2020-10-13 15:08:33 +0800123 std::move(rails)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500124 EXPECT_EQ(device.getID(), "vdd_reg");
125 EXPECT_EQ(device.isRegulator(), false);
Bob Kinga76898f2020-10-13 15:08:33 +0800126 EXPECT_EQ(
127 device.getFRU(),
128 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1");
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500129 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
130 EXPECT_NE(device.getPresenceDetection(), nullptr);
131 EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1);
132 EXPECT_NE(device.getConfiguration(), nullptr);
133 EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), false);
134 EXPECT_EQ(device.getConfiguration()->getActions().size(), 2);
Shawn McCarney32252592021-09-08 15:29:36 -0500135 EXPECT_NE(device.getPhaseFaultDetection(), nullptr);
136 EXPECT_EQ(device.getPhaseFaultDetection()->getActions().size(), 3);
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500137 EXPECT_EQ(device.getRails().size(), 2);
138 }
Shawn McCarneya2461b32019-10-24 18:53:01 -0500139}
140
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500141TEST(DeviceTests, AddToIDMap)
142{
143 std::unique_ptr<PresenceDetection> presenceDetection{};
144 std::unique_ptr<Configuration> configuration{};
Shawn McCarney32252592021-09-08 15:29:36 -0500145 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500146
147 // Create vector of Rail objects
148 std::vector<std::unique_ptr<Rail>> rails{};
149 rails.push_back(std::make_unique<Rail>("vdd0"));
150 rails.push_back(std::make_unique<Rail>("vdd1"));
151
152 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800153 Device device{
154 "vdd_reg",
155 false,
156 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
157 std::move(createI2CInterface()),
158 std::move(presenceDetection),
159 std::move(configuration),
Shawn McCarney32252592021-09-08 15:29:36 -0500160 std::move(phaseFaultDetection),
Bob Kinga76898f2020-10-13 15:08:33 +0800161 std::move(rails)};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500162
163 // Add Device and Rail objects to an IDMap
164 IDMap idMap{};
165 device.addToIDMap(idMap);
166
167 // Verify Device is in the IDMap
168 EXPECT_NO_THROW(idMap.getDevice("vdd_reg"));
169 EXPECT_THROW(idMap.getDevice("vio_reg"), std::invalid_argument);
170
171 // Verify all Rails are in the IDMap
172 EXPECT_NO_THROW(idMap.getRail("vdd0"));
173 EXPECT_NO_THROW(idMap.getRail("vdd1"));
174 EXPECT_THROW(idMap.getRail("vdd2"), std::invalid_argument);
175}
176
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600177TEST(DeviceTests, ClearCache)
178{
179 // Test where Device does not contain a PresenceDetection object
180 try
181 {
182 Device device{
183 "vdd_reg", false,
184 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
185 std::move(createI2CInterface())};
186 device.clearCache();
187 }
188 catch (...)
189 {
190 ADD_FAILURE() << "Should not have caught exception.";
191 }
192
193 // Test where Device contains a PresenceDetection object
194 {
195 // Create PresenceDetection
196 std::vector<std::unique_ptr<Action>> actions{};
197 std::unique_ptr<PresenceDetection> presenceDetection =
198 std::make_unique<PresenceDetection>(std::move(actions));
199 PresenceDetection* presenceDetectionPtr = presenceDetection.get();
200
201 // Create Device
202 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
203 std::unique_ptr<Device> device = std::make_unique<Device>(
204 "reg1", true,
205 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
206 std::move(i2cInterface), std::move(presenceDetection));
207 Device* devicePtr = device.get();
208
209 // Create Chassis that contains Device
210 std::vector<std::unique_ptr<Device>> devices{};
211 devices.emplace_back(std::move(device));
212 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500213 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600214 Chassis* chassisPtr = chassis.get();
215
216 // Create System that contains Chassis
217 std::vector<std::unique_ptr<Rule>> rules{};
218 std::vector<std::unique_ptr<Chassis>> chassisVec{};
219 chassisVec.emplace_back(std::move(chassis));
220 System system{std::move(rules), std::move(chassisVec)};
221
222 // Cache presence value in PresenceDetection
223 MockServices services{};
224 presenceDetectionPtr->execute(services, system, *chassisPtr,
225 *devicePtr);
226 EXPECT_TRUE(presenceDetectionPtr->getCachedPresence().has_value());
227
228 // Clear cached data in Device
229 devicePtr->clearCache();
230
231 // Verify presence value no longer cached in PresenceDetection
232 EXPECT_FALSE(presenceDetectionPtr->getCachedPresence().has_value());
233 }
234}
235
Shawn McCarney371e2442021-05-14 14:18:07 -0500236TEST(DeviceTests, ClearErrorHistory)
237{
238 // Create SensorMonitoring. Will fail with a DBus exception.
239 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
240 EXPECT_CALL(*action, execute)
241 .WillRepeatedly(Throw(TestSDBusError{"Unable to set sensor value"}));
242 std::vector<std::unique_ptr<Action>> actions{};
243 actions.emplace_back(std::move(action));
244 std::unique_ptr<SensorMonitoring> sensorMonitoring =
245 std::make_unique<SensorMonitoring>(std::move(actions));
246
247 // Create Rail
248 std::unique_ptr<Configuration> configuration{};
249 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
250 "vddr1", std::move(configuration), std::move(sensorMonitoring));
251
252 // Create Device that contains Rail
253 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
254 std::make_unique<i2c::MockedI2CInterface>();
255 std::unique_ptr<PresenceDetection> presenceDetection{};
256 std::unique_ptr<Configuration> deviceConfiguration{};
Shawn McCarney32252592021-09-08 15:29:36 -0500257 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
Shawn McCarney371e2442021-05-14 14:18:07 -0500258 std::vector<std::unique_ptr<Rail>> rails{};
259 rails.emplace_back(std::move(rail));
260 std::unique_ptr<Device> device = std::make_unique<Device>(
261 "reg1", true,
262 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
263 std::move(i2cInterface), std::move(presenceDetection),
Shawn McCarney32252592021-09-08 15:29:36 -0500264 std::move(deviceConfiguration), std::move(phaseFaultDetection),
265 std::move(rails));
Shawn McCarney371e2442021-05-14 14:18:07 -0500266 Device* devicePtr = device.get();
267
268 // Create Chassis that contains Device
269 std::vector<std::unique_ptr<Device>> devices{};
270 devices.emplace_back(std::move(device));
271 std::unique_ptr<Chassis> chassis =
272 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
273 Chassis* chassisPtr = chassis.get();
274
275 // Create System that contains Chassis
276 std::vector<std::unique_ptr<Rule>> rules{};
277 std::vector<std::unique_ptr<Chassis>> chassisVec{};
278 chassisVec.emplace_back(std::move(chassis));
279 System system{std::move(rules), std::move(chassisVec)};
280
281 // Create mock services
282 MockServices services{};
283
284 // Expect Sensors service to be called 5+5=10 times
285 MockSensors& sensors = services.getMockSensors();
286 EXPECT_CALL(sensors, startRail).Times(10);
287 EXPECT_CALL(sensors, setValue).Times(0);
288 EXPECT_CALL(sensors, endRail).Times(10);
289
290 // Expect Journal service to be called 3+3=6 times to log error messages
291 MockJournal& journal = services.getMockJournal();
292 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
293 .Times(6);
294 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(6);
295
296 // Expect ErrorLogging service to be called 1+1=2 times to log a DBus error
297 MockErrorLogging& errorLogging = services.getMockErrorLogging();
298 EXPECT_CALL(errorLogging, logDBusError).Times(2);
299
300 // Monitor sensors 5 times. Should fail every time, write to journal 3
301 // times, and log one error.
302 for (int i = 1; i <= 5; ++i)
303 {
304 devicePtr->monitorSensors(services, system, *chassisPtr);
305 }
306
307 // Clear error history
308 devicePtr->clearErrorHistory();
309
310 // Monitor sensors 5 times again. Should fail every time, write to journal
311 // 3 times, and log one error.
312 for (int i = 1; i <= 5; ++i)
313 {
314 devicePtr->monitorSensors(services, system, *chassisPtr);
315 }
316}
317
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500318TEST(DeviceTests, Close)
319{
320 // Test where works: I2C interface is not open
321 {
322 // Create mock I2CInterface
323 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
324 std::make_unique<i2c::MockedI2CInterface>();
325 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(false));
326 EXPECT_CALL(*i2cInterface, close).Times(0);
327
Bob Kingd692d6d2020-09-14 13:42:57 +0800328 // Create mock services. No logError should occur.
329 MockServices services{};
330 MockJournal& journal = services.getMockJournal();
331 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
332 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
333 .Times(0);
334
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500335 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800336 Device device{
337 "vdd_reg", true,
338 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
339 std::move(i2cInterface)};
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500340
341 // Close Device
Bob Kingd692d6d2020-09-14 13:42:57 +0800342 device.close(services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500343 }
344
345 // Test where works: I2C interface is open
346 {
347 // Create mock I2CInterface
348 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
349 std::make_unique<i2c::MockedI2CInterface>();
350 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
351 EXPECT_CALL(*i2cInterface, close).Times(1);
352
Bob Kingd692d6d2020-09-14 13:42:57 +0800353 // Create mock services. No logError should occur.
354 MockServices services{};
355 MockJournal& journal = services.getMockJournal();
356 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
357 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
358 .Times(0);
359
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500360 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800361 Device device{
362 "vdd_reg", true,
363 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
364 std::move(i2cInterface)};
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500365
366 // Close Device
Bob Kingd692d6d2020-09-14 13:42:57 +0800367 device.close(services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500368 }
369
370 // Test where fails: closing I2C interface fails
371 {
372 // Create mock I2CInterface
373 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
374 std::make_unique<i2c::MockedI2CInterface>();
375 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
376 EXPECT_CALL(*i2cInterface, close)
377 .Times(1)
378 .WillOnce(Throw(
379 i2c::I2CException{"Failed to close", "/dev/i2c-1", 0x70}));
380
Shawn McCarney81a2f902021-03-23 21:41:34 -0500381 // Create mock services. Expect logError() and logI2CError() to be
382 // called.
Bob Kingd692d6d2020-09-14 13:42:57 +0800383 MockServices services{};
Shawn McCarney81a2f902021-03-23 21:41:34 -0500384 MockErrorLogging& errorLogging = services.getMockErrorLogging();
Bob Kingd692d6d2020-09-14 13:42:57 +0800385 MockJournal& journal = services.getMockJournal();
386 std::vector<std::string> expectedErrMessagesException{
387 "I2CException: Failed to close: bus /dev/i2c-1, addr 0x70"};
388 EXPECT_CALL(journal, logError("Unable to close device vdd_reg"))
389 .Times(1);
390 EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1);
Shawn McCarney81a2f902021-03-23 21:41:34 -0500391 EXPECT_CALL(errorLogging,
392 logI2CError(Entry::Level::Notice, Ref(journal),
393 "/dev/i2c-1", 0x70, 0))
394 .Times(1);
Bob Kingd692d6d2020-09-14 13:42:57 +0800395
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500396 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800397 Device device{
398 "vdd_reg", true,
399 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
400 std::move(i2cInterface)};
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500401
402 // Close Device
Bob Kingd692d6d2020-09-14 13:42:57 +0800403 device.close(services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500404 }
405}
406
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500407TEST(DeviceTests, Configure)
408{
Shawn McCarney48033bf2021-01-27 17:56:49 -0600409 // Test where device is not present
410 {
411 // Create mock services. No logging should occur.
412 MockServices services{};
413 MockJournal& journal = services.getMockJournal();
414 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
415 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
416
417 // Create PresenceDetection. Indicates device is not present.
418 std::unique_ptr<MockAction> presAction = std::make_unique<MockAction>();
419 EXPECT_CALL(*presAction, execute).Times(1).WillOnce(Return(false));
420 std::vector<std::unique_ptr<Action>> presActions{};
421 presActions.emplace_back(std::move(presAction));
422 std::unique_ptr<PresenceDetection> presenceDetection =
423 std::make_unique<PresenceDetection>(std::move(presActions));
424
425 // Create Configuration. Action inside it should not be executed.
426 std::optional<double> volts{};
427 std::unique_ptr<MockAction> confAction = std::make_unique<MockAction>();
428 EXPECT_CALL(*confAction, execute).Times(0);
429 std::vector<std::unique_ptr<Action>> confActions{};
430 confActions.emplace_back(std::move(confAction));
431 std::unique_ptr<Configuration> configuration =
432 std::make_unique<Configuration>(volts, std::move(confActions));
433
434 // Create Device
435 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
436 std::unique_ptr<Device> device = std::make_unique<Device>(
437 "reg1", true,
438 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
439 std::move(i2cInterface), std::move(presenceDetection),
440 std::move(configuration));
441 Device* devicePtr = device.get();
442
443 // Create Chassis that contains Device
444 std::vector<std::unique_ptr<Device>> devices{};
445 devices.emplace_back(std::move(device));
446 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500447 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney48033bf2021-01-27 17:56:49 -0600448 Chassis* chassisPtr = chassis.get();
449
450 // Create System that contains Chassis
451 std::vector<std::unique_ptr<Rule>> rules{};
452 std::vector<std::unique_ptr<Chassis>> chassisVec{};
453 chassisVec.emplace_back(std::move(chassis));
454 System system{std::move(rules), std::move(chassisVec)};
455
456 // Call configure(). Should do nothing.
457 devicePtr->configure(services, system, *chassisPtr);
458 }
459
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500460 // Test where Configuration and Rails were not specified in constructor
461 {
Bob King5cfe5102020-07-30 16:26:18 +0800462 // Create mock services. No logging should occur.
Bob King23243f82020-07-29 10:38:57 +0800463 MockServices services{};
Bob King5cfe5102020-07-30 16:26:18 +0800464 MockJournal& journal = services.getMockJournal();
465 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
466 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
Bob King23243f82020-07-29 10:38:57 +0800467
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500468 // Create Device
469 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
470 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800471 "reg1", true,
472 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500473 std::move(i2cInterface));
474 Device* devicePtr = device.get();
475
476 // Create Chassis that contains Device
477 std::vector<std::unique_ptr<Device>> devices{};
478 devices.emplace_back(std::move(device));
479 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500480 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500481 Chassis* chassisPtr = chassis.get();
482
483 // Create System that contains Chassis
484 std::vector<std::unique_ptr<Rule>> rules{};
485 std::vector<std::unique_ptr<Chassis>> chassisVec{};
486 chassisVec.emplace_back(std::move(chassis));
487 System system{std::move(rules), std::move(chassisVec)};
488
Bob King5cfe5102020-07-30 16:26:18 +0800489 // Call configure().
Bob King23243f82020-07-29 10:38:57 +0800490 devicePtr->configure(services, system, *chassisPtr);
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500491 }
492
493 // Test where Configuration and Rails were specified in constructor
494 {
495 std::vector<std::unique_ptr<Rail>> rails{};
496
Bob King5cfe5102020-07-30 16:26:18 +0800497 // Create mock services. Expect logDebug() to be called.
498 // For the Device and both Rails, should execute the Configuration
499 // and log a debug message.
500 MockServices services{};
501 MockJournal& journal = services.getMockJournal();
502 EXPECT_CALL(journal, logDebug("Configuring reg1")).Times(1);
503 EXPECT_CALL(journal, logDebug("Configuring vdd0: volts=1.300000"))
504 .Times(1);
505 EXPECT_CALL(journal, logDebug("Configuring vio0: volts=3.200000"))
506 .Times(1);
507 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
508
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500509 // Create Rail vdd0
510 {
511 // Create Configuration for Rail
512 std::optional<double> volts{1.3};
513 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
514 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
515 std::vector<std::unique_ptr<Action>> actions{};
516 actions.emplace_back(std::move(action));
517 std::unique_ptr<Configuration> configuration =
518 std::make_unique<Configuration>(volts, std::move(actions));
519
520 // Create Rail
521 std::unique_ptr<Rail> rail =
522 std::make_unique<Rail>("vdd0", std::move(configuration));
523 rails.emplace_back(std::move(rail));
524 }
525
526 // Create Rail vio0
527 {
528 // Create Configuration for Rail
529 std::optional<double> volts{3.2};
530 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
531 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
532 std::vector<std::unique_ptr<Action>> actions{};
533 actions.emplace_back(std::move(action));
534 std::unique_ptr<Configuration> configuration =
535 std::make_unique<Configuration>(volts, std::move(actions));
536
537 // Create Rail
538 std::unique_ptr<Rail> rail =
539 std::make_unique<Rail>("vio0", std::move(configuration));
540 rails.emplace_back(std::move(rail));
541 }
542
543 // Create Configuration for Device
544 std::optional<double> volts{};
545 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
546 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
547 std::vector<std::unique_ptr<Action>> actions{};
548 actions.emplace_back(std::move(action));
549 std::unique_ptr<Configuration> configuration =
550 std::make_unique<Configuration>(volts, std::move(actions));
551
552 // Create Device
553 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
554 std::unique_ptr<PresenceDetection> presenceDetection{};
Shawn McCarney32252592021-09-08 15:29:36 -0500555 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500556 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800557 "reg1", true,
558 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500559 std::move(i2cInterface), std::move(presenceDetection),
Shawn McCarney32252592021-09-08 15:29:36 -0500560 std::move(configuration), std::move(phaseFaultDetection),
561 std::move(rails));
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500562 Device* devicePtr = device.get();
563
564 // Create Chassis that contains Device
565 std::vector<std::unique_ptr<Device>> devices{};
566 devices.emplace_back(std::move(device));
567 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500568 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500569 Chassis* chassisPtr = chassis.get();
570
571 // Create System that contains Chassis
572 std::vector<std::unique_ptr<Rule>> rules{};
573 std::vector<std::unique_ptr<Chassis>> chassisVec{};
574 chassisVec.emplace_back(std::move(chassis));
575 System system{std::move(rules), std::move(chassisVec)};
576
Bob King5cfe5102020-07-30 16:26:18 +0800577 // Call configure().
Bob King23243f82020-07-29 10:38:57 +0800578 devicePtr->configure(services, system, *chassisPtr);
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500579 }
580}
581
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500582TEST(DeviceTests, GetConfiguration)
Shawn McCarneya2461b32019-10-24 18:53:01 -0500583{
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500584 // Test where Configuration was not specified in constructor
585 {
Bob Kinga76898f2020-10-13 15:08:33 +0800586 Device device{
587 "vdd_reg", true,
588 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
589 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500590 EXPECT_EQ(device.getConfiguration(), nullptr);
591 }
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600592
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500593 // Test where Configuration was specified in constructor
594 {
595 std::unique_ptr<PresenceDetection> presenceDetection{};
596
597 // Create Configuration
598 std::optional<double> volts{3.2};
599 std::vector<std::unique_ptr<Action>> actions{};
600 actions.push_back(std::make_unique<MockAction>());
601 actions.push_back(std::make_unique<MockAction>());
602 std::unique_ptr<Configuration> configuration =
603 std::make_unique<Configuration>(volts, std::move(actions));
604
605 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800606 Device device{
607 "vdd_reg",
608 true,
609 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
610 std::move(createI2CInterface()),
611 std::move(presenceDetection),
612 std::move(configuration)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500613 EXPECT_NE(device.getConfiguration(), nullptr);
614 EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true);
615 EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2);
616 EXPECT_EQ(device.getConfiguration()->getActions().size(), 2);
617 }
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600618}
619
620TEST(DeviceTests, GetFRU)
621{
Bob Kinga76898f2020-10-13 15:08:33 +0800622 Device device{
623 "vdd_reg", true,
624 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
625 std::move(createI2CInterface())};
626 EXPECT_EQ(device.getFRU(),
627 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600628}
629
630TEST(DeviceTests, GetI2CInterface)
631{
632 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
633 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
Bob Kinga76898f2020-10-13 15:08:33 +0800634 Device device{
635 "vdd_reg", true,
636 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
637 std::move(i2cInterface)};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600638 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
Shawn McCarneya2461b32019-10-24 18:53:01 -0500639}
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500640
641TEST(DeviceTests, GetID)
642{
Bob Kinga76898f2020-10-13 15:08:33 +0800643 Device device{
644 "vdd_reg", false,
645 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
646 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500647 EXPECT_EQ(device.getID(), "vdd_reg");
648}
649
Shawn McCarney32252592021-09-08 15:29:36 -0500650TEST(DeviceTests, GetPhaseFaultDetection)
651{
652 // Test where PhaseFaultDetection was not specified in constructor
653 {
654 Device device{
655 "vdd_reg", true,
656 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
657 std::move(createI2CInterface())};
658 EXPECT_EQ(device.getPhaseFaultDetection(), nullptr);
659 }
660
661 // Test where PhaseFaultDetection was specified in constructor
662 {
663 // Create PhaseFaultDetection
664 std::vector<std::unique_ptr<Action>> actions{};
665 actions.push_back(std::make_unique<MockAction>());
666 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection =
667 std::make_unique<PhaseFaultDetection>(std::move(actions));
668
669 // Create Device
670 std::unique_ptr<PresenceDetection> presenceDetection{};
671 std::unique_ptr<Configuration> configuration{};
672 Device device{
673 "vdd_reg",
674 false,
675 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
676 std::move(createI2CInterface()),
677 std::move(presenceDetection),
678 std::move(configuration),
679 std::move(phaseFaultDetection)};
680 EXPECT_NE(device.getPhaseFaultDetection(), nullptr);
681 EXPECT_EQ(device.getPhaseFaultDetection()->getActions().size(), 1);
682 }
683}
684
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500685TEST(DeviceTests, GetPresenceDetection)
686{
687 // Test where PresenceDetection was not specified in constructor
688 {
Bob Kinga76898f2020-10-13 15:08:33 +0800689 Device device{
690 "vdd_reg", true,
691 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
692 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500693 EXPECT_EQ(device.getPresenceDetection(), nullptr);
694 }
695
696 // Test where PresenceDetection was specified in constructor
697 {
698 // Create PresenceDetection
699 std::vector<std::unique_ptr<Action>> actions{};
700 actions.push_back(std::make_unique<MockAction>());
701 std::unique_ptr<PresenceDetection> presenceDetection =
702 std::make_unique<PresenceDetection>(std::move(actions));
703
704 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800705 Device device{
706 "vdd_reg", false,
707 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
708 std::move(createI2CInterface()), std::move(presenceDetection)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500709 EXPECT_NE(device.getPresenceDetection(), nullptr);
710 EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1);
711 }
712}
713
714TEST(DeviceTests, GetRails)
715{
716 // Test where no rails were specified in constructor
717 {
Bob Kinga76898f2020-10-13 15:08:33 +0800718 Device device{
719 "vdd_reg", true,
720 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
721 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500722 EXPECT_EQ(device.getRails().size(), 0);
723 }
724
725 // Test where rails were specified in constructor
726 {
727 std::unique_ptr<PresenceDetection> presenceDetection{};
728 std::unique_ptr<Configuration> configuration{};
Shawn McCarney32252592021-09-08 15:29:36 -0500729 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500730
731 // Create vector of Rail objects
732 std::vector<std::unique_ptr<Rail>> rails{};
733 rails.push_back(std::make_unique<Rail>("vdd0"));
734 rails.push_back(std::make_unique<Rail>("vdd1"));
735
736 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800737 Device device{
738 "vdd_reg",
739 false,
740 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
741 std::move(createI2CInterface()),
742 std::move(presenceDetection),
743 std::move(configuration),
Shawn McCarney32252592021-09-08 15:29:36 -0500744 std::move(phaseFaultDetection),
Bob Kinga76898f2020-10-13 15:08:33 +0800745 std::move(rails)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500746 EXPECT_EQ(device.getRails().size(), 2);
747 EXPECT_EQ(device.getRails()[0]->getID(), "vdd0");
748 EXPECT_EQ(device.getRails()[1]->getID(), "vdd1");
749 }
750}
751
Shawn McCarney48033bf2021-01-27 17:56:49 -0600752TEST(DeviceTests, IsPresent)
753{
754 // Test where PresenceDetection not specified in constructor
755 {
756 // Create Device
757 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
758 std::unique_ptr<Device> device = std::make_unique<Device>(
759 "reg1", true,
760 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
761 std::move(i2cInterface));
762 Device* devicePtr = device.get();
763
764 // Create Chassis that contains Device
765 std::vector<std::unique_ptr<Device>> devices{};
766 devices.emplace_back(std::move(device));
767 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500768 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney48033bf2021-01-27 17:56:49 -0600769 Chassis* chassisPtr = chassis.get();
770
771 // Create System that contains Chassis
772 std::vector<std::unique_ptr<Rule>> rules{};
773 std::vector<std::unique_ptr<Chassis>> chassisVec{};
774 chassisVec.emplace_back(std::move(chassis));
775 System system{std::move(rules), std::move(chassisVec)};
776
777 // Create MockServices
778 MockServices services{};
779
780 // Since no PresenceDetection defined, isPresent() should return true
781 EXPECT_TRUE(devicePtr->isPresent(services, system, *chassisPtr));
782 }
783
784 // Test where PresenceDetection was specified in constructor: Is present
785 {
786 // Create PresenceDetection. Indicates device is present.
787 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
788 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
789 std::vector<std::unique_ptr<Action>> actions{};
790 actions.emplace_back(std::move(action));
791 std::unique_ptr<PresenceDetection> presenceDetection =
792 std::make_unique<PresenceDetection>(std::move(actions));
793
794 // Create Device
795 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
796 std::unique_ptr<Device> device = std::make_unique<Device>(
797 "reg1", true,
798 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
799 std::move(i2cInterface), std::move(presenceDetection));
800 Device* devicePtr = device.get();
801
802 // Create Chassis that contains Device
803 std::vector<std::unique_ptr<Device>> devices{};
804 devices.emplace_back(std::move(device));
805 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500806 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney48033bf2021-01-27 17:56:49 -0600807 Chassis* chassisPtr = chassis.get();
808
809 // Create System that contains Chassis
810 std::vector<std::unique_ptr<Rule>> rules{};
811 std::vector<std::unique_ptr<Chassis>> chassisVec{};
812 chassisVec.emplace_back(std::move(chassis));
813 System system{std::move(rules), std::move(chassisVec)};
814
815 // Create MockServices
816 MockServices services{};
817
818 // PresenceDetection::execute() and isPresent() should return true
819 EXPECT_TRUE(devicePtr->isPresent(services, system, *chassisPtr));
820 }
821
822 // Test where PresenceDetection was specified in constructor: Is not present
823 {
824 // Create PresenceDetection. Indicates device is not present.
825 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
826 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
827 std::vector<std::unique_ptr<Action>> actions{};
828 actions.emplace_back(std::move(action));
829 std::unique_ptr<PresenceDetection> presenceDetection =
830 std::make_unique<PresenceDetection>(std::move(actions));
831
832 // Create Device
833 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
834 std::unique_ptr<Device> device = std::make_unique<Device>(
835 "reg1", true,
836 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
837 std::move(i2cInterface), std::move(presenceDetection));
838 Device* devicePtr = device.get();
839
840 // Create Chassis that contains Device
841 std::vector<std::unique_ptr<Device>> devices{};
842 devices.emplace_back(std::move(device));
843 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500844 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney48033bf2021-01-27 17:56:49 -0600845 Chassis* chassisPtr = chassis.get();
846
847 // Create System that contains Chassis
848 std::vector<std::unique_ptr<Rule>> rules{};
849 std::vector<std::unique_ptr<Chassis>> chassisVec{};
850 chassisVec.emplace_back(std::move(chassis));
851 System system{std::move(rules), std::move(chassisVec)};
852
853 // Create MockServices
854 MockServices services{};
855
856 // PresenceDetection::execute() and isPresent() should return false
857 EXPECT_FALSE(devicePtr->isPresent(services, system, *chassisPtr));
858 }
859}
860
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500861TEST(DeviceTests, IsRegulator)
862{
Bob Kinga76898f2020-10-13 15:08:33 +0800863 Device device{
864 "vdd_reg", false,
865 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
866 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500867 EXPECT_EQ(device.isRegulator(), false);
868}
Bob King8e1cd0b2020-07-08 13:30:27 +0800869
870TEST(DeviceTests, MonitorSensors)
871{
Shawn McCarney48033bf2021-01-27 17:56:49 -0600872 // Test where device is not present
Bob King8e1cd0b2020-07-08 13:30:27 +0800873 {
Shawn McCarney17bac892021-05-08 07:55:52 -0500874 // Create mock services. No Sensors methods should be called.
Bob King8a552922020-08-05 17:02:31 +0800875 MockServices services{};
Shawn McCarney17bac892021-05-08 07:55:52 -0500876 MockSensors& sensors = services.getMockSensors();
877 EXPECT_CALL(sensors, startRail).Times(0);
878 EXPECT_CALL(sensors, setValue).Times(0);
879 EXPECT_CALL(sensors, endRail).Times(0);
Bob King8a552922020-08-05 17:02:31 +0800880
Shawn McCarney17bac892021-05-08 07:55:52 -0500881 // Create SensorMonitoring. Action inside it should not be executed.
882 std::unique_ptr<MockAction> sensAction = std::make_unique<MockAction>();
883 EXPECT_CALL(*sensAction, execute).Times(0);
884 std::vector<std::unique_ptr<Action>> sensActions{};
885 sensActions.emplace_back(std::move(sensAction));
Bob King8e1cd0b2020-07-08 13:30:27 +0800886 std::unique_ptr<SensorMonitoring> sensorMonitoring =
Shawn McCarney17bac892021-05-08 07:55:52 -0500887 std::make_unique<SensorMonitoring>(std::move(sensActions));
Bob King8e1cd0b2020-07-08 13:30:27 +0800888
889 // Create Rail
890 std::unique_ptr<Configuration> configuration{};
891 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
Shawn McCarney17bac892021-05-08 07:55:52 -0500892 "vddr1", std::move(configuration), std::move(sensorMonitoring));
893
894 // Create PresenceDetection. Indicates device is not present.
895 std::unique_ptr<MockAction> presAction = std::make_unique<MockAction>();
896 EXPECT_CALL(*presAction, execute).Times(1).WillOnce(Return(false));
897 std::vector<std::unique_ptr<Action>> presActions{};
898 presActions.emplace_back(std::move(presAction));
899 std::unique_ptr<PresenceDetection> presenceDetection =
900 std::make_unique<PresenceDetection>(std::move(presActions));
Bob King8e1cd0b2020-07-08 13:30:27 +0800901
902 // Create Device
Shawn McCarney17bac892021-05-08 07:55:52 -0500903 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
Bob King8e1cd0b2020-07-08 13:30:27 +0800904 std::unique_ptr<Configuration> deviceConfiguration{};
Shawn McCarney32252592021-09-08 15:29:36 -0500905 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
Shawn McCarney17bac892021-05-08 07:55:52 -0500906 std::vector<std::unique_ptr<Rail>> rails{};
907 rails.emplace_back(std::move(rail));
Bob King8e1cd0b2020-07-08 13:30:27 +0800908 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800909 "reg1", true,
910 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King8e1cd0b2020-07-08 13:30:27 +0800911 std::move(i2cInterface), std::move(presenceDetection),
Shawn McCarney32252592021-09-08 15:29:36 -0500912 std::move(deviceConfiguration), std::move(phaseFaultDetection),
913 std::move(rails));
Bob King8e1cd0b2020-07-08 13:30:27 +0800914 Device* devicePtr = device.get();
915
916 // Create Chassis that contains Device
917 std::vector<std::unique_ptr<Device>> devices{};
918 devices.emplace_back(std::move(device));
919 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500920 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Bob King8e1cd0b2020-07-08 13:30:27 +0800921 Chassis* chassisPtr = chassis.get();
922
923 // Create System that contains Chassis
924 std::vector<std::unique_ptr<Rule>> rules{};
925 std::vector<std::unique_ptr<Chassis>> chassisVec{};
926 chassisVec.emplace_back(std::move(chassis));
927 System system{std::move(rules), std::move(chassisVec)};
928
Shawn McCarney17bac892021-05-08 07:55:52 -0500929 // Call monitorSensors(). Should do nothing.
930 devicePtr->monitorSensors(services, system, *chassisPtr);
931 }
932
933 // Test where Rails were not specified in constructor
934 {
935 // Create mock services. No Sensors methods should be called.
936 MockServices services{};
937 MockSensors& sensors = services.getMockSensors();
938 EXPECT_CALL(sensors, startRail).Times(0);
939 EXPECT_CALL(sensors, setValue).Times(0);
940 EXPECT_CALL(sensors, endRail).Times(0);
941
942 // Create Device
943 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
944 std::unique_ptr<Device> device = std::make_unique<Device>(
945 "reg1", true,
946 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
947 std::move(i2cInterface));
948 Device* devicePtr = device.get();
949
950 // Create Chassis that contains Device
951 std::vector<std::unique_ptr<Device>> devices{};
952 devices.emplace_back(std::move(device));
953 std::unique_ptr<Chassis> chassis =
954 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
955 Chassis* chassisPtr = chassis.get();
956
957 // Create System that contains Chassis
958 std::vector<std::unique_ptr<Rule>> rules{};
959 std::vector<std::unique_ptr<Chassis>> chassisVec{};
960 chassisVec.emplace_back(std::move(chassis));
961 System system{std::move(rules), std::move(chassisVec)};
962
963 // Call monitorSensors(). Should do nothing.
964 devicePtr->monitorSensors(services, system, *chassisPtr);
965 }
966
967 // Test where Rails were specified in constructor
968 {
969 // Create mock services. Set Sensors service expectations.
970 MockServices services{};
971 MockSensors& sensors = services.getMockSensors();
972 EXPECT_CALL(sensors, startRail("vdd0",
973 "/xyz/openbmc_project/inventory/system/"
974 "chassis/motherboard/reg1",
975 chassisInvPath))
976 .Times(1);
977 EXPECT_CALL(sensors, startRail("vio0",
978 "/xyz/openbmc_project/inventory/system/"
979 "chassis/motherboard/reg1",
980 chassisInvPath))
981 .Times(1);
982 EXPECT_CALL(sensors, setValue).Times(0);
983 EXPECT_CALL(sensors, endRail(false)).Times(2);
984
985 std::vector<std::unique_ptr<Rail>> rails{};
986
987 // Create Rail vdd0
988 {
989 // Create SensorMonitoring for Rail
990 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
991 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
992 std::vector<std::unique_ptr<Action>> actions{};
993 actions.emplace_back(std::move(action));
994 std::unique_ptr<SensorMonitoring> sensorMonitoring =
995 std::make_unique<SensorMonitoring>(std::move(actions));
996
997 // Create Rail
998 std::unique_ptr<Configuration> configuration{};
999 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
1000 "vdd0", std::move(configuration), std::move(sensorMonitoring));
1001 rails.emplace_back(std::move(rail));
1002 }
1003
1004 // Create Rail vio0
1005 {
1006 // Create SensorMonitoring for Rail
1007 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
1008 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
1009 std::vector<std::unique_ptr<Action>> actions{};
1010 actions.emplace_back(std::move(action));
1011 std::unique_ptr<SensorMonitoring> sensorMonitoring =
1012 std::make_unique<SensorMonitoring>(std::move(actions));
1013
1014 // Create Rail
1015 std::unique_ptr<Configuration> configuration{};
1016 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
1017 "vio0", std::move(configuration), std::move(sensorMonitoring));
1018 rails.emplace_back(std::move(rail));
1019 }
1020
1021 // Create Device that contains Rails
1022 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
1023 std::unique_ptr<PresenceDetection> presenceDetection{};
1024 std::unique_ptr<Configuration> configuration{};
Shawn McCarney32252592021-09-08 15:29:36 -05001025 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
Shawn McCarney17bac892021-05-08 07:55:52 -05001026 std::unique_ptr<Device> device = std::make_unique<Device>(
1027 "reg1", true,
1028 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
1029 std::move(i2cInterface), std::move(presenceDetection),
Shawn McCarney32252592021-09-08 15:29:36 -05001030 std::move(configuration), std::move(phaseFaultDetection),
1031 std::move(rails));
Shawn McCarney17bac892021-05-08 07:55:52 -05001032 Device* devicePtr = device.get();
1033
1034 // Create Chassis that contains Device
1035 std::vector<std::unique_ptr<Device>> devices{};
1036 devices.emplace_back(std::move(device));
1037 std::unique_ptr<Chassis> chassis =
1038 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
1039 Chassis* chassisPtr = chassis.get();
1040
1041 // Create System that contains Chassis
1042 std::vector<std::unique_ptr<Rule>> rules{};
1043 std::vector<std::unique_ptr<Chassis>> chassisVec{};
1044 chassisVec.emplace_back(std::move(chassis));
1045 System system{std::move(rules), std::move(chassisVec)};
1046
1047 // Call monitorSensors(). Should monitor sensors in both rails.
Bob King8a552922020-08-05 17:02:31 +08001048 devicePtr->monitorSensors(services, system, *chassisPtr);
Bob King8e1cd0b2020-07-08 13:30:27 +08001049 }
1050}