Benjamin Fair | f2f5b7a | 2022-09-09 19:45:02 +0000 | [diff] [blame^] | 1 | #include "topology.hpp" |
| 2 | |
| 3 | #include "gmock/gmock.h" |
| 4 | #include "gtest/gtest.h" |
| 5 | |
| 6 | using ::testing::UnorderedElementsAre; |
| 7 | |
| 8 | const std::string subchassisPath = |
| 9 | "/xyz/openbmc_project/inventory/system/chassis/Subchassis"; |
| 10 | const std::string superchassisPath = |
| 11 | "/xyz/openbmc_project/inventory/system/chassis/Superchassis"; |
| 12 | |
| 13 | const Association subchassisAssoc = |
| 14 | std::make_tuple("contained_by", "containing", superchassisPath); |
| 15 | |
| 16 | const nlohmann::json subchassisExposesItem = nlohmann::json::parse(R"( |
| 17 | { |
| 18 | "ConnectsToType": "BackplanePort", |
| 19 | "Name": "MyDownstreamPort", |
| 20 | "Type": "DownstreamPort" |
| 21 | } |
| 22 | )"); |
| 23 | |
| 24 | const nlohmann::json superchassisExposesItem = nlohmann::json::parse(R"( |
| 25 | { |
| 26 | "Name": "MyBackplanePort", |
| 27 | "Type": "BackplanePort" |
| 28 | } |
| 29 | )"); |
| 30 | |
| 31 | const nlohmann::json otherExposesItem = nlohmann::json::parse(R"( |
| 32 | { |
| 33 | "Name": "MyExposes", |
| 34 | "Type": "OtherType" |
| 35 | } |
| 36 | )"); |
| 37 | |
| 38 | TEST(Topology, Empty) |
| 39 | { |
| 40 | Topology topo; |
| 41 | |
| 42 | auto assocs = topo.getAssocs(); |
| 43 | |
| 44 | EXPECT_EQ(assocs.size(), 0); |
| 45 | } |
| 46 | |
| 47 | TEST(Topology, EmptyExposes) |
| 48 | { |
| 49 | Topology topo; |
| 50 | |
| 51 | topo.addBoard(subchassisPath, "Chassis", nlohmann::json()); |
| 52 | topo.addBoard(superchassisPath, "Chassis", nlohmann::json()); |
| 53 | |
| 54 | auto assocs = topo.getAssocs(); |
| 55 | |
| 56 | EXPECT_EQ(assocs.size(), 0); |
| 57 | } |
| 58 | |
| 59 | TEST(Topology, MissingConnectsTo) |
| 60 | { |
| 61 | const nlohmann::json subchassisMissingConnectsTo = nlohmann::json::parse(R"( |
| 62 | { |
| 63 | "Name": "MyDownstreamPort", |
| 64 | "Type": "DownstreamPort" |
| 65 | } |
| 66 | )"); |
| 67 | |
| 68 | Topology topo; |
| 69 | |
| 70 | topo.addBoard(subchassisPath, "Chassis", subchassisMissingConnectsTo); |
| 71 | topo.addBoard(superchassisPath, "Chassis", superchassisExposesItem); |
| 72 | |
| 73 | auto assocs = topo.getAssocs(); |
| 74 | |
| 75 | EXPECT_EQ(assocs.size(), 0); |
| 76 | } |
| 77 | |
| 78 | TEST(Topology, OtherExposes) |
| 79 | { |
| 80 | Topology topo; |
| 81 | |
| 82 | topo.addBoard(subchassisPath, "Chassis", otherExposesItem); |
| 83 | topo.addBoard(superchassisPath, "Chassis", otherExposesItem); |
| 84 | |
| 85 | auto assocs = topo.getAssocs(); |
| 86 | |
| 87 | EXPECT_EQ(assocs.size(), 0); |
| 88 | } |
| 89 | |
| 90 | TEST(Topology, NoMatchSubchassis) |
| 91 | { |
| 92 | Topology topo; |
| 93 | |
| 94 | topo.addBoard(subchassisPath, "Chassis", otherExposesItem); |
| 95 | topo.addBoard(superchassisPath, "Chassis", superchassisExposesItem); |
| 96 | |
| 97 | auto assocs = topo.getAssocs(); |
| 98 | |
| 99 | EXPECT_EQ(assocs.size(), 0); |
| 100 | } |
| 101 | |
| 102 | TEST(Topology, NoMatchSuperchassis) |
| 103 | { |
| 104 | Topology topo; |
| 105 | |
| 106 | topo.addBoard(subchassisPath, "Chassis", subchassisExposesItem); |
| 107 | topo.addBoard(superchassisPath, "Chassis", otherExposesItem); |
| 108 | |
| 109 | auto assocs = topo.getAssocs(); |
| 110 | |
| 111 | EXPECT_EQ(assocs.size(), 0); |
| 112 | } |
| 113 | |
| 114 | TEST(Topology, Basic) |
| 115 | { |
| 116 | Topology topo; |
| 117 | |
| 118 | topo.addBoard(subchassisPath, "Chassis", subchassisExposesItem); |
| 119 | topo.addBoard(superchassisPath, "Chassis", superchassisExposesItem); |
| 120 | |
| 121 | auto assocs = topo.getAssocs(); |
| 122 | |
| 123 | EXPECT_EQ(assocs.size(), 1); |
| 124 | EXPECT_EQ(assocs[subchassisPath].size(), 1); |
| 125 | EXPECT_EQ(assocs[subchassisPath][0], subchassisAssoc); |
| 126 | } |
| 127 | |
| 128 | TEST(Topology, 2Subchassis) |
| 129 | { |
| 130 | Topology topo; |
| 131 | |
| 132 | topo.addBoard(subchassisPath, "Chassis", subchassisExposesItem); |
| 133 | topo.addBoard(subchassisPath + "2", "Chassis", subchassisExposesItem); |
| 134 | topo.addBoard(superchassisPath, "Chassis", superchassisExposesItem); |
| 135 | |
| 136 | auto assocs = topo.getAssocs(); |
| 137 | |
| 138 | EXPECT_EQ(assocs.size(), 2); |
| 139 | EXPECT_EQ(assocs[subchassisPath].size(), 1); |
| 140 | EXPECT_EQ(assocs[subchassisPath][0], subchassisAssoc); |
| 141 | EXPECT_EQ(assocs[subchassisPath + "2"].size(), 1); |
| 142 | EXPECT_EQ(assocs[subchassisPath + "2"][0], subchassisAssoc); |
| 143 | } |
| 144 | |
| 145 | TEST(Topology, 2Superchassis) |
| 146 | { |
| 147 | const Association subchassisAssoc2 = |
| 148 | std::make_tuple("contained_by", "containing", superchassisPath + "2"); |
| 149 | |
| 150 | Topology topo; |
| 151 | |
| 152 | topo.addBoard(subchassisPath, "Chassis", subchassisExposesItem); |
| 153 | topo.addBoard(superchassisPath, "Chassis", superchassisExposesItem); |
| 154 | topo.addBoard(superchassisPath + "2", "Chassis", superchassisExposesItem); |
| 155 | |
| 156 | auto assocs = topo.getAssocs(); |
| 157 | |
| 158 | EXPECT_EQ(assocs.size(), 1); |
| 159 | EXPECT_EQ(assocs[subchassisPath].size(), 2); |
| 160 | |
| 161 | EXPECT_THAT(assocs[subchassisPath], |
| 162 | UnorderedElementsAre(subchassisAssoc, subchassisAssoc2)); |
| 163 | } |
| 164 | |
| 165 | TEST(Topology, 2SuperchassisAnd2Subchassis) |
| 166 | { |
| 167 | const Association subchassisAssoc2 = |
| 168 | std::make_tuple("contained_by", "containing", superchassisPath + "2"); |
| 169 | |
| 170 | Topology topo; |
| 171 | |
| 172 | topo.addBoard(subchassisPath, "Chassis", subchassisExposesItem); |
| 173 | topo.addBoard(subchassisPath + "2", "Chassis", subchassisExposesItem); |
| 174 | topo.addBoard(superchassisPath, "Chassis", superchassisExposesItem); |
| 175 | topo.addBoard(superchassisPath + "2", "Chassis", superchassisExposesItem); |
| 176 | |
| 177 | auto assocs = topo.getAssocs(); |
| 178 | |
| 179 | EXPECT_EQ(assocs.size(), 2); |
| 180 | EXPECT_EQ(assocs[subchassisPath].size(), 2); |
| 181 | EXPECT_EQ(assocs[subchassisPath + "2"].size(), 2); |
| 182 | |
| 183 | EXPECT_THAT(assocs[subchassisPath], |
| 184 | UnorderedElementsAre(subchassisAssoc, subchassisAssoc2)); |
| 185 | EXPECT_THAT(assocs[subchassisPath + "2"], |
| 186 | UnorderedElementsAre(subchassisAssoc, subchassisAssoc2)); |
| 187 | } |