VOL-1424: Makefile changes to add adapter unit tests
Change-Id: I8ea0e20c044843df090788f3f33ee5dd34c04d3b
diff --git a/Makefile b/Makefile
index 1c4d762..58fa0cf 100644
--- a/Makefile
+++ b/Makefile
@@ -186,7 +186,7 @@
@echo "unum : Build the unum docker container"
@echo "ponsim : Build the ponsim docker container"
@echo "j2 : Build the Jinja2 template container"
- @echo "alarm-generator : Build the alarm-generator container"
+ @echo "alarm-generator : Build the alarm-generator container"
@echo "test_runner : Build a container from which tests are run"
@echo "start : Start VOLTHA on the current system"
@echo "stop : Stop VOLTHA on the current system"
@@ -428,8 +428,12 @@
venv:
endif
+VENV_BIN ?= virtualenv
+VENV_OPTS ?=
+
${VENVDIR}/.built:
- @ virtualenv ${VENVDIR}
+ @ $(VENV_BIN) ${VENV_OPTS} ${VENVDIR}
+ @ $(VENV_BIN) ${VENV_OPTS} --relocatable ${VENVDIR}
@ . ${VENVDIR}/bin/activate && \
pip install --upgrade pip; \
if ! pip install -r requirements.txt; \
@@ -439,6 +443,7 @@
else \
uname -s > ${VENVDIR}/.built; \
fi
+ @ $(VENV_BIN) ${VENV_OPTS} --relocatable ${VENVDIR}
ifneq ($(VOLTHA_BUILD),docker)
test: venv protos run-as-root-tests
@@ -459,11 +464,16 @@
nosetests -s tests --exclude-dir=./tests/itests/run_as_root/
endif
+TEST_ADAPTERS := $(shell find ./voltha/adapters -name test.mk)
+
ifneq ($(VOLTHA_BUILD),docker)
utest: venv protos
@ echo "Executing all unit tests"
- . ${VENVDIR}/bin/activate && \
- for d in $$(find ./tests/utests -type d|sort -nr); do echo $$d:; nosetests $$d; done
+ @ . ${VENVDIR}/bin/activate && nosetests ./tests/utests
+ @ for test in $(TEST_ADAPTERS); do \
+ echo Adapter Tests $$test:; \
+ $(MAKE) -f $$test; \
+ done
else
utest: protos test_runner
docker run \
@@ -475,22 +485,23 @@
--rm --net=host -v /var/run/docker.sock:/var/run/docker.sock \
${REGISTRY}${REPSOITORY}voltha-test_runner:${TAG} \
bash -c \
- 'for d in $$(find ./tests/utests -type d|sort -nr); do \
- echo $$d:; \
- nosetests $$d; \
+ '. ${VENVDIR}/bin/activate && nosetests ./tests/utests; \
+ for test in $(TEST_ADAPTERS); do \
+ echo Adapter Tests $$test:; \
+ $(MAKE) -f $$test; \
done'
endif
+COVERAGE_OPTS=--with-xcoverage --with-xunit --cover-package=voltha,common,ofagent --cover-html\
+ --cover-html-dir=tmp/cover
ifneq ($(VOLTHA_BUILD),docker)
utest-with-coverage: venv protos
@ echo "Executing all unit tests and producing coverage results"
- . ${VENVDIR}/bin/activate && \
- for d in $$(find ./tests/utests -type d|sort -nr); do \
- echo $$d:; \
- nosetests --with-xcoverage --xcoverage-file="$$d/coverage.xml" \
- --with-xunit --xunit-file="$$d/nosetests.xml" \
- --cover-package=voltha,common,ofagent $$d; \
- done
+ @ . ${VENVDIR}/bin/activate && nosetests $(COVERAGE_OPTS) ./tests/utests
+ @ for test in $(TEST_ADAPTERS); do \
+ echo Adapter Tests $$test:; \
+ $(MAKE) -f $$test; \
+ done
else
utest-with-coverage: protos test_runner
@echo "Executing all unit tests and producing coverage results"
@@ -503,12 +514,11 @@
--rm --net=host -v /var/run/docker.sock:/var/run/docker.sock \
${REGISTRY}${REPSOITORY}voltha-test_runner:${TAG} \
bash -c \
- 'for d in $$(find ./tests/utests -type d|sort -nr); do \
- echo $$d:; \
- nosetests --with-xcoverage --xcoverage-file="$$d/coverage.xml" \
- --with-xunit --xunit-file="$$d/nosetests.xml" \
- --cover-package=voltha,common,ofagent $$d; \
- done'
+ 'nosetests ${COVERAGE_OPTS} ./tests/utests; \
+ for test in $(TEST_ADAPTERS); do \
+ echo Adapter Tests $$test:; \
+ $(MAKE) -f $$test; \
+ done'
endif
ifneq ($(VOLTHA_BUILD),docker)
diff --git a/README.md b/README.md
index f0e9383..beb795e 100644
--- a/README.md
+++ b/README.md
@@ -27,4 +27,34 @@
## How can you help?
-Contributions, small and large, are welcome. Minor contributions and bug fixes are always welcome in form of pull requests. For larger work, the best is to check in with the existing developers to see where help is most needed and to make sure your solution is compatible with the general philosophy of Voltha.
\ No newline at end of file
+Contributions, small and large, are welcome. Minor contributions and bug fixes are always welcome in form of pull requests. For larger work, the best is to check in with the existing developers to see where help is most needed and to make sure your solution is compatible with the general philosophy of Voltha.
+
+### Contributing Unit Tests
+
+To begin, make sure to have a development environement installed according to the [OpenCord WIKI](https://wiki.opencord.org/display/CORD/Installing+required+tools).
+Next, In a shell environment
+```bash
+source env.sh; # Source the environment Settings and create a virtual environment
+make utest-with-coverage; # Execute the Unit Test with coverage reporting
+```
+
+### Unit-testing the Core
+New unit tests for the core can be written in the [nosetest](https://nose.readthedocs.io/en/latest/) framework and can be found under <repo>/tests/utest/
+
+### Unit-testing an Adapter
+Each adapter's unit tests are discovered by the presence of a test.mk [submake file](https://www.gnu.org/software/make/manual/html_node/Include.html) underneath the adapter's directory.
+for example)
+
+```Makefile
+# voltha/adapters/my_new_adapter/test.mk
+
+.PHONY test
+test:
+ @echo "Testing my amazing new adapter"
+ @./my_test_harness
+
+```
+
+Voltha's test framework will execute the FIRST Target in the submake file as the unit test function. It may include as many dependencies as needed, such as using a different python framework for testing (pytest, unittest, tox) or even alternate languages (go, rust, php).
+
+In order for you adapter's test-coverage to be reported, make sure that your test_harness creates a coverage report in a [junit xml](https://www.ibm.com/support/knowledgecenter/en/SSUFAU_1.0.0/com.ibm.rsar.analysis.codereview.cobol.doc/topics/cac_useresults_junit.html) format. Most test harnesses can easily produce this report format. The [Jenkins Job](https://jenkins.opencord.org/job/voltha_unit-test/cobertura) will pick up your coverage report file if named appropriately **junit-report.xml** according to the Jenkins configuration.