Skip to content

Commit

Permalink
[VOQ][saidump] Add saidump unit test scripts #3079
Browse files Browse the repository at this point in the history
#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:
#2972
sonic-net/sonic-buildimage#16466
sonic-net/sonic-sairedis#1288
sonic-net/sonic-sairedis#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
  • Loading branch information
JunhongMao committed Dec 13, 2023
1 parent 1ca9676 commit 1322095
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/saidump_test.py
Original file line number Diff line number Diff line change
@@ -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)
41 changes: 41 additions & 0 deletions tests/saidump_test.sh
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1322095

Please sign in to comment.