github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/TESTING.md (about)

     1  # Testing
     2  
     3  This document contains the Moby code testing guidelines. It should answer any 
     4  questions you may have as an aspiring Moby contributor.
     5  
     6  ## Test suites
     7  
     8  Moby has two test suites (and one legacy test suite):
     9  
    10  * Unit tests - use standard `go test` and
    11    [gotest.tools/assert](https://godoc.org/gotest.tools/assert) assertions. They are located in
    12    the package they test. Unit tests should be fast and test only their own 
    13    package.
    14  * API integration tests - use standard `go test` and
    15    [gotest.tools/assert](https://godoc.org/gotest.tools/assert) assertions. They are located in
    16    `./integration/<component>` directories, where `component` is: container,
    17    image, volume, etc. These tests perform HTTP requests to an API endpoint and
    18    check the HTTP response and daemon state after the call.
    19  
    20  The legacy test suite `integration-cli/` is deprecated. No new tests will be 
    21  added to this suite. Any tests in this suite which require updates should be 
    22  ported to either the unit test suite or the new API integration test suite.
    23  
    24  ## Writing new tests
    25  
    26  Most code changes will fall into one of the following categories.
    27  
    28  ### Writing tests for new features
    29  
    30  New code should be covered by unit tests. If the code is difficult to test with
    31  a unit tests then that is a good sign that it should be refactored to make it
    32  easier to reuse and maintain. Consider accepting unexported interfaces instead
    33  of structs so that fakes can be provided for dependencies.
    34  
    35  If the new feature includes a completely new API endpoint then a new API 
    36  integration test should be added to cover the success case of that endpoint.
    37  
    38  If the new feature does not include a completely new API endpoint consider 
    39  adding the new API fields to the existing test for that endpoint. A new 
    40  integration test should **not** be added for every new API field or API error 
    41  case. Error cases should be handled by unit tests.
    42  
    43  ### Writing tests for bug fixes
    44  
    45  Bugs fixes should include a unit test case which exercises the bug.
    46  
    47  A bug fix may also include new assertions in an existing integration tests for the
    48  API endpoint.
    49  
    50  ### Integration tests environment considerations
    51  
    52  When adding new tests or modifying existing test under `integration/`, testing 
    53  environment should be properly considered. `skip.If` from 
    54  [gotest.tools/skip](https://godoc.org/gotest.tools/skip) can be used to make the 
    55  test run conditionally. Full testing environment conditions can be found at 
    56  [environment.go](https://github.com/moby/moby/blob/cb37987ee11655ed6bbef663d245e55922354c68/internal/test/environment/environment.go)
    57  
    58  Here is a quick example. If the test needs to interact with a docker daemon on 
    59  the same host, the following condition should be checked within the test code
    60  
    61  ```go
    62  skip.If(t, testEnv.IsRemoteDaemon())
    63  // your integration test code
    64  ```
    65  
    66  If a remote daemon is detected, the test will be skipped.
    67  
    68  ## Running tests
    69  
    70  ### Unit Tests
    71  
    72  To run the unit test suite:
    73  
    74  ```
    75  make test-unit
    76  ```
    77  
    78  or `hack/test/unit` from inside a `BINDDIR=. make shell` container or properly
    79  configured environment.
    80  
    81  The following environment variables may be used to run a subset of tests:
    82  
    83  * `TESTDIRS` - paths to directories to be tested, defaults to `./...`
    84  * `TESTFLAGS` - flags passed to `go test`, to run tests which match a pattern
    85    use `TESTFLAGS="-test.run TestNameOrPrefix"`
    86  
    87  ### Integration Tests
    88  
    89  To run the integration test suite:
    90  
    91  ```
    92  make test-integration
    93  ```
    94  
    95  This make target runs both the "integration" suite and the "integration-cli"
    96  suite.
    97  
    98  You can specify which integration test dirs to build and run by specifying
    99  the list of dirs in the TEST_INTEGRATION_DIR environment variable.
   100  
   101  You can also explicitly skip either suite by setting (any value) in
   102  TEST_SKIP_INTEGRATION and/or TEST_SKIP_INTEGRATION_CLI environment variables.
   103  
   104  Flags specific to each suite can be set in the TESTFLAGS_INTEGRATION and
   105  TESTFLAGS_INTEGRATION_CLI environment variables.
   106  
   107  If all you want is to specity a test filter to run, you can set the
   108  `TEST_FILTER` environment variable. This ends up getting passed directly to `go
   109  test -run` (or `go test -check-f`, dpenending on the test suite). It will also
   110  automatically set the other above mentioned environment variables accordingly.
   111  
   112  ### Go Version
   113  
   114  You can change a version of golang used for building stuff that is being tested
   115  by setting `GO_VERSION` variable, for example:
   116  
   117  ```
   118  make GO_VERSION=1.12.8 test
   119  ```