Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit c491716

Browse files
authored
Merge pull request #54 from przemeklal/unittests_doc
Document unit testing in README.md
2 parents 163a160 + 06559c0 commit c491716

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

Diff for: README.md

+150
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
* [1. Build the image to be used](#1-build-the-image-to-be-used)
3535
* [2. Create pod with multiple vhostuser interfaces](#2-create-pod-with-multiple-vhostuser-interfaces)
3636
* [3. Open terminal to pod and start testpmd](#3-open-terminal-to-pod-and-start-testpmd)
37+
* [Unit Testing](#unit-testing)
38+
* [Unit Tests Overview](#unit-tests-overview)
39+
* [Unit Tests Inside Container](#unit-tests-inside-container)
3740

3841
# Summary
3942
The Userspace CNI is a Container Network Interface (CNI) plugin
@@ -1027,3 +1030,150 @@ To exit:
10271030
Pktgen:/> stop 0
10281031
Pktgen:/> quit
10291032
```
1033+
1034+
# Unit Testing
1035+
1036+
## Unit Tests Overview
1037+
1038+
The Userspace CNI networking plugin is written in [GO](https://golang.org/) and
1039+
it uses [GO Testing](https://golang.org/pkg/testing) package for unit testing.
1040+
In order to simplify unit test code, a [testify](https://github.com/stretchr/testify)
1041+
toolkit is also used for [assertions](https://godoc.org/github.com/stretchr/testify/assert).
1042+
Unit test files have suffix *_test.go* and they are located at the same directory
1043+
as the production code.
1044+
1045+
For example *userspace* package have one source file and one file with unit tests:
1046+
1047+
```bash
1048+
userspace/userspace.go
1049+
userspace/userspace_test.go
1050+
```
1051+
1052+
In the file `userspace_test.go` you can find a set of test functions with the same
1053+
name as functions from `userspace.go` with prefix `Test`. Each test function
1054+
implements a set of tests to test production code thoroughly with different
1055+
input data, etc.
1056+
1057+
Testing binary is built and tests executed by `go test` command at directory
1058+
with test files.
1059+
1060+
For example, unit tests for userspace package with verbose output can be executed
1061+
as follows:
1062+
1063+
```bash
1064+
cd userspace
1065+
go test -v
1066+
```
1067+
1068+
An example of verbose output of userspace unit tests:
1069+
1070+
```bash
1071+
=== RUN TestPrintVersionString
1072+
=== RUN TestPrintVersionString/verify_version_string
1073+
--- PASS: TestPrintVersionString (0.00s)
1074+
--- PASS: TestPrintVersionString/verify_version_string (0.00s)
1075+
=== RUN TestLoadNetConf
1076+
=== RUN TestLoadNetConf/fail_to_parse_netConf_1
1077+
=== RUN TestLoadNetConf/fail_to_parse_netConf_2
1078+
=== RUN TestLoadNetConf/fail_to_parse_netConf_3
1079+
=== RUN TestLoadNetConf/fail_to_parse_netConf_4
1080+
=== RUN TestLoadNetConf/fail_to_parse_netConf_5
1081+
=== RUN TestLoadNetConf/fail_to_parse_netConf_6
1082+
=== RUN TestLoadNetConf/fail_to_set_default_logging_level
1083+
=== RUN TestLoadNetConf/fail_to_set_log_file
1084+
=== RUN TestLoadNetConf/load_correct_netConf
1085+
--- PASS: TestLoadNetConf (0.00s)
1086+
--- PASS: TestLoadNetConf/fail_to_parse_netConf_1 (0.00s)
1087+
--- PASS: TestLoadNetConf/fail_to_parse_netConf_2 (0.00s)
1088+
--- PASS: TestLoadNetConf/fail_to_parse_netConf_3 (0.00s)
1089+
--- PASS: TestLoadNetConf/fail_to_parse_netConf_4 (0.00s)
1090+
--- PASS: TestLoadNetConf/fail_to_parse_netConf_5 (0.00s)
1091+
--- PASS: TestLoadNetConf/fail_to_parse_netConf_6 (0.00s)
1092+
--- PASS: TestLoadNetConf/fail_to_set_default_logging_level (0.00s)
1093+
--- PASS: TestLoadNetConf/fail_to_set_log_file (0.00s)
1094+
--- PASS: TestLoadNetConf/load_correct_netConf (0.00s)
1095+
...
1096+
```
1097+
1098+
It is possible to execute unit tests directly from the host where the code is
1099+
being developed. However it is strongly recommended to execute them from within
1100+
a container. It will assure that unit tests will be isolated from the host
1101+
and thus avoid any collision with the host environment (e.g. k8s or OVS).
1102+
Another advantage is a possibility to easily execute unit tests at various Linux
1103+
OS distributions. Support of unit test execution inside containers is implemented
1104+
by project Makefile and described in following paragraphs.
1105+
1106+
## Unit Tests Inside Container
1107+
1108+
Project `Makefile` defines a set of targets suitable for unit testing inside
1109+
containers. In order to build and use testing containers, both `docker` and
1110+
`gcc` (C preprocessor is used to assemble Dockerfiles) have to be installed
1111+
at the host.
1112+
1113+
Example of tools installation at Ubuntu:
1114+
1115+
```bash
1116+
apt install docker.io gcc
1117+
```
1118+
1119+
After the installation is completed, then any of unit test specific targets
1120+
can be executed in order to:
1121+
1122+
* build test containers and update them in case that new commit is detected
1123+
* execute unit tests
1124+
* calculate code coverage
1125+
* cleanup unit test container images and generated docker files
1126+
1127+
Targets for container building, unit testing and code coverage calculation
1128+
are prepared in three different versions to be executed at:
1129+
1130+
* Linux OS distribution defined by `UT_OS` environment variable
1131+
1132+
e.g. execute build of container image for CentOS8
1133+
1134+
```bash
1135+
export UT_OS=centos8
1136+
make test-build
1137+
```
1138+
1139+
* Linux OS distribution specified by target suffix
1140+
1141+
e.g. calculate code coverage at Ubuntu 20.04
1142+
1143+
```bash
1144+
make coverage-ubuntu20.04
1145+
```
1146+
1147+
* all supported OS distributions
1148+
1149+
e.g. execute unit tests at all supported OS distros:
1150+
1151+
```bash
1152+
make test-all
1153+
```
1154+
1155+
The list of supported `make` targets is also documented as part of `make help`
1156+
as follows:
1157+
1158+
```
1159+
Make Targets for unit testing inside containers:
1160+
make test-clean - Remove test container images and generated Dockerfiles.
1161+
make test-build - Build container image for unit tests with OS defined by UT_OS: UT_OS=ubuntu20.04
1162+
make test - Run unit tests inside container with OS defined by UT_OS: UT_OS=ubuntu20.04
1163+
make coverage - Calculate code coverage in container with OS defined by UT_OS: UT_OS=ubuntu20.04
1164+
make test-build-<os> - Build container image for unit tests with <os>, e.g. make test-build-centos8
1165+
make test-<os> - Run unit tests inside container with <os>, e.g. make test-centos8
1166+
make coverage-<os> - Calculate code coverage inside container with <os>, e.g. make coverage-centos8
1167+
make test-build-all - Build container images for unit tests for all supported OS distributions
1168+
e.g. make -j 5 test-build-all
1169+
make test-all - Run unit tests inside container for all supported OS distributions
1170+
e.g. make -j 5 test-all
1171+
make coverage-all - Calculate code coverage inside container for all supported OS distributions.
1172+
e.g. make -j 5 coverage-all
1173+
```
1174+
1175+
Following Linux OS distributions are supported for unit testing inside containers:
1176+
1177+
* CentOS 7, 8
1178+
* Fedora 31, 32
1179+
* Ubuntu 16.04, 18.04, 20.04

0 commit comments

Comments
 (0)