You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*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.
19
19
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:
21
22
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.
23
27
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:
26
29
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.
34
35
35
36
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).
37
38
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.
39
39
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
41
46
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:
43
48
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.
47
59
48
-
* Using go test $testdir to run unit test in a specified directory.
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.
53
70
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:
56
72
57
-
```
58
-
#go test ./daemon/mgr/
59
-
../../docker/libnetwork/config/config.go:6:2: cannot find package "github.com/BurntSushi/toml" in any of:
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
65
76
66
-
## Integration Testing
77
+
In order to run unit-test or integration test, install go and configure go environment first.
67
78
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:
69
89
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:
71
95
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
+
```
76
99
77
-
For other files, they are:
100
+
Or using go test $testdir to run unit test in a specified directory.
78
101
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.
84
108
109
+
First you need to make sure `pouch` and `pouchd` binary is installed or builded.
85
110
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.
87
112
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:
89
114
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.
0 commit comments