Skip to content

Commit e992301

Browse files
committed
doc: refine test doc
Signed-off-by: letty <[email protected]>
1 parent 19721d6 commit e992301

File tree

1 file changed

+101
-75
lines changed

1 file changed

+101
-75
lines changed

docs/test/test.md

Lines changed: 101 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,110 +15,136 @@ Test in pouch could be divided into following parts:
1515
* [unit testing](https://en.wikipedia.org/wiki/Unit_testing#Description)
1616
* [integration testing](https://en.wikipedia.org/wiki/Integration_testing)
1717

18-
More info could be gotten from the wiki page.
18+
*Unit testing* uses [go testing](https://golang.org/pkg/testing/) package, named with `_test.go` suffix and always locates in the same directory with the code tested. [client/client_test.go](https://github.com/alibaba/pouch/blob/master/client/client_test.go) is a good example of unit test.
1919

20-
For pouch developer, if your code is only used in a single module, then the unit test is enough. While if your code is called by multiple modules, integration tests are required. In pouch, both of them are developed using go language. More details could be gotten in [Unit Testing](#unit-testing) and [Integration Testing](#integration-testing).
20+
*Integration test* is in `pouch/test`, programmed with `go language`. There are two kinds of integration test, API test named as `api_xxx_test.go` and command line test named as `cli_xxx_test.go` ("xxx" represents the test point).
21+
It uses [go-check](https://labix.org/gocheck) package, a rich testing framework for go language. It provides many useful functions, such as:
2122

22-
# Usage of Test
23+
* SetUpTest: Run before each test to do some common work.
24+
* TearDownTest: Run after each test to do some cleanup work.
25+
* SetUpSuite: Run before each suite to do common work for the whole suite.
26+
* TearDownSuite: Run after each suite to do cleanup work for the whole suite.
2327

24-
Tests could be run through the target provided by `Makefile` in the root directory of pouch code. Also could be run manually.
25-
To run the whole test, you could just run:
28+
For other files, they are:
2629

27-
```
28-
# make test
29-
```
30-
Please note that, in order to run test, the following prerequisites are needed:
31-
32-
* golang is installed
33-
* docker is installed
30+
* `main_test.go` : the entrypoint of integration test.
31+
* `utils.go`: common lib functions.
32+
* `environment directory`: directory environment is used to hold environment variables.
33+
* `command package`: package command is used to encapsulate CLI lib functions.
34+
* `request package`: package request is used to encapsulate http request lib functions.
3435

3536

36-
## Unit Testing
37+
For pouch developer, if your code is only used in a single module, then the unit test is enough. While if your code is called by multiple modules, integration tests are required. In pouch, both of them are developed using go language. More details could be gotten in [Unit Testing](#unit-testing) and [Integration Testing](#integration-testing).
3738

38-
Unit testing uses [go testing](https://golang.org/pkg/testing/) package, named with `_test.go` postfix and always locates in the same directory with the code tested. [client/client_test.go](https://github.com/alibaba/pouch/blob/master/client/client_test.go) is a good example of unit test.
3939

40-
There are two ways to trigger unit test.
40+
# Run Tests
41+
Tests could be run through the target provided by `Makefile` in the root directory of pouch code. Also could be run manually.
42+
To run the test automatically, the following prerequisites are needed:
43+
44+
* golang is installed and GOPATH and GOROOT is set correctly
45+
* docker is installed
4146

42-
* Using [Makefile](https://github.com/alibaba/pouch/blob/master/Makefile) target unit-test to run entire unit test.
47+
Then you could just clone the pouch source to GOPATH and run tests as following:
4348

44-
```
45-
# make unit-test
46-
```
49+
```
50+
# which docker
51+
/usr/bin/docker
52+
# env |grep GO
53+
GOROOT=/usr/local/go
54+
GOPATH=/go
55+
# cd /go/src/github.com/alibaba/pouch
56+
# make test
57+
```
58+
Using `make -n test`, let us take a look at what `make test` has done.
4759

48-
* Using go test $testdir to run unit test in a specified directory.
60+
```
61+
#make -n test
62+
bash -c "env PATH=/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/local/go/bin:/opt/satools:/root/bin hack/make.sh \
63+
check build unit-test integration-test cri-test"
64+
```
4965

50-
```
51-
#go test ./client
52-
ok github.com/alibaba/pouch/client 0.094s
66+
`make test` calls the hack/make.sh script to check code format, build pouch daemon and client, run unit test, run integration test and run cri test.
67+
`hack/make.sh` needs `docker` installed on test machine, as it uses `docker build` to build a test image including tools needed to run `make test`. `go` is also needed to be installed and set `GOPATH` `GOROOT` `PATH` correctly. For more information, you could check the `hack/make.sh` script.
68+
# Run Tests Manually
69+
As a pouch developer, you may need to run tests manually.
5370

54-
```
55-
Note: If you are testing modules importing "github.com/docker/libnetwork/xxx", there may be errors like following:
71+
If you want to do code format check, you could run `make check` in your code directory, but please make sure the following tools are already installed:
5672

57-
```
58-
#go test ./daemon/mgr/
59-
../../docker/libnetwork/config/config.go:6:2: cannot find package "github.com/BurntSushi/toml" in any of:
60-
/usr/local/go/src/github.com/BurntSushi/toml (from $GOROOT)
61-
/home/sit/letty/src/github.com/BurntSushi/toml (from $GOPATH)
62-
<snip>
63-
```
64-
In this case, you may have to run unit test from Makefile. In this way, it will call `hack/build` script, and do setup related to libnetwork package location before running unit test.
73+
* gofmt
74+
* golint
75+
* swagger
6576

66-
## Integration Testing
77+
In order to run unit-test or integration test, install go and configure go environment first.
6778

68-
Integration test is in `pouch/test`, programmed with `go language`. There are two kinds of integration test, API test named as `api_xxx_test.go` and command line test named as `cli_xxx_test.go` ("xxx" represents the test point).
79+
```
80+
# go version
81+
go version go1.9.2 linux/amd64
82+
# which go
83+
/usr/local/go/bin//go
84+
# env |grep GO
85+
GOROOT=/usr/local/go
86+
GOPATH=/go
87+
```
88+
Then copy or clone or link pouch source code to the GOPATH:
6989

70-
It uses [go-check](https://labix.org/gocheck) package, a rich testing framework for go language. It provides many useful functions, such as:
90+
```
91+
# pwd
92+
/go/src/github.com/alibaba/pouch
93+
```
94+
Now you could run unit test as following:
7195

72-
* SetUpTest: Run before each test to do some common work.
73-
* TearDownTest: Run after each test to do some cleanup work.
74-
* SetUpSuite: Run before each suite to do common work for the whole suite.
75-
* TearDownSuite: Run after each suite to do cleanup work for the whole suite.
96+
```
97+
# make unit-test
98+
```
7699

77-
For other files, they are:
100+
Or using go test $testdir to run unit test in a specified directory.
78101

79-
* `main_test.go` : the entrypoint of integration test.
80-
* `utils.go`: common lib functions.
81-
* `environment directory`: directory environment is used to hold environment variables.
82-
* `command package`: package command is used to encapsulate CLI lib functions.
83-
* `request package`: package request is used to encapsulate http request lib functions.
102+
```
103+
#go test ./client
104+
ok github.com/alibaba/pouch/client 0.094s
105+
106+
```
107+
There are more works to do for integration test compared with unit test.
84108

109+
First you need to make sure `pouch` and `pouchd` binary is installed or builded.
85110

86-
There are two ways to trigger integration test.
111+
Then you need to install containerd, runc, lxcfs and so on. You could refer to function `install_pouch` in `hack/make.sh`. There is also a quick way to install them, just install `pouch` package then replace pouch binary with yours.
87112

88-
* Using [Makefile](https://github.com/alibaba/pouch/blob/master/Makefile) target integration-test to run entire integration test.
113+
Next you need to start pouch daemon and pull a busybox image:
89114

90-
```
91-
# make integration-test
92-
```
93-
* Using go test to run integration test. Before running integration test, users need to [build pouch](https://github.com/alibaba/pouch/blob/master/INSTALLATION.md) binary and launch `pouchd` daemon.
115+
```
116+
# pouchd -D --enable-lxcfs=true --lxcfs=/usr/bin/lxcfs >/tmp/log 2>&1 &
117+
# pouch pull registry.hub.docker.com/library/busybox:latest
118+
```
94119
Then integration test could be run as following:
95120

96-
* run entire test:
121+
* run entire test:
97122

98-
```
99-
#go test ./test
100-
```
101-
* run a single test suite:
102-
103-
```
104-
#go test -check.f PouchCreateSuite
105-
OK: 3 passed
106-
PASS
107-
ok github.com/alibaba/pouch/test 3.081s
108-
```
109-
* run a single test case:
123+
```
124+
# cd test
125+
#go test
126+
```
127+
* run a single test suite:
110128

111-
```
112-
#go test -check.f PouchHelpSuite.TestHelpWorks
113-
OK: 1 passed
114-
PASS
115-
ok github.com/alibaba/pouch/test 0.488s
116-
```
117-
* run with more information:
129+
```
130+
#go test -check.f PouchCreateSuite
131+
OK: 3 passed
132+
PASS
133+
ok github.com/alibaba/pouch/test 3.081s
134+
```
135+
* run a single test case:
118136

119-
```
120-
#go test -check.vv
121-
```
137+
```
138+
#go test -check.f PouchHelpSuite.TestHelpWorks
139+
OK: 1 passed
140+
PASS
141+
ok github.com/alibaba/pouch/test 0.488s
142+
```
143+
* run with more information:
144+
145+
```
146+
#go test -check.vv
147+
```
122148

123149
# Development of Test
124150
// TODO

0 commit comments

Comments
 (0)