github.com/rentongzhang/docker@v1.8.2-rc1/docs/project/test-and-docs.md (about)

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