blob: ebb899899824f82e3f0171b451131fceda5c8e8e [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"
Bob King833b8e02020-06-11 14:56:38 +080032#include "system.hpp"
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050033
Bob King833b8e02020-06-11 14:56:38 +080034#include <cstdint>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050035#include <memory>
Bob King833b8e02020-06-11 14:56:38 +080036#include <optional>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050037#include <utility>
38#include <vector>
39
Bob King833b8e02020-06-11 14:56:38 +080040#include <gmock/gmock.h>
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050041#include <gtest/gtest.h>
42
43using namespace phosphor::power::regulators;
Bob King833b8e02020-06-11 14:56:38 +080044using namespace phosphor::power::regulators::pmbus_utils;
45
46using ::testing::A;
Shawn McCarney81a2f902021-03-23 21:41:34 -050047using ::testing::Ref;
Bob King833b8e02020-06-11 14:56:38 +080048using ::testing::Return;
49using ::testing::Throw;
50using ::testing::TypedEq;
Shawn McCarneybc47c1b2020-03-10 15:38:07 -050051
52TEST(SensorMonitoringTests, Constructor)
53{
54 std::vector<std::unique_ptr<Action>> actions{};
55 actions.push_back(std::make_unique<MockAction>());
56
57 SensorMonitoring sensorMonitoring(std::move(actions));
58 EXPECT_EQ(sensorMonitoring.getActions().size(), 1);
59}
60
61TEST(SensorMonitoringTests, Execute)
62{
Bob King833b8e02020-06-11 14:56:38 +080063 // Test where works
64 {
Bob King8a552922020-08-05 17:02:31 +080065 // Create mock services. No logging should occur.
66 MockServices services{};
67 MockJournal& journal = services.getMockJournal();
68 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
69 EXPECT_CALL(journal, logError(A<const std::string&>())).Times(0);
70
Bob King833b8e02020-06-11 14:56:38 +080071 // Create PMBusReadSensorAction
72 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
73 uint8_t command = 0x8C;
74 pmbus_utils::SensorDataFormat format{
75 pmbus_utils::SensorDataFormat::linear_11};
76 std::optional<int8_t> exponent{};
77 std::unique_ptr<PMBusReadSensorAction> action =
78 std::make_unique<PMBusReadSensorAction>(type, command, format,
79 exponent);
80
81 // Create mock I2CInterface.
82 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
83 std::make_unique<i2c::MockedI2CInterface>();
84 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
85 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
86 .Times(1);
87
88 // Create SensorMonitoring
89 std::vector<std::unique_ptr<Action>> actions{};
90 actions.emplace_back(std::move(action));
91 std::unique_ptr<SensorMonitoring> sensorMonitoring =
92 std::make_unique<SensorMonitoring>(std::move(actions));
93 SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get();
94
95 // Create Rail that contains sensorMonitoring
96 std::unique_ptr<Configuration> configuration{};
97 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
98 "vio2", std::move(configuration), std::move(sensorMonitoring));
99 Rail* railPtr = rail.get();
100
101 // Create Device that contains Rail
102 std::unique_ptr<PresenceDetection> presenceDetection{};
103 std::unique_ptr<Configuration> deviceConfiguration{};
104 std::vector<std::unique_ptr<Rail>> rails{};
105 rails.emplace_back(std::move(rail));
106 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800107 "reg1", true,
108 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King833b8e02020-06-11 14:56:38 +0800109 std::move(i2cInterface), std::move(presenceDetection),
110 std::move(deviceConfiguration), std::move(rails));
111 Device* devicePtr = device.get();
112
113 // Create Chassis that contains Device
114 std::vector<std::unique_ptr<Device>> devices{};
115 devices.emplace_back(std::move(device));
116 std::unique_ptr<Chassis> chassis =
117 std::make_unique<Chassis>(1, std::move(devices));
118 Chassis* chassisPtr = chassis.get();
119
120 // Create System that contains Chassis
121 std::vector<std::unique_ptr<Rule>> rules{};
122 std::vector<std::unique_ptr<Chassis>> chassisVec{};
123 chassisVec.emplace_back(std::move(chassis));
124 System system{std::move(rules), std::move(chassisVec)};
125
126 // Execute sensorMonitoring
Bob King8a552922020-08-05 17:02:31 +0800127 sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
128 *railPtr);
Bob King833b8e02020-06-11 14:56:38 +0800129 }
130
131 // Test where fails
132 {
Shawn McCarney81a2f902021-03-23 21:41:34 -0500133 // Create mock services. Expect logError() and logI2CError() to be
134 // called.
Bob King8a552922020-08-05 17:02:31 +0800135 MockServices services{};
Shawn McCarney81a2f902021-03-23 21:41:34 -0500136 MockErrorLogging& errorLogging = services.getMockErrorLogging();
Bob King8a552922020-08-05 17:02:31 +0800137 MockJournal& journal = services.getMockJournal();
138 EXPECT_CALL(journal, logDebug(A<const std::string&>())).Times(0);
139 std::vector<std::string> expectedErrMessagesException{
140 "I2CException: Failed to write byte: bus /dev/i2c-1, addr 0x70",
141 "ActionError: pmbus_read_sensor: { type: iout, command: 0x8C, "
142 "format: linear_11 }"};
Bob King8a552922020-08-05 17:02:31 +0800143 EXPECT_CALL(journal, logError(expectedErrMessagesException)).Times(1);
144 EXPECT_CALL(journal,
145 logError("Unable to monitor sensors for rail vio2"))
146 .Times(1);
Shawn McCarney81a2f902021-03-23 21:41:34 -0500147 EXPECT_CALL(errorLogging,
148 logI2CError(Entry::Level::Warning, Ref(journal),
149 "/dev/i2c-1", 0x70, 0))
150 .Times(1);
Bob King8a552922020-08-05 17:02:31 +0800151
Bob King833b8e02020-06-11 14:56:38 +0800152 // Create PMBusReadSensorAction
153 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
154 uint8_t command = 0x8C;
155 pmbus_utils::SensorDataFormat format{
156 pmbus_utils::SensorDataFormat::linear_11};
157 std::optional<int8_t> exponent{};
158 std::unique_ptr<PMBusReadSensorAction> action =
159 std::make_unique<PMBusReadSensorAction>(type, command, format,
160 exponent);
161
162 // Create mock I2CInterface.
163 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
164 std::make_unique<i2c::MockedI2CInterface>();
165 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
166 EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
167 .Times(1)
168 .WillOnce(Throw(
169 i2c::I2CException{"Failed to write byte", "/dev/i2c-1", 0x70}));
170
171 // Create SensorMonitoring
172 std::vector<std::unique_ptr<Action>> actions{};
173 actions.emplace_back(std::move(action));
174 std::unique_ptr<SensorMonitoring> sensorMonitoring =
175 std::make_unique<SensorMonitoring>(std::move(actions));
176 SensorMonitoring* sensorMonitoringPtr = sensorMonitoring.get();
177
178 // Create Rail that contains sensorMonitoring
179 std::unique_ptr<Configuration> configuration{};
180 std::unique_ptr<Rail> rail = std::make_unique<Rail>(
181 "vio2", std::move(configuration), std::move(sensorMonitoring));
182 Rail* railPtr = rail.get();
183
184 // Create Device that contains Rail
185 std::unique_ptr<PresenceDetection> presenceDetection{};
186 std::unique_ptr<Configuration> deviceConfiguration{};
187 std::vector<std::unique_ptr<Rail>> rails{};
188 rails.emplace_back(std::move(rail));
189 std::unique_ptr<Device> device = std::make_unique<Device>(
Bob Kinga76898f2020-10-13 15:08:33 +0800190 "reg1", true,
191 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
Bob King833b8e02020-06-11 14:56:38 +0800192 std::move(i2cInterface), std::move(presenceDetection),
193 std::move(deviceConfiguration), std::move(rails));
194 Device* devicePtr = device.get();
195
196 // Create Chassis that contains Device
197 std::vector<std::unique_ptr<Device>> devices{};
198 devices.emplace_back(std::move(device));
199 std::unique_ptr<Chassis> chassis =
200 std::make_unique<Chassis>(1, std::move(devices));
201 Chassis* chassisPtr = chassis.get();
202
203 // Create System that contains Chassis
204 std::vector<std::unique_ptr<Rule>> rules{};
205 std::vector<std::unique_ptr<Chassis>> chassisVec{};
206 chassisVec.emplace_back(std::move(chassis));
207 System system{std::move(rules), std::move(chassisVec)};
208
209 // Execute sensorMonitoring
Bob King8a552922020-08-05 17:02:31 +0800210 sensorMonitoringPtr->execute(services, system, *chassisPtr, *devicePtr,
211 *railPtr);
Bob King833b8e02020-06-11 14:56:38 +0800212 }
Shawn McCarneybc47c1b2020-03-10 15:38:07 -0500213}
214
215TEST(SensorMonitoringTests, GetActions)
216{
217 std::vector<std::unique_ptr<Action>> actions{};
218
219 MockAction* action1 = new MockAction{};
220 actions.push_back(std::unique_ptr<MockAction>{action1});
221
222 MockAction* action2 = new MockAction{};
223 actions.push_back(std::unique_ptr<MockAction>{action2});
224
225 SensorMonitoring sensorMonitoring(std::move(actions));
226 EXPECT_EQ(sensorMonitoring.getActions().size(), 2);
227 EXPECT_EQ(sensorMonitoring.getActions()[0].get(), action1);
228 EXPECT_EQ(sensorMonitoring.getActions()[1].get(), action2);
229}