github.com/a4a881d4/docker@v1.9.0-rc2/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 `Makefile` 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  </table>
    86  
    87  Run the entire test suite on your current repository:
    88  
    89  1. Open a terminal on your local host.
    90  
    91  2. Change to the root your Docker repository.
    92  
    93          $ cd docker-fork
    94  
    95  3. Make sure you are in your development branch.
    96  
    97          $ git checkout dry-run-test
    98  
    99  4. Run the `make test` command.
   100  
   101          $ make test
   102  
   103      This command does several things, it creates a container temporarily for
   104      testing. Inside that container, the `make`:
   105  
   106      * creates a new binary
   107      * cross-compiles all the binaries for the various operating systems
   108      * runs all the tests in the system
   109  
   110      It can take approximate one hour to run all the tests. The time depends
   111      on your host performance. The default timeout is 60 minutes, which is
   112      defined in hack/make.sh(${TIMEOUT:=60m}). You can modify the timeout
   113      value on the basis of your host performance. When they complete
   114      successfully, you see the output concludes with something like this:
   115  
   116  
   117          PASS: docker_cli_pull_test.go:133: DockerHubPullSuite.TestPullClientDisconnect	1.127s
   118          PASS: docker_cli_pull_test.go:16: DockerHubPullSuite.TestPullFromCentralRegistry	1.049s
   119          PASS: docker_cli_pull_test.go:65: DockerHubPullSuite.TestPullFromCentralRegistryImplicitRefParts	9.795s
   120          PASS: docker_cli_pull_test.go:42: DockerHubPullSuite.TestPullNonExistingImage	2.158s
   121          PASS: docker_cli_pull_test.go:92: DockerHubPullSuite.TestPullScratchNotAllowed	0.044s
   122          OK: 918 passed, 13 skipped
   123          PASS
   124          coverage: 72.9% of statements
   125          ok  	github.com/docker/docker/integration-cli	1638.553s
   126          ---> Making bundle: .integration-daemon-stop (in bundles/1.9.0-dev/test-integration-cli)
   127          ++++ cat bundles/1.9.0-dev/test-integration-cli/docker.pid
   128          +++ kill 9453
   129          +++ /etc/init.d/apparmor stop
   130           * Clearing AppArmor profiles cache
   131             ...done.
   132          All profile caches have been cleared, but no profiles have been unloaded.
   133          Unloading profiles will leave already running processes permanently
   134          unconfined, which can lead to unexpected situations.
   135  
   136          To set a process to complain mode, use the command line tool
   137          'aa-complain'. To really tear down all profiles, run the init script
   138          with the 'teardown' option."
   139  
   140          ---> Making bundle: test-docker-py (in bundles/1.9.0-dev/test-docker-py)
   141          ---> Making bundle: .integration-daemon-start (in bundles/1.9.0-dev/test-docker-py)
   142          +++ /etc/init.d/apparmor start
   143           * Starting AppArmor profiles
   144          Skipping profile in /etc/apparmor.d/disable: usr.sbin.rsyslogd
   145             ...done.
   146          +++ exec docker daemon --debug --host unix:///go/src/github.com/docker/docker/bundles/1.9.0-dev/test-docker-py/docker.sock --storage-driver overlay --exec-driver native --pidfile bundles/1.9.0-dev/test-docker-py/docker.pid --userland-proxy=true
   147          ..............s..............s......................................
   148          ----------------------------------------------------------------------
   149          Ran 68 tests in 79.135s
   150   
   151  
   152  ### Run test targets inside the development container
   153  
   154  If you are working inside a Docker development container, you use the
   155  `hack/make.sh` script to run tests. The `hack/make.sh` script doesn't
   156  have a single target that runs all the tests. Instead, you provide a single
   157  command line with multiple targets that does the same thing.
   158  
   159  Try this now.
   160  
   161  1. Open a terminal and change to the `docker-fork` root.
   162  
   163  2. Start a Docker development image.
   164  
   165      If you are following along with this guide, you should have a
   166      `dry-run-test` image.
   167  
   168          $ docker run --privileged --rm -ti -v `pwd`:/go/src/github.com/docker/docker dry-run-test /bin/bash
   169  
   170  3. Run the tests using the `hack/make.sh` script.
   171  
   172          root@5f8630b873fe:/go/src/github.com/docker/docker# hack/make.sh dynbinary binary cross test-unit test-integration-cli test-docker-py
   173  
   174      The tests run just as they did within your local host.
   175  
   176  
   177  Of course, you can also run a subset of these targets too. For example, to run
   178  just the unit tests:
   179  
   180      root@5f8630b873fe:/go/src/github.com/docker/docker# hack/make.sh dynbinary binary cross test-unit
   181  
   182  Most test targets require that you build these precursor targets first:
   183  `dynbinary binary cross`
   184  
   185  
   186  ## Running individual or multiple named tests 
   187  
   188  ### Unit tests 
   189  
   190  We use golang standard [testing](https://golang.org/pkg/testing/)
   191  package or [gocheck](https://labix.org/gocheck) for our unit tests. 
   192  
   193  You can use the `TESTDIRS` environment variable to run unit tests for
   194  a single package.
   195  
   196      $ TESTDIRS='opts' make test-unit
   197  
   198  You can also use the `TESTFLAGS` environment variable to run a single test. The
   199  flag's value is passed as arguments to the `go test` command. For example, from
   200  your local host you can run the `TestBuild` test with this command:
   201  
   202      $ TESTFLAGS='-test.run ^TestValidateIPAddress$' make test-unit
   203  
   204  On unit tests, it's better to use `TESTFLAGS` in combination with
   205  `TESTDIRS` to make it quicker to run a specific test.
   206  
   207      $ TESTDIRS='opts' TESTFLAGS='-test.run ^TestValidateIPAddress$' make test-unit
   208  
   209  ### Integration tests 
   210  
   211  We use [gocheck](https://labix.org/gocheck) for our integration-cli tests. 
   212  You can use the `TESTFLAGS` environment variable to run a single test. The
   213  flag's value is passed as arguments to the `go test` command. For example, from
   214  your local host you can run the `TestBuild` test with this command:
   215  
   216      $ TESTFLAGS='-check.f DockerSuite.TestBuild*' make test-integration-cli
   217  
   218  To run the same test inside your Docker development container, you do this:
   219  
   220      root@5f8630b873fe:/go/src/github.com/docker/docker# TESTFLAGS='-check.f TestBuild*' hack/make.sh binary test-integration-cli
   221  
   222  ## Testing the Windows binary against a Linux daemon
   223  
   224  This explains how to test the Windows binary on a Windows machine set up as a
   225  development environment.  The tests will be run against a docker daemon 
   226  running on a remote Linux machine. You'll use  **Git Bash** that came with the 
   227  Git for Windows installation.  **Git Bash**, just as it sounds, allows you to
   228  run a Bash terminal on Windows. 
   229  
   230  1.  If you don't have one open already, start a Git Bash terminal.
   231  
   232  	 ![Git Bash](images/git_bash.png)
   233  
   234  2. Change to the `docker` source directory.
   235  
   236  		$ cd /c/gopath/src/github.com/docker/docker
   237      
   238  3. Set `DOCKER_REMOTE_DAEMON` as follows:
   239  
   240  		$ export DOCKER_REMOTE_DAEMON=1
   241  
   242  4. Set `DOCKER_TEST_HOST` to the `tcp://IP_ADDRESS:2376` value; substitute your
   243  Linux machines actual IP address. For example:
   244  
   245  		$ export DOCKER_TEST_HOST=tcp://213.124.23.200:2376
   246  
   247  5. Make the binary and run the tests:
   248  
   249  		$ hack/make.sh binary test-integration-cli
   250    	
   251     Some tests are skipped on Windows for various reasons. You can see which
   252     tests were skipped by re-running the make and passing in the 
   253     `TESTFLAGS='-test.v'` value. For example 
   254  
   255  		$ TESTFLAGS='-test.v' hack/make.sh binary test-integration-cli
   256  		
   257  	Should you wish to run a single test such as one with the name 
   258  	'TestExample', you can pass in `TESTFLAGS='-check.f TestExample'`. For
   259  	example 
   260  	
   261  		$TESTFLAGS='-check.f TestExample' hack/make.sh binary test-integration-cli
   262          
   263  You can now choose to make changes to the Docker source or the tests. If you
   264  make any changes just run these commands again.
   265  
   266  
   267  ## Build and test the documentation
   268  
   269  The Docker documentation source files are under `docs`. The content is
   270  written using extended Markdown. We use the static generator <a
   271  href="http://www.mkdocs.org/" target="_blank">MkDocs</a> to build Docker's
   272  documentation. Of course, you don't need to install this generator
   273  to build the documentation, it is included with container.
   274  
   275  You should always check your documentation for grammar and spelling. The best
   276  way to do this is with <a href="http://www.hemingwayapp.com/"
   277  target="_blank">an online grammar checker</a>.
   278  
   279  When you change a documentation source file, you should test your change
   280  locally to make sure your content is there and any links work correctly. You
   281  can build the documentation from the local host. The build starts a container
   282  and loads the documentation into a server. As long as this container runs, you
   283  can browse the docs.
   284  
   285  1. In a terminal, change to the root of your `docker-fork` repository.
   286  
   287          $ cd ~/repos/docker-fork
   288  
   289  2. Make sure you are in your feature branch.
   290  
   291          $ git status
   292          On branch dry-run-test
   293          Your branch is up-to-date with 'origin/dry-run-test'.
   294          nothing to commit, working directory clean
   295  
   296  3. Build the documentation.
   297  
   298          $ make docs
   299  
   300      When the build completes, you'll see a final output message similar to the
   301      following:
   302  
   303          Successfully built ee7fe7553123
   304          docker run --rm -it  -e AWS_S3_BUCKET -e NOCACHE -p 8000:8000 "docker-docs:dry-run-test" mkdocs serve
   305          Running at: http://0.0.0.0:8000/
   306          Live reload enabled.
   307          Hold ctrl+c to quit.
   308  
   309  4. Enter the URL in your browser.
   310  
   311      If you are using Docker Machine, replace the default localhost address
   312      (0.0.0.0) with your DOCKERHOST value. You can get this value at any time by
   313      entering `docker-machine ip <machine-name>` at the command line.
   314  
   315  5. Once in the documentation, look for the red notice to verify you are seeing the correct build.
   316  
   317      ![Beta documentation](images/red_notice.png)
   318  
   319  6. Navigate to your new or changed document.
   320  
   321  7. Review both the content and the links.
   322  
   323  8. Return to your terminal and exit out of the running documentation container.
   324  
   325  
   326  ## Where to go next
   327  
   328  Congratulations, you have successfully completed the basics you need to
   329  understand the Docker test framework. In the next steps, you use what you have
   330  learned so far to [contribute to Docker by working on an
   331  issue](make-a-contribution.md).