blob: 2ec8b2e9ad8f5d8bc41705a78f8f0568dccceb90 [file] [log] [blame]
Shawn McCarney71d7fe42024-05-02 16:06:10 -05001/**
2 * Copyright © 2024 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
17#include "mock_pmbus.hpp"
18#include "mock_services.hpp"
19#include "pmbus.hpp"
20#include "rail.hpp"
21#include "services.hpp"
22#include "ucd90x_device.hpp"
23
24#include <cstdint>
25#include <exception>
26#include <map>
27#include <memory>
28#include <optional>
29#include <string>
30#include <utility>
31#include <vector>
32
33#include <gmock/gmock.h>
34#include <gtest/gtest.h>
35
36using namespace phosphor::power::sequencer;
37using namespace phosphor::pmbus;
38
39using ::testing::Return;
40using ::testing::Throw;
41
42/**
43 * Creates a Rail object that checks for a pgood fault using a GPIO.
44 *
45 * @param name Unique name for the rail
46 * @param gpio GPIO line to read to determine the pgood status of the rail
47 * @return Rail object
48 */
49std::unique_ptr<Rail> createRail(const std::string& name, unsigned int gpioLine)
50{
51 std::optional<std::string> presence{};
52 std::optional<uint8_t> page{};
53 bool isPowerSupplyRail{false};
54 bool checkStatusVout{false};
55 bool compareVoltageToLimit{false};
56 bool activeLow{false};
57 std::optional<GPIO> gpio{GPIO{gpioLine, activeLow}};
58 return std::make_unique<Rail>(name, presence, page, isPowerSupplyRail,
59 checkStatusVout, compareVoltageToLimit, gpio);
60}
61
62TEST(UCD90xDeviceTests, Constructor)
63{
64 MockServices services;
65
66 std::string name{"ucd90320"};
67 std::vector<std::unique_ptr<Rail>> rails;
68 rails.emplace_back(createRail("VDD", 5));
69 rails.emplace_back(createRail("VIO", 7));
70 uint8_t bus{3};
71 uint16_t address{0x72};
72 UCD90xDevice device{name, std::move(rails), services, bus, address};
73
74 EXPECT_EQ(device.getName(), name);
75 EXPECT_EQ(device.getRails().size(), 2);
76 EXPECT_EQ(device.getRails()[0]->getName(), "VDD");
77 EXPECT_EQ(device.getRails()[1]->getName(), "VIO");
78 EXPECT_EQ(device.getBus(), bus);
79 EXPECT_EQ(device.getAddress(), address);
80 EXPECT_EQ(device.getDriverName(), "ucd9000");
81 EXPECT_EQ(device.getInstance(), 0);
82 EXPECT_NE(&(device.getPMBusInterface()), nullptr);
83}
84
85TEST(UCD90xDeviceTests, GetMfrStatus)
86{
87 // Test where works
88 {
89 MockServices services;
90
91 std::string name{"ucd90320"};
92 std::vector<std::unique_ptr<Rail>> rails;
93 uint8_t bus{3};
94 uint16_t address{0x72};
95 UCD90xDevice device{name, std::move(rails), services, bus, address};
96
97 MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
98 uint64_t mfrStatus{0x123456789abcull};
99 EXPECT_CALL(pmbus, read("mfr_status", Type::HwmonDeviceDebug, true))
100 .Times(1)
101 .WillOnce(Return(mfrStatus));
102
103 EXPECT_EQ(device.getMfrStatus(), mfrStatus);
104 }
105
106 // Test where fails with exception
107 {
108 MockServices services;
109
110 std::string name{"ucd90320"};
111 std::vector<std::unique_ptr<Rail>> rails;
112 uint8_t bus{3};
113 uint16_t address{0x72};
114 UCD90xDevice device{name, std::move(rails), services, bus, address};
115
116 MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
117 EXPECT_CALL(pmbus, read("mfr_status", Type::HwmonDeviceDebug, true))
118 .Times(1)
119 .WillOnce(Throw(std::runtime_error{"File does not exist"}));
120
121 try
122 {
123 device.getMfrStatus();
124 ADD_FAILURE() << "Should not have reached this line.";
125 }
126 catch (const std::exception& e)
127 {
128 EXPECT_STREQ(e.what(),
129 "Unable to read MFR_STATUS for device ucd90320: "
130 "File does not exist");
131 }
132 }
133}
134
135TEST(UCD90xDeviceTests, StorePgoodFaultDebugData)
136{
137 // This is a protected method and cannot be called directly from a gtest.
138 // Call findPgoodFault() which calls storePgoodFaultDebugData().
139
140 // Test where works
141 {
142 MockServices services;
143 std::vector<int> gpioValues{1, 1, 0};
144 EXPECT_CALL(services, getGPIOValues("ucd90320"))
145 .Times(1)
146 .WillOnce(Return(gpioValues));
147 EXPECT_CALL(services,
148 logInfoMsg("Device ucd90320 GPIO values: [1, 1, 0]"))
149 .Times(1);
150 EXPECT_CALL(services,
151 logInfoMsg("Device ucd90320 MFR_STATUS: 0x123456789abc"))
152 .Times(1);
153 EXPECT_CALL(
154 services,
155 logErrorMsg(
156 "Pgood fault found in rail monitored by device ucd90320"))
157 .Times(1);
158 EXPECT_CALL(services, logErrorMsg("Pgood fault detected in rail VDD"))
159 .Times(1);
160 EXPECT_CALL(
161 services,
162 logErrorMsg(
163 "Rail VDD pgood GPIO line offset 2 has inactive value 0"))
164 .Times(1);
165
166 std::string name{"ucd90320"};
167 std::vector<std::unique_ptr<Rail>> rails;
168 rails.emplace_back(createRail("VDD", 2));
169 uint8_t bus{3};
170 uint16_t address{0x72};
171 UCD90xDevice device{name, std::move(rails), services, bus, address};
172
173 MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
174 EXPECT_CALL(pmbus, getPath(Type::Hwmon))
175 .Times(1)
176 .WillOnce(Return("/tmp"));
177 EXPECT_CALL(pmbus, read("mfr_status", Type::HwmonDeviceDebug, true))
178 .Times(1)
179 .WillOnce(Return(0x123456789abcull));
180
181 // Call findPgoodFault() which calls storePgoodFaultDebugData()
182 std::string powerSupplyError{};
183 std::map<std::string, std::string> additionalData{};
184 std::string error = device.findPgoodFault(services, powerSupplyError,
185 additionalData);
186 EXPECT_EQ(error,
187 "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault");
188 EXPECT_EQ(additionalData.size(), 6);
189 EXPECT_EQ(additionalData["MFR_STATUS"], "0x123456789abc");
190 EXPECT_EQ(additionalData["DEVICE_NAME"], "ucd90320");
191 EXPECT_EQ(additionalData["GPIO_VALUES"], "[1, 1, 0]");
192 EXPECT_EQ(additionalData["RAIL_NAME"], "VDD");
193 EXPECT_EQ(additionalData["GPIO_LINE"], "2");
194 EXPECT_EQ(additionalData["GPIO_VALUE"], "0");
195 }
196
197 // Test where exception thrown trying to get MFR_STATUS
198 {
199 MockServices services;
200 std::vector<int> gpioValues{1, 1, 0};
201 EXPECT_CALL(services, getGPIOValues("ucd90320"))
202 .Times(1)
203 .WillOnce(Return(gpioValues));
204 EXPECT_CALL(services,
205 logInfoMsg("Device ucd90320 GPIO values: [1, 1, 0]"))
206 .Times(1);
207 EXPECT_CALL(
208 services,
209 logErrorMsg(
210 "Pgood fault found in rail monitored by device ucd90320"))
211 .Times(1);
212 EXPECT_CALL(services, logErrorMsg("Pgood fault detected in rail VDD"))
213 .Times(1);
214 EXPECT_CALL(
215 services,
216 logErrorMsg(
217 "Rail VDD pgood GPIO line offset 2 has inactive value 0"))
218 .Times(1);
219
220 std::string name{"ucd90320"};
221 std::vector<std::unique_ptr<Rail>> rails;
222 rails.emplace_back(createRail("VDD", 2));
223 uint8_t bus{3};
224 uint16_t address{0x72};
225 UCD90xDevice device{name, std::move(rails), services, bus, address};
226
227 MockPMBus& pmbus = static_cast<MockPMBus&>(device.getPMBusInterface());
228 EXPECT_CALL(pmbus, getPath(Type::Hwmon))
229 .Times(1)
230 .WillOnce(Return("/tmp"));
231 EXPECT_CALL(pmbus, read("mfr_status", Type::HwmonDeviceDebug, true))
232 .Times(1)
233 .WillOnce(Throw(std::runtime_error{"File does not exist"}));
234
235 // Call findPgoodFault() which calls storePgoodFaultDebugData()
236 std::string powerSupplyError{};
237 std::map<std::string, std::string> additionalData{};
238 std::string error = device.findPgoodFault(services, powerSupplyError,
239 additionalData);
240 EXPECT_EQ(error,
241 "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault");
242 EXPECT_EQ(additionalData.size(), 5);
243 EXPECT_EQ(additionalData["DEVICE_NAME"], "ucd90320");
244 EXPECT_EQ(additionalData["GPIO_VALUES"], "[1, 1, 0]");
245 EXPECT_EQ(additionalData["RAIL_NAME"], "VDD");
246 EXPECT_EQ(additionalData["GPIO_LINE"], "2");
247 EXPECT_EQ(additionalData["GPIO_VALUE"], "0");
248 }
249}