github.com/clcy1243/docker@v1.6.0-rc3/docs/sources/project/test-and-docs.md (about) 1 page_title: Run tests and test documentation 2 page_description: Describes Docker's testing infrastructure 3 page_keywords: make test, make docs, Go tests, gofmt, contributing, running tests 4 5 # Run tests and test documentation 6 7 Contributing includes testing your changes. If you change the Docker code, you 8 may need to add a new test or modify an existing one. Your contribution could 9 even be adding tests to Docker. For this reason, you need to know a little 10 about Docker's test infrastructure. 11 12 Many contributors contribute documentation only. Or, a contributor makes a code 13 contribution that changes how Docker behaves and that change needs 14 documentation. For these reasons, you also need to know how to build, view, and 15 test the Docker documentation. 16 17 In this section, you run tests in the `dry-run-test` branch of your Docker 18 fork. If you have followed along in this guide, you already have this branch. 19 If you don't have this branch, you can create it or simply use another of your 20 branches. 21 22 ## Understand testing at Docker 23 24 Docker tests use the Go language's test framework. In this framework, files 25 whose names end in `_test.go` contain test code; you'll find test files like 26 this throughout the Docker repo. Use these files for inspiration when writing 27 your own tests. For information on Go's test framework, see <a 28 href="http://golang.org/pkg/testing/" target="_blank">Go's testing package 29 documentation</a> and the <a href="http://golang.org/cmd/go/#hdr-Test_packages" 30 target="_blank">go test help</a>. 31 32 You are responsible for _unit testing_ your contribution when you add new or 33 change existing Docker code. A unit test is a piece of code that invokes a 34 single, small piece of code ( _unit of work_ ) to verify the unit works as 35 expected. 36 37 Depending on your contribution, you may need to add _integration tests_. These 38 are tests that combine two or more work units into one component. These work 39 units each have unit tests and then, together, integration tests that test the 40 interface between the components. The `integration` and `integration-cli` 41 directories in the Docker repository contain integration test code. 42 43 Testing is its own speciality. If you aren't familiar with testing techniques, 44 there is a lot of information available to you on the Web. For now, you should 45 understand that, the Docker maintainers may ask you to write a new test or 46 change an existing one. 47 48 ### Run tests on your local host 49 50 Before submitting any code change, you should run the entire Docker test suite. 51 The `Makefile` contains a target for the entire test suite. The target's name 52 is simply `test`. The make file contains several targets for testing: 53 54 <style type="text/css"> 55 .monospaced {font-family: Monaco, Consolas, "Lucida Console", monospace !important;} 56 </style> 57 <table> 58 <tr> 59 <th>Target</th> 60 <th>What this target does</th> 61 </tr> 62 <tr> 63 <td class="monospaced">test</td> 64 <td>Run all the tests.</td> 65 </tr> 66 <tr> 67 <td class="monospaced">test-unit</td> 68 <td>Run just the unit tests.</td> 69 </tr> 70 <tr> 71 <td class="monospaced">test-integration</td> 72 <td>Run just integration tests.</td> 73 </tr> 74 <tr> 75 <td class="monospaced">test-integration-cli</td> 76 <td>Run the test for the integration command line interface.</td> 77 </tr> 78 <tr> 79 <td class="monospaced">test-docker-py</td> 80 <td>Run the tests for Docker API client.</td> 81 </tr> 82 <tr> 83 <td class="monospaced">docs-test</td> 84 <td>Runs the documentation test build.</td> 85 </tr> 86 </table> 87 88 Run the entire test suite on your current repository: 89 90 1. Open a terminal on your local host. 91 92 2. Change to the root your Docker repository. 93 94 $ cd docker-fork 95 96 3. Make sure you are in your development branch. 97 98 $ git checkout dry-run-test 99 100 4. Run the `make test` command. 101 102 $ make test 103 104 This command does several things, it creates a container temporarily for 105 testing. Inside that container, the `make`: 106 107 * creates a new binary 108 * cross-compiles all the binaries for the various operating systems 109 * runs the all the tests in the system 110 111 It can take several minutes to run all the tests. When they complete 112 successfully, you see the output concludes with something like this: 113 114 115 [PASSED]: top - sleep process should be listed in privileged mode 116 [PASSED]: version - verify that it works and that the output is properly formatted 117 PASS 118 coverage: 70.8% of statements 119 ---> Making bundle: test-docker-py (in bundles/1.5.0-dev/test-docker-py) 120 +++ exec docker --daemon --debug --host unix:///go/src/github.com/docker/docker/bundles/1.5.0-dev/test-docker-py/docker.sock --storage-driver vfs --exec-driver native --pidfile /go/src/github.com/docker/docker/bundles/1.5.0-dev/test-docker-py/docker.pid 121 ................................................................. 122 ---------------------------------------------------------------------- 123 Ran 65 tests in 89.266s 124 125 126 ### Run test targets inside the development container 127 128 If you are working inside a Docker development container, you use the 129 `hack/make.sh` script to run tests. The `hack/make.sh` script doesn't 130 have a single target that runs all the tests. Instead, you provide a single 131 commmand line with multiple targets that does the same thing. 132 133 Try this now. 134 135 1. Open a terminal and change to the `docker-fork` root. 136 137 2. Start a Docker development image. 138 139 If you are following along with this guide, you should have a 140 `dry-run-test` image. 141 142 $ docker run --privileged --rm -ti -v `pwd`:/go/src/github.com/docker/docker dry-run-test /bin/bash 143 144 3. Run the tests using the `hack/make.sh` script. 145 146 root@5f8630b873fe:/go/src/github.com/docker/docker# hack/make.sh dynbinary binary cross test-unit test-integration test-integration-cli test-docker-py 147 148 The tests run just as they did within your local host. 149 150 151 Of course, you can also run a subset of these targets too. For example, to run 152 just the unit tests: 153 154 root@5f8630b873fe:/go/src/github.com/docker/docker# hack/make.sh dynbinary binary cross test-unit 155 156 Most test targets require that you build these precursor targets first: 157 `dynbinary binary cross` 158 159 160 ## Running individual or multiple named tests 161 162 You can use the `TESTFLAGS` environment variable to run a single test. The 163 flag's value is passed as arguments to the `go test` command. For example, from 164 your local host you can run the `TestBuild` test with this command: 165 166 $ TESTFLAGS='-test.run ^TestBuild$' make test 167 168 To run the same test inside your Docker development container, you do this: 169 170 root@5f8630b873fe:/go/src/github.com/docker/docker# TESTFLAGS='-run ^TestBuild$' hack/make.sh 171 172 ## If test under Boot2Docker fail do to space errors 173 174 Running the tests requires about 2GB of memory. If you are running your 175 container on bare metal, that is you are not running with Boot2Docker, your 176 Docker development container is able to take the memory it requires directly 177 from your local host. 178 179 If you are running Docker using Boot2Docker, the VM uses 2048MB by default. 180 This means you can exceed the memory of your VM running tests in a Boot2Docker 181 environment. When the test suite runs out of memory, it returns errors similar 182 to the following: 183 184 server.go:1302 Error: Insertion failed because database is full: database or 185 disk is full 186 187 utils_test.go:179: Error copy: exit status 1 (cp: writing 188 '/tmp/docker-testd5c9-[...]': No space left on device 189 190 To increase the memory on your VM, you need to reinitialize the Boot2Docker VM 191 with new memory settings. 192 193 1. Stop all running containers. 194 195 2. View the current memory setting. 196 197 $ boot2docker info 198 { 199 "Name": "boot2docker-vm", 200 "UUID": "491736fd-4075-4be7-a6f5-1d4cdcf2cc74", 201 "Iso": "/Users/mary/.boot2docker/boot2docker.iso", 202 "State": "running", 203 "CPUs": 8, 204 "Memory": 2048, 205 "VRAM": 8, 206 "CfgFile": "/Users/mary/VirtualBox VMs/boot2docker-vm/boot2docker-vm.vbox", 207 "BaseFolder": "/Users/mary/VirtualBox VMs/boot2docker-vm", 208 "OSType": "", 209 "Flag": 0, 210 "BootOrder": null, 211 "DockerPort": 0, 212 "SSHPort": 2022, 213 "SerialFile": "/Users/mary/.boot2docker/boot2docker-vm.sock" 214 } 215 216 217 3. Delete your existing `boot2docker` profile. 218 219 $ boot2docker delete 220 221 4. Reinitialize `boot2docker` and specify a higher memory. 222 223 $ boot2docker init -m 5555 224 225 5. Verify the memory was reset. 226 227 $ boot2docker info 228 229 6. Restart your container and try your test again. 230 231 232 ## Build and test the documentation 233 234 The Docker documentation source files are under `docs/sources`. The content is 235 written using extended Markdown. We use the static generator <a 236 href="http://www.mkdocs.org/" target="_blank">MkDocs</a> to build Docker's 237 documentation. Of course, you don't need to install this generator 238 to build the documentation, it is included with container. 239 240 You should always check your documentation for grammar and spelling. The best 241 way to do this is with <a href="http://www.hemingwayapp.com/" 242 target="_blank">an online grammar checker</a>. 243 244 When you change a documentation source file, you should test your change 245 locally to make sure your content is there and any links work correctly. You 246 can build the documentation from the local host. The build starts a container 247 and loads the documentation into a server. As long as this container runs, you 248 can browse the docs. 249 250 1. In a terminal, change to the root of your `docker-fork` repository. 251 252 $ cd ~/repos/dry-run-test 253 254 2. Make sure you are in your feature branch. 255 256 $ git status 257 On branch dry-run-test 258 Your branch is up-to-date with 'origin/dry-run-test'. 259 nothing to commit, working directory clean 260 261 3. Build the documentation. 262 263 $ make docs 264 265 When the build completes, you'll see a final output message similar to the 266 following: 267 268 Successfully built ee7fe7553123 269 docker run --rm -it -e AWS_S3_BUCKET -e NOCACHE -p 8000:8000 "docker-docs:dry-run-test" mkdocs serve 270 Running at: http://0.0.0.0:8000/ 271 Live reload enabled. 272 Hold ctrl+c to quit. 273 274 4. Enter the URL in your browser. 275 276 If you are running Boot2Docker, replace the default localhost address 277 (0.0.0.0) with your DOCKERHOST value. You can get this value at any time by 278 entering `boot2docker ip` at the command line. 279 280 5. Once in the documentation, look for the red notice to verify you are seeing the correct build. 281 282  283 284 6. Navigate to your new or changed document. 285 286 7. Review both the content and the links. 287 288 8. Return to your terminal and exit out of the running documentation container. 289 290 291 ## Where to go next 292 293 Congratulations, you have successfully completed the basics you need to 294 understand the Docker test framework. In the next steps, you use what you have 295 learned so far to [contribute to Docker by working on an 296 issue](/project/make-a-contribution/).