参考:
https://github.com/Azure/sonic-swss/blob/412c5ebd1762f94afc51b1129f54a27a53f132bb/tests/README.md
https://github.com/Azure/sonic-swss-common/blob/7ee11792342f2673ea6905952d556e3f9a27234d/README.md
准备工作
- Ubuntu 16.04
- 已经编译好的docker-sonic-vs.gz文件
测试环境搭建
- 安装pytest所需工具
nonames@Ubuntu16:~$ sudo pip install docker==3.5.0
nonames@Ubuntu16:~$ sudo pip install pytest==3.3.0
nonames@Ubuntu16:~$ sudo pip install flaky redis
- 确保attrs的版本是19.1.0而不是19.3.0
nonames@Ubuntu16:~$ pip list | grep att
attrs 19.1.0
nonames@Ubuntu16:~$
#如果不符,重新安装attrs==19.1.0
nonames@Ubuntu16:~$ sudo pip install attrs==19.1.0
- 安装必要的docker工具
nonames@Ubuntu16:~$ sudo apt-get remove docker-engine docker-ce docker.io
nonames@Ubuntu16:~$ sudo apt-get update
nonames@Ubuntu16:~$ sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
nonames@Ubuntu16:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
nonames@Ubuntu16:~$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
nonames@Ubuntu16:~$ sudo apt-get update
nonames@Ubuntu16:~$ sudo apt-get install -y docker-ce
- 将当前用户加入到docker group
nonames@Ubuntu16:~$ sudo gpasswd -a ${USER} docker
Adding user nocsys to group docker
nonames@Ubuntu16:~$
nonames@Ubuntu16:~$ sudo service docker restart
nonames@Ubuntu16:~$ exit
#记得一定要退出登录,然后再重新登录进来,group的属性设置才会生效
- 运行一个debian容器
nonames@Ubuntu16:~$ docker run --privileged -id --name sw debian bash
nonames@Ubuntu16:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84a887f6e18c debian "bash" 15 hours ago Up 15 hours sw
nonames@Ubuntu16:~$
- 运行脚本,创建namespace和接口
nonames@Ubuntu16:~$ cat create_vnet.sh
#!/bin/bash
SWNAME=$1
pid=$(docker inspect --format '{{.State.Pid}}' $SWNAME)
echo Seting up servers
SERVERS=31
for srv in `seq 0 $SERVERS`; do
SRV="$SWNAME-srv$srv"
NSS="ip netns exec $SRV"
ip netns add $SRV
$NSS ip addr add 127.0.0.1/8 dev lo
$NSS ip addr add ::1/128 dev lo
$NSS ip link set dev lo up
# add virtual link between neighbor and the virtual switch docker
IF="eth$((srv+1))"
ip link add ${SRV}eth0 type veth peer name $IF
ip link set ${SRV}eth0 netns $SRV
ip link set $IF netns ${pid}
echo "Bring ${SRV}eth0 up"
$NSS ip link set dev ${SRV}eth0 name eth0
$NSS ip link set dev eth0 up
echo "Bring $IF up in the virtual switch docker"
nsenter -t $pid -n ip link set dev $IF up
done
nonames@Ubuntu16:~$ chmod +x create_vnet.sh
nonames@Ubuntu16:~$ sudo ./create_vnet.sh sw
- 进入debian容器查看确认
nonames@Ubuntu16:~$ docker exec -it 84a887f6e18c bash
root@84a887f6e18c:/# ip add
- load已经编好的vs镜像
nonames@Ubuntu16:~$ docker load < docker-sonic-vs.gz
55b19a5e648f: Loading layer [==================================================>] 105.6MB/105.6MB
ad806406fa9e: Loading layer [==================================================>] 89.87MB/89.87MB
b558b958c618: Loading layer [==================================================>] 101MB/101MB
54ca9ea9d47b: Loading layer [==================================================>] 143MB/143MB
Loaded image: docker-sonic-vs:latest
nonames@Ubuntu16:~$
nonames@Ubuntu16:~$ docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-sonic-vs latest 76353f9bcf86 2 weeks ago 425MB
hello-world latest bf756fb1ae65 3 months ago 13.3kB
nonames@Ubuntu16:~$
- 运行vs容器
nonames@Ubuntu16:~$ docker run --privileged -v /var/run/redis-vs/sw:/var/run/redis --network container:sw -d --name vs docker-sonic-vs
88c5033fb2aca4db0955ffcba528b56ed593182cb1bb9f87a779520458b8c71f
nonames@Ubuntu16:~$
nonames@Ubuntu16:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
88c5033fb2ac docker-sonic-vs "/usr/bin/supervisord" 7 seconds ago Up 6 seconds vs
84a887f6e18c debian "bash" 15 hours ago Up 15 hours sw
nonames@Ubuntu16:~$
- 获取sonic-swss和sonic-swss-common源码
nonames@Ubuntu16:~$ git clone --recursive https://github.com/Azure/sonic-swss
nonames@Ubuntu16:~$ git clone --recursive https://github.com/Azure/sonic-swss-common
- 安装必要的软件
nonames@Ubuntu16:~/sonic-swss-common$ sudo apt-get install make libtool m4 autoconf dh-exec debhelper cmake pkg-config \
> libhiredis-dev libnl-3-dev libnl-genl-3-dev libnl-route-3-dev swig3.0 \
> libpython2.7-dev libgtest-dev
- 安装编译gtest
nonames@Ubuntu16:~/sonic-swss-common$ sudo apt-get install cmake libgtest-dev
nonames@Ubuntu16:~/sonic-swss-common$ cd /usr/src/gtest && sudo cmake . && sudo make
- 编译sonic-swss-common
nonames@Ubuntu16:/usr/src/gtest$ cd -
/home/nocsys/sonic-swss-common
nonames@Ubuntu16:~/sonic-swss-common$
nonames@Ubuntu16:~/sonic-swss-common$ ./autogen.sh
nonames@Ubuntu16:~/sonic-swss-common$ ./configure --enable-debug 'CXXFLAGS=-O0 -g'
nonames@Ubuntu16:~/sonic-swss-common$ GCC_COLORS=1 make
nonames@Ubuntu16:~/sonic-swss-common$ dpkg-buildpackage -us -uc -b
...
dpkg-deb: building package 'libswsscommon' in '../libswsscommon_1.0.0_amd64.deb'.
dpkg-deb: building package 'libswsscommon-dbg' in '../libswsscommon-dbg_1.0.0_amd64.deb'.
dpkg-deb: building package 'libswsscommon-dev' in '../libswsscommon-dev_1.0.0_amd64.deb'.
dpkg-deb: building package 'python-swsscommon' in '../python-swsscommon_1.0.0_amd64.deb'.
dpkg-genchanges -b >../sonic_1.0.0_amd64.changes
dpkg-genchanges: binary-only upload (no source code included)
dpkg-source --after-build sonic-swss-common
dpkg-buildpackage: binary-only upload (no source included)
nonames@Ubuntu16:~/sonic-swss-common$
- 查看编译出的包
nonames@Ubuntu16:~/sonic-swss-common$ ll ../ | grep deb
-rw-r--r-- 1 nocsys nocsys 162824 4月 28 10:00 libswsscommon_1.0.0_amd64.deb
-rw-r--r-- 1 nocsys nocsys 2706748 4月 28 10:00 libswsscommon-dbg_1.0.0_amd64.deb
-rw-r--r-- 1 nocsys nocsys 52474 4月 28 10:00 libswsscommon-dev_1.0.0_amd64.deb
-rw-r--r-- 1 nocsys nocsys 1258606 4月 28 10:00 python-swsscommon_1.0.0_amd64.deb
nonames@Ubuntu16:~/sonic-swss-common$
- 安装编译出的两个包
nonames@Ubuntu16:~/sonic-swss-common$ sudo dpkg -i ../libswsscommon_1.0.0_amd64.deb
nonames@Ubuntu16:~/sonic-swss-common$ sudo dpkg -i ../python-swsscommon_1.0.0_amd64.deb
运行测试用例
- 进入测试用例所在目录
nonames@Ubuntu16:~/sonic-swss/tests$
- 运行单个测试用例
nonames@Ubuntu16:~/sonic-swss/tests$ sudo pytest -s -v --dvsname=vs test_port.py::TestPort::test_PortMtu
========================================================================================== test session starts ==========================================================================================
platform linux2 -- Python 2.7.12, pytest-3.3.0, py-1.8.1, pluggy-0.6.0 -- /usr/bin/python
cachedir: .cache
rootdir: /home/nocsys/sonic-swss/tests, inifile:
plugins: flaky-3.6.1
collected 1 item
test_port.py::TestPort::test_PortMtu remove extra link dummy
PASSED [100%]
======================================================================================= 1 passed in 42.29 seconds =======================================================================================
nonames@Ubuntu16:~/sonic-swss/tests$
- 如果要跑目录下所有测试用例
nonames@Ubuntu16:~/sonic-swss/tests$ sudo pytest -s -v --dvsname=vs ./
- 用pytest-html来生成更加漂亮的测试结果
nonames@Ubuntu16:~/sonic-swss/tests$ sudo pip install pytest-html
然后
nonames@Ubuntu16:~/sonic-swss/tests$ sudo pytest -s -v --dvsname=vs ./ --html=report.html --self-contained-html
-
查看report