From e9984d8aa70318ab5b259cfbff214469a9aa84a7 Mon Sep 17 00:00:00 2001 From: mint570 <70396898+mint570@users.noreply.github.com> Date: Thu, 21 Jul 2022 18:29:13 -0700 Subject: [PATCH] Enable p4orch unit test for test workflow. (#2375) * Enable p4orch unit test for test workflow. * Add unit test coverage into coverage report. * Remove unit test files from coverage report. --- .azure-pipelines/gcov.yml | 2 +- orchagent/Makefile.am | 6 +- .../flex_counter/flex_counter_manager.cpp | 5 +- orchagent/flexcounterorch.h | 2 +- orchagent/p4orch/tests/Makefile.am | 9 +- .../p4orch/tests/fake_flexcounterorch.cpp | 30 +++ .../tests/fake_flowcounterrouteorch.cpp | 178 ++++++++++++++++++ orchagent/p4orch/tests/fake_portorch.cpp | 7 +- orchagent/p4orch/tests/test_main.cpp | 16 ++ tests/gcov_support.sh | 73 +++---- 10 files changed, 277 insertions(+), 51 deletions(-) create mode 100644 orchagent/p4orch/tests/fake_flexcounterorch.cpp create mode 100644 orchagent/p4orch/tests/fake_flowcounterrouteorch.cpp diff --git a/.azure-pipelines/gcov.yml b/.azure-pipelines/gcov.yml index 1bee32a03e01..0bd769222d82 100644 --- a/.azure-pipelines/gcov.yml +++ b/.azure-pipelines/gcov.yml @@ -14,7 +14,7 @@ parameters: - name: timeout type: number - default: 180 + default: 240 - name: sonic_slave type: string diff --git a/orchagent/Makefile.am b/orchagent/Makefile.am index 9524a61a196d..5755b1446ec5 100644 --- a/orchagent/Makefile.am +++ b/orchagent/Makefile.am @@ -6,6 +6,10 @@ INCLUDES = -I $(top_srcdir)/lib \ -I pbh \ -I nhg +if GCOV_ENABLED +SUBDIRS = p4orch/tests +endif + CFLAGS_SAI = -I /usr/include/sai swssdir = $(datadir)/swss @@ -18,7 +22,7 @@ dist_swss_DATA = \ pfc_detect_barefoot.lua \ pfc_detect_nephos.lua \ pfc_detect_cisco-8000.lua \ - pfc_detect_vs.lua \ + pfc_detect_vs.lua \ pfc_restore.lua \ pfc_restore_cisco-8000.lua \ port_rates.lua \ diff --git a/orchagent/flex_counter/flex_counter_manager.cpp b/orchagent/flex_counter/flex_counter_manager.cpp index ecccf415b2f7..95fb28171d0a 100644 --- a/orchagent/flex_counter/flex_counter_manager.cpp +++ b/orchagent/flex_counter/flex_counter_manager.cpp @@ -128,7 +128,10 @@ FlexCounterManager::~FlexCounterManager() flex_counter_table->del(getFlexCounterTableKey(group_name, counter)); } - flex_counter_group_table->del(group_name); + if (flex_counter_group_table != nullptr) + { + flex_counter_group_table->del(group_name); + } SWSS_LOG_DEBUG("Deleted flex counter group '%s'.", group_name.c_str()); } diff --git a/orchagent/flexcounterorch.h b/orchagent/flexcounterorch.h index c00a435b68b8..132bfa3b5e62 100644 --- a/orchagent/flexcounterorch.h +++ b/orchagent/flexcounterorch.h @@ -26,7 +26,7 @@ class FlexCounterOrch: public Orch std::shared_ptr m_flexCounterDb = nullptr; std::shared_ptr m_flexCounterGroupTable = nullptr; std::shared_ptr m_gbflexCounterDb = nullptr; - shared_ptr m_gbflexCounterGroupTable = nullptr; + std::shared_ptr m_gbflexCounterGroupTable = nullptr; bool m_port_counter_enabled = false; bool m_port_buffer_drop_counter_enabled = false; bool m_hostif_trap_counter_enabled = false; diff --git a/orchagent/p4orch/tests/Makefile.am b/orchagent/p4orch/tests/Makefile.am index 489acd8f992c..daeb48413621 100644 --- a/orchagent/p4orch/tests/Makefile.am +++ b/orchagent/p4orch/tests/Makefile.am @@ -4,7 +4,9 @@ INCLUDES = -I $(top_srcdir) -I $(ORCHAGENT_DIR) -I $(P4ORCH_DIR) -I $(top_srcdir CFLAGS_SAI = -I /usr/include/sai -bin_PROGRAMS = p4orch_tests p4orch_tests_asan p4orch_tests_tsan p4orch_tests_usan +TESTS = p4orch_tests p4orch_tests_asan p4orch_tests_tsan p4orch_tests_usan + +noinst_PROGRAMS = p4orch_tests p4orch_tests_asan p4orch_tests_tsan p4orch_tests_usan if DEBUG DBGFLAGS = -ggdb -DDEBUG @@ -27,6 +29,7 @@ p4orch_tests_SOURCES = $(ORCHAGENT_DIR)/orch.cpp \ $(ORCHAGENT_DIR)/switchorch.cpp \ $(ORCHAGENT_DIR)/request_parser.cpp \ $(ORCHAGENT_DIR)/flex_counter/flex_counter_manager.cpp \ + $(ORCHAGENT_DIR)/flex_counter/flow_counter_handler.cpp \ $(P4ORCH_DIR)/p4oidmapper.cpp \ $(P4ORCH_DIR)/p4orch.cpp \ $(P4ORCH_DIR)/p4orch_util.cpp \ @@ -39,9 +42,11 @@ p4orch_tests_SOURCES = $(ORCHAGENT_DIR)/orch.cpp \ $(P4ORCH_DIR)/acl_rule_manager.cpp \ $(P4ORCH_DIR)/wcmp_manager.cpp \ $(P4ORCH_DIR)/mirror_session_manager.cpp \ - $(top_srcdir)/tests/mock_tests/fake_response_publisher.cpp \ + $(top_srcdir)/tests/mock_tests/fake_response_publisher.cpp \ fake_portorch.cpp \ fake_crmorch.cpp \ + fake_flexcounterorch.cpp \ + fake_flowcounterrouteorch.cpp \ fake_dbconnector.cpp \ fake_producertable.cpp \ fake_consumerstatetable.cpp \ diff --git a/orchagent/p4orch/tests/fake_flexcounterorch.cpp b/orchagent/p4orch/tests/fake_flexcounterorch.cpp new file mode 100644 index 000000000000..a98795bd7776 --- /dev/null +++ b/orchagent/p4orch/tests/fake_flexcounterorch.cpp @@ -0,0 +1,30 @@ +#include "copporch.h" +#include "flexcounterorch.h" + +FlexCounterOrch::FlexCounterOrch(swss::DBConnector *db, std::vector &tableNames) + : Orch(db, tableNames), m_flexCounterConfigTable(db, CFG_FLEX_COUNTER_TABLE_NAME) +{ +} + +FlexCounterOrch::~FlexCounterOrch(void) +{ +} + +void FlexCounterOrch::doTask(Consumer &consumer) +{ +} + +bool FlexCounterOrch::getPortCountersState() const +{ + return true; +} + +bool FlexCounterOrch::getPortBufferDropCountersState() const +{ + return true; +} + +bool FlexCounterOrch::bake() +{ + return true; +} \ No newline at end of file diff --git a/orchagent/p4orch/tests/fake_flowcounterrouteorch.cpp b/orchagent/p4orch/tests/fake_flowcounterrouteorch.cpp new file mode 100644 index 000000000000..08caf52fe6d5 --- /dev/null +++ b/orchagent/p4orch/tests/fake_flowcounterrouteorch.cpp @@ -0,0 +1,178 @@ +#include "copporch.h" +#include "flowcounterrouteorch.h" + +extern size_t gMaxBulkSize; +extern sai_route_api_t *sai_route_api; + +#define ROUTE_FLOW_COUNTER_POLLING_INTERVAL_MS 10000 + +FlowCounterRouteOrch::FlowCounterRouteOrch(swss::DBConnector *db, const std::vector &tableNames) + : Orch(db, tableNames), mRouteFlowCounterMgr(ROUTE_FLOW_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ, + ROUTE_FLOW_COUNTER_POLLING_INTERVAL_MS, false), + gRouteBulker(sai_route_api, gMaxBulkSize) +{ +} + +FlowCounterRouteOrch::~FlowCounterRouteOrch(void) +{ +} + +void FlowCounterRouteOrch::generateRouteFlowStats() +{ +} + +void FlowCounterRouteOrch::clearRouteFlowStats() +{ +} + +void FlowCounterRouteOrch::addRoutePattern(const std::string &pattern, size_t) +{ +} + +void FlowCounterRouteOrch::removeRoutePattern(const std::string &pattern) +{ +} + +void FlowCounterRouteOrch::onAddMiscRouteEntry(sai_object_id_t vrf_id, const IpPrefix &ip_prefix, bool add_to_cache) +{ +} + +void FlowCounterRouteOrch::onAddMiscRouteEntry(sai_object_id_t vrf_id, const sai_ip_prefix_t &ip_pfx, bool add_to_cache) +{ +} + +void FlowCounterRouteOrch::onRemoveMiscRouteEntry(sai_object_id_t vrf_id, const IpPrefix &ip_prefix, + bool remove_from_cache) +{ +} + +void FlowCounterRouteOrch::onRemoveMiscRouteEntry(sai_object_id_t vrf_id, const sai_ip_prefix_t &ip_pfx, + bool remove_from_cache) +{ +} + +void FlowCounterRouteOrch::onAddVR(sai_object_id_t vrf_id) +{ +} + +void FlowCounterRouteOrch::onRemoveVR(sai_object_id_t vrf_id) +{ +} + +void FlowCounterRouteOrch::handleRouteAdd(sai_object_id_t vrf_id, const IpPrefix &ip_prefix) +{ +} + +void FlowCounterRouteOrch::handleRouteRemove(sai_object_id_t vrf_id, const IpPrefix &ip_prefix) +{ +} + +void FlowCounterRouteOrch::processRouteFlowCounterBinding() +{ +} + +void FlowCounterRouteOrch::doTask(Consumer &consumer) +{ +} + +void FlowCounterRouteOrch::doTask(SelectableTimer &timer) +{ +} + +void FlowCounterRouteOrch::initRouteFlowCounterCapability() +{ +} + +void FlowCounterRouteOrch::removeRoutePattern(const RoutePattern &route_pattern) +{ +} + +void FlowCounterRouteOrch::removeRouteFlowCounterFromDB(sai_object_id_t vrf_id, const IpPrefix &ip_prefix, + sai_object_id_t counter_oid) +{ +} + +bool FlowCounterRouteOrch::bindFlowCounter(const RoutePattern &route_pattern, sai_object_id_t vrf_id, + const IpPrefix &ip_prefix) +{ + return true; +} + +void FlowCounterRouteOrch::unbindFlowCounter(const RoutePattern &route_pattern, sai_object_id_t vrf_id, + const IpPrefix &ip_prefix, sai_object_id_t counter_oid) +{ +} + +void FlowCounterRouteOrch::pendingUpdateFlexDb(const RoutePattern &route_pattern, const IpPrefix &ip_prefix, + sai_object_id_t counter_oid) +{ +} + +void FlowCounterRouteOrch::updateRouterFlowCounterCache(const RoutePattern &route_pattern, const IpPrefix &ip_prefix, + sai_object_id_t counter_oid, RouterFlowCounterCache &cache) +{ +} + +bool FlowCounterRouteOrch::validateRoutePattern(const RoutePattern &route_pattern) const +{ + return true; +} + +void FlowCounterRouteOrch::onRoutePatternMaxMatchCountChange(RoutePattern &route_pattern, size_t new_max_match_count) +{ +} + +bool FlowCounterRouteOrch::isRouteAlreadyBound(const RoutePattern &route_pattern, const IpPrefix &ip_prefix) const +{ + return true; +} + +void FlowCounterRouteOrch::createRouteFlowCounterByPattern(const RoutePattern &route_pattern, size_t currentBoundCount) +{ +} + +bool FlowCounterRouteOrch::removeRouteFlowCounter(const RoutePattern &route_pattern, sai_object_id_t vrf_id, + const IpPrefix &ip_prefix) +{ + return true; +} + +void FlowCounterRouteOrch::createRouteFlowCounterFromVnetRoutes(const RoutePattern &route_pattern, + size_t ¤t_bound_count) +{ +} + +void FlowCounterRouteOrch::reapRouteFlowCounterByPattern(const RoutePattern &route_pattern, size_t currentBoundCount) +{ +} + +bool FlowCounterRouteOrch::isRouteFlowCounterEnabled() const +{ + return true; +} + +void FlowCounterRouteOrch::getRouteFlowCounterNameMapKey(sai_object_id_t vrf_id, const IpPrefix &ip_prefix, + std::string &key) +{ +} + +size_t FlowCounterRouteOrch::getRouteFlowCounterSizeByPattern(const RoutePattern &route_pattern) const +{ + return 0; +} + +bool FlowCounterRouteOrch::parseRouteKeyForRoutePattern(const std::string &key, char sep, sai_object_id_t &vrf_id, + IpPrefix &ip_prefix, std::string &vrf_name) +{ + return true; +} + +bool FlowCounterRouteOrch::getVrfIdByVnetName(const std::string &vnet_name, sai_object_id_t &vrf_id) +{ + return true; +} + +bool FlowCounterRouteOrch::getVnetNameByVrfId(sai_object_id_t vrf_id, std::string &vnet_name) +{ + return true; +} diff --git a/orchagent/p4orch/tests/fake_portorch.cpp b/orchagent/p4orch/tests/fake_portorch.cpp index aaf766e1aa74..51ff4503127c 100644 --- a/orchagent/p4orch/tests/fake_portorch.cpp +++ b/orchagent/p4orch/tests/fake_portorch.cpp @@ -206,7 +206,8 @@ bool PortsOrch::removeAclTableGroup(const Port &p) return true; } -bool PortsOrch::addSubPort(Port &port, const string &alias, const bool &adminUp, const uint32_t &mtu) +bool PortsOrch::addSubPort(Port &port, const string &alias, const string &vlan, const bool &adminUp, + const uint32_t &mtu) { return true; } @@ -400,7 +401,7 @@ void PortsOrch::initializePriorityGroups(Port &port) { } -void PortsOrch::initializePortMaximumHeadroom(Port &port) +void PortsOrch::initializePortBufferMaximumParameters(Port &port) { } @@ -685,7 +686,7 @@ void PortsOrch::voqSyncDelLagMember(Port &lag, Port &port) { } -std::unordered_set PortsOrch::generateCounterStats(const string &type) +std::unordered_set PortsOrch::generateCounterStats(const string &type, bool gearbox) { return {}; } \ No newline at end of file diff --git a/orchagent/p4orch/tests/test_main.cpp b/orchagent/p4orch/tests/test_main.cpp index 23cf37d8e130..203344e434c3 100644 --- a/orchagent/p4orch/tests/test_main.cpp +++ b/orchagent/p4orch/tests/test_main.cpp @@ -11,6 +11,7 @@ extern "C" #include "crmorch.h" #include "dbconnector.h" #include "directory.h" +#include "flowcounterrouteorch.h" #include "mock_sai_virtual_router.h" #include "p4orch.h" #include "portsorch.h" @@ -37,6 +38,8 @@ sai_object_id_t gUnderlayIfId; #define DEFAULT_BATCH_SIZE 128 int gBatchSize = DEFAULT_BATCH_SIZE; +#define DEFAULT_MAX_BULK_SIZE 1000 +size_t gMaxBulkSize = DEFAULT_MAX_BULK_SIZE; bool gSairedisRecord = true; bool gSwssRecord = true; bool gLogRotate = false; @@ -50,6 +53,7 @@ PortsOrch *gPortsOrch; CrmOrch *gCrmOrch; P4Orch *gP4Orch; VRFOrch *gVrfOrch; +FlowCounterRouteOrch *gFlowCounterRouteOrch; SwitchOrch *gSwitchOrch; Directory gDirectory; ofstream gRecordOfs; @@ -73,6 +77,8 @@ sai_switch_api_t *sai_switch_api; sai_mirror_api_t *sai_mirror_api; sai_udf_api_t *sai_udf_api; sai_tunnel_api_t *sai_tunnel_api; +sai_my_mac_api_t *sai_my_mac_api; +sai_counter_api_t *sai_counter_api; namespace { @@ -162,6 +168,9 @@ int main(int argc, char *argv[]) sai_switch_api_t switch_api; sai_mirror_api_t mirror_api; sai_udf_api_t udf_api; + sai_my_mac_api_t my_mac_api; + sai_tunnel_api_t tunnel_api; + sai_counter_api_t counter_api; sai_router_intfs_api = &router_intfs_api; sai_neighbor_api = &neighbor_api; sai_next_hop_api = &next_hop_api; @@ -174,6 +183,9 @@ int main(int argc, char *argv[]) sai_switch_api = &switch_api; sai_mirror_api = &mirror_api; sai_udf_api = &udf_api; + sai_my_mac_api = &my_mac_api; + sai_tunnel_api = &tunnel_api; + sai_counter_api = &counter_api; swss::DBConnector appl_db("APPL_DB", 0); swss::DBConnector state_db("STATE_DB", 0); @@ -193,6 +205,10 @@ int main(int argc, char *argv[]) gVrfOrch = &vrf_orch; gDirectory.set(static_cast(&vrf_orch)); + FlowCounterRouteOrch flow_counter_route_orch(gConfigDb, std::vector{}); + gFlowCounterRouteOrch = &flow_counter_route_orch; + gDirectory.set(static_cast(&flow_counter_route_orch)); + // Setup ports for all tests. SetupPorts(); AddVrf(); diff --git a/tests/gcov_support.sh b/tests/gcov_support.sh index 1395f0914907..b9d334bd1672 100755 --- a/tests/gcov_support.sh +++ b/tests/gcov_support.sh @@ -65,16 +65,6 @@ list_lcov_path() echo "Start searching .gcda files..." exec 4>$TMP_FILE find_gcda_file=`find ${gcda_dir} -name *.gcda` - echo "Start rm unused gcno files for speed up" - find_gcno_file=`find ${gcda_dir} -name *.gcno` - for line in ${find_gcno_file} - do - temp_gcda=${line/.gcno/$gcdastr} - if [ ! -f ${temp_gcda} ]; then - rm ${line} - fi - done - echo ${find_gcda_file} RESULT=${find_gcda_file} echo "$RESULT" >&4 @@ -93,8 +83,7 @@ lcov_genhtml_report() do local fullpath=$line local infoname=${INFO_FILE_PREFIX}${fullpath##*/}.info - htmldirname=${HTML_FILE_PREFIX}${fullpath##*/} - + echo ${fullpath} pushd ${fullpath} @@ -102,7 +91,7 @@ lcov_genhtml_report() echo "gcda count: $GCDA_COUNT" if [ $GCDA_COUNT -ge 1 ]; then echo "Executing lcov -c -d . -o ${infoname}" - lcov -c -d . -o ${infoname} + lcov -c -d . -o ${infoname} &>/dev/null if [ "$?" != "0" ]; then echo "lcov fail!" rm ${infoname} @@ -112,12 +101,6 @@ lcov_genhtml_report() done < ${gcda_file_range}/gcda_dir_list.txt } -rm_unused_gcno() -{ - cur_dir = $1/ - -} - # generate html reports for all eligible submodules lcov_genhtml_all() { @@ -143,6 +126,11 @@ lcov_merge_all() done < infolist lcov --extract total.info '*sonic-gcov/*' -o total.info + + # Remove unit test files. + lcov -o total.info -r total.info "*sonic-gcov/common_work/gcov/orchagent/p4orch/tests/*" + lcov -o total.info -r total.info "*sonic-gcov/common_work/gcov/tests/*" + cp $1/lcov_cobertura.py $1/common_work/gcov/ python $1/common_work/gcov/lcov_cobertura.py total.info -o coverage.xml @@ -214,7 +202,8 @@ gcov_support_generate_report() mkdir -p gcov_output/info #for same code path - mkdir -p common_work + mkdir -p common_work/gcov + tar -zxvf swss.tar.gz -C common_work/gcov cat container_dir_list while read line @@ -223,7 +212,6 @@ gcov_support_generate_report() echo ${container_id} cp -rf ${container_id}/* common_work - tar -zxvf swss.tar.gz -C common_work/gcov cd common_work/gcov/ find -name gcda*.tar.gz > tmp_gcda.txt while read LINE ; do @@ -233,13 +221,16 @@ gcov_support_generate_report() done < tmp_gcda.txt rm tmp_gcda.txt - find -name gcno*.tar.gz > tmp_gcno.txt - while read LINE ; do - echo ${LINE} - echo ${LINE%%.*} - tar -zxvf ${LINE} - done < tmp_gcno.txt - rm tmp_gcno.txt + gcno_count=`find -name "*.gcno" | wc -l` + if [ ${gcno_count} -lt 1 ]; then + find -name gcno*.tar.gz > tmp_gcno.txt + while read LINE ; do + echo ${LINE} + echo ${LINE%%.*} + tar -zxvf ${LINE} + done < tmp_gcno.txt + rm tmp_gcno.txt + fi cd - ls -lh common_work/* @@ -248,22 +239,20 @@ gcov_support_generate_report() echo "###lcov operation fail.." return 0 fi - cd common_work - find . -name "*.gcda" -o -name "*.gcno" -o -name "*.gz" -o -name "*.cpp" -o -name "*.h"| xargs rm -rf - cd ../ - cp -rf common_work/* ${container_id}/* - cd ${container_id} - find . -name "*.gcda" -o -name "*.gcno" -o -name "*.gz" -o -name "*.cpp" -o -name "*.h"| xargs rm -rf - cd ../ - - rm -rf common_work/* - - cp -rf ${container_id} gcov_output/ + mkdir -p gcov_output/${container_id} + cp -rf common_work/* gcov_output/${container_id}/* + pushd gcov_output/${container_id} + find . -name "*.gcda" -o -name "*.gcno" -o -name "*.gz" -o -name "*.cpp" -o -name "*.h" | xargs rm -rf + popd + pushd common_work + find . -name "*.gcda" -o -name "*.gz" -o -name "*.info" | xargs rm -rf + popd done < container_dir_list # generate report with code - mkdir -p common_work/gcov - tar -zxvf swss.tar.gz -C common_work/gcov + pushd common_work/gcov + find . -name "*.gcno" | xargs rm -rf + popd echo "### Make info generating completed !!" } @@ -339,7 +328,7 @@ gcov_support_collect_gcno() echo " === Start collecting .gcno files... === " submodule_name=$1 exec 3>$GCNO_LIST_FILE - find_command=`find -name *.gcno` + find_command=`find -name "*.gcno" -o -name "*.gcda"` echo "${find_command}" if [ -z "${find_command}" ]; then echo "### Error! no gcno files found!"