test

package
v0.26.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2024 License: Apache-2.0 Imports: 65 Imported by: 1

README

Tests

To run tests:

# Land the latest code
ko apply -f ./config/

# Run unit tests
go test ./...

# Run integration tests (against your current kube cluster)
go test -v -count=1 -tags=e2e -timeout=20m ./test

Unit tests

Unit tests live side by side with the code they are testing and can be run with:

go test ./...

By default go test will not run the end to end tests, which need -tags=e2e to be enabled.

End to end tests

Setup

Environment variables used by end to end tests:

  • KO_DOCKER_REPO - Set this to an image registry your tests can push images to
Running

End to end tests live in this directory. To run these tests, you must provide go with -tags=e2e. By default the tests run against your current kubeconfig context, but you can change that and other settings with the flags. Run e2e tests with:

go test -v -count=1 -tags=e2e -timeout=20m ./test
go test -v -count=1 -tags=e2e -timeout=20m ./test --kubeconfig ~/special/kubeconfig --cluster myspecialcluster

You can also use all flags defined in knative/pkg/test.

Flags
  • By default the e2e tests run against the current cluster in ~/.kube/config using the environment specified in your environment variables.
  • Since these tests are fairly slow, running them with logging enabled is recommended (-v).
  • Using --logverbose will show the verbose log output from test as well as from k8s libraries.
  • Using -count=1 is the idiomatic way to disable test caching.
  • The e2e tests take a long time to run, so a value like -timeout=20m can be useful depending on what you're running.

You can use test flags to control the environment your tests run against, i.e. override your environment variables:

go test -v -tags=e2e -count=1 ./test --kubeconfig ~/special/kubeconfig --cluster myspecialcluster

Tests importing github.com/tektoncd/triggers/test recognize the flags added by knative/pkg/test.

Tests are run in a new random namespace prefixed with the word arakkis-. Unless you set the TEST_KEEP_NAMESPACES environment variable the namespace will get automatically cleaned up after running each test.

Running specific test cases

To run all the test cases with their names starting with the same letters, e.g. EventListener, use the -run flag with go test:

go test -v -tags=e2e -count=1 ./test -run ^TestEventListener
Running YAML tests

To run the YAML e2e tests, run the following command:

./test/e2e-tests-yaml.sh
Adding integration tests

In the test dir you will find several libraries in the test package you can use in your tests.

This library exists partially in this directory and partially in knative/pkg/test.

The libs in this dir can:

All integration tests must be marked with the e2e build constraint so that go test ./... can be used to run only the unit tests, i.e.:

// +build e2e
Cleaning up cluster-scoped resources

Each integration test runs in its own Namespace; each Namespace is torn down after its integration test completes. However, cluster-scoped resources will not be deleted when the Namespace is deleted. So, each test must delete all the cluster-scoped resources that it creates.

Setup tests

The setup function in init_tests.go will initialize client objects, create a new unique Namespace for the test, and initialize anything needed globally by the tests (i.e. logs and metrics).

clients, namespace := setup(t)

The clients struct contains initialized clients for accessing:

See init_test.go and clients.go for more information.

Poll resources

After creating, updating, or deleting kubernetes resources, you will need to wait for the system to realize these changes. You can use polling methods to check the resources reach the desired state.

The WaitFor* functions use the Kubernetes wait package. For polling they use PollImmediate with a ConditionFunc callback function, which returns a bool to indicate if the polling should stop and an error to indicate if there was an error.

See wait.go for more information.

Generate random names

You can use the names package from the Tekton Pipeline project to append a random string, so that your tests can use unique names each time they run.

import "github.com/tektoncd/pipeline/pkg/names"

namespace := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("arrakis")
Running presubmit integration tests

The presubmit integration tests entrypoint will run:

When run using Prow, integration tests will try to get a new cluster using boskos, which only the tektoncd/plumbing OWNERS have access to.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTektonResources added in v0.2.0

func AddTektonResources(clientset *fakekubeclientset.Clientset)

AddTektonResources will update clientset to know it knows about the types it is expected to be able to interact with.

func CollectPodLogsWithLabel

func CollectPodLogsWithLabel(c kubernetes.Interface, namespace, labelSelector string) (string, error)

CollectPodLogsWithLabel will get the logs of all the Pods given a labelSelector

func CompareParams added in v0.2.0

func CompareParams(x, y v1beta1.Param) bool

CompareParams can be used with comparison options such as `cmpopts.SortSlices` so we can ignore the ordering of params.

func ConfigMapFromTestFile added in v0.16.0

func ConfigMapFromTestFile(t *testing.T, name string) *corev1.ConfigMap

ConfigMapFromTestFile creates a v1.ConfigMap from a YAML file It loads the YAML file from the testdata folder.

func FeatureFlagsToContext added in v0.16.0

func FeatureFlagsToContext(ctx context.Context, flags map[string]string) (context.Context, error)

FeatureFlagsToContext takes a map of feature flags and adds it to the context nolint:unused,deadcode

func HMACHeader added in v0.11.0

func HMACHeader(t testing.TB, secret string, body []byte, algorithm string) string

HMACHeader generates a X-Hub-Signature header given a secret token and the request body See https://developer.github.com/webhooks/securing/#validating-payloads-from-github algorithm must be one of (sha1, sha256)

func RawExtension added in v0.9.0

func RawExtension(t testing.TB, a interface{}) runtime.RawExtension

RawExtenstion is a test helper to generate RawExtension objects for tests

func SetupFakeContext added in v0.18.0

func SetupFakeContext(t testing.TB) (context.Context, []controller.Informer)

func ToUnstructured added in v0.2.0

func ToUnstructured(t *testing.T, in interface{}) *unstructured.Unstructured

ToUnstructured returns an Unstructured object from interface in.

func ToV1JSON added in v0.13.0

func ToV1JSON(t testing.TB, v interface{}) apiextensionsv1.JSON

ToV1JSON is a wrapper around json.Marshal to easily convert to the Kubernetes apiextensionsv1.JSON type

func WaitFor

func WaitFor(waitFunc wait.ConditionFunc) error

WaitFor waits for the specified ConditionFunc every internal until the timeout.

Types

type Assets added in v0.2.0

type Assets struct {
	Controller *controller.Impl
	Clients    Clients
}

Assets holds references to the controller and clients.

type Clients

Clients holds references to clients which are useful for reconciler tests.

func SeedResources added in v0.2.0

func SeedResources(t *testing.T, ctx context.Context, r Resources) Clients

SeedResources returns Clients populated with the given Resources nolint: revive

type Resources added in v0.2.0

type Resources struct {
	Namespaces             []*corev1.Namespace
	ClusterTriggerBindings []*v1beta1.ClusterTriggerBinding
	EventListeners         []*v1beta1.EventListener
	ClusterInterceptors    []*v1alpha1.ClusterInterceptor
	Interceptors           []*v1alpha1.Interceptor
	TriggerBindings        []*v1beta1.TriggerBinding
	TriggerTemplates       []*v1beta1.TriggerTemplate
	Triggers               []*v1beta1.Trigger
	Deployments            []*appsv1.Deployment
	Services               []*corev1.Service
	Secrets                []*corev1.Secret
	ServiceAccounts        []*corev1.ServiceAccount
	Pods                   []*corev1.Pod
	WithPod                []*duckv1.WithPod
}

Resources represents the desired state of the system (i.e. existing resources) to seed controllers with.

func GetResourcesFromClients added in v0.2.0

func GetResourcesFromClients(c Clients) (*Resources, error)

GetResourcesFromClients returns the Resources in the Clients provided Precondition: all Namespaces used in Resources must be listed in Resources.Namespaces nolint: golint

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL