blob: b8d56ffb7bf2ccd190f8ddc8d8672c71bb99bcb6 [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"
Shawn McCarney32252592021-09-08 15:29:36 -050027#include "phase_fault_detection.hpp"
Bob King833b8e02020-06-11 14:56:38 +080028#include "pmbus_read_sensor_action.hpp"
29#include "pmbus_utils.hpp"
30#include "presence_detection.hpp"
31#include "rail.hpp"
32#include "rule.hpp"
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050033#include "sensor_monitoring.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050034#include "sensors.hpp"
Bob King833b8e02020-06-11 14:56:38 +080035#include "system.hpp"
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050036
Bob King833b8e02020-06-11 14:56:38 +080037#include <cstdint>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050038#include <memory>
Bob King833b8e02020-06-11 14:56:38 +080039#include <optional>
Shawn McCarney17bac892021-05-08 07:55:52 -050040#include <string>
41#include <tuple>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050042#include <utility>
43#include <vector>
44
Bob King833b8e02020-06-11 14:56:38 +080045#include <gmock/gmock.h>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050046#include <gtest/gtest.h>
47
48using namespace phosphor::power::regulators;
Bob King833b8e02020-06-11 14:56:38 +080049using namespace phosphor::power::regulators::pmbus_utils;
50
51using ::testing::A;
Shawn McCarney81a2f902021-03-23 21:41:34 -050052using ::testing::Ref;
Bob King833b8e02020-06-11 14:56:38 +080053using ::testing::Return;
Shawn McCarney17bac892021-05-08 07:55:52 -050054using ::testing::SetArgReferee;
Bob King833b8e02020-06-11 14:56:38 +080055using ::testing::Throw;
56using ::testing::TypedEq;
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050057
Shawn McCarney17bac892021-05-08 07:55:52 -050058/**
59 * Creates the parent objects that normally contain a SensorMonitoring object.
60 *
61 * A SensorMonitoring object is normally contained within a hierarchy of System,
62 * Chassis, Device, and Rail objects. These objects are required in order to
63 * call the execute() method.
64 *
65 * Creates the System, Chassis, Device, and Rail objects. The SensorMonitoring
66 * object is moved into the Rail object.
67 *
68 * @param monitoring SensorMonitoring object to move into object hierarchy
69 * @return Tuple containing pointers the parent objects and the
70 * MockedI2CInterface object. They are all contained within the System
71 * object and will be automatically destructed.
72 */
73std::tuple<std::unique_ptr<System>, Chassis*, Device*, i2c::MockedI2CInterface*,
74 Rail*>
75 createParentObjects(std::unique_ptr<SensorMonitoring> monitoring)
76{
77 // Create Rail that contains SensorMonitoring
78 std::unique_ptr<Configuration> configuration{};
79 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
80 "vdd", std::move(configuration), std::move(monitoring));
81 Rail* railPtr = rail.get();
82
83 // Create mock I2CInterface
84 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
85 std::make_unique<i2c::MockedI2CInterface>();
86 i2c::MockedI2CInterface* i2cInterfacePtr = i2cInterface.get();
87
88 // Create Device that contains Rail
89 std::unique_ptr<PresenceDetection> presenceDetection{};
90 std::unique_ptr<Configuration> deviceConfiguration{};
Shawn McCarney32252592021-09-08 15:29:36 -050091 std::unique_ptr<PhaseFaultDetection> phaseFaultDetection{};
Shawn McCarney17bac892021-05-08 07:55:52 -050092 std::vector<std::unique_ptr<Rail>> rails{};
93 rails.emplace_back(std::move(rail));
94 std::unique_ptr<Device> device = std::make_unique<Device>(
95 "vdd_reg", true,
96 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2",
97 std::move(i2cInterface), std::move(presenceDetection),
Shawn McCarney32252592021-09-08 15:29:36 -050098 std::move(deviceConfiguration), std::move(phaseFaultDetection),
99 std::move(rails));
Shawn McCarney17bac892021-05-08 07:55:52 -0500100 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 = std::make_unique<Chassis>(
106 1, "/xyz/openbmc_project/inventory/system/chassis", 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 std::unique_ptr<System> system =
114 std::make_unique<System>(std::move(rules), std::move(chassisVec));
115
116 return std::make_tuple(std::move(system), chassisPtr, devicePtr,
117 i2cInterfacePtr, railPtr);
118}
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500119
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500120TEST(SensorMonitoringTests, Constructor)
121{
122 std::vector<std::unique_ptr<Action>> actions{};
123 actions.push_back(std::make_unique<MockAction>());
124
125 SensorMonitoring sensorMonitoring(std::move(actions));
126 EXPECT_EQ(sensorMonitoring.getActions().size(), 1);
127}
128
Shawn McCarney17bac892021-05-08 07:55:52 -0500129TEST(SensorMonitoringTests, ClearErrorHistory)
130{
131 // Create PMBusReadSensorAction
132 SensorType type{SensorType::iout};
133 uint8_t command{0x8C};
134 SensorDataFormat format{SensorDataFormat::linear_11};
135 std::optional<int8_t> exponent{};
136 std::unique_ptr<PMBusReadSensorAction> action =
137 std::make_unique<PMBusReadSensorAction>(type, command, format,
138 exponent);
139
140 // Create SensorMonitoring
141 std::vector<std::unique_ptr<Action>> actions{};
142 actions.emplace_back(std::move(action));
143 SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions));
144
145 // Create parent objects that contain SensorMonitoring
146 auto [system, chassis, device, i2cInterface, rail] =
147 createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring});
148
149 // Set I2CInterface expectations
150 EXPECT_CALL(*i2cInterface, isOpen).WillRepeatedly(Return(true));
151 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
152 .WillRepeatedly(Throw(
153 i2c::I2CException{"Failed to read word data", "/dev/i2c-1", 0x70}));
154
155 // Perform sensor monitoring 10 times to set error history data members
156 {
157 // Create mock services
158 MockServices services{};
159
160 // Set Sensors service expectations. SensorMonitoring will be executed
161 // 10 times.
162 MockSensors& sensors = services.getMockSensors();
163 EXPECT_CALL(sensors, startRail).Times(10);
164 EXPECT_CALL(sensors, setValue).Times(0);
165 EXPECT_CALL(sensors, endRail(true)).Times(10);
166
167 // Set Journal service expectations. SensorMonitoring should log error
168 // messages 3 times and then stop.
169 MockJournal& journal = services.getMockJournal();
170 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
171 .Times(3);
172 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(3);
173
174 // Set ErrorLogging service expectations. SensorMonitoring should log
175 // an error only once.
176 MockErrorLogging& errorLogging = services.getMockErrorLogging();
177 EXPECT_CALL(errorLogging, logI2CError).Times(1);
178
179 // Execute SensorMonitoring 10 times
180 for (int i = 1; i <= 10; ++i)
181 {
182 monitoring->execute(services, *system, *chassis, *device, *rail);
183 }
184 }
185
186 // Clear error history
187 monitoring->clearErrorHistory();
188
189 // Perform sensor monitoring one more time. Should log errors again.
190 {
191 // Create mock services
192 MockServices services{};
193
194 // Set Sensors service expectations. SensorMonitoring will be executed
195 // 1 time.
196 MockSensors& sensors = services.getMockSensors();
197 EXPECT_CALL(sensors, startRail).Times(1);
198 EXPECT_CALL(sensors, setValue).Times(0);
199 EXPECT_CALL(sensors, endRail(true)).Times(1);
200
201 // Set Journal service expectations. SensorMonitoring should log error
202 // messages 1 time.
203 MockJournal& journal = services.getMockJournal();
204 EXPECT_CALL(journal, logError(A<const std::vector<std::string>&>()))
205 .Times(1);
206 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(1);
207
208 // Set ErrorLogging server expectations. SensorMonitoring should log an
209 // error.
210 MockErrorLogging& errorLogging = services.getMockErrorLogging();
211 EXPECT_CALL(errorLogging, logI2CError).Times(1);
212
213 // Execute SensorMonitoring
214 monitoring->execute(services, *system, *chassis, *device, *rail);
215 }
216}
217
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500218TEST(SensorMonitoringTests, Execute)
219{
Bob King833b8e02020-06-11 14:56:38 +0800220 // Test where works
221 {
222 // Create PMBusReadSensorAction
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500223 SensorType type{SensorType::iout};
Shawn McCarney17bac892021-05-08 07:55:52 -0500224 uint8_t command{0x8C};
225 SensorDataFormat format{SensorDataFormat::linear_11};
Bob King833b8e02020-06-11 14:56:38 +0800226 std::optional<int8_t> exponent{};
227 std::unique_ptr<PMBusReadSensorAction> action =
228 std::make_unique<PMBusReadSensorAction>(type, command, format,
229 exponent);
230
Bob King833b8e02020-06-11 14:56:38 +0800231 // Create SensorMonitoring
232 std::vector<std::unique_ptr<Action>> actions{};
233 actions.emplace_back(std::move(action));
Shawn McCarney17bac892021-05-08 07:55:52 -0500234 SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions));
Bob King833b8e02020-06-11 14:56:38 +0800235
Shawn McCarney17bac892021-05-08 07:55:52 -0500236 // Create parent objects that contain SensorMonitoring
237 auto [system, chassis, device, i2cInterface, rail] =
238 createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring});
Bob King833b8e02020-06-11 14:56:38 +0800239
Shawn McCarney17bac892021-05-08 07:55:52 -0500240 // Set I2CInterface expectations
241 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
242 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
243 .Times(1)
244 .WillOnce(SetArgReferee<1>(0xD2E0));
Bob King833b8e02020-06-11 14:56:38 +0800245
Shawn McCarney17bac892021-05-08 07:55:52 -0500246 // Create mock services. Set Sensors service expectations.
247 MockServices services{};
248 MockSensors& sensors = services.getMockSensors();
249 EXPECT_CALL(sensors,
250 startRail("vdd",
251 "/xyz/openbmc_project/inventory/system/chassis/"
252 "motherboard/reg2",
253 "/xyz/openbmc_project/inventory/system/chassis"))
254 .Times(1);
255 EXPECT_CALL(sensors, setValue(SensorType::iout, 11.5)).Times(1);
256 EXPECT_CALL(sensors, endRail(false)).Times(1);
Bob King833b8e02020-06-11 14:56:38 +0800257
Shawn McCarney17bac892021-05-08 07:55:52 -0500258 // Execute SensorMonitoring
259 monitoring->execute(services, *system, *chassis, *device, *rail);
Bob King833b8e02020-06-11 14:56:38 +0800260 }
261
262 // Test where fails
263 {
Shawn McCarney17bac892021-05-08 07:55:52 -0500264 // Create PMBusReadSensorAction
265 SensorType type{SensorType::iout};
266 uint8_t command{0x8C};
267 SensorDataFormat format{SensorDataFormat::linear_11};
268 std::optional<int8_t> exponent{};
269 std::unique_ptr<PMBusReadSensorAction> action =
270 std::make_unique<PMBusReadSensorAction>(type, command, format,
271 exponent);
272
273 // Create SensorMonitoring
274 std::vector<std::unique_ptr<Action>> actions{};
275 actions.emplace_back(std::move(action));
276 SensorMonitoring* monitoring = new SensorMonitoring(std::move(actions));
277
278 // Create parent objects that contain SensorMonitoring
279 auto [system, chassis, device, i2cInterface, rail] =
280 createParentObjects(std::unique_ptr<SensorMonitoring>{monitoring});
281
282 // Set I2CInterface expectations. Should read register 0x8C 4 times.
283 EXPECT_CALL(*i2cInterface, isOpen)
284 .Times(4)
285 .WillRepeatedly(Return(true));
286 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
287 .Times(4)
288 .WillRepeatedly(Throw(i2c::I2CException{"Failed to read word data",
289 "/dev/i2c-1", 0x70}));
290
291 // Create mock services
Bob King8a552922020-08-05 17:02:31 +0800292 MockServices services{};
Shawn McCarney17bac892021-05-08 07:55:52 -0500293
294 // Set Sensors service expectations. SensorMonitoring will be executed
295 // 4 times, and all should fail.
296 MockSensors& sensors = services.getMockSensors();
297 EXPECT_CALL(sensors,
298 startRail("vdd",
299 "/xyz/openbmc_project/inventory/system/chassis/"
300 "motherboard/reg2",
301 "/xyz/openbmc_project/inventory/system/chassis"))
302 .Times(4);
303 EXPECT_CALL(sensors, setValue).Times(0);
304 EXPECT_CALL(sensors, endRail(true)).Times(4);
305
306 // Set Journal service expectations. SensorMonitoring should log error
307 // messages 3 times and then stop.
Bob King8a552922020-08-05 17:02:31 +0800308 MockJournal& journal = services.getMockJournal();
Bob King8a552922020-08-05 17:02:31 +0800309 std::vector<std::string> expectedErrMessagesException{
Shawn McCarney17bac892021-05-08 07:55:52 -0500310 "I2CException: Failed to read word data: bus /dev/i2c-1, addr 0x70",
Bob King8a552922020-08-05 17:02:31 +0800311 "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
312 "format: linear_11 }"};
Shawn McCarney17bac892021-05-08 07:55:52 -0500313 EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(3);
314 EXPECT_CALL(journal, logError("Unable to monitor sensors for rail vdd"))
315 .Times(3);
316
317 // Set ErrorLogging service expectations. SensorMonitoring should log
318 // an error only once.
319 MockErrorLogging& errorLogging = services.getMockErrorLogging();
Shawn McCarney81a2f902021-03-23 21:41:34 -0500320 EXPECT_CALL(errorLogging,
321 logI2CError(Entry::Level::Warning, Ref(journal),
322 "/dev/i2c-1", 0x70, 0))
323 .Times(1);
Bob King8a552922020-08-05 17:02:31 +0800324
Shawn McCarney17bac892021-05-08 07:55:52 -0500325 // Execute SensorMonitoring 4 times
326 for (int i = 1; i <= 4; ++i)
327 {
328 monitoring->execute(services, *system, *chassis, *device, *rail);
329 }
Bob King833b8e02020-06-11 14:56:38 +0800330 }
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500331}
332
333TEST(SensorMonitoringTests, GetActions)
334{
335 std::vector<std::unique_ptr<Action>> actions{};
336
337 MockAction* action1 = new MockAction{};
338 actions.push_back(std::unique_ptr<MockAction>{action1});
339
340 MockAction* action2 = new MockAction{};
341 actions.push_back(std::unique_ptr<MockAction>{action2});
342
343 SensorMonitoring sensorMonitoring(std::move(actions));
344 EXPECT_EQ(sensorMonitoring.getActions().size(), 2);
345 EXPECT_EQ(sensorMonitoring.getActions()[0].get(), action1);
346 EXPECT_EQ(sensorMonitoring.getActions()[1].get(), action2);
347}