blob: f2f43599c06b58b94188a28a9c879b5b8b057180 [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
53TEST(SensorMonitoringTests, Constructor)
54{
55 std::vector<std::unique_ptr<Action>> actions{};
56 actions.push_back(std::make_unique<MockAction>());
57
58 SensorMonitoring sensorMonitoring(std::move(actions));
59 EXPECT_EQ(sensorMonitoring.getActions().size(), 1);
60}
61
62TEST(SensorMonitoringTests, Execute)
63{
Bob King833b8e02020-06-11 14:56:38 +080064 // Test where works
65 {
Bob King8a552922020-08-05 17:02:31 +080066 // Create mock services. No logging should occur.
67 MockServices services{};
68 MockJournal& journal = services.getMockJournal();
69 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
70 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
71
Bob King833b8e02020-06-11 14:56:38 +080072 // Create PMBusReadSensorAction
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050073 SensorType type{SensorType::iout};
Bob King833b8e02020-06-11 14:56:38 +080074 uint8_t command = 0x8C;
75 pmbus_utils::SensorDataFormat format{
76 pmbus_utils::SensorDataFormat::linear_11};
77 std::optional<int8_t> exponent{};
78 std::unique_ptr<PMBusReadSensorAction> action =
79 std::make_unique<PMBusReadSensorAction>(type, command, format,
80 exponent);
81
82 // Create mock I2CInterface.
83 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
84 std::make_unique<i2c::MockedI2CInterface>();
85 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
86 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
87 .Times(1);
88
89 // Create SensorMonitoring
90 std::vector<std::unique_ptr<Action>> actions{};
91 actions.emplace_back(std::move(action));
92 std::unique_ptr<SensorMonitoring> sensorMonitoring =
93 std::make_unique<SensorMonitoring>(std::move(actions));
94 SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get();
95
96 // Create Rail that contains sensorMonitoring
97 std::unique_ptr<Configuration> configuration{};
98 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
99 "vio2", std::move(configuration), std::move(sensorMonitoring));
100 Rail* railPtr = rail.get();
101
102 // Create Device that contains Rail
103 std::unique_ptr<PresenceDetection> presenceDetection{};
104 std::unique_ptr<Configuration> deviceConfiguration{};
105 std::vector<std::unique_ptr<Rail>> rails{};
106 rails.emplace_back(std::move(rail));
107 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800108 "reg1", true,
109 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King833b8e02020-06-11 14:56:38 +0800110 std::move(i2cInterface), std::move(presenceDetection),
111 std::move(deviceConfiguration), std::move(rails));
112 Device* devicePtr = device.get();
113
114 // Create Chassis that contains Device
115 std::vector<std::unique_ptr<Device>> devices{};
116 devices.emplace_back(std::move(device));
117 std::unique_ptr<Chassis> chassis =
118 std::make_unique<Chassis>(1, std::move(devices));
119 Chassis* chassisPtr = chassis.get();
120
121 // Create System that contains Chassis
122 std::vector<std::unique_ptr<Rule>> rules{};
123 std::vector<std::unique_ptr<Chassis>> chassisVec{};
124 chassisVec.emplace_back(std::move(chassis));
125 System system{std::move(rules), std::move(chassisVec)};
126
127 // Execute sensorMonitoring
Bob King8a552922020-08-05 17:02:31 +0800128 sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
129 *railPtr);
Bob King833b8e02020-06-11 14:56:38 +0800130 }
131
132 // Test where fails
133 {
Shawn McCarney81a2f902021-03-23 21:41:34 -0500134 // Create mock services. Expect logError() and logI2CError() to be
135 // called.
Bob King8a552922020-08-05 17:02:31 +0800136 MockServices services{};
Shawn McCarney81a2f902021-03-23 21:41:34 -0500137 MockErrorLogging& errorLogging = services.getMockErrorLogging();
Bob King8a552922020-08-05 17:02:31 +0800138 MockJournal& journal = services.getMockJournal();
139 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
140 std::vector<std::string> expectedErrMessagesException{
141 "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70",
142 "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
143 "format: linear_11 }"};
Bob King8a552922020-08-05 17:02:31 +0800144 EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1);
145 EXPECT_CALL(journal,
146 logError("Unable to monitor sensors for rail vio2"))
147 .Times(1);
Shawn McCarney81a2f902021-03-23 21:41:34 -0500148 EXPECT_CALL(errorLogging,
149 logI2CError(Entry::Level::Warning, Ref(journal),
150 "/dev/i2c-1", 0x70, 0))
151 .Times(1);
Bob King8a552922020-08-05 17:02:31 +0800152
Bob King833b8e02020-06-11 14:56:38 +0800153 // Create PMBusReadSensorAction
Shawn McCarney2f9e14f2021-04-29 02:45:18 -0500154 SensorType type{SensorType::iout};
Bob King833b8e02020-06-11 14:56:38 +0800155 uint8_t command = 0x8C;
156 pmbus_utils::SensorDataFormat format{
157 pmbus_utils::SensorDataFormat::linear_11};
158 std::optional<int8_t> exponent{};
159 std::unique_ptr<PMBusReadSensorAction> action =
160 std::make_unique<PMBusReadSensorAction>(type, command, format,
161 exponent);
162
163 // Create mock I2CInterface.
164 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
165 std::make_unique<i2c::MockedI2CInterface>();
166 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
167 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
168 .Times(1)
169 .WillOnce(Throw(
170 i2c::I2CException{"Failed to write byte", "/dev/i2c-1", 0x70}));
171
172 // Create SensorMonitoring
173 std::vector<std::unique_ptr<Action>> actions{};
174 actions.emplace_back(std::move(action));
175 std::unique_ptr<SensorMonitoring> sensorMonitoring =
176 std::make_unique<SensorMonitoring>(std::move(actions));
177 SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get();
178
179 // Create Rail that contains sensorMonitoring
180 std::unique_ptr<Configuration> configuration{};
181 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
182 "vio2", std::move(configuration), std::move(sensorMonitoring));
183 Rail* railPtr = rail.get();
184
185 // Create Device that contains Rail
186 std::unique_ptr<PresenceDetection> presenceDetection{};
187 std::unique_ptr<Configuration> deviceConfiguration{};
188 std::vector<std::unique_ptr<Rail>> rails{};
189 rails.emplace_back(std::move(rail));
190 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800191 "reg1", true,
192 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King833b8e02020-06-11 14:56:38 +0800193 std::move(i2cInterface), std::move(presenceDetection),
194 std::move(deviceConfiguration), std::move(rails));
195 Device* devicePtr = device.get();
196
197 // Create Chassis that contains Device
198 std::vector<std::unique_ptr<Device>> devices{};
199 devices.emplace_back(std::move(device));
200 std::unique_ptr<Chassis> chassis =
201 std::make_unique<Chassis>(1, std::move(devices));
202 Chassis* chassisPtr = chassis.get();
203
204 // Create System that contains Chassis
205 std::vector<std::unique_ptr<Rule>> rules{};
206 std::vector<std::unique_ptr<Chassis>> chassisVec{};
207 chassisVec.emplace_back(std::move(chassis));
208 System system{std::move(rules), std::move(chassisVec)};
209
210 // Execute sensorMonitoring
Bob King8a552922020-08-05 17:02:31 +0800211 sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
212 *railPtr);
Bob King833b8e02020-06-11 14:56:38 +0800213 }
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500214}
215
216TEST(SensorMonitoringTests, GetActions)
217{
218 std::vector<std::unique_ptr<Action>> actions{};
219
220 MockAction* action1 = new MockAction{};
221 actions.push_back(std::unique_ptr<MockAction>{action1});
222
223 MockAction* action2 = new MockAction{};
224 actions.push_back(std::unique_ptr<MockAction>{action2});
225
226 SensorMonitoring sensorMonitoring(std::move(actions));
227 EXPECT_EQ(sensorMonitoring.getActions().size(), 2);
228 EXPECT_EQ(sensorMonitoring.getActions()[0].get(), action1);
229 EXPECT_EQ(sensorMonitoring.getActions()[1].get(), action2);
230}