knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/README.md (about)

     1  # Test
     2  
     3  This directory contains tests and testing docs.
     4  
     5  - [Test library](#test-library) contains code you can use in your `knative`
     6    tests
     7  - [Flags](#flags) added by [the test library](#test-library)
     8  - [Unit tests](#running-unit-tests) currently reside in the codebase alongside
     9    the code they test
    10  
    11  ## Running unit tests
    12  
    13  To run all unit tests:
    14  
    15  ```bash
    16  go test ./...
    17  ```
    18  
    19  ## Test library
    20  
    21  You can use the test library in this dir to:
    22  
    23  - [Use common test flags](#use-common-test-flags)
    24  - [Output logs](#output-logs)
    25  - [Ensure test cleanup](#ensure-test-cleanup)
    26  
    27  ### Use common test flags
    28  
    29  These flags are useful for running against an existing cluster, making use of
    30  your existing
    31  [environment setup](https://github.com/knative/serving/blob/main/DEVELOPMENT.md#environment-setup).
    32  
    33  By importing `knative.dev/pkg/test` you get access to a global variable called
    34  `test.Flags` which holds the values of
    35  [the command line flags](/test/README.md#flags).
    36  
    37  ```go
    38  logger.Infof("Using namespace %s", test.Flags.Namespace)
    39  ```
    40  
    41  _See [e2e_flags.go](./e2e_flags.go)._
    42  
    43  ### Output logs
    44  
    45  [When tests are run with `--logverbose` option](README.md#output-verbose-logs),
    46  debug logs will be emitted to stdout.
    47  
    48  We are using a generic
    49  [FormatLogger](https://github.com/knative/pkg/blob/main/test/logging/logging.go#L49)
    50  that can be passed in any existing logger that satisfies it. Test can use the
    51  generic [logging methods](https://golang.org/pkg/testing/#T) to log info and
    52  error logs. All the common methods accept generic FormatLogger as a parameter
    53  and tests can pass in `t.Logf` like this:
    54  
    55  ```go
    56  _, err = pkgTest.WaitForEndpointState(
    57      kubeClient,
    58      t.Logf,
    59      ...),
    60  ```
    61  
    62  _See [logging.go](./logging/logging.go)._
    63  
    64  ### Check Knative Serving resources
    65  
    66  _WARNING: this code also exists in
    67  [`knative/serving`](https://github.com/knative/serving/blob/main/test/adding_tests.md#make-requests-against-deployed-services)._
    68  
    69  After creating Knative Serving resources or making changes to them, you will
    70  need to wait for the system to realize those changes. You can use the Knative
    71  Serving CRD check and polling methods to check the resources are either in or
    72  reach the desired state.
    73  
    74  The `WaitFor*` functions use the kubernetes
    75  [`wait` package](https://godoc.org/k8s.io/apimachinery/pkg/util/wait). To poll
    76  they use
    77  [`PollImmediate`](https://godoc.org/k8s.io/apimachinery/pkg/util/wait#PollImmediate)
    78  and the return values of the function you provide behave the same as
    79  [`ConditionFunc`](https://godoc.org/k8s.io/apimachinery/pkg/util/wait#ConditionFunc):
    80  a `bool` to indicate if the function should stop or continue polling, and an
    81  `error` to indicate if there has been an error.
    82  
    83  For example, you can poll a `Configuration` object to find the name of the
    84  `Revision` that was created for it:
    85  
    86  ```go
    87  var revisionName string
    88  err := test.WaitForConfigurationState(
    89      clients.ServingClient, configName, func(c *v1alpha1.Configuration) (bool, error) {
    90          if c.Status.LatestCreatedRevisionName != "" {
    91              revisionName = c.Status.LatestCreatedRevisionName
    92              return true, nil
    93          }
    94          return false, nil
    95      }, "ConfigurationUpdatedWithRevision")
    96  ```
    97  
    98  _See [kube_checks.go](./kube_checks.go)._
    99  
   100  ### Ensure test cleanup
   101  
   102  To ensure your test is cleaned up, you should defer cleanup to execute after
   103  your test completes and also ensure the cleanup occurs if the test is
   104  interrupted:
   105  
   106  ```go
   107  defer tearDown(clients)
   108  test.CleanupOnInterrupt(func() { tearDown(clients) })
   109  ```
   110  
   111  _See [cleanup.go](./cleanup.go)._
   112  
   113  ## Flags
   114  
   115  Importing [the test library](#test-library) adds flags that are useful for end
   116  to end tests that need to run against a cluster.
   117  
   118  Tests importing [`knative.dev/pkg/test`](#test-library) recognize these flags:
   119  
   120  - [`--kubeconfig`](#specifying-kubeconfig)
   121  - [`--cluster`](#specifying-cluster)
   122  - [`--namespace`](#specifying-namespace)
   123  - [`--logverbose`](#output-verbose-logs)
   124  - [`--ingressendpoint`](#specifying-ingress-endpoint)
   125  - [`--dockerrepo`](#specifying-docker-repo)
   126  - [`--tag`](#specifying-tag)
   127  - [`--imagetemplate`](#specifying-image-template)
   128  
   129  ### Specifying kubeconfig
   130  
   131  By default the tests will use the
   132  [kubeconfig file](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/)
   133  at `~/.kube/config`. If there is an error getting the current user, it will use
   134  `kubeconfig` instead as the default value. You can specify a different config
   135  file with the argument `--kubeconfig`.
   136  
   137  To run tests with a non-default kubeconfig file:
   138  
   139  ```bash
   140  go test ./test --kubeconfig /my/path/kubeconfig
   141  ```
   142  
   143  ### Specifying cluster
   144  
   145  The `--cluster` argument lets you use a different cluster than
   146  [your specified kubeconfig's](#specifying-kubeconfig) active context.
   147  
   148  ```bash
   149  go test ./test --cluster your-cluster-name
   150  ```
   151  
   152  The current cluster names can be obtained by running:
   153  
   154  ```bash
   155  kubectl config get-clusters
   156  ```
   157  
   158  ### Specifying ingress endpoint
   159  
   160  The `--ingressendpoint` argument lets you specify a static url to use as the
   161  ingress server during tests. This is useful for Kubernetes configurations which
   162  do not provide external IPs.
   163  
   164  ```bash
   165  go test ./test --ingressendpoint <k8s-controller-ip>:32380
   166  ```
   167  
   168  ### Specifying namespace
   169  
   170  The `--namespace` argument lets you specify the namespace to use for the tests.
   171  By default, tests will use `serving-tests`.
   172  
   173  ```bash
   174  go test ./test --namespace your-namespace-name
   175  ```
   176  
   177  ### Output verbose logs
   178  
   179  The `--logverbose` argument lets you see verbose test logs and k8s logs.
   180  
   181  ```bash
   182  go test ./test --logverbose
   183  ```
   184  
   185  ### Specifying docker repo
   186  
   187  The `--dockerrepo` argument lets you specify a uri of the docker repo where you
   188  have uploaded the test image to using `uploadtestimage.sh`. Defaults to
   189  `$KO_DOCKER_REPO`
   190  
   191  ```bash
   192  go test ./test --dockerrepo myspecialdockerrepo
   193  ```
   194  
   195  ### Specifying tag
   196  
   197  The `--tag` argument lets you specify the version tag for the test images.
   198  
   199  ```bash
   200  go test ./test --tag v1.0
   201  ```
   202  
   203  ### Specifying image template
   204  
   205  The `--imagetemplate` argument lets you specify a template to generate the
   206  reference to an image from the test. Defaults to
   207  `{{.Repository}}/{{.Name}}:{{.Tag}}`
   208  
   209  ```bash
   210  go test ./test --imagetemplate {{.Repository}}/{{.Name}}:{{.Tag}}
   211  ```
   212  
   213  ---
   214  
   215  Except as otherwise noted, the content of this page is licensed under the
   216  [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/),
   217  and code samples are licensed under the
   218  [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0).