pkg/test
Markus Thömmes 565516e224
Add errorlint and fix all existing issues (#1855)
2020-10-29 01:14:35 -07:00
..
cmd Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
flags Remove re-definition of kubeconfig from pkg/test (#1820) 2020-10-19 16:30:58 -07:00
gcs Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
ghutil Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
gke Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
ha use k8s.interface (#1808) 2020-10-14 09:51:31 -07:00
helpers Drop the usage of knative.dev/pkg/test in the helpers package (#1791) 2020-10-09 08:37:21 -07:00
imports Ban importing aliased domains. (#696) 2020-01-10 10:11:56 -08:00
ingress use k8s.interface (#1808) 2020-10-14 09:51:31 -07:00
interactive Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
logging Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
logstream More test coverage for logstream (#1826) 2020-10-20 15:10:17 -07:00
mako Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
monitoring Spelling (#1797) 2020-10-18 14:22:57 -07:00
prometheus Remove unnecessary override for dependencies (#1816) 2020-10-19 02:39:58 -07:00
prow v2 (#1754) 2020-09-29 13:18:29 -07:00
slackutil Escape test name before matching slack message history (#1011) 2020-01-27 15:03:26 -08:00
spoof Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
upgrade Improve readability with backtics (#1830) 2020-10-21 13:08:32 -07:00
vegeta/pacers Update vegeta to a version supporting go modules (#1811) 2020-10-15 07:25:57 -07:00
zipkin Support short suffix in zipkin config (#1840) 2020-10-26 09:28:41 -07:00
OWNERS Update OWNERS to use OWNERS_ALIASES (#422) 2019-05-29 20:04:33 -07:00
README.md Format markdown (#967) 2019-12-30 10:37:37 -08:00
cleanup.go Migrate pkg to use the knative.dev/pkg import path (#489) 2019-06-26 13:02:06 -07:00
clients.go use kubernetes.Interface for some test helpers (#1825) 2020-10-20 08:28:59 -07:00
crd.go Various nits around the code (#1719) 2020-09-18 16:22:45 -07:00
e2e_flags.go Simplify defaulting and force-default kubeconfig location in tests (#1822) 2020-10-20 07:37:58 -07:00
kube_checks.go use k8s.interface (#1808) 2020-10-14 09:51:31 -07:00
presubmit-tests.sh use the default unit test runner (#1871) 2020-10-28 07:28:34 -07:00
request.go use kubernetes.Interface for some test helpers (#1825) 2020-10-20 08:28:59 -07:00
test-reconciler-codegen.sh use new hack repo (#1858) 2020-10-27 15:38:33 -07:00
tinterface.go Remove re-definition of kubeconfig from pkg/test (#1820) 2020-10-19 16:30:58 -07:00

README.md

Test

This directory contains tests and testing docs.

Running unit tests

To run all unit tests:

go test ./...

Test library

You can use the test library in this dir to:

Use common test flags

These flags are useful for running against an existing cluster, making use of your existing environment setup.

By importing knative.dev/pkg/test you get access to a global variable called test.Flags which holds the values of the command line flags.

logger.Infof("Using namespace %s", test.Flags.Namespace)

See e2e_flags.go.

Output logs

When tests are run with --logverbose option, debug logs will be emitted to stdout.

We are using a generic FormatLogger that can be passed in any existing logger that satisfies it. Test can use the generic logging methods to log info and error logs. All the common methods accept generic FormatLogger as a parameter and tests can pass in t.Logf like this:

_, err = pkgTest.WaitForEndpointState(
    clients.KubeClient,
    t.Logf,
    ...),

See logging.go.

Check Knative Serving resources

WARNING: this code also exists in knative/serving.

After creating Knative Serving resources or making changes to them, you will need to wait for the system to realize those changes. You can use the Knative Serving CRD check and polling methods to check the resources are either in or reach the desired state.

The WaitFor* functions use the kubernetes wait package. To poll they use PollImmediate and the return values of the function you provide behave the same as ConditionFunc: a bool to indicate if the function should stop or continue polling, and an error to indicate if there has been an error.

For example, you can poll a Configuration object to find the name of the Revision that was created for it:

var revisionName string
err := test.WaitForConfigurationState(
    clients.ServingClient, configName, func(c *v1alpha1.Configuration) (bool, error) {
        if c.Status.LatestCreatedRevisionName != "" {
            revisionName = c.Status.LatestCreatedRevisionName
            return true, nil
        }
        return false, nil
    }, "ConfigurationUpdatedWithRevision")

See kube_checks.go.

Ensure test cleanup

To ensure your test is cleaned up, you should defer cleanup to execute after your test completes and also ensure the cleanup occurs if the test is interrupted:

defer tearDown(clients)
test.CleanupOnInterrupt(func() { tearDown(clients) })

See cleanup.go.

Flags

Importing the test library adds flags that are useful for end to end tests that need to run against a cluster.

Tests importing knative.dev/pkg/test recognize these flags:

Specifying kubeconfig

By default the tests will use the kubeconfig file at ~/.kube/config. If there is an error getting the current user, it will use kubeconfig instead as the default value. You can specify a different config file with the argument --kubeconfig.

To run tests with a non-default kubeconfig file:

go test ./test --kubeconfig /my/path/kubeconfig

Specifying cluster

The --cluster argument lets you use a different cluster than your specified kubeconfig's active context.

go test ./test --cluster your-cluster-name

The current cluster names can be obtained by running:

kubectl config get-clusters

Specifying ingress endpoint

The --ingressendpoint argument lets you specify a static url to use as the ingress server during tests. This is useful for Kubernetes configurations which do not provide external IPs.

go test ./test --ingressendpoint <k8s-controller-ip>:32380

Specifying namespace

The --namespace argument lets you specify the namespace to use for the tests. By default, tests will use serving-tests.

go test ./test --namespace your-namespace-name

Output verbose logs

The --logverbose argument lets you see verbose test logs and k8s logs.

go test ./test --logverbose

Specifying docker repo

The --dockerrepo argument lets you specify a uri of the docker repo where you have uploaded the test image to using uploadtestimage.sh. Defaults to $KO_DOCKER_REPO

go test ./test --dockerrepo myspecialdockerrepo

Specifying tag

The --tag argument lets you specify the version tag for the test images.

go test ./test --tag v1.0

Specifying image template

The --imagetemplate argument lets you specify a template to generate the reference to an image from the test. Defaults to {{.Repository}}/{{.Name}}:{{.Tag}}

go test ./test --imagetemplate {{.Repository}}/{{.Name}}:{{.Tag}}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License.