From 132209547a0366762bf044509baadfc216297035 Mon Sep 17 00:00:00 2001 From: jumao Date: Tue, 12 Dec 2023 17:44:01 -0500 Subject: [PATCH] [VOQ][saidump] Add saidump unit test scripts #3079 https://github.com/sonic-net/sonic-utilities/pull/2972 added two below functions into scripts/generate_dump. get_route_table_size_by_asic_id_and_ipver save_saidump_by_route_size The unittest scripts need to be added. Related PRs: https://github.com/sonic-net/sonic-utilities/pull/2972 https://github.com/sonic-net/sonic-buildimage/pull/16466 https://github.com/sonic-net/sonic-sairedis/pull/1288 https://github.com/sonic-net/sonic-sairedis/pull/1298 Microsoft ADO (25892277): Add two scripts: tests/saidump_test.py tests/saidump_test.sh To use below 6 test cases to verify the functionality of get_route_table_size_by_asic_id_and_ipver and save_saidump_by_route_size behave correctly. ``` saidump test list format: [ACIS number, ipv4 and ipv6 route table size, expected function save_cmd arguments] saidump_test_list = [ [1, 10000, "docker exec syncd saidump saidump"], [1, 12000, "docker exec syncd saidump saidump"], [1, 12001, "docker exec syncd saidump.sh saidump"], [1, 20000, "docker exec syncd saidump.sh saidump"], [2, 10000, "docker exec syncd0 saidump saidump0\ndocker exec syncd1 saidump saidump1"], [2, 12000, "docker exec syncd0 saidump saidump0\ndocker exec syncd1 saidump saidump1"], [2, 12001, "docker exec syncd0 saidump.sh saidump0\ndocker exec syncd1 saidump.sh saidump1"], [2, 20000, "docker exec syncd0 saidump.sh saidump0\ndocker exec syncd1 saidump.sh saidump1"] ] ``` During the compiling stage, run the below command to check if it's PASSED. jumao@1b1ffba5949a:/sonic/src/sonic-utilities$ time python3 setup.py test tests/saidump_test.py::test_saidump PASSED --- tests/saidump_test.py | 56 +++++++++++++++++++++++++++++++++++++++++++ tests/saidump_test.sh | 41 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100755 tests/saidump_test.py create mode 100755 tests/saidump_test.sh diff --git a/tests/saidump_test.py b/tests/saidump_test.py new file mode 100755 index 0000000000..2ee6fb2da1 --- /dev/null +++ b/tests/saidump_test.py @@ -0,0 +1,56 @@ +import os +import subprocess + +GENERATE_DUMP_FILE = "/tmp/generate_dump_tmp" + +test_path = os.path.dirname(os.path.abspath(__file__)) +modules_path = os.path.dirname(test_path) +scripts_path = os.path.join(modules_path, "scripts") + + +def run_saidump(asic_num, route_num): + result = subprocess.run([". " + test_path + "/saidump_test.sh && test_saidump " + str(asic_num) + + " " + str(route_num)], capture_output=True, text=True, shell=True, executable='/bin/bash') + return result.stdout.strip() + + +# saidump test list format: [ACIS number, ipv4 and ipv6 route table size, expected function save_cmd arguments] +saidump_test_list = [ + [1, 10000, "docker exec syncd saidump saidump"], + [1, 12000, "docker exec syncd saidump saidump"], + [1, 12001, "docker exec syncd saidump.sh saidump"], + [1, 20000, "docker exec syncd saidump.sh saidump"], + [2, 10000, "docker exec syncd0 saidump saidump0\ndocker exec syncd1 saidump saidump1"], + [2, 12000, "docker exec syncd0 saidump saidump0\ndocker exec syncd1 saidump saidump1"], + [2, 12001, "docker exec syncd0 saidump.sh saidump0\ndocker exec syncd1 saidump.sh saidump1"], + [2, 20000, "docker exec syncd0 saidump.sh saidump0\ndocker exec syncd1 saidump.sh saidump1"] +] + + +def shell_pre_process(in_file, out_file): + buf = [] + + with open(in_file, 'r') as file: + lines = file.readlines() + + for line in lines: + if line.strip().startswith("while getopts"): + break + else: + buf.append(line) + + # Write the modified content back to the file + with open(out_file, 'w') as file: + file.writelines(buf) + + +def test_saidump(): + # preprocess the generate_dump to a new script without non-functional codes + shell_pre_process(scripts_path + '/generate_dump', GENERATE_DUMP_FILE) + + for item in saidump_test_list: + output = run_saidump(item[0], item[1]) + assert output == item[2] + + if os.path.exists(GENERATE_DUMP_FILE): + os.remove(GENERATE_DUMP_FILE) diff --git a/tests/saidump_test.sh b/tests/saidump_test.sh new file mode 100755 index 0000000000..429c300279 --- /dev/null +++ b/tests/saidump_test.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +GENERATE_DUMP_FILE="/tmp/generate_dump_tmp" +ROUTE_FILE="/tmp/route_summary.txt" +shopt -s expand_aliases +alias eval="mock_eval" +alias save_cmd="mock_save_cmd" + +source $GENERATE_DUMP_FILE + +mock_route_summary(){ + local routenum=$1 + echo "\ + { + \"routesTotal\":$routenum + }" > $ROUTE_FILE +} + +route_num=0 +save_cmd_arguments="" + +# Mock eval command in function get_route_table_size_by_asic_id_and_ipver +mock_eval(){ + mock_route_summary $route_num +} + +# Mock function save_cmd called in function save_saidump_by_route_size +mock_save_cmd(){ + save_cmd_arguments="$save_cmd_arguments$*\n" +} + +test_saidump(){ + NUM_ASICS=$1 + route_num=$2 + save_saidump_by_route_size > /dev/null + echo -e $save_cmd_arguments + local ret=$save_cmd_arguments + save_cmd_arguments="" + rm $ROUTE_FILE + return $ret +} \ No newline at end of file