blob: 7c1dccfcd352ffebcd5c69a2e93ae731fb45b98e [file] [log] [blame]
SunnySrivastava198497f8df02020-05-30 12:05:53 -05001#include "reader_test.hpp"
2
3#include "const.hpp"
4#include "reader_impl.hpp"
5#include "types.hpp"
6
SunnySrivastava198497f8df02020-05-30 12:05:53 -05007#include <nlohmann/json.hpp>
Patrick Williamsc78d8872023-05-10 07:50:56 -05008
9#include <fstream>
SunnySrivastava198497f8df02020-05-30 12:05:53 -050010#include <tuple>
11
12#include <gmock/gmock.h>
13#include <gtest/gtest.h>
14
15using namespace openpower::vpd::manager::reader;
16using namespace openpower::vpd::inventory;
17using namespace openpower::vpd::constants;
18
19class vpdManagerReaderTest : public ::testing::Test
20{
21 protected:
22 nlohmann::json jsonFile;
23
24 // map to hold the mapping of location code and inventory path
25 LocationCodeMap fruLocationCode;
26
27 // dummy value of node number
28 const uint8_t nodeNumber = 1;
29
30 public:
31 // constructor
32 vpdManagerReaderTest()
33 {
34 processJson();
35 }
36
37 void processJson();
38};
39
40using namespace openpower::vpd;
41
42void vpdManagerReaderTest::processJson()
43{
44 // read the json file and parse it
45 std::ifstream json("vpd-manager-test/vpd_reader_test.json",
46 std::ios::binary);
47
48 if (!json)
49 {
50 throw std::runtime_error("json file not found");
51 }
52
53 jsonFile = nlohmann::json::parse(json);
54
55 if (jsonFile.find("frus") == jsonFile.end())
56 {
57 throw std::runtime_error("frus group not found in json");
58 }
59
60 const nlohmann::json& groupFRUS =
61 jsonFile["frus"].get_ref<const nlohmann::json::object_t&>();
62 for (const auto& itemFRUS : groupFRUS.items())
63 {
64 const std::vector<nlohmann::json>& groupEEPROM =
65 itemFRUS.value().get_ref<const nlohmann::json::array_t&>();
66 for (const auto& itemEEPROM : groupEEPROM)
67 {
68 fruLocationCode.emplace(
Alpana Kumari414d5ae2021-03-04 21:06:35 +000069 itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF]
70 ["LocationCode"]
71 .get_ref<const nlohmann::json::string_t&>(),
SunnySrivastava198497f8df02020-05-30 12:05:53 -050072 itemEEPROM["inventoryPath"]
73 .get_ref<const nlohmann::json::string_t&>());
74 }
75 }
76}
77
78TEST_F(vpdManagerReaderTest, isValidLocationCode_invalid)
79{
80 // No MTS or FCS in the collapsed location code
81 std::string unexpandedLocationCode_Invalid = "Uabc-X0";
82
83 MockUtilCalls uCalls;
84 ReaderImpl read(uCalls);
85 EXPECT_ANY_THROW({
86 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber,
87 fruLocationCode);
88 });
89
90 // not starting with U
91 unexpandedLocationCode_Invalid = "Mabc-X0";
92 EXPECT_ANY_THROW({
93 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber,
94 fruLocationCode);
95 });
96}
97
98TEST_F(vpdManagerReaderTest, getExpandedLocationCode_Invalid)
99{
100 std::string unexpandedLocationCode_Invalid = "Uabc-X0";
101
102 MockUtilCalls uCalls;
103 ReaderImpl read(uCalls);
104 EXPECT_ANY_THROW({
105 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber,
106 fruLocationCode);
107 });
108}
109
110TEST_F(vpdManagerReaderTest, getExpandedLocationCode_Valid)
111{
112 // mock the call to read bus property
113 MockUtilCalls uCalls;
Alpana Kumari414d5ae2021-03-04 21:06:35 +0000114 EXPECT_CALL(uCalls, readBusProperty(SYSTEM_OBJECT, IBM_LOCATION_CODE_INF,
115 "LocationCode"))
SunnySrivastava198497f8df02020-05-30 12:05:53 -0500116 .Times(1)
117 .WillOnce(testing::Return("U78DA.ND1.1234567-P0"));
118
119 std::string unexpandedLocationCode = "Ufcs-P0";
120 ReaderImpl read(uCalls);
121 std::string res = read.getExpandedLocationCode(unexpandedLocationCode,
122 nodeNumber, fruLocationCode);
123
124 EXPECT_EQ(res, "U78DA.ND1.1234567-P0");
125}
126
127TEST_F(vpdManagerReaderTest, getFrusAtLocation_Invalid)
128{
129 // invalid lication code
130 std::string unexpandedLocationCode = "Uabc-X0";
131
132 MockUtilCalls uCalls;
133 ReaderImpl read(uCalls);
134 EXPECT_ANY_THROW({
135 read.getFrusAtLocation(unexpandedLocationCode, nodeNumber,
136 fruLocationCode);
137 });
138
139 // map to hold the mapping of location code and inventory path, empty in
140 // this case
141 LocationCodeMap mapLocationCode;
142 unexpandedLocationCode = "Ufcs-P0";
143 EXPECT_ANY_THROW({
144 read.getFrusAtLocation(unexpandedLocationCode, nodeNumber,
145 mapLocationCode);
146 });
147}
148
149TEST_F(vpdManagerReaderTest, getFrusAtLocation_Valid)
150{
151 std::string LocationCode = "Ufcs-P0";
152
153 MockUtilCalls uCalls;
154 ReaderImpl read(uCalls);
Patrick Williamsc78d8872023-05-10 07:50:56 -0500155 ListOfPaths paths = read.getFrusAtLocation(LocationCode, nodeNumber,
156 fruLocationCode);
SunnySrivastava198497f8df02020-05-30 12:05:53 -0500157 std::string expected =
158 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
159 EXPECT_EQ(paths.at(0), expected);
160}
161
162TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_invalid)
163{
164 // not starting from U
165 std::string locationCode = "9105.22A.SIMP10R";
166
167 MockUtilCalls uCalls;
168 ReaderImpl read(uCalls);
169 ListOfPaths paths;
170 EXPECT_ANY_THROW({
Patrick Williamsc78d8872023-05-10 07:50:56 -0500171 paths = read.getFRUsByExpandedLocationCode(locationCode,
172 fruLocationCode);
SunnySrivastava198497f8df02020-05-30 12:05:53 -0500173 });
174
175 // unused variable warning
176 (void)paths;
177
178 // length is les sthan 17 for expanded location code
179 locationCode = "U9105.22A.SIMP10";
180 EXPECT_ANY_THROW({
Patrick Williamsc78d8872023-05-10 07:50:56 -0500181 paths = read.getFRUsByExpandedLocationCode(locationCode,
182 fruLocationCode);
SunnySrivastava198497f8df02020-05-30 12:05:53 -0500183 });
184
185 // Invalid location code. No "."
186 locationCode = "U78DAND11234567-P0";
187
188 // Mock readBUsproperty call
189 EXPECT_CALL(uCalls,
190 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC"))
191 .Times(1)
192 .WillOnce(
193 testing::Return("78DAPQRS")); // return a dummy value for FC keyword
194
195 // unused variable warning
196 (void)paths;
197 EXPECT_ANY_THROW({
Patrick Williamsc78d8872023-05-10 07:50:56 -0500198 paths = read.getFRUsByExpandedLocationCode(locationCode,
199 fruLocationCode);
SunnySrivastava198497f8df02020-05-30 12:05:53 -0500200 });
201}
202
203TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_Valid_FC)
204{
205 // valid location code with FC kwd.
206 std::string validLocationCode = "U78DA.ND1.1234567-P0";
207
208 // Mock readBUsproperty call
209 MockUtilCalls uCalls;
210 EXPECT_CALL(uCalls,
211 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC"))
212 .WillRepeatedly(
213 testing::Return("78DAPQRS")); // return a dummy value for FC keyword
214
215 ReaderImpl read(uCalls);
Patrick Williamsc78d8872023-05-10 07:50:56 -0500216 ListOfPaths paths = read.getFRUsByExpandedLocationCode(validLocationCode,
217 fruLocationCode);
SunnySrivastava198497f8df02020-05-30 12:05:53 -0500218
219 std::string expected =
220 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
221 EXPECT_EQ(paths.at(0), expected);
222}
223
224TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_Valid_TM)
225{
226 // valid location code with TM kwd.
227 std::string validLocationCode = "U9105.22A.SIMP10R";
228
229 // Mock readBUsproperty call
230 MockUtilCalls uCalls;
231 EXPECT_CALL(uCalls,
232 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC"))
233 .Times(1)
234 .WillOnce(
235 testing::Return("78DAPQRS")); // return a dummy value for FC keyword
236
237 EXPECT_CALL(uCalls,
238 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VSYS", "TM"))
239 .Times(1)
240 .WillOnce(
241 testing::Return("9105PQRS")); // return a dummy value for TM keyword
242
243 ReaderImpl read(uCalls);
Patrick Williamsc78d8872023-05-10 07:50:56 -0500244 ListOfPaths paths = read.getFRUsByExpandedLocationCode(validLocationCode,
245 fruLocationCode);
SunnySrivastava198497f8df02020-05-30 12:05:53 -0500246
247 std::string expected = "/xyz/openbmc_project/inventory/system";
248 EXPECT_EQ(paths.at(0), expected);
249}
250
251int main(int argc, char** argv)
252{
253 ::testing::InitGoogleTest(&argc, argv);
254
255 return RUN_ALL_TESTS();
Patrick Williamsc78d8872023-05-10 07:50:56 -0500256}