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