blob: c4875bff43990fed4e60c1eea898a4b18adcfbe3 [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"
17#include "configuration.hpp"
Shawn McCarneya2461b32019-10-24 18:53:01 -050018#include "device.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060019#include "i2c_interface.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050020#include "id_map.hpp"
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050021#include "mock_action.hpp"
22#include "presence_detection.hpp"
23#include "rail.hpp"
Shawn McCarney8a3afd72020-03-12 14:28:44 -050024#include "test_utils.hpp"
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060025
26#include <memory>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050027#include <optional>
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060028#include <utility>
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050029#include <vector>
Shawn McCarneya2461b32019-10-24 18:53:01 -050030
31#include <gtest/gtest.h>
32
33using namespace phosphor::power::regulators;
Shawn McCarney8a3afd72020-03-12 14:28:44 -050034using namespace phosphor::power::regulators::test_utils;
Shawn McCarneyafb7fc32019-12-11 19:42:03 -060035
Shawn McCarneya2461b32019-10-24 18:53:01 -050036TEST(DeviceTests, Constructor)
37{
Shawn McCarney0b1a0e72020-03-11 18:01:44 -050038 // Test where only required parameters are specified
39 {
40 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
41 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
42 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
43 std::move(i2cInterface)};
44 EXPECT_EQ(device.getID(), "vdd_reg");
45 EXPECT_EQ(device.isRegulator(), true);
46 EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2");
47 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
48 EXPECT_EQ(device.getPresenceDetection(), nullptr);
49 EXPECT_EQ(device.getConfiguration(), nullptr);
50 EXPECT_EQ(device.getRails().size(), 0);
51 }
52
53 // Test where all parameters are specified
54 {
55 // Create I2CInterface
56 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
57 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
58
59 // Create PresenceDetection
60 std::vector<std::unique_ptr<Action>> actions{};
61 actions.push_back(std::make_unique<MockAction>());
62 std::unique_ptr<PresenceDetection> presenceDetection =
63 std::make_unique<PresenceDetection>(std::move(actions));
64
65 // Create Configuration
66 std::optional<double> volts{};
67 actions.clear();
68 actions.push_back(std::make_unique<MockAction>());
69 actions.push_back(std::make_unique<MockAction>());
70 std::unique_ptr<Configuration> configuration =
71 std::make_unique<Configuration>(volts, std::move(actions));
72
73 // Create vector of Rail objects
74 std::vector<std::unique_ptr<Rail>> rails{};
75 rails.push_back(std::make_unique<Rail>("vdd0"));
76 rails.push_back(std::make_unique<Rail>("vdd1"));
77
78 // Create Device
79 Device device{"vdd_reg",
80 false,
81 "/system/chassis/motherboard/reg1",
82 std::move(i2cInterface),
83 std::move(presenceDetection),
84 std::move(configuration),
85 std::move(rails)};
86 EXPECT_EQ(device.getID(), "vdd_reg");
87 EXPECT_EQ(device.isRegulator(), false);
88 EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg1");
89 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
90 EXPECT_NE(device.getPresenceDetection(), nullptr);
91 EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1);
92 EXPECT_NE(device.getConfiguration(), nullptr);
93 EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), false);
94 EXPECT_EQ(device.getConfiguration()->getActions().size(), 2);
95 EXPECT_EQ(device.getRails().size(), 2);
96 }
Shawn McCarneya2461b32019-10-24 18:53:01 -050097}
98
Shawn McCarneydb0b8332020-04-06 14:13:04 -050099TEST(DeviceTests, AddToIDMap)
100{
101 std::unique_ptr<PresenceDetection> presenceDetection{};
102 std::unique_ptr<Configuration> configuration{};
103
104 // Create vector of Rail objects
105 std::vector<std::unique_ptr<Rail>> rails{};
106 rails.push_back(std::make_unique<Rail>("vdd0"));
107 rails.push_back(std::make_unique<Rail>("vdd1"));
108
109 // Create Device
110 Device device{"vdd_reg",
111 false,
112 "/system/chassis/motherboard/reg2",
113 std::move(createI2CInterface()),
114 std::move(presenceDetection),
115 std::move(configuration),
116 std::move(rails)};
117
118 // Add Device and Rail objects to an IDMap
119 IDMap idMap{};
120 device.addToIDMap(idMap);
121
122 // Verify Device is in the IDMap
123 EXPECT_NO_THROW(idMap.getDevice("vdd_reg"));
124 EXPECT_THROW(idMap.getDevice("vio_reg"), std::invalid_argument);
125
126 // Verify all Rails are in the IDMap
127 EXPECT_NO_THROW(idMap.getRail("vdd0"));
128 EXPECT_NO_THROW(idMap.getRail("vdd1"));
129 EXPECT_THROW(idMap.getRail("vdd2"), std::invalid_argument);
130}
131
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500132TEST(DeviceTests, GetConfiguration)
Shawn McCarneya2461b32019-10-24 18:53:01 -0500133{
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500134 // Test where Configuration was not specified in constructor
135 {
136 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
137 std::move(createI2CInterface())};
138 EXPECT_EQ(device.getConfiguration(), nullptr);
139 }
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600140
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500141 // Test where Configuration was specified in constructor
142 {
143 std::unique_ptr<PresenceDetection> presenceDetection{};
144
145 // Create Configuration
146 std::optional<double> volts{3.2};
147 std::vector<std::unique_ptr<Action>> actions{};
148 actions.push_back(std::make_unique<MockAction>());
149 actions.push_back(std::make_unique<MockAction>());
150 std::unique_ptr<Configuration> configuration =
151 std::make_unique<Configuration>(volts, std::move(actions));
152
153 // Create Device
154 Device device{"vdd_reg",
155 true,
156 "/system/chassis/motherboard/reg2",
157 std::move(createI2CInterface()),
158 std::move(presenceDetection),
159 std::move(configuration)};
160 EXPECT_NE(device.getConfiguration(), nullptr);
161 EXPECT_EQ(device.getConfiguration()->getVolts().has_value(), true);
162 EXPECT_EQ(device.getConfiguration()->getVolts().value(), 3.2);
163 EXPECT_EQ(device.getConfiguration()->getActions().size(), 2);
164 }
Shawn McCarneyafb7fc32019-12-11 19:42:03 -0600165}
166
167TEST(DeviceTests, GetFRU)
168{
169 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
170 std::move(createI2CInterface())};
171 EXPECT_EQ(device.getFRU(), "/system/chassis/motherboard/reg2");
172}
173
174TEST(DeviceTests, GetI2CInterface)
175{
176 std::unique_ptr<i2c::I2CInterface> i2cInterface = createI2CInterface();
177 i2c::I2CInterface* i2cInterfacePtr = i2cInterface.get();
178 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
179 std::move(i2cInterface)};
180 EXPECT_EQ(&(device.getI2CInterface()), i2cInterfacePtr);
Shawn McCarneya2461b32019-10-24 18:53:01 -0500181}
Shawn McCarney0b1a0e72020-03-11 18:01:44 -0500182
183TEST(DeviceTests, GetID)
184{
185 Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2",
186 std::move(createI2CInterface())};
187 EXPECT_EQ(device.getID(), "vdd_reg");
188}
189
190TEST(DeviceTests, GetPresenceDetection)
191{
192 // Test where PresenceDetection was not specified in constructor
193 {
194 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
195 std::move(createI2CInterface())};
196 EXPECT_EQ(device.getPresenceDetection(), nullptr);
197 }
198
199 // Test where PresenceDetection was specified in constructor
200 {
201 // Create PresenceDetection
202 std::vector<std::unique_ptr<Action>> actions{};
203 actions.push_back(std::make_unique<MockAction>());
204 std::unique_ptr<PresenceDetection> presenceDetection =
205 std::make_unique<PresenceDetection>(std::move(actions));
206
207 // Create Device
208 Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2",
209 std::move(createI2CInterface()),
210 std::move(presenceDetection)};
211 EXPECT_NE(device.getPresenceDetection(), nullptr);
212 EXPECT_EQ(device.getPresenceDetection()->getActions().size(), 1);
213 }
214}
215
216TEST(DeviceTests, GetRails)
217{
218 // Test where no rails were specified in constructor
219 {
220 Device device{"vdd_reg", true, "/system/chassis/motherboard/reg2",
221 std::move(createI2CInterface())};
222 EXPECT_EQ(device.getRails().size(), 0);
223 }
224
225 // Test where rails were specified in constructor
226 {
227 std::unique_ptr<PresenceDetection> presenceDetection{};
228 std::unique_ptr<Configuration> configuration{};
229
230 // Create vector of Rail objects
231 std::vector<std::unique_ptr<Rail>> rails{};
232 rails.push_back(std::make_unique<Rail>("vdd0"));
233 rails.push_back(std::make_unique<Rail>("vdd1"));
234
235 // Create Device
236 Device device{"vdd_reg",
237 false,
238 "/system/chassis/motherboard/reg2",
239 std::move(createI2CInterface()),
240 std::move(presenceDetection),
241 std::move(configuration),
242 std::move(rails)};
243 EXPECT_EQ(device.getRails().size(), 2);
244 EXPECT_EQ(device.getRails()[0]->getID(), "vdd0");
245 EXPECT_EQ(device.getRails()[1]->getID(), "vdd1");
246 }
247}
248
249TEST(DeviceTests, IsRegulator)
250{
251 Device device{"vdd_reg", false, "/system/chassis/motherboard/reg2",
252 std::move(createI2CInterface())};
253 EXPECT_EQ(device.isRegulator(), false);
254}