blob: b3e4d0c84cef0bdaa8240737dbcf78eb095e879d [file] [log] [blame]
#include "topology.hpp"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::UnorderedElementsAre;
const std::string subchassisPath =
"/xyz/openbmc_project/inventory/system/chassis/Subchassis";
const std::string superchassisPath =
"/xyz/openbmc_project/inventory/system/chassis/Superchassis";
const Association subchassisAssoc =
std::make_tuple("contained_by", "containing", superchassisPath);
const Association powerAssoc =
std::make_tuple("powered_by", "powering", subchassisPath);
const nlohmann::json subchassisExposesItem = nlohmann::json::parse(R"(
{
"ConnectsToType": "BackplanePort",
"Name": "MyDownstreamPort",
"Type": "DownstreamPort"
}
)");
const nlohmann::json powerExposesItem = nlohmann::json::parse(R"(
{
"ConnectsToType": "BackplanePort",
"Name": "MyDownstreamPort",
"Type": "DownstreamPort",
"PowerPort": true
}
)");
const nlohmann::json superchassisExposesItem = nlohmann::json::parse(R"(
{
"Name": "MyBackplanePort",
"Type": "BackplanePort"
}
)");
const nlohmann::json otherExposesItem = nlohmann::json::parse(R"(
{
"Name": "MyExposes",
"Type": "OtherType"
}
)");
using BoardMap = std::map<std::string, std::string>;
TEST(Topology, Empty)
{
Topology topo;
BoardMap boards;
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 0);
}
TEST(Topology, EmptyExposes)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"}, {superchassisPath, "BoardB"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", nlohmann::json());
topo.addBoard(superchassisPath, "Chassis", "BoardB", nlohmann::json());
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 0);
}
TEST(Topology, MissingConnectsTo)
{
const nlohmann::json subchassisMissingConnectsTo = nlohmann::json::parse(R"(
{
"Name": "MyDownstreamPort",
"Type": "DownstreamPort"
}
)");
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"}, {superchassisPath, "BoardB"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA",
subchassisMissingConnectsTo);
topo.addBoard(superchassisPath, "Chassis", "BoardB",
superchassisExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 0);
}
TEST(Topology, OtherExposes)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"}, {superchassisPath, "BoardB"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", otherExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardB", otherExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 0);
}
TEST(Topology, NoMatchSubchassis)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"}, {superchassisPath, "BoardB"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", otherExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardB",
superchassisExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 0);
}
TEST(Topology, NoMatchSuperchassis)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"}, {superchassisPath, "BoardB"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", subchassisExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardB", otherExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 0);
}
TEST(Topology, Basic)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"}, {superchassisPath, "BoardB"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", subchassisExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardB",
superchassisExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 1);
EXPECT_EQ(assocs[subchassisPath].size(), 1);
EXPECT_EQ(assocs[subchassisPath][0], subchassisAssoc);
}
TEST(Topology, BasicPower)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"}, {superchassisPath, "BoardB"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", powerExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardB",
superchassisExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 2);
EXPECT_EQ(assocs[subchassisPath].size(), 1);
EXPECT_EQ(assocs[subchassisPath][0], subchassisAssoc);
EXPECT_EQ(assocs[superchassisPath].size(), 1);
EXPECT_EQ(assocs[superchassisPath][0], powerAssoc);
}
TEST(Topology, NoNewBoards)
{
Topology topo;
BoardMap boards;
topo.addBoard(subchassisPath, "Chassis", "BoardA", subchassisExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardB",
superchassisExposesItem);
// Boards A and B aren't new, so no assocs are returned.
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 0);
}
TEST(Topology, 2Subchassis)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"},
{subchassisPath + "2", "BoardB"},
{superchassisPath, "BoardC"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", subchassisExposesItem);
topo.addBoard(subchassisPath + "2", "Chassis", "BoardB",
subchassisExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardC",
superchassisExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 2);
EXPECT_EQ(assocs[subchassisPath].size(), 1);
EXPECT_EQ(assocs[subchassisPath][0], subchassisAssoc);
EXPECT_EQ(assocs[subchassisPath + "2"].size(), 1);
EXPECT_EQ(assocs[subchassisPath + "2"][0], subchassisAssoc);
}
TEST(Topology, OneNewBoard)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", subchassisExposesItem);
topo.addBoard(subchassisPath + "2", "Chassis", "BoardB",
subchassisExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardC",
superchassisExposesItem);
// Only the assoc for BoardA should be returned
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 1);
EXPECT_EQ(assocs[subchassisPath].size(), 1);
EXPECT_EQ(assocs[subchassisPath][0], subchassisAssoc);
}
TEST(Topology, 2Superchassis)
{
const Association subchassisAssoc2 =
std::make_tuple("contained_by", "containing", superchassisPath + "2");
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"},
{superchassisPath, "BoardB"},
{superchassisPath + "2", "BoardC"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", subchassisExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardB",
superchassisExposesItem);
topo.addBoard(superchassisPath + "2", "Chassis", "BoardC",
superchassisExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 1);
EXPECT_EQ(assocs[subchassisPath].size(), 2);
EXPECT_THAT(assocs[subchassisPath],
UnorderedElementsAre(subchassisAssoc, subchassisAssoc2));
}
TEST(Topology, 2SuperchassisAnd2Subchassis)
{
const Association subchassisAssoc2 =
std::make_tuple("contained_by", "containing", superchassisPath + "2");
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"},
{subchassisPath + "2", "BoardB"},
{superchassisPath, "BoardC"},
{superchassisPath + "2", "BoardD"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", subchassisExposesItem);
topo.addBoard(subchassisPath + "2", "Chassis", "BoardB",
subchassisExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardC",
superchassisExposesItem);
topo.addBoard(superchassisPath + "2", "Chassis", "BoardD",
superchassisExposesItem);
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 2);
EXPECT_EQ(assocs[subchassisPath].size(), 2);
EXPECT_EQ(assocs[subchassisPath + "2"].size(), 2);
EXPECT_THAT(assocs[subchassisPath],
UnorderedElementsAre(subchassisAssoc, subchassisAssoc2));
EXPECT_THAT(assocs[subchassisPath + "2"],
UnorderedElementsAre(subchassisAssoc, subchassisAssoc2));
}
TEST(Topology, Remove)
{
Topology topo;
BoardMap boards{{subchassisPath, "BoardA"},
{subchassisPath + "2", "BoardB"},
{superchassisPath, "BoardC"}};
topo.addBoard(subchassisPath, "Chassis", "BoardA", subchassisExposesItem);
topo.addBoard(subchassisPath + "2", "Chassis", "BoardB",
subchassisExposesItem);
topo.addBoard(superchassisPath, "Chassis", "BoardC",
superchassisExposesItem);
{
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 2);
EXPECT_EQ(assocs[subchassisPath].size(), 1);
EXPECT_EQ(assocs[subchassisPath][0], subchassisAssoc);
EXPECT_EQ(assocs[subchassisPath + "2"].size(), 1);
EXPECT_EQ(assocs[subchassisPath + "2"][0], subchassisAssoc);
}
{
topo.remove("BoardA");
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 1);
EXPECT_EQ(assocs[subchassisPath + "2"].size(), 1);
EXPECT_EQ(assocs[subchassisPath + "2"][0], subchassisAssoc);
}
{
topo.remove("BoardB");
auto assocs = topo.getAssocs(boards);
EXPECT_EQ(assocs.size(), 0);
}
}