test: Clear out old mocks prior to testing
This ensures we don't have stale mock data left over after each run
that will leak into unrelated tests.
Change-Id: I08bc12f99f1103c5d3024bf90047105b221622a2
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/mock_syscall.cpp b/test/mock_syscall.cpp
index 3f96e1d..021c304 100644
--- a/test/mock_syscall.cpp
+++ b/test/mock_syscall.cpp
@@ -41,6 +41,15 @@
std::map<int, std::string> mock_if_indextoname;
std::map<std::string, ether_addr> mock_macs;
+void mock_clear()
+{
+ mock_ifaddrs = nullptr;
+ ifaddr_count = 0;
+ mock_if_nametoindex.clear();
+ mock_if_indextoname.clear();
+ mock_macs.clear();
+}
+
void mock_addIF(const std::string& name, int idx, const ether_addr& mac)
{
if (idx == 0)
diff --git a/test/mock_syscall.hpp b/test/mock_syscall.hpp
index 4d934b3..74a693e 100644
--- a/test/mock_syscall.hpp
+++ b/test/mock_syscall.hpp
@@ -3,6 +3,10 @@
#include <string>
+/** @brief Clears out the interfaces and IPs configured for mocking
+ */
+void mock_clear();
+
/** @brief Adds the given interface and addr info
* into the ifaddr list.
* @param[in] name - Interface name.
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index c25bba9..5ea7f7a 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -56,6 +56,7 @@
static EthernetInterface makeInterface(sdbusplus::bus::bus& bus,
MockManager& manager)
{
+ mock_clear();
mock_addIF("test0", 1, mac);
return {bus, "/xyz/openbmc_test/network/test0", false, manager};
}
diff --git a/test/test_neighbor.cpp b/test/test_neighbor.cpp
index ffb0d5d..ecde351 100644
--- a/test/test_neighbor.cpp
+++ b/test/test_neighbor.cpp
@@ -61,6 +61,7 @@
TEST(ParseNeighbor, NoAttrs)
{
+ mock_clear();
mock_addIF(ifStr, ifIdx);
nlmsghdr hdr{};
@@ -78,6 +79,7 @@
TEST(ParseNeighbor, NoAddress)
{
+ mock_clear();
mock_addIF(ifStr, ifIdx);
nlmsghdr hdr{};
@@ -104,6 +106,7 @@
TEST(ParseNeighbor, NoMAC)
{
+ mock_clear();
mock_addIF(ifStr, ifIdx);
nlmsghdr hdr{};
@@ -137,6 +140,7 @@
TEST(ParseNeighbor, Full)
{
+ mock_clear();
mock_addIF(ifStr, ifIdx);
nlmsghdr hdr{};
diff --git a/test/test_netlink.cpp b/test/test_netlink.cpp
index b788dd1..41cd1ca 100644
--- a/test/test_netlink.cpp
+++ b/test/test_netlink.cpp
@@ -1,3 +1,4 @@
+#include "mock_syscall.hpp"
#include "netlink.hpp"
#include "util.hpp"
diff --git a/test/test_network_manager.cpp b/test/test_network_manager.cpp
index f4de35a..2837296 100644
--- a/test/test_network_manager.cpp
+++ b/test/test_network_manager.cpp
@@ -70,6 +70,8 @@
bool caughtException = false;
try
{
+ mock_clear();
+
// Adds the following ip in the getifaddrs list.
mock_addIF("igb1", 2);
mock_addIP("igb1", "192.0.2.3", "255.255.255.128",
@@ -93,6 +95,8 @@
{
try
{
+ mock_clear();
+
mock_addIF("igb0", 1);
mock_addIP("igb0", "192.0.2.2", "255.255.255.128",
IFF_UP | IFF_RUNNING);
diff --git a/test/test_rtnetlink.cpp b/test/test_rtnetlink.cpp
index 794a0dd..6a8c02d 100644
--- a/test/test_rtnetlink.cpp
+++ b/test/test_rtnetlink.cpp
@@ -87,6 +87,7 @@
TEST_F(TestRtNetlink, WithSingleInterface)
{
using namespace std::chrono;
+ mock_clear();
// Adds the following ip in the getifaddrs list.
mock_addIF("igb5", 6);
mock_addIP("igb5", "127.0.0.1", "255.255.255.128", IFF_UP | IFF_RUNNING);
diff --git a/test/test_vlan_interface.cpp b/test/test_vlan_interface.cpp
index 0ff893c..1dffc7e 100644
--- a/test/test_vlan_interface.cpp
+++ b/test/test_vlan_interface.cpp
@@ -48,6 +48,7 @@
static EthernetInterface makeInterface(sdbusplus::bus::bus& bus,
MockManager& manager)
{
+ mock_clear();
mock_addIF("test0", 1);
return {bus, "/xyz/openbmc_test/network/test0", false, manager};
}