blob: 8caa68df229ff384917c82d0b028a577754855fc [file] [log] [blame]
Kamalkumar Patel4e69d252024-05-10 08:48:03 -05001#include "../utils.hpp"
Kamalkumar Patel516122e2024-05-07 04:39:32 -05002#include "common/utils.hpp"
George Liudf9a6d32020-12-22 16:27:16 +08003
Andrew Jeffery4668f5c2024-01-15 14:59:17 +10304#include <libpldm/pdr.h>
5
George Liudf9a6d32020-12-22 16:27:16 +08006#include <filesystem>
7
8#include <gtest/gtest.h>
9
10namespace fs = std::filesystem;
11using namespace pldm;
12using namespace pldm::utils;
Kamalkumar Patel516122e2024-05-07 04:39:32 -050013using namespace pldm::hostbmc::utils;
14
15TEST(EntityAssociation, parseEntityMap)
16{
17 EntityMaps entityMaps = parseEntityMap("./entitymap_test.json");
18 EXPECT_EQ(entityMaps.size(), 10);
19}
George Liudf9a6d32020-12-22 16:27:16 +080020
21TEST(EntityAssociation, addObjectPathEntityAssociations1)
22{
George Liuf3c91cf2021-10-15 17:34:35 +080023 pldm_entity entities[8]{};
George Liudf9a6d32020-12-22 16:27:16 +080024
25 entities[0].entity_type = 45;
26 entities[0].entity_container_id = 0;
27
28 entities[1].entity_type = 64;
29 entities[1].entity_container_id = 1;
30
31 entities[2].entity_type = 67;
32 entities[2].entity_container_id = 2;
33 entities[3].entity_type = 67;
34 entities[3].entity_container_id = 2;
35
36 entities[4].entity_type = 135;
37 entities[4].entity_container_id = 3;
38 entities[5].entity_type = 135;
39 entities[5].entity_container_id = 3;
40 entities[6].entity_type = 135;
41 entities[6].entity_container_id = 3;
42 entities[7].entity_type = 135;
43 entities[7].entity_container_id = 3;
44
45 auto tree = pldm_entity_association_tree_init();
46
George Liuf3c91cf2021-10-15 17:34:35 +080047 auto l1 = pldm_entity_association_tree_add_entity(
48 tree, &entities[0], 1, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true,
49 true, 0xFFFF);
George Liudf9a6d32020-12-22 16:27:16 +080050
George Liuf3c91cf2021-10-15 17:34:35 +080051 auto l2 = pldm_entity_association_tree_add_entity(
52 tree, &entities[1], 1, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
53 0xFFFF);
George Liudf9a6d32020-12-22 16:27:16 +080054
George Liuf3c91cf2021-10-15 17:34:35 +080055 auto l3a = pldm_entity_association_tree_add_entity(
56 tree, &entities[2], 0, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
57 0xFFFF);
58 auto l3b = pldm_entity_association_tree_add_entity(
59 tree, &entities[3], 1, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
60 0xFFFF);
George Liudf9a6d32020-12-22 16:27:16 +080061
George Liuf3c91cf2021-10-15 17:34:35 +080062 auto l4a = pldm_entity_association_tree_add_entity(
63 tree, &entities[4], 0, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
64 0xFFFF);
65 auto l4b = pldm_entity_association_tree_add_entity(
66 tree, &entities[5], 1, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
67 0xFFFF);
George Liudf9a6d32020-12-22 16:27:16 +080068
George Liuf3c91cf2021-10-15 17:34:35 +080069 auto l5a = pldm_entity_association_tree_add_entity(
70 tree, &entities[6], 0, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
71 0xFFFF);
72 auto l5b = pldm_entity_association_tree_add_entity(
73 tree, &entities[7], 1, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
74 0xFFFF);
George Liudf9a6d32020-12-22 16:27:16 +080075
76 EntityAssociations entityAssociations = {
77 {l1, l2}, {l2, l3a, l3b}, {l3a, l4a, l4b}, {l3b, l5a, l5b}};
78
79 ObjectPathMaps retObjectMaps = {
80 {"/xyz/openbmc_project/inventory/chassis1", l1},
81 {"/xyz/openbmc_project/inventory/chassis1/motherboard1", l2},
82 {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm0", l3a},
George Liudf9a6d32020-12-22 16:27:16 +080083 {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm0/cpu0", l4a},
84 {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm0/cpu1", l4b},
George Liuf3c91cf2021-10-15 17:34:35 +080085 {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm1", l3b},
George Liudf9a6d32020-12-22 16:27:16 +080086 {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm1/cpu0", l5a},
George Liuf3c91cf2021-10-15 17:34:35 +080087 {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm1/cpu1",
88 l5b}};
George Liudf9a6d32020-12-22 16:27:16 +080089
90 ObjectPathMaps objPathMap;
Kamalkumar Patel516122e2024-05-07 04:39:32 -050091 EntityMaps entityMaps = parseEntityMap("./entitymap_test.json");
Kamalkumar Patel15ce5a12024-05-07 11:45:11 -050092 updateEntityAssociation(entityAssociations, tree, objPathMap, entityMaps,
93 nullptr);
George Liudf9a6d32020-12-22 16:27:16 +080094
95 EXPECT_EQ(objPathMap.size(), retObjectMaps.size());
96
97 int index = 0;
98 for (auto& obj : objPathMap)
99 {
100 if (retObjectMaps.contains(obj.first))
101 {
102 index++;
103 pldm_entity entity = pldm_entity_extract(obj.second);
104 pldm_entity retEntity =
105 pldm_entity_extract(retObjectMaps[obj.first]);
106 EXPECT_EQ(entity.entity_type, retEntity.entity_type);
107 EXPECT_EQ(entity.entity_instance_num,
108 retEntity.entity_instance_num);
109 EXPECT_EQ(pldm_entity_node_get_remote_container_id(obj.second),
110 pldm_entity_node_get_remote_container_id(
111 retObjectMaps[obj.first]));
112 }
113 }
114 EXPECT_EQ(index, retObjectMaps.size());
115 pldm_entity_association_tree_destroy(tree);
116}