diff --git a/.gitignore b/.gitignore index c414cc7ab..f0c75650c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ __pycache__ translib/ocbinds/ocbinds.go models/yang/*.md .idea +test/bookworm/*.deb +test/bookworm/*.whl +container diff --git a/README.md b/README.md new file mode 100644 index 000000000..730e6aa72 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +## How to execute and clean up unit-tests in sonic-mgmt-common: +* `make -f makefile.test`: to execute all unit tests +* `make -f makefile.test clean`: clean up all the generated build artifacts +* `make -f makefile.test container`: build the test container image. The file `container` contains the container image information + +## How to build unit-test container with customer +Note, it is best to `make -f makefile.test clean` before running a test with a customized library. + +### With a new LIBYANG version in ../../target/debs/bullseye/ +* `LIBYANG=1.0.74 make -f makefile.test` +### With a LIBYANG 1.0.75 in the user home directory +* `LIBYANG=1.0.74 SONIC_TARGET_DEBS=~ make -f makefile.test` +### With a sonic_yang_models in the user home directory +* `SONIC_TARGET_WHL=~ make -f makefile.test` + diff --git a/makefile.test b/makefile.test new file mode 100644 index 000000000..2fa752b2c --- /dev/null +++ b/makefile.test @@ -0,0 +1,56 @@ +TOPDIR := $(abspath .) + +.PHONY: test + +LIBYANG_VER ?= 1.0.73 +LIBYANG_DEBS := libyang_$(LIBYANG_VER)_amd64.deb \ + libyang-dev_$(LIBYANG_VER)_amd64.deb \ + libyang-dbgsym_$(LIBYANG_VER)_amd64.deb \ + libyang-cpp_$(LIBYANG_VER)_amd64.deb + +YANG_MODELS := sonic_yang_models-1.0-py3-none-any.whl + +SONIC_WS ?= $(TOPDIR)/../.. +SONIC_TARGET_DEBS ?= $(SONIC_WS)/target/debs/bookworm/ +SONIC_TARGET_WHL ?= $(SONIC_WS)/target/python-wheels/bookworm/ + +CONTAINER_IMG = sonic-mgmt-common-test:$(USER) + +DOCKER_RUN := docker run -it -v $(TOPDIR):$(TOPDIR):rw -v $(SONIC_WS):$(SONIC_WS):rw \ + -e no_proxy=$(no_proxy) \ + -e http_proxy=$(http_proxy) \ + -e https_proxy=$(https_proxy) \ + -e ftp_proxy=$(ftp_proxy) \ + $(CONTAINER_IMG) + +test: container + $(DOCKER_RUN) bash -c "cd $(TOPDIR) && ./run_test.sh && exit 0" + +dev: container + $(DOCKER_RUN) bash + +container: $(addprefix test/bookworm/, $(LIBYANG_DEBS)) \ + test/bookworm/$(YANG_MODELS) + docker build --build-arg no_proxy=$(no_proxy) \ + --build-arg http_proxy=$(http_proxy) \ + --build-arg https_proxy=$(https_proxy) \ + --build-arg ftp_proxy=$(ftp_proxy) \ + -t $(CONTAINER_IMG) \ + test/bookworm + docker inspect $(CONTAINER_IMG) > $@ + +test/bookworm/libyang%$(LIBYANG_VER)_amd64.deb: $(SONIC_TARGET_DEBS)/libyang%$(LIBYANG_VER)_amd64.deb + cp $< $(@D) + +test/bookworm/$(YANG_MODELS): $(SONIC_TARGET_WHL)/$(YANG_MODELS) + cp $< $(@D) + +clean: + rm -f container + rm -f test/bookworm/libyang*.deb + rm -f test/bookworm/$(YANG_MODELS) + + for id in $(shell docker ps -a -q --filter ancestor=$(CONTAINER_IMG) --format="{{.ID}}"); do \ + docker rm $$id; \ + done + docker image rm $(CONTAINER_IMG) diff --git a/models/yang/sonic/import.mk b/models/yang/sonic/import.mk index 20410c055..76507fb30 100644 --- a/models/yang/sonic/import.mk +++ b/models/yang/sonic/import.mk @@ -5,10 +5,15 @@ # or glob patterns of basenames (like sonic-telemetry*.yang) can be specified. # Other sonic yangs referred by these will also be copied. # + +ifneq ($(SONIC_YANG_IMPORTS),) +SONICYANG_IMPORTS = $(shell echo $(SONIC_YANG_IMPORTS)) +endif + SONICYANG_IMPORTS += sonic-sflow.yang SONICYANG_IMPORTS += sonic-interface.yang SONICYANG_IMPORTS += sonic-port.yang SONICYANG_IMPORTS += sonic-portchannel.yang SONICYANG_IMPORTS += sonic-mclag.yang SONICYANG_IMPORTS += sonic-types.yang -SONICYANG_IMPORTS += sonic-vrf.yang \ No newline at end of file +SONICYANG_IMPORTS += sonic-vrf.yang diff --git a/run_test.sh b/run_test.sh new file mode 100755 index 000000000..8435b7ddb --- /dev/null +++ b/run_test.sh @@ -0,0 +1,80 @@ +#!/bin/bash -x + +# Run sanity tests for sonic-mgmt-common. +# Assumes sonic-mgmt-common is already compiled and all dependencies +# are installed. + +STATUS=0 +DEBDIR=$(realpath debian/sonic-mgmt-common) + +OUTPUT_DIR=./ +DETAILED_COV=n +SKIP_BUILD=n +while getopts 'dhto:' opt; do + case "$opt" in + t) + echo "Running test only" + SKIP_BUILD=y + ;; + o) + echo "Coverage HTML directory" + OUTPUT_DIR="$OPTARG" + ;; + d) + DETAILED_COV=y + ;; + ?|h) + echo "Usage: $(basename $0) [-t] [-d] [-o dir]" + exit 100 + ;; + esac +done + +# build debian packages +INCLUDE_TEST_MODELS=y dpkg-buildpackage -rfakeroot -us -uc -b -j$(nproc) +if [ "$?" -ne "0" ];then + echo "Error!!! Compilation failed" + exit 1 +fi + +function generate_html_report() { + /usr/local/go/bin/go tool cover -html=$1 -o ${OUTPUT_DIR}/$1.html +} + +redis_ready=$(ps aux | grep -ie "redis-server" | grep -ie bin) +if [ -z "$redis_ready" ]; then + sudo service redis-server start + echo "Starting redis-server status: $?" +else + echo "redis-server already started: $redis_ready" +fi + +# Update unixsocket path in database_config.json +tools/test/dbconfig.py -o ${PWD}/build/tests/database_config.json +export DB_CONFIG_PATH=${PWD}/build/tests/database_config.json + +# Run CVL tests +pushd build/tests/cvl +CVL_SCHEMA_PATH=testdata/schema \ + ./cvl.test -test.v -alsologtostderr -test.coverprofile coverage.cvl || STATUS=1 +generate_html_report coverage.cvl +popd + +# Run translib tests +pushd build/tests/translib +export CVL_SCHEMA_PATH=${DEBDIR}/usr/sbin/schema +export YANG_MODELS_PATH=${DEBDIR}/usr/models/yang +./db.test -test.v -alsologtostderr -test.coverprofile coverage.db || STATUS=2 +generate_html_report coverage.db + +# Populates test data in essential tables like PORT, DEVICE_METADATA, SWITCH_TABLE, USER_TABLE etc. +${PWD}/../../../tools/test/dbinit.py --overwrite + +./translib.test -test.v -alsologtostderr -test.coverprofile coverage.translib || STATUS=3 +generate_html_report coverage.translib + +./testapp.test -test.v -alsologtostderr -test.coverprofile coverage.transformer || STATUS=4 +./transformer.test -test.v -alsologtostderr -test.coverprofile coverage.transformer || STATUS=5 +generate_html_report coverage.transformer +popd +exit ${STATUS} diff --git a/test/bookworm/Dockerfile b/test/bookworm/Dockerfile new file mode 100644 index 000000000..d31fc39ea --- /dev/null +++ b/test/bookworm/Dockerfile @@ -0,0 +1,14 @@ +FROM sonicdev-microsoft.azurecr.io:443/sonic-slave-bookworm:latest + +RUN apt-get -y update +RUN apt-get install -y redis-server +RUN sed -ri 's/^# unixsocket/unixsocket/' /etc/redis/redis.conf +RUN sed -ri 's/^unixsocketperm .../unixsocketperm 777/' /etc/redis/redis.conf +RUN sed -ri 's/redis-server.sock/redis.sock/' /etc/redis/redis.conf + +ARG LIBYANG_VER=1.0.73 +COPY libyang*${LIBYANG_VER}*.deb /tmp/ +RUN dpkg -i /tmp/libyang*${LIBYANG_VER}*.deb + +COPY sonic_yang_models-1.0-py3-none-any.whl /tmp/ +RUN pip3 install /tmp/sonic_yang_models-1.0-py3-none-any.whl