blob: 6ed7e86380db4c6a0860d1509c9b61108d2dd8d1 [file] [log] [blame]
Shawn McCarneybc47c1b2020-03-10 15:38:07 -05001/**
2 * Copyright © 2020 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 */
16#include "action.hpp"
Bob King833b8e02020-06-11 14:56:38 +080017#include "chassis.hpp"
18#include "configuration.hpp"
19#include "device.hpp"
20#include "i2c_interface.hpp"
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050021#include "mock_action.hpp"
Shawn McCarney81a2f902021-03-23 21:41:34 -050022#include "mock_error_logging.hpp"
Bob King833b8e02020-06-11 14:56:38 +080023#include "mock_journal.hpp"
Shawn McCarney17bac892021-05-08 07:55:52 -050024#include "mock_sensors.hpp"
Bob King8a552922020-08-05 17:02:31 +080025#include "mock_services.hpp"
Bob King833b8e02020-06-11 14:56:38 +080026#include "mocked_i2c_interface.hpp"
27#include "pmbus_read_sensor_action.hpp"
28#include "pmbus_utils.hpp"
29#include "presence_detection.hpp"
30#include "rail.hpp"
31#include "rule.hpp"
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050032#include "sensor_monitoring.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050033#include "sensors.hpp"
Bob King833b8e02020-06-11 14:56:38 +080034#include "system.hpp"
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050035
Bob King833b8e02020-06-11 14:56:38 +080036#include <cstdint>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050037#include <memory>
Bob King833b8e02020-06-11 14:56:38 +080038#include <optional>
Shawn McCarney17bac892021-05-08 07:55:52 -050039#include <string>
40#include <tuple>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050041#include <utility>
42#include <vector>
43
Bob King833b8e02020-06-11 14:56:38 +080044#include <gmock/gmock.h>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050045#include <gtest/gtest.h>
46
47using namespace phosphor::power::regulators;
Bob King833b8e02020-06-11 14:56:38 +080048using namespace phosphor::power::regulators::pmbus_utils;
49
50using ::testing::A;
Shawn McCarney81a2f902021-03-23 21:41:34 -050051using ::testing::Ref;
Bob King833b8e02020-06-11 14:56:38 +080052using ::testing::Return;
Shawn McCarney17bac892021-05-08 07:55:52 -050053using ::testing::SetArgReferee;
Bob King833b8e02020-06-11 14:56:38 +080054using ::testing::Throw;
55using ::testing::TypedEq;
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050056
Shawn McCarney17bac892021-05-08 07:55:52 -050057/**
58 * Creates the parent objects that normally contain a SensorMonitoring object.
59 *
60 * A SensorMonitoring object is normally contained within a hierarchy of System,
61 * Chassis, Device, and Rail objects. These objects are required in order to
62 * call the execute() method.
63 *
64 * Creates the System, Chassis, Device, and Rail objects. The SensorMonitoring
65 * object is moved into the Rail object.
66 *
67 * @param monitoring SensorMonitoring object to move into object hierarchy
68 * @return Tuple containing pointers the parent objects and the
69 * MockedI2CInterface object. They are all contained within the System
70 * object and will be automatically destructed.
71 */
72std::tuple<std::unique_ptr<System>, Chassis*, Device*, i2c::MockedI2CInterface*,
73 Rail*>
74 createParentObjects(std::unique_ptr<SensorMonitoring> monitoring)
75{
76 // Create Rail that contains SensorMonitoring
77 std::unique_ptr<Configuration> configuration{};
78 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
79 "vdd", std::move(configuration), std::move(monitoring));
80 Rail* railPtr = rail.get();
81
82 // Create mock I2CInterface
83 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
84 std::make_unique<i2c::MockedI2CInterface>();
85 i2c::MockedI2CInterface* i2cInterfacePtr = i2cInterface.get();
86
87 // Create Device that contains Rail
88 std::unique_ptr<PresenceDetection> presenceDetection{};
89 std::unique_ptr<Configuration> deviceConfiguration{};
90 std::vector<std::unique_ptr<Rail>> rails{};
91 rails.emplace_back(std::move(rail));
92 std::unique_ptr<Device> device = std::make_unique<Device>(
93 "vdd_reg", true,
94 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
95 std::move(i2cInterface), std::move(presenceDetection),
96 std::move(deviceConfiguration), std::move(rails));
97 Device* devicePtr = device.get();
98
99 // Create Chassis that contains Device
100 std::vector<std::unique_ptr<Device>> devices{};
101 devices.emplace_back(std::move(device));
102 std::unique_ptr<Chassis> chassis = std::make_unique<Chassis>(
103 1, "/xyz/openbmc_project/inventory/system/chassis", std::move(devices));
104 Chassis* chassisPtr = chassis.get();
105
106 // Create System that contains Chassis
107 std::vector<std::unique_ptr<Rule>> rules{};
108 std::vector<std::unique_ptr<Chassis>> chassisVec{};
109 chassisVec.emplace_back(std::move(chassis));
110 std::unique_ptr<System> system =
111 std::make_unique<System>(std::move(rules), std::move(chassisVec));
112
113 return std::make_tuple(std::move(system), chassisPtr, devicePtr,
114 i2cInterfacePtr, railPtr);
115}
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500116
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500117TEST(SensorMonitoringTests, Constructor)
118{
119 std::vector<std::unique_ptr<Action>> actions{};
120 actions.push_back(std::make_unique<MockAction>());
121
122 SensorMonitoring sensorMonitoring(std::move(actions));
123 EXPECT_EQ(sensorMonitoring.getActions().size(), 1);
124}
125
Shawn McCarney17bac892021-05-08 07:55:52 -0500126TEST(SensorMonitoringTests, ClearErrorHistory)
127{
128 // Create PMBusReadSensorAction
129 SensorType type{SensorType::iout};
130 uint8_t command{0x8C};
131 SensorDataFormat format{SensorDataFormat::linear_11};
132 std::optional<int8_t> exponent{};
133 std::unique_ptr<PMBusReadSensorAction> action =
134 std::make_unique<PMBusReadSensorAction>(type, command, format,
135 exponent);
136
137 // Create SensorMonitoring
138 std::vector<std::unique_ptr<Action>> actions{};
139 actions.emplace_back(std::move(action));
140 SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions));
141
142 // Create parent objects that contain SensorMonitoring
143 auto [system, chassis, device, i2cInterface, rail] =
144 createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring});
145
146 // Set I2CInterface expectations
147 EXPECT_CALL(*i2cInterface, isOpen).WillRepeatedly(Return(true));
148 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
149 .WillRepeatedly(Throw(
150 i2c::I2CException{"Failed to read word data", "/dev/i2c-1", 0x70}));
151
152 // Perform sensor monitoring 10 times to set error history data members
153 {
154 // Create mock services
155 MockServices services{};
156
157 // Set Sensors service expectations. SensorMonitoring will be executed
158 // 10 times.
159 MockSensors& sensors = services.getMockSensors();
160 EXPECT_CALL(sensors, startRail).Times(10);
161 EXPECT_CALL(sensors, setValue).Times(0);
162 EXPECT_CALL(sensors, endRail(true)).Times(10);
163
164 // Set Journal service expectations. SensorMonitoring should log error
165 // messages 3 times and then stop.
166 MockJournal& journal = services.getMockJournal();
167 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
168 .Times(3);
169 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(3);
170
171 // Set ErrorLogging service expectations. SensorMonitoring should log
172 // an error only once.
173 MockErrorLogging& errorLogging = services.getMockErrorLogging();
174 EXPECT_CALL(errorLogging, logI2CError).Times(1);
175
176 // Execute SensorMonitoring 10 times
177 for (int i = 1; i <= 10; ++i)
178 {
179 monitoring->execute(services, *system, *chassis, *device, *rail);
180 }
181 }
182
183 // Clear error history
184 monitoring->clearErrorHistory();
185
186 // Perform sensor monitoring one more time. Should log errors again.
187 {
188 // Create mock services
189 MockServices services{};
190
191 // Set Sensors service expectations. SensorMonitoring will be executed
192 // 1 time.
193 MockSensors& sensors = services.getMockSensors();
194 EXPECT_CALL(sensors, startRail).Times(1);
195 EXPECT_CALL(sensors, setValue).Times(0);
196 EXPECT_CALL(sensors, endRail(true)).Times(1);
197
198 // Set Journal service expectations. SensorMonitoring should log error
199 // messages 1 time.
200 MockJournal& journal = services.getMockJournal();
201 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
202 .Times(1);
203 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(1);
204
205 // Set ErrorLogging server expectations. SensorMonitoring should log an
206 // error.
207 MockErrorLogging& errorLogging = services.getMockErrorLogging();
208 EXPECT_CALL(errorLogging, logI2CError).Times(1);
209
210 // Execute SensorMonitoring
211 monitoring->execute(services, *system, *chassis, *device, *rail);
212 }
213}
214
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500215TEST(SensorMonitoringTests, Execute)
216{
Bob King833b8e02020-06-11 14:56:38 +0800217 // Test where works
218 {
219 // Create PMBusReadSensorAction
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500220 SensorType type{SensorType::iout};
Shawn McCarney17bac892021-05-08 07:55:52 -0500221 uint8_t command{0x8C};
222 SensorDataFormat format{SensorDataFormat::linear_11};
Bob King833b8e02020-06-11 14:56:38 +0800223 std::optional<int8_t> exponent{};
224 std::unique_ptr<PMBusReadSensorAction> action =
225 std::make_unique<PMBusReadSensorAction>(type, command, format,
226 exponent);
227
Bob King833b8e02020-06-11 14:56:38 +0800228 // Create SensorMonitoring
229 std::vector<std::unique_ptr<Action>> actions{};
230 actions.emplace_back(std::move(action));
Shawn McCarney17bac892021-05-08 07:55:52 -0500231 SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions));
Bob King833b8e02020-06-11 14:56:38 +0800232
Shawn McCarney17bac892021-05-08 07:55:52 -0500233 // Create parent objects that contain SensorMonitoring
234 auto [system, chassis, device, i2cInterface, rail] =
235 createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring});
Bob King833b8e02020-06-11 14:56:38 +0800236
Shawn McCarney17bac892021-05-08 07:55:52 -0500237 // Set I2CInterface expectations
238 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
239 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
240 .Times(1)
241 .WillOnce(SetArgReferee<1>(0xD2E0));
Bob King833b8e02020-06-11 14:56:38 +0800242
Shawn McCarney17bac892021-05-08 07:55:52 -0500243 // Create mock services. Set Sensors service expectations.
244 MockServices services{};
245 MockSensors& sensors = services.getMockSensors();
246 EXPECT_CALL(sensors,
247 startRail("vdd",
248 "/xyz/openbmc_project/inventory/system/chassis/"
249 "motherboard/reg2",
250 "/xyz/openbmc_project/inventory/system/chassis"))
251 .Times(1);
252 EXPECT_CALL(sensors, setValue(SensorType::iout, 11.5)).Times(1);
253 EXPECT_CALL(sensors, endRail(false)).Times(1);
Bob King833b8e02020-06-11 14:56:38 +0800254
Shawn McCarney17bac892021-05-08 07:55:52 -0500255 // Execute SensorMonitoring
256 monitoring->execute(services, *system, *chassis, *device, *rail);
Bob King833b8e02020-06-11 14:56:38 +0800257 }
258
259 // Test where fails
260 {
Shawn McCarney17bac892021-05-08 07:55:52 -0500261 // Create PMBusReadSensorAction
262 SensorType type{SensorType::iout};
263 uint8_t command{0x8C};
264 SensorDataFormat format{SensorDataFormat::linear_11};
265 std::optional<int8_t> exponent{};
266 std::unique_ptr<PMBusReadSensorAction> action =
267 std::make_unique<PMBusReadSensorAction>(type, command, format,
268 exponent);
269
270 // Create SensorMonitoring
271 std::vector<std::unique_ptr<Action>> actions{};
272 actions.emplace_back(std::move(action));
273 SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions));
274
275 // Create parent objects that contain SensorMonitoring
276 auto [system, chassis, device, i2cInterface, rail] =
277 createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring});
278
279 // Set I2CInterface expectations. Should read register 0x8C 4 times.
280 EXPECT_CALL(*i2cInterface, isOpen)
281 .Times(4)
282 .WillRepeatedly(Return(true));
283 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
284 .Times(4)
285 .WillRepeatedly(Throw(i2c::I2CException{"Failed to read word data",
286 "/dev/i2c-1", 0x70}));
287
288 // Create mock services
Bob King8a552922020-08-05 17:02:31 +0800289 MockServices services{};
Shawn McCarney17bac892021-05-08 07:55:52 -0500290
291 // Set Sensors service expectations. SensorMonitoring will be executed
292 // 4 times, and all should fail.
293 MockSensors& sensors = services.getMockSensors();
294 EXPECT_CALL(sensors,
295 startRail("vdd",
296 "/xyz/openbmc_project/inventory/system/chassis/"
297 "motherboard/reg2",
298 "/xyz/openbmc_project/inventory/system/chassis"))
299 .Times(4);
300 EXPECT_CALL(sensors, setValue).Times(0);
301 EXPECT_CALL(sensors, endRail(true)).Times(4);
302
303 // Set Journal service expectations. SensorMonitoring should log error
304 // messages 3 times and then stop.
Bob King8a552922020-08-05 17:02:31 +0800305 MockJournal& journal = services.getMockJournal();
Bob King8a552922020-08-05 17:02:31 +0800306 std::vector<std::string> expectedErrMessagesException{
Shawn McCarney17bac892021-05-08 07:55:52 -0500307 "I2CException: Failed to read word data: bus /dev/i2c-1, addr 0x70",
Bob King8a552922020-08-05 17:02:31 +0800308 "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
309 "format: linear_11 }"};
Shawn McCarney17bac892021-05-08 07:55:52 -0500310 EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(3);
311 EXPECT_CALL(journal, logError("Unable to monitor sensors for rail vdd"))
312 .Times(3);
313
314 // Set ErrorLogging service expectations. SensorMonitoring should log
315 // an error only once.
316 MockErrorLogging& errorLogging = services.getMockErrorLogging();
Shawn McCarney81a2f902021-03-23 21:41:34 -0500317 EXPECT_CALL(errorLogging,
318 logI2CError(Entry::Level::Warning, Ref(journal),
319 "/dev/i2c-1", 0x70, 0))
320 .Times(1);
Bob King8a552922020-08-05 17:02:31 +0800321
Shawn McCarney17bac892021-05-08 07:55:52 -0500322 // Execute SensorMonitoring 4 times
323 for (int i = 1; i <= 4; ++i)
324 {
325 monitoring->execute(services, *system, *chassis, *device, *rail);
326 }
Bob King833b8e02020-06-11 14:56:38 +0800327 }
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500328}
329
330TEST(SensorMonitoringTests, GetActions)
331{
332 std::vector<std::unique_ptr<Action>> actions{};
333
334 MockAction* action1 = new MockAction{};
335 actions.push_back(std::unique_ptr<MockAction>{action1});
336
337 MockAction* action2 = new MockAction{};
338 actions.push_back(std::unique_ptr<MockAction>{action2});
339
340 SensorMonitoring sensorMonitoring(std::move(actions));
341 EXPECT_EQ(sensorMonitoring.getActions().size(), 2);
342 EXPECT_EQ(sensorMonitoring.getActions()[0].get(), action1);
343 EXPECT_EQ(sensorMonitoring.getActions()[1].get(), action2);
344}