Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unittest infrastructure #5

Merged
merged 26 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a31e497
Add unittest infrastructure
kellyyeh Jun 17, 2022
6e06a4d
Test infra changes/improvements
saiarcot895 Jun 17, 2022
4f204a1
Add unittests
kellyyeh May 17, 2022
14a0f07
Get tests compiling and running
saiarcot895 Jul 28, 2022
9ba8014
Publish test results and code coverage
saiarcot895 Jul 28, 2022
a345c12
Fix some compile warnings
saiarcot895 Jul 31, 2022
235601b
Start up redis server, and initialize Sonic DB for tests
saiarcot895 Jul 31, 2022
2f5e1a6
Actually start the redis server
saiarcot895 Jul 31, 2022
4662418
Improve test cases
saiarcot895 Aug 17, 2022
67b7ac0
Add new libswsscommon dependency in lgtm (#11)
kellyyeh Aug 12, 2022
da42bf1
Clean Up
kellyyeh Sep 26, 2022
7bbf43f
Fix azure pipeline and sonic-swss-common dependency conflict (#13)
kellyyeh Aug 22, 2022
9721542
Add libzmq3-dev (#14)
kellyyeh Aug 22, 2022
9a1cc2a
[dhcprelay] Replace memset function (#12)
maipbui Aug 29, 2022
142a7ae
Add interface-id option (#8)
kellyyeh Sep 1, 2022
bd5329a
Add interface id
kellyyeh Sep 26, 2022
8c03080
Install libyang to azure pipeline (#17)
liuh-80 Sep 16, 2022
ea4570a
Open different socket to enable interface filtering for dual tor scen…
kellyyeh Sep 19, 2022
78c9d5f
Fix conflict
kellyyeh Oct 10, 2022
8e4cd87
Clean up
kellyyeh Oct 10, 2022
0330abc
Fix redis in azure-pipeline
kellyyeh Oct 12, 2022
713a305
Fix redis
kellyyeh Oct 12, 2022
02fd127
Remove global redis_db
kellyyeh Oct 24, 2022
5fd56e4
Add dualtor tests
kellyyeh Oct 24, 2022
0542a1a
Merge branch 'master' of https://github.com/kellyyeh/sonic-dhcp-relay…
kellyyeh Nov 15, 2022
2ecfe1d
Deleted callback tests
kellyyeh Nov 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .azure-pipelines/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ jobs:
libnl-3-dev \
libnl-route-3-dev \
libnl-genl-3-dev \
libnl-nf-3-dev
libnl-nf-3-dev \
redis-server
sudo sed -ri 's/^# unixsocket/unixsocket/' /etc/redis/redis.conf
sudo sed -ri 's/^unixsocketperm .../unixsocketperm 777/' /etc/redis/redis.conf
sudo sed -ri 's/redis-server.sock/redis.sock/' /etc/redis/redis.conf
sudo service redis-server start

displayName: "Install dependencies"
- checkout: self
Expand Down Expand Up @@ -94,9 +99,13 @@ jobs:
- publish: $(Build.ArtifactStagingDirectory)
artifact: sonic-dhcp-relay.${{ parameters.arch }}
displayName: "Archive dhcp-relay debian packages"
- task: PublishTestResults@2
inputs:
testResultsFiles: build-test/dhcp6relay-test-test-result.xml
- ${{ if and(eq(parameters.arch, 'amd64'), parameters.codeCoverage) }}:
- task: PublishCodeCoverageResults@1
inputs:
summaryFileLocation: dhcprelay-test-result.xml
summaryFileLocation: build-test/dhcp6relay-test-code-coverage.xml
pathToSources: $(Build.SourcesDirectory)
reportDirectory: $(Build.SourcesDirectory)/build-test
codeCoverageTool: 'Cobertura'
56 changes: 43 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,66 @@
RM := rm -rf
DHCP6RELAY_TARGET := dhcp6relay
BUILD_DIR := build
BUILD_TEST_DIR := build-test
DHCP6RELAY_TARGET := $(BUILD_DIR)/dhcp6relay
DHCP6RELAY_TEST_TARGET := $(BUILD_TEST_DIR)/dhcp6relay-test
CP := cp
MKDIR := mkdir
MV := mv
FIND := find
GCOVR := gcovr
override LDLIBS += -levent -lhiredis -lswsscommon -pthread -lboost_thread -lboost_system
override CPPFLAGS += -Wall -std=c++17 -fPIE -I/usr/include/swss
override CPPFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)"
CPPFLAGS_TEST := --coverage -fprofile-arcs -ftest-coverage -fprofile-generate -fsanitize=address
LDLIBS_TEST := --coverage -lgtest -pthread -lstdc++fs -fsanitize=address
PWD := $(shell pwd)

all: $(DHCP6RELAY_TARGET)
all: $(DHCP6RELAY_TARGET) $(DHCP6RELAY_TEST_TARGET)

-include src/subdir.mk
-include test/subdir.mk

# Use different build directories based on whether it's a regular build or a
# test build. This is because in the test build, code coverage is enabled,
# which means the object files that get built will be different
OBJS = $(SRCS:%.cpp=$(BUILD_DIR)/%.o)
TEST_OBJS = $(TEST_SRCS:%.cpp=$(BUILD_TEST_DIR)/%.o)

ifneq ($(MAKECMDGOALS),clean)
-include $(OBJS:%.o=%.d)
-include $(TEST_OBJS:%.o=%.d)
endif

-include src/subdir.mk
$(BUILD_DIR)/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<

$(DHCP6RELAY_TARGET): $(OBJS)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

install:
$(MKDIR) -p $(DESTDIR)/usr/sbin
$(MV) $(DHCP6RELAY_TARGET) $(DESTDIR)/usr/sbin
$(BUILD_TEST_DIR)/%.o: %.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(CPPFLAGS_TEST) -c -o $@ $<

deinstall:
$(RM) $(DESTDIR)/usr/sbin/$(DHCP6RELAY_TARGET)
$(RM) -rf $(DESTDIR)/usr/sbin
$(DHCP6RELAY_TEST_TARGET): $(TEST_OBJS)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) $(LDLIBS_TEST) -o $@

clean:
-$(RM) $(EXECUTABLES) $(OBJS:%.o=%.d) $(OBJS) $(DHCP6RELAY_TARGET)
-@echo ' '
test: $(DHCP6RELAY_TEST_TARGET)
sudo ASAN_OPTIONS=detect_leaks=0 ./$(DHCP6RELAY_TEST_TARGET) --gtest_output=xml:$(DHCP6RELAY_TEST_TARGET)-test-result.xml || true
$(GCOVR) -r ./ --html --html-details -o $(DHCP6RELAY_TEST_TARGET)-code-coverage.html
$(GCOVR) -r ./ --xml-pretty -o $(DHCP6RELAY_TEST_TARGET)-code-coverage.xml

install: $(DHCP6RELAY_TARGET)
install -D $(DHCP6RELAY_TARGET) $(DESTDIR)/usr/sbin/$(notdir $(DHCP6RELAY_TARGET))

.PHONY: all clean dependents
uninstall:
$(RM) $(DESTDIR)/usr/sbin/$(notdir $(DHCP6RELAY_TARGET))

clean:
-$(RM) $(BUILD_DIR) $(BUILD_TEST_DIR) *.html *.xml
$(FIND) . -name *.gcda -exec rm -f {} \;
$(FIND) . -name *.gcno -exec rm -f {} \;
$(FIND) . -name *.gcov -exec rm -f {} \;
-@echo ' '

.PHONY: all clean test install uninstall
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
arch: amd64
pool:
vmImage: 'ubuntu-20.04'
codeCoverage: false
codeCoverage: true
containerImage: sonicdev-microsoft.azurecr.io:443/sonic-slave-bullseye:latest
- template: .azure-pipelines/build.yml
parameters:
Expand Down
22 changes: 12 additions & 10 deletions src/configInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ constexpr auto DEFAULT_TIMEOUT_MSEC = 1000;

bool pollSwssNotifcation = true;
std::shared_ptr<boost::thread> mSwssThreadPtr;

std::shared_ptr<swss::DBConnector> configDbPtr = std::make_shared<swss::DBConnector> ("CONFIG_DB", 0);
swss::SubscriberStateTable ipHelpersTable(configDbPtr.get(), "DHCP_RELAY");
swss::Select swssSelect;

/**
Expand All @@ -22,9 +19,14 @@ swss::Select swssSelect;
void initialize_swss(std::vector<relay_config> *vlans)
{
try {
std::shared_ptr<swss::DBConnector> configDbPtr = std::make_shared<swss::DBConnector> ("CONFIG_DB", 0);
swss::SubscriberStateTable ipHelpersTable(configDbPtr.get(), "DHCP_RELAY");
swssSelect.addSelectable(&ipHelpersTable);
get_dhcp(vlans);
mSwssThreadPtr = std::make_shared<boost::thread> (&handleSwssNotification, vlans);
get_dhcp(vlans, &ipHelpersTable);
struct swssNotification test;
test.vlans = vlans;
test.ipHelpersTable = &ipHelpersTable;
mSwssThreadPtr = std::make_shared<boost::thread> (&handleSwssNotification, test);
}
catch (const std::bad_alloc &e) {
syslog(LOG_ERR, "Failed allocate memory. Exception details: %s", e.what());
Expand Down Expand Up @@ -52,15 +54,15 @@ void deinitialize_swss()
*
* @return none
*/
void get_dhcp(std::vector<relay_config> *vlans) {
void get_dhcp(std::vector<relay_config> *vlans, swss::SubscriberStateTable *ipHelpersTable) {
swss::Selectable *selectable;
int ret = swssSelect.select(&selectable, DEFAULT_TIMEOUT_MSEC);
if (ret == swss::Select::ERROR) {
syslog(LOG_WARNING, "Select: returned ERROR");
} else if (ret == swss::Select::TIMEOUT) {
}
if (selectable == static_cast<swss::Selectable *> (&ipHelpersTable)) {
handleRelayNotification(ipHelpersTable, vlans);
if (selectable == static_cast<swss::Selectable *> (ipHelpersTable)) {
handleRelayNotification(*ipHelpersTable, vlans);
}
}
/**
Expand All @@ -72,10 +74,10 @@ void get_dhcp(std::vector<relay_config> *vlans) {
*
* @return none
*/
void handleSwssNotification(std::vector<relay_config> *vlans)
void handleSwssNotification(swssNotification test)
{
while (pollSwssNotifcation) {
get_dhcp(vlans);
get_dhcp(test.vlans, test.ipHelpersTable);
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/configInterface.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#pragma once

#include <boost/thread.hpp>
#include "subscriberstatetable.h"
#include "select.h"
#include "relay.h"

struct swssNotification {
std::vector<relay_config> *vlans;
swss::SubscriberStateTable *ipHelpersTable;
};
/**
* @code void initialize_swss()
*
Expand All @@ -28,18 +34,18 @@ void deinitialize_swss();
*
* @return none
*/
void get_dhcp(std::vector<relay_config> *vlans);
void get_dhcp(std::vector<relay_config> *vlans, swss::SubscriberStateTable *ipHelpersTable);

/**
* @code void handleSwssNotification(std::vector<relay_config> *vlans)
* @code void swssNotification test
*
* @brief main thread for handling SWSS notification
*
* @param vlans list of vlans/argument config that contains strings of server and option
* @param test swssNotification that includes list of vlans/argument config that contains strings of server and option
*
* @return none
*/
void handleSwssNotification(std::vector<relay_config> *vlans);
void handleSwssNotification(swssNotification test);

/**
* @code void handleRelayNotification(swss::SubscriberStateTable &ipHelpersTable, std::vector<relay_config> *vlans)
Expand Down
40 changes: 15 additions & 25 deletions src/relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ struct event *ev_sigterm;
static std::string vlan_member = "VLAN_MEMBER|";

static std::string counter_table = "DHCPv6_COUNTER_TABLE|";
struct database redis_db;

/* DHCPv6 filter */
/* sudo tcpdump -dd "inbound and ip6 dst ff02::1:2 && udp dst port 547" */
Expand Down Expand Up @@ -221,24 +220,9 @@ const struct dhcpv6_option *parse_dhcpv6_opt(const uint8_t *buffer, const uint8_
return option;
}

/**
* @code void send_udp(int sock, uint8_t *buffer, struct sockaddr_in6 target, uint32_t n, relay_config *config, uint8_t msg_type);
*
* @brief send udp packet
*
* @param *buffer message buffer
* @param sockaddr_in6 target target socket
* @param n length of message
* @param relay_config *config pointer to relay_config
* @param uint8_t msg_type message type of dhcpv6 option of relayed message
*
* @return dhcpv6_option end of dhcpv6 message option
*/
void send_udp(int sock, uint8_t *buffer, struct sockaddr_in6 target, uint32_t n, relay_config *config, uint8_t msg_type) {
void process_sent_msg(relay_config *config, uint8_t msg_type) {
std::string counterVlan = counter_table;
if(sendto(sock, buffer, n, 0, (const struct sockaddr *)&target, sizeof(target)) == -1)
syslog(LOG_ERR, "sendto: Failed to send to target address\n");
else if (counterMap.find(msg_type) != counterMap.end()) {
if (counterMap.find(msg_type) != counterMap.end()) {
counters[msg_type]++;
update_counter(config->state_db, counterVlan.append(config->interface), msg_type);
} else {
Expand Down Expand Up @@ -497,7 +481,9 @@ void relay_client(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_h
current_buffer_position += dhcp_message_length + sizeof(dhcpv6_option);

for(auto server: config->servers_sock) {
send_udp(sock, buffer, server, current_buffer_position - buffer, config, new_message.msg_type);
if(send_udp(sock, buffer, server, current_buffer_position - buffer)) {
process_sent_msg(config, new_message.msg_type);
}
}
}

Expand Down Expand Up @@ -537,7 +523,9 @@ void relay_relay_forw(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *
current_buffer_position += dhcp_message_length + sizeof(dhcpv6_option);

for(auto server: config->servers_sock) {
send_udp(sock, buffer, server, current_buffer_position - buffer, config, new_message.msg_type);
if(send_udp(sock, buffer, server, current_buffer_position - buffer)) {
process_sent_msg(config, new_message.msg_type);
}
}
}

Expand Down Expand Up @@ -589,7 +577,9 @@ void relay_relay_forw(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *
target_addr.sin6_port = htons(CLIENT_PORT);
target_addr.sin6_scope_id = if_nametoindex(config->interface.c_str());

send_udp(sock, buffer, target_addr, current_buffer_position - buffer, config, type);
if(send_udp(sock, buffer, target_addr, current_buffer_position - buffer)) {
process_sent_msg(config, type);
}
}

/**
Expand Down Expand Up @@ -727,9 +717,9 @@ void callback_dual_tor(evutil_socket_t fd, short event, void *arg) {
return;
std::string state;
std::string intf(interfaceName);
redis_db.muxTable->hget(intf, "state", state);
config->mux_table->hget(intf, "state", state);

if (state != "standby" && redis_db.config_db->exists(key.append(intf))) {
if (state != "standby" && config->config_db->exists(key.append(intf))) {
char* ptr = (char *)message_buffer;
const uint8_t *current_position = (uint8_t *)ptr;
const uint8_t *tmp = NULL;
Expand Down Expand Up @@ -1006,8 +996,6 @@ void loop_relay(std::vector<relay_config> *vlans) {
std::shared_ptr<swss::Table> mStateDbMuxTablePtr = std::make_shared<swss::Table> (
state_db.get(), "HW_MUX_CABLE_TABLE"
);
redis_db.config_db = config_db;
redis_db.muxTable = mStateDbMuxTablePtr;

int filter = 0;
filter = sock_open(&ether_relay_fprog);
Expand All @@ -1017,6 +1005,8 @@ void loop_relay(std::vector<relay_config> *vlans) {
relay_config *config = &vlan;
int local_sock = 0;
int server_sock = 0;
config->config_db = config_db;
config->mux_table = mStateDbMuxTablePtr;
config->state_db = state_db;
config->mux_key = vlan_member + config->interface + "|";

Expand Down
Loading