diff options
author | Ratan Gupta <ratagupt@linux.vnet.ibm.com> | 2018-11-28 18:40:16 +0530 |
---|---|---|
committer | Ratan Gupta <ratagupt@linux.vnet.ibm.com> | 2018-11-30 20:42:02 +0530 |
commit | 35297177b81061c3ac6658412c6306556849790a (patch) | |
tree | 9a2a63e74cf4cb2930bca7587fef91f855e8aa0e | |
parent | 450b346910a7bcf9abde55d76693539a83bf33f7 (diff) | |
download | phosphor-networkd-35297177b81061c3ac6658412c6306556849790a.tar.gz phosphor-networkd-35297177b81061c3ac6658412c6306556849790a.zip |
Re-enable unit test
Unit test were broken after sd bus calls started throwing exception,
In this repo we start the systemd-networkd with the use of sd bus
calls, which throws exception and it was not handled in the test case.
This commit fixes the problem by mocking the network manager which
mock the functionality of function which makes the sdbus call.
Change-Id: I5b60a2117a661cffa36200415ca611b85dd2fda1
Signed-off-by: Ratan Gupta <ratagupt@linux.vnet.ibm.com>
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | network_manager.cpp | 17 | ||||
-rw-r--r-- | network_manager.hpp | 18 | ||||
-rw-r--r-- | test/Makefile.am | 2 | ||||
-rw-r--r-- | test/mock_network_manager.hpp | 28 | ||||
-rw-r--r-- | test/test_ethernet_interface.cpp | 5 | ||||
-rw-r--r-- | test/test_network_manager.cpp | 8 | ||||
-rw-r--r-- | test/test_rtnetlink.cpp | 10 | ||||
-rw-r--r-- | test/test_util.cpp | 11 | ||||
-rw-r--r-- | test/test_vlan_interface.cpp | 4 |
10 files changed, 71 insertions, 34 deletions
diff --git a/Makefile.am b/Makefile.am index fa3f92b..43f4643 100644 --- a/Makefile.am +++ b/Makefile.am @@ -99,4 +99,4 @@ xyz/openbmc_project/Network/IP/Create/server.hpp: xyz/openbmc_project/Network/IP $(SDBUSPLUSPLUS) -r $(srcdir) interface server-header xyz.openbmc_project.Network.IP.Create > $@ sed -i '5i #include \"xyz\/openbmc_project\/Network\/IP\/server.hpp\"' $@ -#SUBDIRS = test +SUBDIRS = test diff --git a/network_manager.cpp b/network_manager.cpp index 35a9fc8..bd1244d 100644 --- a/network_manager.cpp +++ b/network_manager.cpp @@ -212,5 +212,22 @@ void Manager::restartTimers() } } +void Manager::restartSystemdUnit(const std::string& unit) +{ + try + { + auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, + SYSTEMD_INTERFACE, "RestartUnit"); + method.append(unit.c_str(), "replace"); + bus.call_noreply(method); + } + catch (const sdbusplus::exception::SdBusError& ex) + { + log<level::ERR>("Failed to restart nslcd service", + entry("ERR=%s", ex.what())); + elog<InternalFailure>(); + } +} + } // namespace network } // namespace phosphor diff --git a/network_manager.hpp b/network_manager.hpp index 7a98f9a..a4d066c 100644 --- a/network_manager.hpp +++ b/network_manager.hpp @@ -35,9 +35,6 @@ using VLANCreateIface = details::ServerObject< } // namespace details -class TestNetworkManager; // forward declaration -class TestRtNetlink; // forward declaration - /** @class Manager * @brief OpenBMC network manager implementation. */ @@ -119,17 +116,9 @@ class Manager : public details::VLANCreateIface * @param[in] unit - systemd unit name which needs to be * restarted. */ - inline void restartSystemdUnit(const std::string& unit) - { - auto bus = sdbusplus::bus::new_default(); - auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, - SYSTEMD_INTERFACE, "RestartUnit"); + virtual void restartSystemdUnit(const std::string& unit); - method.append(unit, "replace"); - bus.call_noreply(method); - } - - private: + protected: /** @brief Persistent sdbusplus DBus bus connection. */ sdbusplus::bus::bus& bus; @@ -151,9 +140,6 @@ class Manager : public details::VLANCreateIface /** @brief Network Configuration directory. */ fs::path confDir; - - friend class TestNetworkManager; - friend class TestRtNetlink; }; } // namespace network diff --git a/test/Makefile.am b/test/Makefile.am index b726e53..78636fc 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -25,7 +25,7 @@ generic_cxx_flags = $(PTHREAD_CFLAGS) \ $(PHOSPHOR_LOGGING_CFLAGS) \ $(PHOSPHOR_DBUS_INTERFACES_CFLAGS) -generic_ld_flags = -lgtest_main -lgtest -lstdc++fs \ +generic_ld_flags = -lgtest_main -lgtest -lgmock -lstdc++fs \ $(OESDK_TESTCASE_FLAGS) \ $(SYSTEMD_LIBS) \ $(SDBUSPLUS_LIBS) \ diff --git a/test/mock_network_manager.hpp b/test/mock_network_manager.hpp new file mode 100644 index 0000000..a71301e --- /dev/null +++ b/test/mock_network_manager.hpp @@ -0,0 +1,28 @@ +#include "config.h" + +#include "network_manager.hpp" + +#include <gmock/gmock.h> + +namespace phosphor +{ +namespace network +{ + +class MockManager : public phosphor::network::Manager +{ + public: + MockManager(sdbusplus::bus::bus& bus, const char* path, + const std::string& dir) : + phosphor::network::Manager(bus, path, dir) + { + } + + MOCK_METHOD1(restartSystemdUnit, void(const std::string& service)); + + friend class TestNetworkManager; + friend class TestRtNetlink; +}; + +} // namespace network +} // namespace phosphor diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp index 1b284a2..9e611a6 100644 --- a/test/test_ethernet_interface.cpp +++ b/test/test_ethernet_interface.cpp @@ -1,7 +1,7 @@ #include "config_parser.hpp" #include "ipaddress.hpp" +#include "mock_network_manager.hpp" #include "mock_syscall.hpp" -#include "network_manager.hpp" #include <arpa/inet.h> #include <net/if.h> @@ -23,7 +23,7 @@ class TestEthernetInterface : public testing::Test { public: sdbusplus::bus::bus bus; - Manager manager; + MockManager manager; EthernetInterface interface; std::string confDir; TestEthernetInterface() : @@ -188,6 +188,7 @@ TEST_F(TestEthernetInterface, addNameServers) TEST_F(TestEthernetInterface, addNTPServers) { ServerList servers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"}; + EXPECT_CALL(manager, restartSystemdUnit(networkdService)).Times(1); interface.nTPServers(servers); fs::path filePath = confDir; filePath /= "00-bmc-test0.network"; diff --git a/test/test_network_manager.cpp b/test/test_network_manager.cpp index d97b182..911a1bd 100644 --- a/test/test_network_manager.cpp +++ b/test/test_network_manager.cpp @@ -1,5 +1,5 @@ +#include "mock_network_manager.hpp" #include "mock_syscall.hpp" -#include "network_manager.hpp" #include "xyz/openbmc_project/Common/error.hpp" #include <arpa/inet.h> @@ -9,7 +9,6 @@ #include <exception> #include <experimental/filesystem> -#include <phosphor-logging/elog-errors.hpp> #include <sdbusplus/bus.hpp> #include <gtest/gtest.h> @@ -19,13 +18,16 @@ namespace phosphor namespace network { +std::unique_ptr<Timer> refreshObjectTimer = nullptr; +std::unique_ptr<Timer> restartTimer = nullptr; + namespace fs = std::experimental::filesystem; class TestNetworkManager : public testing::Test { public: sdbusplus::bus::bus bus; - Manager manager; + MockManager manager; std::string confDir; TestNetworkManager() : bus(sdbusplus::bus::new_default()), diff --git a/test/test_rtnetlink.cpp b/test/test_rtnetlink.cpp index ee43bf4..9284600 100644 --- a/test/test_rtnetlink.cpp +++ b/test/test_rtnetlink.cpp @@ -1,5 +1,5 @@ +#include "mock_network_manager.hpp" #include "mock_syscall.hpp" -#include "network_manager.hpp" #include "rtnetlink_server.hpp" #include "types.hpp" @@ -19,9 +19,9 @@ namespace phosphor namespace network { sdbusplus::bus::bus bus(sdbusplus::bus::new_default()); -std::unique_ptr<Manager> manager = nullptr; -std::unique_ptr<Timer> refreshObjectTimer = nullptr; -std::unique_ptr<Timer> restartTimer = nullptr; +std::unique_ptr<MockManager> manager = nullptr; +extern std::unique_ptr<Timer> refreshObjectTimer; +extern std::unique_ptr<Timer> restartTimer; EventPtr eventPtr = nullptr; /** @brief refresh the network objects. */ @@ -50,7 +50,7 @@ class TestRtNetlink : public testing::Test TestRtNetlink() { manager = - std::make_unique<Manager>(bus, "/xyz/openbmc_test/bcd", "/tmp"); + std::make_unique<MockManager>(bus, "/xyz/openbmc_test/bcd", "/tmp"); sd_event* events; sd_event_default(&events); eventPtr.reset(events); diff --git a/test/test_util.cpp b/test/test_util.cpp index 437b3cf..6c647ed 100644 --- a/test/test_util.cpp +++ b/test/test_util.cpp @@ -2,6 +2,8 @@ #include <netinet/in.h> +#include <xyz/openbmc_project/Common/error.hpp> + #include <gtest/gtest.h> namespace phosphor @@ -9,6 +11,8 @@ namespace phosphor namespace network { +using InternalFailure = + sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; class TestUtil : public testing::Test { public: @@ -184,11 +188,10 @@ TEST_F(TestUtil, getNetworkAddress) address = getNetworkID(AF_INET6, "2001:db8:abcd:dd12::0", 64); EXPECT_EQ("2001:db8:abcd:dd12::", address); - address = getNetworkID(AF_INET, "a.b.c.d", 25); - EXPECT_EQ("", address); + EXPECT_THROW(getNetworkID(AF_INET, "a.b.c.d", 25), InternalFailure); - address = getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64); - EXPECT_EQ("", address); + EXPECT_THROW(getNetworkID(AF_INET6, "2001:db8:gghh:dd12::0", 64), + InternalFailure); address = getNetworkID(AF_INET6, "fe80::201:6cff:fe80:228", 64); EXPECT_EQ("fe80::", address); diff --git a/test/test_vlan_interface.cpp b/test/test_vlan_interface.cpp index 1bcd87a..7179212 100644 --- a/test/test_vlan_interface.cpp +++ b/test/test_vlan_interface.cpp @@ -1,7 +1,7 @@ #include "config_parser.hpp" #include "ipaddress.hpp" +#include "mock_network_manager.hpp" #include "mock_syscall.hpp" -#include "network_manager.hpp" #include "vlan_interface.hpp" #include <arpa/inet.h> @@ -25,7 +25,7 @@ class TestVlanInterface : public testing::Test { public: sdbusplus::bus::bus bus; - Manager manager; + MockManager manager; EthernetInterface interface; std::string confDir; TestVlanInterface() : |