Skip to content

Commit

Permalink
[fpmsyncd]: Add unit tests for SRv6 steer routes
Browse files Browse the repository at this point in the history
* Added a unit test to check that fpmsyncd updates the APP_DB correctly
when it receives an SRv6 steer route with an IPv4 destination prefix
(`RecevingSRv6SteerRoutesWithIPv4Prefix`).
* Added a unit test to check that fpmsyncd updates the APP_DB correctly
when it receives an SRv6 steer route with an IPv6 destination prefix
(`RecevingSRv6SteerRoutesWithIPv6Prefix`).

Signed-off-by: Carmine Scarpitta <carmine.scarpitta@uniroma2.it>
  • Loading branch information
cscarpitta committed Nov 14, 2022
1 parent f50814e commit f9a7cdf
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 2 deletions.
23 changes: 21 additions & 2 deletions tests/mock_tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ P4_ORCH_DIR = $(top_srcdir)/orchagent/p4orch

CFLAGS_SAI = -I /usr/include/sai

TESTS = tests tests_intfmgrd tests_portsyncd
TESTS = tests tests_intfmgrd tests_portsyncd tests_fpmsyncd

noinst_PROGRAMS = tests tests_intfmgrd tests_portsyncd
noinst_PROGRAMS = tests tests_intfmgrd tests_portsyncd tests_fpmsyncd

LDADD_SAI = -lsaimeta -lsaimetadata -lsaivs -lsairedis

Expand Down Expand Up @@ -166,3 +166,22 @@ tests_intfmgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST
tests_intfmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_intfmgrd_INCLUDES)
tests_intfmgrd_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis \
-lswsscommon -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 -lpthread

## fpmsyncd unit tests

tests_fpmsyncd_SOURCES = fpmsyncd/receive_srv6_steer_routes_ut.cpp \
fpmsyncd/ut_helpers_fpmsyncd.cpp \
$(top_srcdir)/fpmsyncd/routesync.cpp \
$(top_srcdir)/fpmsyncd/fpmlink.cpp \
$(top_srcdir)/warmrestart/warmRestartHelper.cpp \
mock_dbconnector.cpp \
mock_hiredis.cpp \
mock_redisreply.cpp \
mock_table.cpp

tests_fpmsyncd_INCLUDES = -I $(top_srcdir)/fpmsyncd -I $(top_srcdir)/warmrestart
tests_fpmsyncd_CXXFLAGS = -Wl,-wrap,rtnl_link_i2name
tests_fpmsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST)
tests_fpmsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(tests_fpmsyncd_INCLUDES)
tests_fpmsyncd_LDADD = $(LDADD_GTEST) -lhiredis -lhiredis \
-lswsscommon -lswsscommon -lgtest -lgtest_main -lpthread -lgmock
118 changes: 118 additions & 0 deletions tests/mock_tests/fpmsyncd/receive_srv6_steer_routes_ut.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "ut_helpers_fpmsyncd.h"
#include "gtest/gtest.h"
#include <gmock/gmock.h>
#include "mock_table.h"
#include <net/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include "ipaddress.h"
#include "ipprefix.h"

#define private public // Need to modify internal cache
#include "fpmlink.h"
#include "routesync.h"
#undef private

using namespace swss;
using namespace testing;

/*
Test Fixture
*/
namespace ut_fpmsyncd
{
struct FpmSyncdSRv6RoutesTest : public ::testing::Test
{
std::shared_ptr<swss::DBConnector> m_app_db;
std::shared_ptr<swss::RedisPipeline> pipeline;
std::shared_ptr<RouteSync> m_routeSync;
std::shared_ptr<FpmLink> m_fpmLink;
std::shared_ptr<swss::Table> m_routeTable;

virtual void SetUp() override
{
testing_db::reset();

m_app_db = std::make_shared<swss::DBConnector>("APPL_DB", 0);

/* Construct dependencies */

/* 1) RouteSync */
pipeline = std::make_shared<swss::RedisPipeline>(m_app_db.get());
m_routeSync = std::make_shared<RouteSync>(pipeline.get());

/* 2) FpmLink */
m_fpmLink = std::make_shared<FpmLink>(m_routeSync.get());

/* 3) ROUTE_TABLE in APP_DB */
m_routeTable = std::make_shared<swss::Table>(m_app_db.get(), APP_ROUTE_TABLE_NAME);
}

virtual void TearDown() override
{
}
};
}

namespace ut_fpmsyncd
{
/* Test Receiving an SRv6 Steer Route (with an IPv4 prefix) */
TEST_F(FpmSyncdSRv6RoutesTest, RecevingSRv6SteerRoutesWithIPv4Prefix)
{
ASSERT_NE(m_routeSync, nullptr);

/* Create a Netlink object containing an SRv6 Steer Route */
IpPrefix _dst = IpPrefix("192.168.6.0/24");
IpAddress _vpn_sid = IpAddress("fc00:0:2:1::");
IpAddress _encap_src_addr = IpAddress("fc00:0:1:1::1");

struct nlmsg *nl_obj = create_srv6_steer_route_nlmsg(RTM_NEWROUTE, &_dst, &_encap_src_addr, &_vpn_sid);
if (!nl_obj)
throw std::runtime_error("SRv6 Steer Route creation failed");

/* Send the Netlink object to the FpmLink */
m_fpmLink->processRawMsg(&nl_obj->n);

/* Check that fpmsyncd created the correct entries in APP_DB */
std::string vpn_sid;
ASSERT_EQ(m_routeTable->hget("Vrf10:192.168.6.0/24", "vpn_sid", vpn_sid), true);
ASSERT_EQ(vpn_sid, "fc00:0:2:1::");

std::string seg_src;
ASSERT_EQ(m_routeTable->hget("Vrf10:192.168.6.0/24", "seg_src", seg_src), true);
ASSERT_EQ(seg_src, "fc00:0:1:1::1");

/* Destroy the Netlink object and free the memory */
free_nlobj(nl_obj);
}

/* Test Receiving an SRv6 Steer Route (with an IPv6 prefix) */
TEST_F(FpmSyncdSRv6RoutesTest, RecevingSRv6SteerRoutesWithIPv6Prefix)
{
ASSERT_NE(m_routeSync, nullptr);

/* Create a Netlink object containing an SRv6 Steer Route */
IpPrefix _dst = IpPrefix("fd00:0:21::/64");
IpAddress _vpn_sid = IpAddress("fc00:0:2:1::");
IpAddress _encap_src_addr = IpAddress("fc00:0:1:1::1");

struct nlmsg *nl_obj = create_srv6_steer_route_nlmsg(RTM_NEWROUTE, &_dst, &_encap_src_addr, &_vpn_sid);
if (!nl_obj)
throw std::runtime_error("SRv6 Steer Route creation failed");

/* Send the Netlink object to the FpmLink */
m_fpmLink->processRawMsg(&nl_obj->n);

/* Check that fpmsyncd created the correct entries in APP_DB */
std::string vpn_sid;
ASSERT_EQ(m_routeTable->hget("Vrf10:fd00:0:21::/64", "vpn_sid", vpn_sid), true);
ASSERT_EQ(vpn_sid, "fc00:0:2:1::");

std::string seg_src;
ASSERT_EQ(m_routeTable->hget("Vrf10:fd00:0:21::/64", "seg_src", seg_src), true);
ASSERT_EQ(seg_src, "fc00:0:1:1::1");

/* Destroy the Netlink object and free the memory */
free_nlobj(nl_obj);
}
}

0 comments on commit f9a7cdf

Please sign in to comment.