Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ __pycache__
translib/ocbinds/ocbinds.go
models/yang/*.md
.idea
test/bookworm/*.deb
test/bookworm/*.whl
container
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible generate all outputs under build directory?

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`

56 changes: 56 additions & 0 deletions makefile.test
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 6 additions & 1 deletion models/yang/sonic/import.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What values will be passed through SONIC_YANG_IMPORTS variable?? I don't see any usage.. Also, $(shell echo $(SONIC_YANG_IMPORTS)) will be same as $(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
SONICYANG_IMPORTS += sonic-vrf.yang
80 changes: 80 additions & 0 deletions run_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash -x

# Run sanity tests for sonic-mgmt-common.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to run tests locally on the development server? If yes please explore the tools/test/translib-test.sh script as well. You can execute gotests without compiling a test binary.

# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see DETAILED_COV and SKIP_BUILD used anywhere. Is this an incomplete script?

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these coverage files will be generated under repo root, right? I don't think there is any cleanup step. Better to use some subdirectory under build directory as OUTPUT_DIR

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}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this script to test or tools/test directory?
Also, the content looks duplicate of steps in azure-pipelines.yml file. Can the pipeline sanity step just invoke this script?

14 changes: 14 additions & 0 deletions test/bookworm/Dockerfile
Original file line number Diff line number Diff line change
@@ -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