pkg/test
Prashant Rewar fef5da99bd
Replace all usages of deprecated wait.PollImmediate with wait.PollUntilContextTimeout (#3004)
Signed-off-by: Prashant Rewar <108176843+prashantrewar@users.noreply.github.com>
2024-04-02 14:44:03 +00:00
..
environment fix package name (#2159) 2021-06-16 12:52:22 -07:00
gcs Go1.19 changes (#2631) 2022-11-03 22:34:05 +00:00
ghutil fix lint warnings (#2589) 2022-09-12 18:15:45 +00:00
gke Use t.Setenv instead of os.Setenv in tests (#2454) 2022-03-10 10:00:48 -08:00
ha Replace all usages of deprecated wait.PollImmediate with wait.PollUntilContextTimeout (#3004) 2024-04-02 14:44:03 +00:00
helpers Add length limit for the generated names used in tests (#2217) 2021-08-25 00:00:25 -07:00
imports Go1.19 changes (#2631) 2022-11-03 22:34:05 +00:00
ingress Go1.19 changes (#2631) 2022-11-03 22:34:05 +00:00
interactive Go1.19 changes (#2631) 2022-11-03 22:34:05 +00:00
logging switch to klog/v2 (#2386) 2022-01-11 13:02:14 -08:00
logstream Replace all usages of deprecated wait.PollImmediate with wait.PollUntilContextTimeout (#3004) 2024-04-02 14:44:03 +00:00
mako update OWNERS files to use teams with active members (#2672) 2023-02-06 16:58:45 +00:00
migrate Check Knative CustomResourceDefinitions stored version (#2272) 2021-10-04 06:38:27 -07:00
monitoring Tidy helper to avoid need for nosec comment (#2377) 2021-12-14 04:44:51 -08:00
prometheus bump k8s dependencies and update min version to v1.25 (#2745) 2023-05-23 14:06:11 +00:00
prow Use t.Setenv instead of os.Setenv in tests (#2454) 2022-03-10 10:00:48 -08:00
security Function AllowRestrictedPodSecurityStandard for modifying test pods (#2625) 2022-11-07 17:11:17 +00:00
slackutil fix lint warnings (#2589) 2022-09-12 18:15:45 +00:00
spoof Replace all usages of deprecated wait.PollImmediate with wait.PollUntilContextTimeout (#3004) 2024-04-02 14:44:03 +00:00
upgrade Print each line separately in shell executor (#2867) 2023-10-16 14:25:34 +00:00
vegeta/pacers Go1.19 changes (#2631) 2022-11-03 22:34:05 +00:00
zipkin Use go sync/atomic instead of go.uber.org/atomic (#2777) 2023-08-02 16:26:48 +00:00
OWNERS Update OWNERS_ALIASES to match autogen in community (#2078) 2021-04-08 07:42:51 -07:00
README.md use more general kubernetes.Interface (#2135) 2021-05-31 06:44:32 -07:00
cleanup.go support multiple cleanup functions (#2141) 2021-06-10 05:30:43 -07:00
cleanup_test.go Replace all usages of deprecated wait.PollImmediate with wait.PollUntilContextTimeout (#3004) 2024-04-02 14:44:03 +00:00
clients.go Use default Kubeconfig loading rules (#2197) 2021-07-22 11:42:03 -07:00
crd.go Various nits around the code (#1719) 2020-09-18 16:22:45 -07:00
e2e_flags.go removing default for KUBECONFIG to enable in cluster tests (#2191) 2021-07-15 06:35:32 -07:00
kube_checks.go Replace all usages of deprecated wait.PollImmediate with wait.PollUntilContextTimeout (#3004) 2024-04-02 14:44:03 +00:00
presubmit-tests.sh remove defunct markdown linting variable (#2552) 2022-07-22 17:59:21 +00:00
request.go Remove unclear comment for makeSpoofClient() (#2347) 2021-11-11 03:49:38 -08:00
test-reconciler-codegen.sh use new hack repo (#1858) 2020-10-27 15:38:33 -07:00
tinterface.go Fix revive related linting issues (#2131) 2021-05-26 01:10:28 -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(
    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.