Skip to content

Commit

Permalink
Transformer Infra Unit Test (sonic-net#81)
Browse files Browse the repository at this point in the history
* Added openconfig style test yang for transformer unit-test

* Add sonic test yang and annotation files, Makefile changes to build the test yangs

* Transfomer callbacks for transfomer test yang annotations for unit testing
Makefile rules to build transformer test binary
Unit tests for transformer test yang exercising subtree-xfmr, virtual-table, table-name, key and field transformer cases.

* Add unit test utility functions, test xfmr yang calbacks and UT cases for table and key transformers and sonic yang SET and GET operations

* Added readme for transformer unit test

* corrected copyright

* Corrected copyright info

* Addressed review comments

* Updated transformer test README file

* Added review comments addressed files missing in last commit

* Modified the make file rules for test yang as per review comments

* Made sure test yangs are present before building transformer test binary

* Avoid building test xfmr callbacks into transformer pkg as part of tranlib test due to build tag being same

* include transformer test yang in models_list

---------

Co-authored-by: ranjinidn <ranjini.nagaraj@dell.com>
Co-authored-by: ranjinidn <ranjini_nagaraj@dell.com>
  • Loading branch information
3 people authored May 24, 2023
1 parent 1340e29 commit cc214fb
Show file tree
Hide file tree
Showing 12 changed files with 2,359 additions and 3 deletions.
24 changes: 22 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,31 @@ translib-all: $(GO_DEPS)
translib-clean:
$(MAKE) -C ./translib clean

transformer-test:
ifdef INCLUDE_TEST_MODELS
cp ./translib/transformer/test/*test-xfmr-annot.yang models/yang/annotations/.
cp ./translib/transformer/test/openconfig-test-xfmr.yang models/yang/.
cp ./translib/transformer/test/sonic-test-xfmr.yang models/yang/sonic/.
if ! grep -q test ./config/transformer/models_list; then \
echo "openconfig-test-xfmr.yang" >> ./config/transformer/models_list; \
echo "openconfig-test-xfmr-annot.yang" >> ./config/transformer/models_list; \
echo "sonic-test-xfmr-annot.yang" >> ./config/transformer/models_list; \
fi
endif

transformer-test-clean:
ifdef INCLUDE_TEST_MODELS
$(RM) ./models/yang/annotations/*test*.yang
$(RM) ./models/yang/*test*.yang
$(RM) ./models/yang/sonic/*test*.yang
sed -i '/test/d' ./config/transformer/models_list
endif

.PHONY: models
models:
models: transformer-test
$(MAKE) -C models/yang

models-clean:
models-clean: transformer-test-clean
$(MAKE) -C models/yang clean

annotgen: $(GOYANG_BIN)
Expand Down
4 changes: 3 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ stages:
- script: |
ls -l
dpkg-buildpackage -rfakeroot -us -uc -b -j$(nproc) && cp ../*.deb .
INCLUDE_TEST_MODELS=y dpkg-buildpackage -rfakeroot -us -uc -b -j$(nproc) && cp ../*.deb .
displayName: "Build"
- publish: $(System.DefaultWorkingDirectory)/
Expand Down Expand Up @@ -96,6 +96,8 @@ stages:
./translib.test -test.v -logtostderr || STATUS=1
./transformer.test -test.v -logtostderr || STATUS=1
popd
exit ${STATUS}
Expand Down
11 changes: 11 additions & 0 deletions translib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ TRANSLIB_TEST_DIR = $(BUILD_DIR)/tests/translib
TRANSLIB_TEST_BIN = $(TRANSLIB_TEST_DIR)/translib.test
TRANSL_DB_TEST_BIN = $(TRANSLIB_TEST_DIR)/db.test

TRANSFORMER_TEST_BIN = $(TRANSLIB_TEST_DIR)/transformer.test
TRANSFORMER_SRCS = $(filter ./transformer/%, $(shell find . -name '*.go' -not -name '*_test.go'))
TRANSFORMER_TESTS := $(shell find . -name '*_test.go')
TRANSFORMER_TEST_SRCS = $(filter ./transformer/%, $(TRANSFORMER_TESTS))

YANG_FILES = $(shell find $(TOPDIR)/models/yang -name '*.yang')
YGOT_BINDS = ocbinds/ocbinds.go

DEFAULT_TARGETS = $(YGOT_BINDS)
ifeq ($(NO_TEST_BINS),)
DEFAULT_TARGETS += $(TRANSLIB_TEST_BIN) $(TRANSL_DB_TEST_BIN)
ifdef INCLUDE_TEST_MODELS
DEFAULT_TARGETS += $(TRANSFORMER_TEST_BIN)
endif
endif

default: $(DEFAULT_TARGETS)
Expand All @@ -42,6 +50,9 @@ $(TRANSLIB_TEST_BIN): $(TRANSLIB_MAIN_SRCS) $(TRANSLIB_TEST_SRCS) $(YGOT_BINDS)
$(TRANSL_DB_TEST_BIN) : $(TRANSL_DB_ALL_SRCS)
$(GO) test -mod=vendor -cover -c ../translib/db -o $@

$(TRANSFORMER_TEST_BIN): $(TRANSFORMER_TEST_SRCS) $(TRANSFORMER_SRCS) $(TRANSLIB_MAIN_SRCS) $(YGOT_BINDS)
$(GO) test -mod=vendor -tags xfmrtest -c -vet=off -cover -coverpkg=../translib/transformer ../translib/transformer -o $@

$(YGOT_BINDS): $(YANG_FILES)
$(RM) $@
cd ocbinds && $(GO) generate
Expand Down
15 changes: 15 additions & 0 deletions translib/transformer/test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Transformer Unit Testing

Following are the instructions on how to build and execute transformer unit test.
The transformer folder (sonic-mgmt-common/translib/transformer) contains all the necessary source code files that includes transformer unit test framework files, callbacks to serve the annotations, unit-test file to exercise the transformer-translations.The test folder (sonic-mgmt-common/translib/transformer/test) contains the test-yangs and annotations. All these files are needed to build and execute transformer test binary.

* Generate transformer.test by building sonic-mgmt-common with the MAKE flag INCLUDE_TEST_MODELS=y to have the test yangs built
* Copy the openconfig-test-xfmr.yang, openconfig-test-xfmr-annot.yang, sonic-test-xfmr.yang, sonic-test-xfmr-annot.yang to mgmt-framework docker /usr/models/yang directory
* Edit the models_list file in mgmt-framework docker /usr/models/yang directory to include openconfig-test-xfmr.yang, openconfig-test-xfmr-annot.yang and sonic-test-xfmr-annot.yang files
* Copy the sonic-test-xfmr.yin file from sonic-mgmtcommon/build/cvl/schema/ to /usr/sbin/schema/ in mgmt-framework docker
* Copy the transformer.test binary to mgmt-framework docker in /usr/sbin directory and then execute :
```shell
(./transformer.test -test.v -test.coverprofile=transformer.cover -logtostderr -v=5 | tee transformer.out ) >& transformer.log
```
* View the results in file transformer.out (All test-cases should have PASS prefix)
* View the transformer.log file to view debug logs for debugging the test-case failures if any.
144 changes: 144 additions & 0 deletions translib/transformer/test/openconfig-test-xfmr-annot.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
module openconfig-test-xfmr-annot {

yang-version "1";

namespace "http://openconfig.net/yang/annotation/oc-test-xfmr-annot";
prefix "oc-test-xfmr-annot";

import openconfig-extensions { prefix oc-ext; }
import openconfig-test-xfmr { prefix oc-test-xfmr; }
import sonic-extensions { prefix sonic-ext; }

deviation /oc-test-xfmr:test-xfmr {
deviate add {
sonic-ext:pre-transformer "test_pre_xfmr";
sonic-ext:post-transformer "test_post_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group {
deviate add {
sonic-ext:table-name "TEST_SENSOR_GROUP";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:config/oc-test-xfmr:id {
deviate add {
sonic-ext:field-transformer "test_sensor_group_id_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:config/oc-test-xfmr:group-colors {
deviate add {
sonic-ext:field-name "colors";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:state/oc-test-xfmr:id {
deviate add {
sonic-ext:field-transformer "test_sensor_group_id_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:state/oc-test-xfmr:group-colors {
deviate add {
sonic-ext:field-name "colors";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:state/oc-test-xfmr:counters {
deviate add {
sonic-ext:table-name "TEST_SENSOR_GROUP_COUNTERS";
sonic-ext:db-name "COUNTERS_DB";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:test-sensor-types {
deviate add {
sonic-ext:table-name "NONE";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:test-sensor-types/oc-test-xfmr:test-sensor-type {
deviate add {
sonic-ext:table-transformer "test_sensor_type_tbl_xfmr";
sonic-ext:key-transformer "test_sensor_type_key_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:test-sensor-types/oc-test-xfmr:test-sensor-type/oc-test-xfmr:config/oc-test-xfmr:type {
deviate add {
sonic-ext:field-transformer "test_sensor_type_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:test-sensor-types/oc-test-xfmr:test-sensor-type/oc-test-xfmr:config/oc-test-xfmr:exclude-filter {
deviate add {
sonic-ext:field-transformer "exclude_filter_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:test-sensor-types/oc-test-xfmr:test-sensor-type/oc-test-xfmr:state/oc-test-xfmr:type {
deviate add {
sonic-ext:field-transformer "test_sensor_type_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sensor-groups/oc-test-xfmr:test-sensor-group/oc-test-xfmr:test-sensor-types/oc-test-xfmr:test-sensor-type/oc-test-xfmr:state/oc-test-xfmr:exclude-filter {
deviate add {
sonic-ext:field-transformer "exclude_filter_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sets/oc-test-xfmr:test-set {
deviate add {
sonic-ext:table-name "TEST_SET_TABLE";
sonic-ext:key-transformer "test_set_key_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sets/oc-test-xfmr:test-set/oc-test-xfmr:config/oc-test-xfmr:type {
deviate add {
sonic-ext:field-transformer "test_set_type_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sets/oc-test-xfmr:test-set/oc-test-xfmr:config/oc-test-xfmr:name {
deviate add {
sonic-ext:field-transformer "test_set_name_field_xfmr";
}
}


deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sets/oc-test-xfmr:test-set/oc-test-xfmr:config/oc-test-xfmr:description {
deviate add {
sonic-ext:field-transformer "test_set_description_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sets/oc-test-xfmr:test-set/oc-test-xfmr:state/oc-test-xfmr:name {
deviate add {
sonic-ext:field-transformer "test_set_name_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sets/oc-test-xfmr:test-set/oc-test-xfmr:state/oc-test-xfmr:type {
deviate add {
sonic-ext:field-transformer "test_set_type_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:test-sets/oc-test-xfmr:test-set/oc-test-xfmr:state/oc-test-xfmr:description {
deviate add {
sonic-ext:field-transformer "test_set_description_field_xfmr";
}
}

deviation /oc-test-xfmr:test-xfmr/oc-test-xfmr:interfaces {
deviate add {
sonic-ext:subtree-transformer "test_port_bindings_xfmr";
sonic-ext:virtual-table "true";
}
}
}

Loading

0 comments on commit cc214fb

Please sign in to comment.