blob: 5781973bf58fda12ba0c274ff52d50db80d470df [file] [log] [blame]
Shawn McCarneya2461b32019-10-24 18:53:01 -05001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Shawn McCarney4bf310e2020-03-10 17:48:11 -050016#include "action.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050017#include "chassis.hpp"
Shawn McCarney4bf310e2020-03-10 17:48:11 -050018#include "configuration.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050019#include "device.hpp"
20#include "i2c_interface.hpp"
21#include "journal.hpp"
Shawn McCarney4bf310e2020-03-10 17:48:11 -050022#include "mock_action.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050023#include "mock_journal.hpp"
24#include "mocked_i2c_interface.hpp"
25#include "presence_detection.hpp"
Shawn McCarneya2461b32019-10-24 18:53:01 -050026#include "rail.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050027#include "rule.hpp"
Shawn McCarney4bf310e2020-03-10 17:48:11 -050028#include "sensor_monitoring.hpp"
Shawn McCarney779b9562020-04-13 17:05:45 -050029#include "system.hpp"
Shawn McCarney4bf310e2020-03-10 17:48:11 -050030
31#include <memory>
32#include <optional>
33#include <utility>
34#include <vector>
Shawn McCarneya2461b32019-10-24 18:53:01 -050035
36#include <gtest/gtest.h>
37
38using namespace phosphor::power::regulators;
39
Shawn McCarney779b9562020-04-13 17:05:45 -050040using ::testing::Return;
41
Shawn McCarneya2461b32019-10-24 18:53:01 -050042TEST(RailTests, Constructor)
43{
Shawn McCarney4bf310e2020-03-10 17:48:11 -050044 // Test where only required parameters are specified
45 {
46 Rail rail{"vdd0"};
47 EXPECT_EQ(rail.getID(), "vdd0");
48 EXPECT_EQ(rail.getConfiguration(), nullptr);
49 EXPECT_EQ(rail.getSensorMonitoring(), nullptr);
50 }
51
52 // Test where all parameters are specified
53 {
54 // Create Configuration
55 std::optional<double> volts{1.3};
56 std::vector<std::unique_ptr<Action>> actions{};
57 actions.push_back(std::make_unique<MockAction>());
58 actions.push_back(std::make_unique<MockAction>());
59 std::unique_ptr<Configuration> configuration =
60 std::make_unique<Configuration>(volts, std::move(actions));
61
62 // Create SensorMonitoring
63 actions.clear();
64 actions.push_back(std::make_unique<MockAction>());
65 std::unique_ptr<SensorMonitoring> sensorMonitoring =
66 std::make_unique<SensorMonitoring>(std::move(actions));
67
68 // Create Rail
69 Rail rail{"vddr1", std::move(configuration),
70 std::move(sensorMonitoring)};
71 EXPECT_EQ(rail.getID(), "vddr1");
72 EXPECT_NE(rail.getConfiguration(), nullptr);
73 EXPECT_EQ(rail.getConfiguration()->getVolts().has_value(), true);
74 EXPECT_EQ(rail.getConfiguration()->getVolts().value(), 1.3);
75 EXPECT_EQ(rail.getConfiguration()->getActions().size(), 2);
76 EXPECT_NE(rail.getSensorMonitoring(), nullptr);
77 EXPECT_EQ(rail.getSensorMonitoring()->getActions().size(), 1);
78 }
79}
80
Shawn McCarney779b9562020-04-13 17:05:45 -050081TEST(RailTests, Configure)
82{
83 // Test where Configuration was not specified in constructor
84 {
85 // Create Rail
86 std::unique_ptr<Rail> rail = std::make_unique<Rail>("vdd0");
87 Rail* railPtr = rail.get();
88
89 // Create Device that contains Rail
90 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
91 std::make_unique<i2c::MockedI2CInterface>();
92 std::unique_ptr<PresenceDetection> presenceDetection{};
93 std::unique_ptr<Configuration> deviceConfiguration{};
94 std::vector<std::unique_ptr<Rail>> rails{};
95 rails.emplace_back(std::move(rail));
96 std::unique_ptr<Device> device = std::make_unique<Device>(
97 "reg1", true, "/system/chassis/motherboard/reg1",
98 std::move(i2cInterface), std::move(presenceDetection),
99 std::move(deviceConfiguration), std::move(rails));
100 Device* devicePtr = device.get();
101
102 // Create Chassis that contains Device
103 std::vector<std::unique_ptr<Device>> devices{};
104 devices.emplace_back(std::move(device));
105 std::unique_ptr<Chassis> chassis =
106 std::make_unique<Chassis>(1, std::move(devices));
107 Chassis* chassisPtr = chassis.get();
108
109 // Create System that contains Chassis
110 std::vector<std::unique_ptr<Rule>> rules{};
111 std::vector<std::unique_ptr<Chassis>> chassisVec{};
112 chassisVec.emplace_back(std::move(chassis));
113 System system{std::move(rules), std::move(chassisVec)};
114
115 // Call configure(). Should do nothing.
116 journal::clear();
117 railPtr->configure(system, *chassisPtr, *devicePtr);
118 EXPECT_EQ(journal::getDebugMessages().size(), 0);
119 EXPECT_EQ(journal::getErrMessages().size(), 0);
120 }
121
122 // Test where Configuration was specified in constructor
123 {
124 // Create Configuration
125 std::optional<double> volts{1.3};
126 std::unique_ptr<MockAction> action = std::make_unique<MockAction>();
127 EXPECT_CALL(*action, execute).Times(1).WillOnce(Return(true));
128 std::vector<std::unique_ptr<Action>> actions{};
129 actions.emplace_back(std::move(action));
130 std::unique_ptr<Configuration> configuration =
131 std::make_unique<Configuration>(volts, std::move(actions));
132
133 // Create Rail
134 std::unique_ptr<Rail> rail =
135 std::make_unique<Rail>("vddr1", std::move(configuration));
136 Rail* railPtr = rail.get();
137
138 // Create Device that contains Rail
139 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
140 std::make_unique<i2c::MockedI2CInterface>();
141 std::unique_ptr<PresenceDetection> presenceDetection{};
142 std::unique_ptr<Configuration> deviceConfiguration{};
143 std::vector<std::unique_ptr<Rail>> rails{};
144 rails.emplace_back(std::move(rail));
145 std::unique_ptr<Device> device = std::make_unique<Device>(
146 "reg1", true, "/system/chassis/motherboard/reg1",
147 std::move(i2cInterface), std::move(presenceDetection),
148 std::move(deviceConfiguration), std::move(rails));
149 Device* devicePtr = device.get();
150
151 // Create Chassis that contains Device
152 std::vector<std::unique_ptr<Device>> devices{};
153 devices.emplace_back(std::move(device));
154 std::unique_ptr<Chassis> chassis =
155 std::make_unique<Chassis>(1, std::move(devices));
156 Chassis* chassisPtr = chassis.get();
157
158 // Create System that contains Chassis
159 std::vector<std::unique_ptr<Rule>> rules{};
160 std::vector<std::unique_ptr<Chassis>> chassisVec{};
161 chassisVec.emplace_back(std::move(chassis));
162 System system{std::move(rules), std::move(chassisVec)};
163
164 // Call configure(). Should execute Configuration and log debug message
165 // to journal.
166 journal::clear();
167 railPtr->configure(system, *chassisPtr, *devicePtr);
168 std::vector<std::string> expectedDebugMessages{
169 "Configuring vddr1: volts=1.300000"};
170 EXPECT_EQ(journal::getDebugMessages(), expectedDebugMessages);
171 EXPECT_EQ(journal::getErrMessages().size(), 0);
172 }
173}
174
Shawn McCarney4bf310e2020-03-10 17:48:11 -0500175TEST(RailTests, GetConfiguration)
176{
177 // Test where Configuration was not specified in constructor
178 {
179 Rail rail{"vdd0"};
180 EXPECT_EQ(rail.getConfiguration(), nullptr);
181 }
182
183 // Test where Configuration was specified in constructor
184 {
185 // Create Configuration
186 std::optional<double> volts{3.2};
187 std::vector<std::unique_ptr<Action>> actions{};
188 actions.push_back(std::make_unique<MockAction>());
189 std::unique_ptr<Configuration> configuration =
190 std::make_unique<Configuration>(volts, std::move(actions));
191
192 // Create Rail
193 Rail rail{"vddr1", std::move(configuration)};
194 EXPECT_NE(rail.getConfiguration(), nullptr);
195 EXPECT_EQ(rail.getConfiguration()->getVolts().has_value(), true);
196 EXPECT_EQ(rail.getConfiguration()->getVolts().value(), 3.2);
197 EXPECT_EQ(rail.getConfiguration()->getActions().size(), 1);
198 }
Shawn McCarneya2461b32019-10-24 18:53:01 -0500199}
200
Shawn McCarney4afb2852019-10-27 18:28:53 -0500201TEST(RailTests, GetID)
Shawn McCarneya2461b32019-10-24 18:53:01 -0500202{
Shawn McCarney4bf310e2020-03-10 17:48:11 -0500203 Rail rail{"vio2"};
Shawn McCarney4afb2852019-10-27 18:28:53 -0500204 EXPECT_EQ(rail.getID(), "vio2");
Shawn McCarneya2461b32019-10-24 18:53:01 -0500205}
Shawn McCarney4bf310e2020-03-10 17:48:11 -0500206
207TEST(RailTests, GetSensorMonitoring)
208{
209 // Test where SensorMonitoring was not specified in constructor
210 {
211 Rail rail{"vdd0", nullptr, nullptr};
212 EXPECT_EQ(rail.getSensorMonitoring(), nullptr);
213 }
214
215 // Test where SensorMonitoring was specified in constructor
216 {
217 std::unique_ptr<Configuration> configuration{};
218
219 // Create SensorMonitoring
220 std::vector<std::unique_ptr<Action>> actions{};
221 actions.push_back(std::make_unique<MockAction>());
222 actions.push_back(std::make_unique<MockAction>());
223 std::unique_ptr<SensorMonitoring> sensorMonitoring =
224 std::make_unique<SensorMonitoring>(std::move(actions));
225
226 // Create Rail
227 Rail rail{"vddr1", std::move(configuration),
228 std::move(sensorMonitoring)};
229 EXPECT_NE(rail.getSensorMonitoring(), nullptr);
230 EXPECT_EQ(rail.getSensorMonitoring()->getActions().size(), 2);
231 }
232}