diff options
author | Andrew Geissler <geissonator@yahoo.com> | 2019-02-05 11:23:30 -0600 |
---|---|---|
committer | Matt Spinler <spinler@us.ibm.com> | 2019-04-05 15:05:32 +0000 |
commit | 271b7dd00648c8a0d004e57b2c53ca642a6d3a42 (patch) | |
tree | e103e7968c7a298a9c1b14c21dd8eaf589056227 | |
parent | a80a3af035d4fe989acd81c45aee4acf8c3d998a (diff) | |
download | phosphor-objmgr-271b7dd00648c8a0d004e57b2c53ca642a6d3a42.tar.gz phosphor-objmgr-271b7dd00648c8a0d004e57b2c53ca642a6d3a42.zip |
unit-test: Test removeAssociations interface
Testing: Verified code coverage shows 100% of new interface
Change-Id: I517acc02b06bbff971921e66a697fb297fde45c6
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
-rw-r--r-- | Makefile.am | 7 | ||||
-rw-r--r-- | src/associations.hpp | 2 | ||||
-rw-r--r-- | src/test/Makefile.am.include | 6 | ||||
-rw-r--r-- | src/test/associations.cpp | 162 |
4 files changed, 174 insertions, 3 deletions
diff --git a/Makefile.am b/Makefile.am index 668a529..fe71472 100644 --- a/Makefile.am +++ b/Makefile.am @@ -9,9 +9,12 @@ endif AM_LIBS = $(CODE_COVERAGE_LIBS) AM_CPPFLAGS = $(CODE_COVERAGE_CPPFLAGS) -UNDEBUG $(GTEST_CPPFLAGS) AM_CFLAGS = $(CODE_COVERAGE_CFLAGS) -AM_CXXFLAGS = $(CODE_COVERAGE_CXXFLAGS) +AM_CXXFLAGS = $(CODE_COVERAGE_CXXFLAGS) \ + -DBOOST_SYSTEM_NO_DEPRECATED -DBOOST_ERROR_CODE_HEADER_ONLY \ + -DBOOST_ALL_NO_LIB AM_LDFLAGS = $(GMOCK_LIBS) -lgmock_main \ - $(GTEST_LIBS) $(OESDK_TESTCASE_FLAGS) $(PTHREAD_LIBS) + $(GTEST_LIBS) $(OESDK_TESTCASE_FLAGS) $(PTHREAD_LIBS) \ + $(SDBUSPLUS_LIBS) -lboost_system sbin_PROGRAMS = mapper mapperx diff --git a/src/associations.hpp b/src/associations.hpp index 41df157..8ebef43 100644 --- a/src/associations.hpp +++ b/src/associations.hpp @@ -5,6 +5,7 @@ #include <memory> #include <sdbusplus/asio/object_server.hpp> #include <string> +#include <tuple> #include <vector> // Associations and some metadata are stored in associationInterfaces. @@ -15,6 +16,7 @@ static constexpr auto ifacePos = 0; static constexpr auto endpointsPos = 1; using Endpoints = std::vector<std::string>; +// map[interface path: tuple[dbus_interface,vector[endpoint paths]]] using AssociationInterfaces = boost::container::flat_map< std::string, std::tuple<std::shared_ptr<sdbusplus::asio::dbus_interface>, Endpoints>>; diff --git a/src/test/Makefile.am.include b/src/test/Makefile.am.include index 23ba45a..b54ddbf 100644 --- a/src/test/Makefile.am.include +++ b/src/test/Makefile.am.include @@ -3,6 +3,10 @@ src_test_well_known_SOURCES = %reldir%/well_known.cpp src/processing.cpp src_test_need_to_introspect_SOURCES = %reldir%/need_to_introspect.cpp \ src/processing.cpp +src_test_associations_SOURCES = %reldir%/associations.cpp \ + src/associations.cpp + check_PROGRAMS += \ %reldir%/well_known \ - %reldir%/need_to_introspect + %reldir%/need_to_introspect \ + %reldir%/associations diff --git a/src/test/associations.cpp b/src/test/associations.cpp new file mode 100644 index 0000000..d5f86f4 --- /dev/null +++ b/src/test/associations.cpp @@ -0,0 +1,162 @@ +#include "src/associations.hpp" + +#include <sdbusplus/asio/connection.hpp> +#include <sdbusplus/asio/object_server.hpp> + +#include <gtest/gtest.h> + +class TestAssociations : public testing::Test +{ + protected: + // Make this global to the whole test suite since we want to share + // the asio::object_server accross the test cases + // NOTE - latest googltest changed to SetUpTestSuite() + static void SetUpTestCase() + { + boost::asio::io_context io; + auto conn = std::make_shared<sdbusplus::asio::connection>(io); + + conn->request_name("xyz.openbmc_project.ObjMgr.Test"); + server = new sdbusplus::asio::object_server(conn); + } + + // NOTE - latest googltest changed to TearDownTestSuite() + static void TearDownTestCase() + { + delete server; + server = nullptr; + } + + static sdbusplus::asio::object_server* server; +}; + +sdbusplus::asio::object_server* TestAssociations::server = nullptr; + +const std::string DEFAULT_SOURCE_PATH = "/logging/entry/1"; +const std::string DEFAULT_DBUS_SVC = "xyz.openbmc_project.New.Interface"; +const std::string DEFAULT_FWD_PATH = {DEFAULT_SOURCE_PATH + "/" + "inventory"}; +const std::string DEFAULT_ENDPOINT = + "/xyz/openbmc_project/inventory/system/chassis"; +const std::string DEFAULT_REV_PATH = {DEFAULT_ENDPOINT + "/" + "error"}; +const std::string EXTRA_ENDPOINT = "/xyz/openbmc_project/differnt/endpoint"; + +// Create a default AssociationOwnersType object +AssociationOwnersType createDefaultOwnerAssociation() +{ + AssociationPaths assocPathMap = {{DEFAULT_FWD_PATH, {DEFAULT_ENDPOINT}}, + {DEFAULT_REV_PATH, {DEFAULT_SOURCE_PATH}}}; + boost::container::flat_map<std::string, AssociationPaths> serviceMap = { + {DEFAULT_DBUS_SVC, assocPathMap}}; + AssociationOwnersType ownerAssoc = {{DEFAULT_SOURCE_PATH, serviceMap}}; + return ownerAssoc; +} + +// Create a default AssociationInterfaces object +AssociationInterfaces + createDefaultInterfaceAssociation(sdbusplus::asio::object_server* server) +{ + AssociationInterfaces interfaceAssoc; + + auto& iface = interfaceAssoc[DEFAULT_FWD_PATH]; + auto& endpoints = std::get<endpointsPos>(iface); + endpoints.push_back(DEFAULT_ENDPOINT); + server->add_interface(DEFAULT_FWD_PATH, DEFAULT_DBUS_SVC); + + auto& iface2 = interfaceAssoc[DEFAULT_REV_PATH]; + auto& endpoints2 = std::get<endpointsPos>(iface2); + endpoints2.push_back(DEFAULT_SOURCE_PATH); + server->add_interface(DEFAULT_REV_PATH, DEFAULT_DBUS_SVC); + + return interfaceAssoc; +} + +// Just add an extra endpoint to the first association +void addEndpointToInterfaceAssociation(AssociationInterfaces& interfaceAssoc) +{ + auto iface = interfaceAssoc[DEFAULT_FWD_PATH]; + auto endpoints = std::get<endpointsPos>(iface); + endpoints.push_back(EXTRA_ENDPOINT); +} + +// Verify call when path is not in associated owners +TEST_F(TestAssociations, SourcePathNotInAssociations) +{ + EXPECT_NE(nullptr, server); + std::string sourcePath = "/xyz/openbmc_project/no/association"; + AssociationOwnersType assocOwners; + AssociationInterfaces assocInterfaces; + + removeAssociation(sourcePath, DEFAULT_DBUS_SVC, *server, assocOwners, + assocInterfaces); +} + +// Verify call when owner is not in associated owners +TEST_F(TestAssociations, OwnerNotInAssociations) +{ + AssociationInterfaces assocInterfaces; + + auto assocOwners = createDefaultOwnerAssociation(); + + removeAssociation(DEFAULT_SOURCE_PATH, DEFAULT_DBUS_SVC, *server, + assocOwners, assocInterfaces); +} + +// Verify call when path is not in associated interfaces +TEST_F(TestAssociations, PathNotInAssocInterfaces) +{ + AssociationInterfaces assocInterfaces; + + auto assocOwners = createDefaultOwnerAssociation(); + + removeAssociation(DEFAULT_SOURCE_PATH, DEFAULT_DBUS_SVC, *server, + assocOwners, assocInterfaces); + + EXPECT_TRUE(assocOwners.empty()); +} + +// Verify call when path is in associated interfaces +TEST_F(TestAssociations, PathIsInAssociatedInterfaces) +{ + // Build up these objects so that an associated interface will match + // with the associated owner being removed + auto assocOwners = createDefaultOwnerAssociation(); + auto assocInterfaces = createDefaultInterfaceAssociation(server); + + removeAssociation(DEFAULT_SOURCE_PATH, DEFAULT_DBUS_SVC, *server, + assocOwners, assocInterfaces); + + // Verify owner association was deleted + EXPECT_TRUE(assocOwners.empty()); + + // Verify endpoint was deleted from interface association + auto intfEndpoints = + std::get<endpointsPos>(assocInterfaces[DEFAULT_FWD_PATH]); + EXPECT_EQ(intfEndpoints.size(), 0); + intfEndpoints = std::get<endpointsPos>(assocInterfaces[DEFAULT_REV_PATH]); + EXPECT_EQ(intfEndpoints.size(), 0); +} + +// Verify call when path is in associated interfaces, with extra endpoints +TEST_F(TestAssociations, PathIsInAssociatedInterfacesExtraEndpoints) +{ + // Build up these objects so that an associated interface will match + // with the associated owner being removed + auto assocOwners = createDefaultOwnerAssociation(); + auto assocInterfaces = createDefaultInterfaceAssociation(server); + + // Add another endpoint to the assoc interfaces + addEndpointToInterfaceAssociation(assocInterfaces); + + removeAssociation(DEFAULT_SOURCE_PATH, DEFAULT_DBUS_SVC, *server, + assocOwners, assocInterfaces); + + // Verify owner association was deleted + EXPECT_TRUE(assocOwners.empty()); + + // Verify all endpoints are deleted since source path was deleted + auto intfEndpoints = + std::get<endpointsPos>(assocInterfaces[DEFAULT_FWD_PATH]); + EXPECT_EQ(intfEndpoints.size(), 0); + intfEndpoints = std::get<endpointsPos>(assocInterfaces[DEFAULT_REV_PATH]); + EXPECT_EQ(intfEndpoints.size(), 0); +} |