blob: b6cf80c04e706776f3b4ed3b2feb71ab605757e2 [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(
68 itemEEPROM["extraInterfaces"][LOCATION_CODE_INF]["LocationCode"]
69 .get_ref<const nlohmann::json::string_t&>(),
70 itemEEPROM["inventoryPath"]
71 .get_ref<const nlohmann::json::string_t&>());
72 }
73 }
74}
75
76TEST_F(vpdManagerReaderTest, isValidLocationCode_invalid)
77{
78 // No MTS or FCS in the collapsed location code
79 std::string unexpandedLocationCode_Invalid = "Uabc-X0";
80
81 MockUtilCalls uCalls;
82 ReaderImpl read(uCalls);
83 EXPECT_ANY_THROW({
84 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber,
85 fruLocationCode);
86 });
87
88 // not starting with U
89 unexpandedLocationCode_Invalid = "Mabc-X0";
90 EXPECT_ANY_THROW({
91 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber,
92 fruLocationCode);
93 });
94}
95
96TEST_F(vpdManagerReaderTest, getExpandedLocationCode_Invalid)
97{
98 std::string unexpandedLocationCode_Invalid = "Uabc-X0";
99
100 MockUtilCalls uCalls;
101 ReaderImpl read(uCalls);
102 EXPECT_ANY_THROW({
103 read.getExpandedLocationCode(unexpandedLocationCode_Invalid, nodeNumber,
104 fruLocationCode);
105 });
106}
107
108TEST_F(vpdManagerReaderTest, getExpandedLocationCode_Valid)
109{
110 // mock the call to read bus property
111 MockUtilCalls uCalls;
112 EXPECT_CALL(uCalls, readBusProperty("/system/chassis/motherboard",
113 LOCATION_CODE_INF, "LocationCode"))
114 .Times(1)
115 .WillOnce(testing::Return("U78DA.ND1.1234567-P0"));
116
117 std::string unexpandedLocationCode = "Ufcs-P0";
118 ReaderImpl read(uCalls);
119 std::string res = read.getExpandedLocationCode(unexpandedLocationCode,
120 nodeNumber, fruLocationCode);
121
122 EXPECT_EQ(res, "U78DA.ND1.1234567-P0");
123}
124
125TEST_F(vpdManagerReaderTest, getFrusAtLocation_Invalid)
126{
127 // invalid lication code
128 std::string unexpandedLocationCode = "Uabc-X0";
129
130 MockUtilCalls uCalls;
131 ReaderImpl read(uCalls);
132 EXPECT_ANY_THROW({
133 read.getFrusAtLocation(unexpandedLocationCode, nodeNumber,
134 fruLocationCode);
135 });
136
137 // map to hold the mapping of location code and inventory path, empty in
138 // this case
139 LocationCodeMap mapLocationCode;
140 unexpandedLocationCode = "Ufcs-P0";
141 EXPECT_ANY_THROW({
142 read.getFrusAtLocation(unexpandedLocationCode, nodeNumber,
143 mapLocationCode);
144 });
145}
146
147TEST_F(vpdManagerReaderTest, getFrusAtLocation_Valid)
148{
149 std::string LocationCode = "Ufcs-P0";
150
151 MockUtilCalls uCalls;
152 ReaderImpl read(uCalls);
153 ListOfPaths paths =
154 read.getFrusAtLocation(LocationCode, nodeNumber, fruLocationCode);
155 std::string expected =
156 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
157 EXPECT_EQ(paths.at(0), expected);
158}
159
160TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_invalid)
161{
162 // not starting from U
163 std::string locationCode = "9105.22A.SIMP10R";
164
165 MockUtilCalls uCalls;
166 ReaderImpl read(uCalls);
167 ListOfPaths paths;
168 EXPECT_ANY_THROW({
169 paths =
170 read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode);
171 });
172
173 // unused variable warning
174 (void)paths;
175
176 // length is les sthan 17 for expanded location code
177 locationCode = "U9105.22A.SIMP10";
178 EXPECT_ANY_THROW({
179 paths =
180 read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode);
181 });
182
183 // Invalid location code. No "."
184 locationCode = "U78DAND11234567-P0";
185
186 // Mock readBUsproperty call
187 EXPECT_CALL(uCalls,
188 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC"))
189 .Times(1)
190 .WillOnce(
191 testing::Return("78DAPQRS")); // return a dummy value for FC keyword
192
193 // unused variable warning
194 (void)paths;
195 EXPECT_ANY_THROW({
196 paths =
197 read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode);
198 });
199}
200
201TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_Valid_FC)
202{
203 // valid location code with FC kwd.
204 std::string validLocationCode = "U78DA.ND1.1234567-P0";
205
206 // Mock readBUsproperty call
207 MockUtilCalls uCalls;
208 EXPECT_CALL(uCalls,
209 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC"))
210 .WillRepeatedly(
211 testing::Return("78DAPQRS")); // return a dummy value for FC keyword
212
213 ReaderImpl read(uCalls);
214 ListOfPaths paths =
215 read.getFRUsByExpandedLocationCode(validLocationCode, fruLocationCode);
216
217 std::string expected =
218 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
219 EXPECT_EQ(paths.at(0), expected);
220}
221
222TEST_F(vpdManagerReaderTest, getFRUsByExpandedLocationCode_Valid_TM)
223{
224 // valid location code with TM kwd.
225 std::string validLocationCode = "U9105.22A.SIMP10R";
226
227 // Mock readBUsproperty call
228 MockUtilCalls uCalls;
229 EXPECT_CALL(uCalls,
230 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VCEN", "FC"))
231 .Times(1)
232 .WillOnce(
233 testing::Return("78DAPQRS")); // return a dummy value for FC keyword
234
235 EXPECT_CALL(uCalls,
236 readBusProperty(SYSTEM_OBJECT, "com.ibm.ipzvpd.VSYS", "TM"))
237 .Times(1)
238 .WillOnce(
239 testing::Return("9105PQRS")); // return a dummy value for TM keyword
240
241 ReaderImpl read(uCalls);
242 ListOfPaths paths =
243 read.getFRUsByExpandedLocationCode(validLocationCode, fruLocationCode);
244
245 std::string expected = "/xyz/openbmc_project/inventory/system";
246 EXPECT_EQ(paths.at(0), expected);
247}
248
249int main(int argc, char** argv)
250{
251 ::testing::InitGoogleTest(&argc, argv);
252
253 return RUN_ALL_TESTS();
254}