blob: 4dfd415995d189a413f9b274095502160ae67d4c [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"
Bob King8a552922020-08-05 17:02:31 +080024#include "mock_services.hpp"
Bob King833b8e02020-06-11 14:56:38 +080025#include "mocked_i2c_interface.hpp"
26#include "pmbus_read_sensor_action.hpp"
27#include "pmbus_utils.hpp"
28#include "presence_detection.hpp"
29#include "rail.hpp"
30#include "rule.hpp"
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050031#include "sensor_monitoring.hpp"
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050032#include "sensors.hpp"
Bob King833b8e02020-06-11 14:56:38 +080033#include "system.hpp"
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050034
Bob King833b8e02020-06-11 14:56:38 +080035#include <cstdint>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050036#include <memory>
Bob King833b8e02020-06-11 14:56:38 +080037#include <optional>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050038#include <utility>
39#include <vector>
40
Bob King833b8e02020-06-11 14:56:38 +080041#include <gmock/gmock.h>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050042#include <gtest/gtest.h>
43
44using namespace phosphor::power::regulators;
Bob King833b8e02020-06-11 14:56:38 +080045using namespace phosphor::power::regulators::pmbus_utils;
46
47using ::testing::A;
Shawn McCarney81a2f902021-03-23 21:41:34 -050048using ::testing::Ref;
Bob King833b8e02020-06-11 14:56:38 +080049using ::testing::Return;
50using ::testing::Throw;
51using ::testing::TypedEq;
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050052
Shawn McCarneycb3f6a62021-04-30 10:54:30 -050053static const std::string chassisInvPath{
54 "/xyz/openbmc_project/inventory/system/chassis"};
55
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050056TEST(SensorMonitoringTests, Constructor)
57{
58 std::vector<std::unique_ptr<Action>> actions{};
59 actions.push_back(std::make_unique<MockAction>());
60
61 SensorMonitoring sensorMonitoring(std::move(actions));
62 EXPECT_EQ(sensorMonitoring.getActions().size(), 1);
63}
64
65TEST(SensorMonitoringTests, Execute)
66{
Bob King833b8e02020-06-11 14:56:38 +080067 // Test where works
68 {
Bob King8a552922020-08-05 17:02:31 +080069 // Create mock services. No logging should occur.
70 MockServices services{};
71 MockJournal& journal = services.getMockJournal();
72 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
73 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
74
Bob King833b8e02020-06-11 14:56:38 +080075 // Create PMBusReadSensorAction
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050076 SensorType type{SensorType::iout};
Bob King833b8e02020-06-11 14:56:38 +080077 uint8_t command = 0x8C;
78 pmbus_utils::SensorDataFormat format{
79 pmbus_utils::SensorDataFormat::linear_11};
80 std::optional<int8_t> exponent{};
81 std::unique_ptr<PMBusReadSensorAction> action =
82 std::make_unique<PMBusReadSensorAction>(type, command, format,
83 exponent);
84
85 // Create mock I2CInterface.
86 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
87 std::make_unique<i2c::MockedI2CInterface>();
88 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
89 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
90 .Times(1);
91
92 // Create SensorMonitoring
93 std::vector<std::unique_ptr<Action>> actions{};
94 actions.emplace_back(std::move(action));
95 std::unique_ptr<SensorMonitoring> sensorMonitoring =
96 std::make_unique<SensorMonitoring>(std::move(actions));
97 SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get();
98
99 // Create Rail that contains sensorMonitoring
100 std::unique_ptr<Configuration> configuration{};
101 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
102 "vio2", std::move(configuration), std::move(sensorMonitoring));
103 Rail* railPtr = rail.get();
104
105 // Create Device that contains Rail
106 std::unique_ptr<PresenceDetection> presenceDetection{};
107 std::unique_ptr<Configuration> deviceConfiguration{};
108 std::vector<std::unique_ptr<Rail>> rails{};
109 rails.emplace_back(std::move(rail));
110 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800111 "reg1", true,
112 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King833b8e02020-06-11 14:56:38 +0800113 std::move(i2cInterface), std::move(presenceDetection),
114 std::move(deviceConfiguration), std::move(rails));
115 Device* devicePtr = device.get();
116
117 // Create Chassis that contains Device
118 std::vector<std::unique_ptr<Device>> devices{};
119 devices.emplace_back(std::move(device));
120 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500121 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Bob King833b8e02020-06-11 14:56:38 +0800122 Chassis* chassisPtr = chassis.get();
123
124 // Create System that contains Chassis
125 std::vector<std::unique_ptr<Rule>> rules{};
126 std::vector<std::unique_ptr<Chassis>> chassisVec{};
127 chassisVec.emplace_back(std::move(chassis));
128 System system{std::move(rules), std::move(chassisVec)};
129
130 // Execute sensorMonitoring
Bob King8a552922020-08-05 17:02:31 +0800131 sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
132 *railPtr);
Bob King833b8e02020-06-11 14:56:38 +0800133 }
134
135 // Test where fails
136 {
Shawn McCarney81a2f902021-03-23 21:41:34 -0500137 // Create mock services. Expect logError() and logI2CError() to be
138 // called.
Bob King8a552922020-08-05 17:02:31 +0800139 MockServices services{};
Shawn McCarney81a2f902021-03-23 21:41:34 -0500140 MockErrorLogging& errorLogging = services.getMockErrorLogging();
Bob King8a552922020-08-05 17:02:31 +0800141 MockJournal& journal = services.getMockJournal();
142 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
143 std::vector<std::string> expectedErrMessagesException{
144 "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70",
145 "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
146 "format: linear_11 }"};
Bob King8a552922020-08-05 17:02:31 +0800147 EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1);
148 EXPECT_CALL(journal,
149 logError("Unable to monitor sensors for rail vio2"))
150 .Times(1);
Shawn McCarney81a2f902021-03-23 21:41:34 -0500151 EXPECT_CALL(errorLogging,
152 logI2CError(Entry::Level::Warning, Ref(journal),
153 "/dev/i2c-1", 0x70, 0))
154 .Times(1);
Bob King8a552922020-08-05 17:02:31 +0800155
Bob King833b8e02020-06-11 14:56:38 +0800156 // Create PMBusReadSensorAction
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500157 SensorType type{SensorType::iout};
Bob King833b8e02020-06-11 14:56:38 +0800158 uint8_t command = 0x8C;
159 pmbus_utils::SensorDataFormat format{
160 pmbus_utils::SensorDataFormat::linear_11};
161 std::optional<int8_t> exponent{};
162 std::unique_ptr<PMBusReadSensorAction> action =
163 std::make_unique<PMBusReadSensorAction>(type, command, format,
164 exponent);
165
166 // Create mock I2CInterface.
167 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
168 std::make_unique<i2c::MockedI2CInterface>();
169 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
170 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
171 .Times(1)
172 .WillOnce(Throw(
173 i2c::I2CException{"Failed to write byte", "/dev/i2c-1", 0x70}));
174
175 // Create SensorMonitoring
176 std::vector<std::unique_ptr<Action>> actions{};
177 actions.emplace_back(std::move(action));
178 std::unique_ptr<SensorMonitoring> sensorMonitoring =
179 std::make_unique<SensorMonitoring>(std::move(actions));
180 SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get();
181
182 // Create Rail that contains sensorMonitoring
183 std::unique_ptr<Configuration> configuration{};
184 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
185 "vio2", std::move(configuration), std::move(sensorMonitoring));
186 Rail* railPtr = rail.get();
187
188 // Create Device that contains Rail
189 std::unique_ptr<PresenceDetection> presenceDetection{};
190 std::unique_ptr<Configuration> deviceConfiguration{};
191 std::vector<std::unique_ptr<Rail>> rails{};
192 rails.emplace_back(std::move(rail));
193 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800194 "reg1", true,
195 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King833b8e02020-06-11 14:56:38 +0800196 std::move(i2cInterface), std::move(presenceDetection),
197 std::move(deviceConfiguration), std::move(rails));
198 Device* devicePtr = device.get();
199
200 // Create Chassis that contains Device
201 std::vector<std::unique_ptr<Device>> devices{};
202 devices.emplace_back(std::move(device));
203 std::unique_ptr<Chassis> chassis =
Shawn McCarneycb3f6a62021-04-30 10:54:30 -0500204 std::make_unique<Chassis>(1, chassisInvPath, std::move(devices));
Bob King833b8e02020-06-11 14:56:38 +0800205 Chassis* chassisPtr = chassis.get();
206
207 // Create System that contains Chassis
208 std::vector<std::unique_ptr<Rule>> rules{};
209 std::vector<std::unique_ptr<Chassis>> chassisVec{};
210 chassisVec.emplace_back(std::move(chassis));
211 System system{std::move(rules), std::move(chassisVec)};
212
213 // Execute sensorMonitoring
Bob King8a552922020-08-05 17:02:31 +0800214 sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
215 *railPtr);
Bob King833b8e02020-06-11 14:56:38 +0800216 }
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500217}
218
219TEST(SensorMonitoringTests, GetActions)
220{
221 std::vector<std::unique_ptr<Action>> actions{};
222
223 MockAction* action1 = new MockAction{};
224 actions.push_back(std::unique_ptr<MockAction>{action1});
225
226 MockAction* action2 = new MockAction{};
227 actions.push_back(std::unique_ptr<MockAction>{action2});
228
229 SensorMonitoring sensorMonitoring(std::move(actions));
230 EXPECT_EQ(sensorMonitoring.getActions().size(), 2);
231 EXPECT_EQ(sensorMonitoring.getActions()[0].get(), action1);
232 EXPECT_EQ(sensorMonitoring.getActions()[1].get(), action2);
233}