blob: 21eecc98bd3ec3b31e3873454cb5a86399763bde [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 McCarney0b1a0e72020-03-11 18:01:44 -050028#include "presence_detection.hpp"
29#include "rail.hpp"
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050030#include "rule.hpp"
Shawn McCarney17bac892021-05-08 07:55:52 -050031#include "sensor_monitoring.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050032#include "sensors.hpp"
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050033#include "system.hpp"
Shawn McCarney8a3afd72020-03-12 14:28:44 -050034#include "test_utils.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060035
36#include <memory>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050037#include <optional>
Shawn McCarney525e20c2020-04-14 11:05:39 -050038#include <string>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060039#include <utility>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050040#include <vector>
Shawn McCarneya2461b32019-10-24 18:53:01 -050041
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050042#include <gmock/gmock.h>
Shawn McCarneya2461b32019-10-24 18:53:01 -050043#include <gtest/gtest.h>
44
45using namespace phosphor::power::regulators;
Shawn McCarney8a3afd72020-03-12 14:28:44 -050046using namespace phosphor::power::regulators::test_utils;
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060047
Bob King8e1cd0b2020-07-08 13:30:27 +080048using ::testing::A;
Shawn McCarney81a2f902021-03-23 21:41:34 -050049using ::testing::Ref;
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050050using ::testing::Return;
Shawn McCarneyb4d18a42020-06-02 10:27:05 -050051using ::testing::Throw;
Bob King8e1cd0b2020-07-08 13:30:27 +080052using ::testing::TypedEq;
Shawn McCarneyeb7bec42020-04-14 09:38:15 -050053
Shawn McCarneycb3f6a62021-04-30 10:54:30 -050054static const std::string chassisInvPath{
55 "/xyz/openbmc_project/inventory/system/chassis"};
56
Shawn McCarneya2461b32019-10-24 18:53:01 -050057TEST(DeviceTests, Constructor)
58{
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050059 // Test where only required parameters are specified
60 {
61 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
62 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
Bob Kinga76898f2020-10-13 15:08:33 +080063 Device device{
64 "vdd_reg", true,
65 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
66 std::move(i2cInterface)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050067 EXPECT_EQ(device.getID(), "vdd_reg");
68 EXPECT_EQ(device.isRegulator(), true);
Bob Kinga76898f2020-10-13 15:08:33 +080069 EXPECT_EQ(
70 device.getFRU(),
71 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050072 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
73 EXPECT_EQ(device.getPresenceDetection(), nullptr);
74 EXPECT_EQ(device.getConfiguration(), nullptr);
75 EXPECT_EQ(device.getRails().size(), 0);
76 }
77
78 // Test where all parameters are specified
79 {
80 // Create I2CInterface
81 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
82 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
83
84 // Create PresenceDetection
85 std::vector<std::unique_ptr<Action>> actions{};
86 actions.push_back(std::make_unique<MockAction>());
87 std::unique_ptr<PresenceDetection> presenceDetection =
88 std::make_unique<PresenceDetection>(std::move(actions));
89
90 // Create Configuration
91 std::optional<double> volts{};
92 actions.clear();
93 actions.push_back(std::make_unique<MockAction>());
94 actions.push_back(std::make_unique<MockAction>());
95 std::unique_ptr<Configuration> configuration =
96 std::make_unique<Configuration>(volts, std::move(actions));
97
98 // Create vector of Rail objects
99 std::vector<std::unique_ptr<Rail>> rails{};
100 rails.push_back(std::make_unique<Rail>("vdd0"));
101 rails.push_back(std::make_unique<Rail>("vdd1"));
102
103 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800104 Device device{
105 "vdd_reg",
106 false,
107 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
108 std::move(i2cInterface),
109 std::move(presenceDetection),
110 std::move(configuration),
111 std::move(rails)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500112 EXPECT_EQ(device.getID(), "vdd_reg");
113 EXPECT_EQ(device.isRegulator(), false);
Bob Kinga76898f2020-10-13 15:08:33 +0800114 EXPECT_EQ(
115 device.getFRU(),
116 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1");
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500117 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
118 EXPECT_NE(device.getPresenceDetection(), nullptr);
119 EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1);
120 EXPECT_NE(device.getConfiguration(), nullptr);
121 EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), false);
122 EXPECT_EQ(device.getConfiguration()->getActions().size(), 2);
123 EXPECT_EQ(device.getRails().size(), 2);
124 }
Shawn McCarneya2461b32019-10-24 18:53:01 -0500125}
126
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500127TEST(DeviceTests, AddToIDMap)
128{
129 std::unique_ptr<PresenceDetection> presenceDetection{};
130 std::unique_ptr<Configuration> configuration{};
131
132 // Create vector of Rail objects
133 std::vector<std::unique_ptr<Rail>> rails{};
134 rails.push_back(std::make_unique<Rail>("vdd0"));
135 rails.push_back(std::make_unique<Rail>("vdd1"));
136
137 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800138 Device device{
139 "vdd_reg",
140 false,
141 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
142 std::move(createI2CInterface()),
143 std::move(presenceDetection),
144 std::move(configuration),
145 std::move(rails)};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500146
147 // Add Device and Rail objects to an IDMap
148 IDMap idMap{};
149 device.addToIDMap(idMap);
150
151 // Verify Device is in the IDMap
152 EXPECT_NO_THROW(idMap.getDevice("vdd_reg"));
153 EXPECT_THROW(idMap.getDevice("vio_reg"), std::invalid_argument);
154
155 // Verify all Rails are in the IDMap
156 EXPECT_NO_THROW(idMap.getRail("vdd0"));
157 EXPECT_NO_THROW(idMap.getRail("vdd1"));
158 EXPECT_THROW(idMap.getRail("vdd2"), std::invalid_argument);
159}
160
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600161TEST(DeviceTests, ClearCache)
162{
163 // Test where Device does not contain a PresenceDetection object
164 try
165 {
166 Device device{
167 "vdd_reg", false,
168 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
169 std::move(createI2CInterface())};
170 device.clearCache();
171 }
172 catch (...)
173 {
174 ADD_FAILURE() << "Should not have caught exception.";
175 }
176
177 // Test where Device contains a PresenceDetection object
178 {
179 // Create PresenceDetection
180 std::vector<std::unique_ptr<Action>> actions{};
181 std::unique_ptr<PresenceDetection> presenceDetection =
182 std::make_unique<PresenceDetection>(std::move(actions));
183 PresenceDetection* presenceDetectionPtr = presenceDetection.get();
184
185 // Create Device
186 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
187 std::unique_ptr<Device> device = std::make_unique<Device>(
188 "reg1", true,
189 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
190 std::move(i2cInterface), std::move(presenceDetection));
191 Device* devicePtr = device.get();
192
193 // Create Chassis that contains Device
194 std::vector<std::unique_ptr<Device>> devices{};
195 devices.emplace_back(std::move(device));
196 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500197 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600198 Chassis* chassisPtr = chassis.get();
199
200 // Create System that contains Chassis
201 std::vector<std::unique_ptr<Rule>> rules{};
202 std::vector<std::unique_ptr<Chassis>> chassisVec{};
203 chassisVec.emplace_back(std::move(chassis));
204 System system{std::move(rules), std::move(chassisVec)};
205
206 // Cache presence value in PresenceDetection
207 MockServices services{};
208 presenceDetectionPtr->execute(services, system, *chassisPtr,
209 *devicePtr);
210 EXPECT_TRUE(presenceDetectionPtr->getCachedPresence().has_value());
211
212 // Clear cached data in Device
213 devicePtr->clearCache();
214
215 // Verify presence value no longer cached in PresenceDetection
216 EXPECT_FALSE(presenceDetectionPtr->getCachedPresence().has_value());
217 }
218}
219
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500220TEST(DeviceTests, Close)
221{
222 // Test where works: I2C interface is not open
223 {
224 // Create mock I2CInterface
225 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
226 std::make_unique<i2c::MockedI2CInterface>();
227 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(false));
228 EXPECT_CALL(*i2cInterface, close).Times(0);
229
Bob Kingd692d6d2020-09-14 13:42:57 +0800230 // Create mock services. No logError should occur.
231 MockServices services{};
232 MockJournal& journal = services.getMockJournal();
233 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
234 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
235 .Times(0);
236
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500237 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800238 Device device{
239 "vdd_reg", true,
240 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
241 std::move(i2cInterface)};
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500242
243 // Close Device
Bob Kingd692d6d2020-09-14 13:42:57 +0800244 device.close(services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500245 }
246
247 // Test where works: I2C interface is open
248 {
249 // Create mock I2CInterface
250 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
251 std::make_unique<i2c::MockedI2CInterface>();
252 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
253 EXPECT_CALL(*i2cInterface, close).Times(1);
254
Bob Kingd692d6d2020-09-14 13:42:57 +0800255 // Create mock services. No logError should occur.
256 MockServices services{};
257 MockJournal& journal = services.getMockJournal();
258 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
259 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
260 .Times(0);
261
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500262 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800263 Device device{
264 "vdd_reg", true,
265 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
266 std::move(i2cInterface)};
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500267
268 // Close Device
Bob Kingd692d6d2020-09-14 13:42:57 +0800269 device.close(services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500270 }
271
272 // Test where fails: closing I2C interface fails
273 {
274 // Create mock I2CInterface
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, close)
279 .Times(1)
280 .WillOnce(Throw(
281 i2c::I2CException{"Failed to close", "/dev/i2c-1", 0x70}));
282
Shawn McCarney81a2f902021-03-23 21:41:34 -0500283 // Create mock services. Expect logError() and logI2CError() to be
284 // called.
Bob Kingd692d6d2020-09-14 13:42:57 +0800285 MockServices services{};
Shawn McCarney81a2f902021-03-23 21:41:34 -0500286 MockErrorLogging& errorLogging = services.getMockErrorLogging();
Bob Kingd692d6d2020-09-14 13:42:57 +0800287 MockJournal& journal = services.getMockJournal();
288 std::vector<std::string> expectedErrMessagesException{
289 "I2CException: Failed to close: bus /dev/i2c-1, addr 0x70"};
290 EXPECT_CALL(journal, logError("Unable to close device vdd_reg"))
291 .Times(1);
292 EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1);
Shawn McCarney81a2f902021-03-23 21:41:34 -0500293 EXPECT_CALL(errorLogging,
294 logI2CError(Entry::Level::Notice, Ref(journal),
295 "/dev/i2c-1", 0x70, 0))
296 .Times(1);
Bob Kingd692d6d2020-09-14 13:42:57 +0800297
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500298 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800299 Device device{
300 "vdd_reg", true,
301 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
302 std::move(i2cInterface)};
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500303
304 // Close Device
Bob Kingd692d6d2020-09-14 13:42:57 +0800305 device.close(services);
Shawn McCarneyb4d18a42020-06-02 10:27:05 -0500306 }
307}
308
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500309TEST(DeviceTests, Configure)
310{
Shawn McCarney48033bf2021-01-27 17:56:49 -0600311 // Test where device is not present
312 {
313 // Create mock services. No logging should occur.
314 MockServices services{};
315 MockJournal& journal = services.getMockJournal();
316 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
317 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
318
319 // Create PresenceDetection. Indicates device is not present.
320 std::unique_ptr<MockAction> presAction = std::make_unique<MockAction>();
321 EXPECT_CALL(*presAction, execute).Times(1).WillOnce(Return(false));
322 std::vector<std::unique_ptr<Action>> presActions{};
323 presActions.emplace_back(std::move(presAction));
324 std::unique_ptr<PresenceDetection> presenceDetection =
325 std::make_unique<PresenceDetection>(std::move(presActions));
326
327 // Create Configuration. Action inside it should not be executed.
328 std::optional<double> volts{};
329 std::unique_ptr<MockAction> confAction = std::make_unique<MockAction>();
330 EXPECT_CALL(*confAction, execute).Times(0);
331 std::vector<std::unique_ptr<Action>> confActions{};
332 confActions.emplace_back(std::move(confAction));
333 std::unique_ptr<Configuration> configuration =
334 std::make_unique<Configuration>(volts, std::move(confActions));
335
336 // Create Device
337 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
338 std::unique_ptr<Device> device = std::make_unique<Device>(
339 "reg1", true,
340 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
341 std::move(i2cInterface), std::move(presenceDetection),
342 std::move(configuration));
343 Device* devicePtr = device.get();
344
345 // Create Chassis that contains Device
346 std::vector<std::unique_ptr<Device>> devices{};
347 devices.emplace_back(std::move(device));
348 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500349 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney48033bf2021-01-27 17:56:49 -0600350 Chassis* chassisPtr = chassis.get();
351
352 // Create System that contains Chassis
353 std::vector<std::unique_ptr<Rule>> rules{};
354 std::vector<std::unique_ptr<Chassis>> chassisVec{};
355 chassisVec.emplace_back(std::move(chassis));
356 System system{std::move(rules), std::move(chassisVec)};
357
358 // Call configure(). Should do nothing.
359 devicePtr->configure(services, system, *chassisPtr);
360 }
361
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500362 // Test where Configuration and Rails were not specified in constructor
363 {
Bob King5cfe5102020-07-30 16:26:18 +0800364 // Create mock services. No logging should occur.
Bob King23243f82020-07-29 10:38:57 +0800365 MockServices services{};
Bob King5cfe5102020-07-30 16:26:18 +0800366 MockJournal& journal = services.getMockJournal();
367 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
368 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
Bob King23243f82020-07-29 10:38:57 +0800369
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500370 // Create Device
371 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
372 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800373 "reg1", true,
374 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500375 std::move(i2cInterface));
376 Device* devicePtr = device.get();
377
378 // Create Chassis that contains Device
379 std::vector<std::unique_ptr<Device>> devices{};
380 devices.emplace_back(std::move(device));
381 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500382 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500383 Chassis* chassisPtr = chassis.get();
384
385 // Create System that contains Chassis
386 std::vector<std::unique_ptr<Rule>> rules{};
387 std::vector<std::unique_ptr<Chassis>> chassisVec{};
388 chassisVec.emplace_back(std::move(chassis));
389 System system{std::move(rules), std::move(chassisVec)};
390
Bob King5cfe5102020-07-30 16:26:18 +0800391 // Call configure().
Bob King23243f82020-07-29 10:38:57 +0800392 devicePtr->configure(services, system, *chassisPtr);
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500393 }
394
395 // Test where Configuration and Rails were specified in constructor
396 {
397 std::vector<std::unique_ptr<Rail>> rails{};
398
Bob King5cfe5102020-07-30 16:26:18 +0800399 // Create mock services. Expect logDebug() to be called.
400 // For the Device and both Rails, should execute the Configuration
401 // and log a debug message.
402 MockServices services{};
403 MockJournal& journal = services.getMockJournal();
404 EXPECT_CALL(journal, logDebug("Configuring reg1")).Times(1);
405 EXPECT_CALL(journal, logDebug("Configuring vdd0: volts=1.300000"))
406 .Times(1);
407 EXPECT_CALL(journal, logDebug("Configuring vio0: volts=3.200000"))
408 .Times(1);
409 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
410
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500411 // Create Rail vdd0
412 {
413 // Create Configuration for Rail
414 std::optional<double> volts{1.3};
415 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
416 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
417 std::vector<std::unique_ptr<Action>> actions{};
418 actions.emplace_back(std::move(action));
419 std::unique_ptr<Configuration> configuration =
420 std::make_unique<Configuration>(volts, std::move(actions));
421
422 // Create Rail
423 std::unique_ptr<Rail> rail =
424 std::make_unique<Rail>("vdd0", std::move(configuration));
425 rails.emplace_back(std::move(rail));
426 }
427
428 // Create Rail vio0
429 {
430 // Create Configuration for Rail
431 std::optional<double> volts{3.2};
432 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
433 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
434 std::vector<std::unique_ptr<Action>> actions{};
435 actions.emplace_back(std::move(action));
436 std::unique_ptr<Configuration> configuration =
437 std::make_unique<Configuration>(volts, std::move(actions));
438
439 // Create Rail
440 std::unique_ptr<Rail> rail =
441 std::make_unique<Rail>("vio0", std::move(configuration));
442 rails.emplace_back(std::move(rail));
443 }
444
445 // Create Configuration for Device
446 std::optional<double> volts{};
447 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
448 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
449 std::vector<std::unique_ptr<Action>> actions{};
450 actions.emplace_back(std::move(action));
451 std::unique_ptr<Configuration> configuration =
452 std::make_unique<Configuration>(volts, std::move(actions));
453
454 // Create Device
455 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
456 std::unique_ptr<PresenceDetection> presenceDetection{};
457 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800458 "reg1", true,
459 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500460 std::move(i2cInterface), std::move(presenceDetection),
461 std::move(configuration), std::move(rails));
462 Device* devicePtr = device.get();
463
464 // Create Chassis that contains Device
465 std::vector<std::unique_ptr<Device>> devices{};
466 devices.emplace_back(std::move(device));
467 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500468 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500469 Chassis* chassisPtr = chassis.get();
470
471 // Create System that contains Chassis
472 std::vector<std::unique_ptr<Rule>> rules{};
473 std::vector<std::unique_ptr<Chassis>> chassisVec{};
474 chassisVec.emplace_back(std::move(chassis));
475 System system{std::move(rules), std::move(chassisVec)};
476
Bob King5cfe5102020-07-30 16:26:18 +0800477 // Call configure().
Bob King23243f82020-07-29 10:38:57 +0800478 devicePtr->configure(services, system, *chassisPtr);
Shawn McCarneyeb7bec42020-04-14 09:38:15 -0500479 }
480}
481
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500482TEST(DeviceTests, GetConfiguration)
Shawn McCarneya2461b32019-10-24 18:53:01 -0500483{
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500484 // Test where Configuration was not specified in constructor
485 {
Bob Kinga76898f2020-10-13 15:08:33 +0800486 Device device{
487 "vdd_reg", true,
488 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
489 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500490 EXPECT_EQ(device.getConfiguration(), nullptr);
491 }
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600492
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500493 // Test where Configuration was specified in constructor
494 {
495 std::unique_ptr<PresenceDetection> presenceDetection{};
496
497 // Create Configuration
498 std::optional<double> volts{3.2};
499 std::vector<std::unique_ptr<Action>> actions{};
500 actions.push_back(std::make_unique<MockAction>());
501 actions.push_back(std::make_unique<MockAction>());
502 std::unique_ptr<Configuration> configuration =
503 std::make_unique<Configuration>(volts, std::move(actions));
504
505 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800506 Device device{
507 "vdd_reg",
508 true,
509 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
510 std::move(createI2CInterface()),
511 std::move(presenceDetection),
512 std::move(configuration)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500513 EXPECT_NE(device.getConfiguration(), nullptr);
514 EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true);
515 EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2);
516 EXPECT_EQ(device.getConfiguration()->getActions().size(), 2);
517 }
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600518}
519
520TEST(DeviceTests, GetFRU)
521{
Bob Kinga76898f2020-10-13 15:08:33 +0800522 Device device{
523 "vdd_reg", true,
524 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
525 std::move(createI2CInterface())};
526 EXPECT_EQ(device.getFRU(),
527 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600528}
529
530TEST(DeviceTests, GetI2CInterface)
531{
532 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
533 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
Bob Kinga76898f2020-10-13 15:08:33 +0800534 Device device{
535 "vdd_reg", true,
536 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
537 std::move(i2cInterface)};
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600538 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
Shawn McCarneya2461b32019-10-24 18:53:01 -0500539}
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500540
541TEST(DeviceTests, GetID)
542{
Bob Kinga76898f2020-10-13 15:08:33 +0800543 Device device{
544 "vdd_reg", false,
545 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
546 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500547 EXPECT_EQ(device.getID(), "vdd_reg");
548}
549
550TEST(DeviceTests, GetPresenceDetection)
551{
552 // Test where PresenceDetection was not specified in constructor
553 {
Bob Kinga76898f2020-10-13 15:08:33 +0800554 Device device{
555 "vdd_reg", true,
556 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
557 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500558 EXPECT_EQ(device.getPresenceDetection(), nullptr);
559 }
560
561 // Test where PresenceDetection was specified in constructor
562 {
563 // Create PresenceDetection
564 std::vector<std::unique_ptr<Action>> actions{};
565 actions.push_back(std::make_unique<MockAction>());
566 std::unique_ptr<PresenceDetection> presenceDetection =
567 std::make_unique<PresenceDetection>(std::move(actions));
568
569 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800570 Device device{
571 "vdd_reg", false,
572 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
573 std::move(createI2CInterface()), std::move(presenceDetection)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500574 EXPECT_NE(device.getPresenceDetection(), nullptr);
575 EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1);
576 }
577}
578
579TEST(DeviceTests, GetRails)
580{
581 // Test where no rails were specified in constructor
582 {
Bob Kinga76898f2020-10-13 15:08:33 +0800583 Device device{
584 "vdd_reg", true,
585 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
586 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500587 EXPECT_EQ(device.getRails().size(), 0);
588 }
589
590 // Test where rails were specified in constructor
591 {
592 std::unique_ptr<PresenceDetection> presenceDetection{};
593 std::unique_ptr<Configuration> configuration{};
594
595 // Create vector of Rail objects
596 std::vector<std::unique_ptr<Rail>> rails{};
597 rails.push_back(std::make_unique<Rail>("vdd0"));
598 rails.push_back(std::make_unique<Rail>("vdd1"));
599
600 // Create Device
Bob Kinga76898f2020-10-13 15:08:33 +0800601 Device device{
602 "vdd_reg",
603 false,
604 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
605 std::move(createI2CInterface()),
606 std::move(presenceDetection),
607 std::move(configuration),
608 std::move(rails)};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500609 EXPECT_EQ(device.getRails().size(), 2);
610 EXPECT_EQ(device.getRails()[0]->getID(), "vdd0");
611 EXPECT_EQ(device.getRails()[1]->getID(), "vdd1");
612 }
613}
614
Shawn McCarney48033bf2021-01-27 17:56:49 -0600615TEST(DeviceTests, IsPresent)
616{
617 // Test where PresenceDetection not specified in constructor
618 {
619 // Create Device
620 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
621 std::unique_ptr<Device> device = std::make_unique<Device>(
622 "reg1", true,
623 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
624 std::move(i2cInterface));
625 Device* devicePtr = device.get();
626
627 // Create Chassis that contains Device
628 std::vector<std::unique_ptr<Device>> devices{};
629 devices.emplace_back(std::move(device));
630 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500631 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney48033bf2021-01-27 17:56:49 -0600632 Chassis* chassisPtr = chassis.get();
633
634 // Create System that contains Chassis
635 std::vector<std::unique_ptr<Rule>> rules{};
636 std::vector<std::unique_ptr<Chassis>> chassisVec{};
637 chassisVec.emplace_back(std::move(chassis));
638 System system{std::move(rules), std::move(chassisVec)};
639
640 // Create MockServices
641 MockServices services{};
642
643 // Since no PresenceDetection defined, isPresent() should return true
644 EXPECT_TRUE(devicePtr->isPresent(services, system, *chassisPtr));
645 }
646
647 // Test where PresenceDetection was specified in constructor: Is present
648 {
649 // Create PresenceDetection. Indicates device is present.
650 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
651 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
652 std::vector<std::unique_ptr<Action>> actions{};
653 actions.emplace_back(std::move(action));
654 std::unique_ptr<PresenceDetection> presenceDetection =
655 std::make_unique<PresenceDetection>(std::move(actions));
656
657 // Create Device
658 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
659 std::unique_ptr<Device> device = std::make_unique<Device>(
660 "reg1", true,
661 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
662 std::move(i2cInterface), std::move(presenceDetection));
663 Device* devicePtr = device.get();
664
665 // Create Chassis that contains Device
666 std::vector<std::unique_ptr<Device>> devices{};
667 devices.emplace_back(std::move(device));
668 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500669 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney48033bf2021-01-27 17:56:49 -0600670 Chassis* chassisPtr = chassis.get();
671
672 // Create System that contains Chassis
673 std::vector<std::unique_ptr<Rule>> rules{};
674 std::vector<std::unique_ptr<Chassis>> chassisVec{};
675 chassisVec.emplace_back(std::move(chassis));
676 System system{std::move(rules), std::move(chassisVec)};
677
678 // Create MockServices
679 MockServices services{};
680
681 // PresenceDetection::execute() and isPresent() should return true
682 EXPECT_TRUE(devicePtr->isPresent(services, system, *chassisPtr));
683 }
684
685 // Test where PresenceDetection was specified in constructor: Is not present
686 {
687 // Create PresenceDetection. Indicates device is not present.
688 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
689 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(false));
690 std::vector<std::unique_ptr<Action>> actions{};
691 actions.emplace_back(std::move(action));
692 std::unique_ptr<PresenceDetection> presenceDetection =
693 std::make_unique<PresenceDetection>(std::move(actions));
694
695 // Create Device
696 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
697 std::unique_ptr<Device> device = std::make_unique<Device>(
698 "reg1", true,
699 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
700 std::move(i2cInterface), std::move(presenceDetection));
701 Device* devicePtr = device.get();
702
703 // Create Chassis that contains Device
704 std::vector<std::unique_ptr<Device>> devices{};
705 devices.emplace_back(std::move(device));
706 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500707 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Shawn McCarney48033bf2021-01-27 17:56:49 -0600708 Chassis* chassisPtr = chassis.get();
709
710 // Create System that contains Chassis
711 std::vector<std::unique_ptr<Rule>> rules{};
712 std::vector<std::unique_ptr<Chassis>> chassisVec{};
713 chassisVec.emplace_back(std::move(chassis));
714 System system{std::move(rules), std::move(chassisVec)};
715
716 // Create MockServices
717 MockServices services{};
718
719 // PresenceDetection::execute() and isPresent() should return false
720 EXPECT_FALSE(devicePtr->isPresent(services, system, *chassisPtr));
721 }
722}
723
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500724TEST(DeviceTests, IsRegulator)
725{
Bob Kinga76898f2020-10-13 15:08:33 +0800726 Device device{
727 "vdd_reg", false,
728 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
729 std::move(createI2CInterface())};
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500730 EXPECT_EQ(device.isRegulator(), false);
731}
Bob King8e1cd0b2020-07-08 13:30:27 +0800732
733TEST(DeviceTests, MonitorSensors)
734{
Shawn McCarney48033bf2021-01-27 17:56:49 -0600735 // Test where device is not present
Bob King8e1cd0b2020-07-08 13:30:27 +0800736 {
Shawn McCarney17bac892021-05-08 07:55:52 -0500737 // Create mock services. No Sensors methods should be called.
Bob King8a552922020-08-05 17:02:31 +0800738 MockServices services{};
Shawn McCarney17bac892021-05-08 07:55:52 -0500739 MockSensors& sensors = services.getMockSensors();
740 EXPECT_CALL(sensors, startRail).Times(0);
741 EXPECT_CALL(sensors, setValue).Times(0);
742 EXPECT_CALL(sensors, endRail).Times(0);
Bob King8a552922020-08-05 17:02:31 +0800743
Shawn McCarney17bac892021-05-08 07:55:52 -0500744 // Create SensorMonitoring. Action inside it should not be executed.
745 std::unique_ptr<MockAction> sensAction = std::make_unique<MockAction>();
746 EXPECT_CALL(*sensAction, execute).Times(0);
747 std::vector<std::unique_ptr<Action>> sensActions{};
748 sensActions.emplace_back(std::move(sensAction));
Bob King8e1cd0b2020-07-08 13:30:27 +0800749 std::unique_ptr<SensorMonitoring> sensorMonitoring =
Shawn McCarney17bac892021-05-08 07:55:52 -0500750 std::make_unique<SensorMonitoring>(std::move(sensActions));
Bob King8e1cd0b2020-07-08 13:30:27 +0800751
752 // Create Rail
753 std::unique_ptr<Configuration> configuration{};
754 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
Shawn McCarney17bac892021-05-08 07:55:52 -0500755 "vddr1", std::move(configuration), std::move(sensorMonitoring));
756
757 // Create PresenceDetection. Indicates device is not present.
758 std::unique_ptr<MockAction> presAction = std::make_unique<MockAction>();
759 EXPECT_CALL(*presAction, execute).Times(1).WillOnce(Return(false));
760 std::vector<std::unique_ptr<Action>> presActions{};
761 presActions.emplace_back(std::move(presAction));
762 std::unique_ptr<PresenceDetection> presenceDetection =
763 std::make_unique<PresenceDetection>(std::move(presActions));
Bob King8e1cd0b2020-07-08 13:30:27 +0800764
765 // Create Device
Shawn McCarney17bac892021-05-08 07:55:52 -0500766 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
Bob King8e1cd0b2020-07-08 13:30:27 +0800767 std::unique_ptr<Configuration> deviceConfiguration{};
Shawn McCarney17bac892021-05-08 07:55:52 -0500768 std::vector<std::unique_ptr<Rail>> rails{};
769 rails.emplace_back(std::move(rail));
Bob King8e1cd0b2020-07-08 13:30:27 +0800770 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800771 "reg1", true,
772 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King8e1cd0b2020-07-08 13:30:27 +0800773 std::move(i2cInterface), std::move(presenceDetection),
774 std::move(deviceConfiguration), std::move(rails));
775 Device* devicePtr = device.get();
776
777 // Create Chassis that contains Device
778 std::vector<std::unique_ptr<Device>> devices{};
779 devices.emplace_back(std::move(device));
780 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500781 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Bob King8e1cd0b2020-07-08 13:30:27 +0800782 Chassis* chassisPtr = chassis.get();
783
784 // Create System that contains Chassis
785 std::vector<std::unique_ptr<Rule>> rules{};
786 std::vector<std::unique_ptr<Chassis>> chassisVec{};
787 chassisVec.emplace_back(std::move(chassis));
788 System system{std::move(rules), std::move(chassisVec)};
789
Shawn McCarney17bac892021-05-08 07:55:52 -0500790 // Call monitorSensors(). Should do nothing.
791 devicePtr->monitorSensors(services, system, *chassisPtr);
792 }
793
794 // Test where Rails were not specified in constructor
795 {
796 // Create mock services. No Sensors methods should be called.
797 MockServices services{};
798 MockSensors& sensors = services.getMockSensors();
799 EXPECT_CALL(sensors, startRail).Times(0);
800 EXPECT_CALL(sensors, setValue).Times(0);
801 EXPECT_CALL(sensors, endRail).Times(0);
802
803 // Create Device
804 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
805 std::unique_ptr<Device> device = std::make_unique<Device>(
806 "reg1", true,
807 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
808 std::move(i2cInterface));
809 Device* devicePtr = device.get();
810
811 // Create Chassis that contains Device
812 std::vector<std::unique_ptr<Device>> devices{};
813 devices.emplace_back(std::move(device));
814 std::unique_ptr<Chassis> chassis =
815 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
816 Chassis* chassisPtr = chassis.get();
817
818 // Create System that contains Chassis
819 std::vector<std::unique_ptr<Rule>> rules{};
820 std::vector<std::unique_ptr<Chassis>> chassisVec{};
821 chassisVec.emplace_back(std::move(chassis));
822 System system{std::move(rules), std::move(chassisVec)};
823
824 // Call monitorSensors(). Should do nothing.
825 devicePtr->monitorSensors(services, system, *chassisPtr);
826 }
827
828 // Test where Rails were specified in constructor
829 {
830 // Create mock services. Set Sensors service expectations.
831 MockServices services{};
832 MockSensors& sensors = services.getMockSensors();
833 EXPECT_CALL(sensors, startRail("vdd0",
834 "/xyz/openbmc_project/inventory/system/"
835 "chassis/motherboard/reg1",
836 chassisInvPath))
837 .Times(1);
838 EXPECT_CALL(sensors, startRail("vio0",
839 "/xyz/openbmc_project/inventory/system/"
840 "chassis/motherboard/reg1",
841 chassisInvPath))
842 .Times(1);
843 EXPECT_CALL(sensors, setValue).Times(0);
844 EXPECT_CALL(sensors, endRail(false)).Times(2);
845
846 std::vector<std::unique_ptr<Rail>> rails{};
847
848 // Create Rail vdd0
849 {
850 // Create SensorMonitoring for Rail
851 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
852 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
853 std::vector<std::unique_ptr<Action>> actions{};
854 actions.emplace_back(std::move(action));
855 std::unique_ptr<SensorMonitoring> sensorMonitoring =
856 std::make_unique<SensorMonitoring>(std::move(actions));
857
858 // Create Rail
859 std::unique_ptr<Configuration> configuration{};
860 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
861 "vdd0", std::move(configuration), std::move(sensorMonitoring));
862 rails.emplace_back(std::move(rail));
863 }
864
865 // Create Rail vio0
866 {
867 // Create SensorMonitoring for Rail
868 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
869 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
870 std::vector<std::unique_ptr<Action>> actions{};
871 actions.emplace_back(std::move(action));
872 std::unique_ptr<SensorMonitoring> sensorMonitoring =
873 std::make_unique<SensorMonitoring>(std::move(actions));
874
875 // Create Rail
876 std::unique_ptr<Configuration> configuration{};
877 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
878 "vio0", std::move(configuration), std::move(sensorMonitoring));
879 rails.emplace_back(std::move(rail));
880 }
881
882 // Create Device that contains Rails
883 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
884 std::unique_ptr<PresenceDetection> presenceDetection{};
885 std::unique_ptr<Configuration> configuration{};
886 std::unique_ptr<Device> device = std::make_unique<Device>(
887 "reg1", true,
888 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
889 std::move(i2cInterface), std::move(presenceDetection),
890 std::move(configuration), std::move(rails));
891 Device* devicePtr = device.get();
892
893 // Create Chassis that contains Device
894 std::vector<std::unique_ptr<Device>> devices{};
895 devices.emplace_back(std::move(device));
896 std::unique_ptr<Chassis> chassis =
897 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
898 Chassis* chassisPtr = chassis.get();
899
900 // Create System that contains Chassis
901 std::vector<std::unique_ptr<Rule>> rules{};
902 std::vector<std::unique_ptr<Chassis>> chassisVec{};
903 chassisVec.emplace_back(std::move(chassis));
904 System system{std::move(rules), std::move(chassisVec)};
905
906 // Call monitorSensors(). Should monitor sensors in both rails.
Bob King8a552922020-08-05 17:02:31 +0800907 devicePtr->monitorSensors(services, system, *chassisPtr);
Bob King8e1cd0b2020-07-08 13:30:27 +0800908 }
909}