blob: 357659da72cbd76cbb91626b86eabe7d26748894 [file] [log] [blame]
Matt Spinler99e66a02019-03-06 14:11:22 -06001#include "association_manager.hpp"
2
3#include <filesystem>
4#include <fstream>
5
6#include <gtest/gtest.h>
7
8using namespace phosphor::inventory::manager::associations;
9namespace fs = std::filesystem;
10
11static const auto goodJson = R"(
12[
13 {
14 "path": "system/PS0",
15 "endpoints":
16 [
17 {
18 "types":
19 {
20 "rType": "inventory",
21 "fType": "sensors"
22 },
23 "paths":
24 [
25 "power/ps0_input_power",
26 "voltage/ps0_input_voltage",
27 "current/ps0_output_current",
28 "voltage/ps0_output_voltage"
29 ]
30 },
31 {
32 "types":
33 {
34 "rType": "inventory",
35 "fType": "fans"
36 },
37 "paths":
38 [
39 "fan_tach/ps0_fan"
40 ]
41 }
42 ]
43 },
44 {
45 "path": "system/fan42",
46 "endpoints":
47 [
48 {
49 "types":
50 {
51 "rType": "inventory",
52 "fType": "sensors"
53 },
54 "paths":
55 [
56 "fan_tach/fan42"
57 ]
58 },
59 {
60 "types":
61 {
62 "rType": "inventory",
63 "fType": "led"
64 },
65 "paths":
66 [
67 "led/fan42"
68 ]
69 }
70 ]
71 }
72])";
73
74// Malformed JSON
75static const auto badJson0 = R"(
76 "hello": world
77})";
78
79// Uses 'blah' instead of 'paths'
80static const auto badJson1 = R"(
81[
82 {
83 "blah": "system/PS0",
84 "endpoints":
85 [
86 {
87 "types":
88 {
89 "fType": "inventory",
90 "rType": "sensors"
91 },
92 "paths":
93 [
94 "ps0_input_power",
95 ]
96 }
97 ]
98 }
99])";
100
101// Uses 'blah' instead of 'rType'
102static const auto badJson2 = R"(
103[
104 {
105 "paths": "system/PS0",
106 "endpoints":
107 [
108 {
109 "types":
110 {
111 "blah": "inventory",
112 "fType": "sensors"
113 },
114 "paths":
115 [
116 "ps0_input_power",
117 ]
118 }
119 ]
120 }
121])";
122
123// Missing the endpoints/paths array
124static const auto badJson3 = R"(
125[
126 {
127 "paths": "system/PS0",
128 "endpoints":
129 [
130 {
131 "types":
132 {
133 "rType": "inventory",
134 "fType": "sensors"
135 }
136 }
137 ]
138 }
139])";
140
141class AssocsTest : public ::testing::Test
142{
143 protected:
144 AssocsTest() : ::testing::Test(), bus(sdbusplus::bus::new_default())
145 {
146 }
147
148 fs::path jsonDir;
149 sdbusplus::bus::bus bus;
150
151 virtual void SetUp()
152 {
153 char dir[] = {"assocTestXXXXXX"};
154 jsonDir = mkdtemp(dir);
155 }
156
157 virtual void TearDown()
158 {
159 fs::remove_all(jsonDir);
160 }
161
162 std::string writeFile(const char* data)
163 {
164 fs::path path = jsonDir / "associations.json";
165
166 std::ofstream f{path};
167 f << data;
168 f.close();
169
170 return path;
171 }
172};
173
174TEST_F(AssocsTest, TEST_NO_JSON)
175{
176 try
177 {
178 Manager m{bus};
179 EXPECT_TRUE(false);
180 }
181 catch (std::exception& e)
182 {
183 }
184}
185
186TEST_F(AssocsTest, TEST_GOOD_JSON)
187{
188 auto path = writeFile(goodJson);
189 Manager m(bus, path);
190
191 const auto& a = m.getAssociationsConfig();
192 EXPECT_EQ(a.size(), 2);
193
194 {
195 auto x = a.find("/xyz/openbmc_project/inventory/system/PS0");
196 EXPECT_NE(x, a.end());
197
198 auto& endpoints = x->second;
199 EXPECT_EQ(endpoints.size(), 2);
200
201 {
202 auto& types = std::get<0>(endpoints[0]);
203 EXPECT_EQ(std::get<0>(types), "sensors");
204 EXPECT_EQ(std::get<1>(types), "inventory");
205
206 auto& paths = std::get<1>(endpoints[0]);
207 EXPECT_EQ(paths.size(), 4);
208 }
209 {
210 auto& types = std::get<0>(endpoints[1]);
211 EXPECT_EQ(std::get<0>(types), "fans");
212 EXPECT_EQ(std::get<1>(types), "inventory");
213
214 auto& paths = std::get<1>(endpoints[1]);
215 EXPECT_EQ(paths.size(), 1);
216 }
217 }
218 {
219 auto x = a.find("/xyz/openbmc_project/inventory/system/fan42");
220 EXPECT_NE(x, a.end());
221
222 auto& endpoints = x->second;
223 EXPECT_EQ(endpoints.size(), 2);
224
225 {
226 auto& types = std::get<0>(endpoints[0]);
227 EXPECT_EQ(std::get<0>(types), "sensors");
228 EXPECT_EQ(std::get<1>(types), "inventory");
229
230 auto& paths = std::get<1>(endpoints[0]);
231 EXPECT_EQ(paths.size(), 1);
232 }
233 {
234 auto& types = std::get<0>(endpoints[1]);
235 EXPECT_EQ(std::get<0>(types), "led");
236 EXPECT_EQ(std::get<1>(types), "inventory");
237
238 auto& paths = std::get<1>(endpoints[1]);
239 EXPECT_EQ(paths.size(), 1);
240 }
241 }
242}
243
244TEST_F(AssocsTest, TEST_BAD_JSON0)
245{
246 auto path = writeFile(badJson0);
247
248 try
249 {
250 Manager m(bus, path);
251
252 EXPECT_TRUE(false);
253 }
254 catch (std::exception& e)
255 {
256 }
257}
258
259TEST_F(AssocsTest, TEST_BAD_JSON1)
260{
261 auto path = writeFile(badJson1);
262
263 try
264 {
265 Manager m(bus, path);
266
267 EXPECT_TRUE(false);
268 }
269 catch (std::exception& e)
270 {
271 }
272}
273
274TEST_F(AssocsTest, TEST_BAD_JSON2)
275{
276 auto path = writeFile(badJson2);
277
278 try
279 {
280 Manager m(bus, path);
281
282 EXPECT_TRUE(false);
283 }
284 catch (std::exception& e)
285 {
286 }
287}
288
289TEST_F(AssocsTest, TEST_BAD_JSON3)
290{
291 auto path = writeFile(badJson3);
292
293 try
294 {
295 Manager m(bus, path);
296
297 EXPECT_TRUE(false);
298 }
299 catch (std::exception& e)
300 {
301 }
302}