test

package
v0.0.0-...-ca5f1ce Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 27 Imported by: 218

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Flags holds the command line flags or defaults for settings in the user's environment.
	// See EnvironmentFlags for a list of supported fields.
	// Deprecated: use test/flags.Flags()
	Flags = initializeFlags()
)
View Source
var IsOneOfStatusCodes = spoof.IsOneOfStatusCodes

IsOneOfStatusCodes checks that the response code is equal to the given one.

Deprecated: Use the spoof package version

View Source
var IsStatusOK = spoof.IsStatusOK

IsStatusOK checks that the response code is a 200.

Deprecated: Use the spoof package version

View Source
var MatchesAllBodies = spoof.MatchesAllBodies

MatchesAllBodies checks that the *first* response body matches the "expected" body, otherwise failing.

Deprecated: Use the spoof package version

View Source
var MatchesAllOf = spoof.MatchesAllOf

MatchesAllOf combines multiple ResponseCheckers to one ResponseChecker with a logical AND. The checkers are executed in order. The first function to trigger an error or a retry will short-circuit the other functions (they will not be executed).

This is useful for combining a body with a status check like: MatchesAllOf(IsStatusOK, MatchesBody("test"))

The MatchesBody check will only be executed after the IsStatusOK has passed.

Deprecated: Use the spoof package version

View Source
var MatchesBody = spoof.MatchesBody

MatchesBody checks that the *first* response body matches the "expected" body, otherwise failing.

Deprecated: Use the spoof package version

View Source
var WithHeader = spoof.WithHeader

WithHeader will add the provided headers to the request.

Deprecated: Use the spoof package version

Functions

func BuildClientConfig

func BuildClientConfig(kubeConfigPath string, clusterName string) (*rest.Config, error)

BuildClientConfig builds the client config specified by the config path and the cluster name

func CheckEndpointState

func CheckEndpointState(
	ctx context.Context,
	kubeClient kubernetes.Interface,
	logf logging.FormatLogger,
	url *url.URL,
	inState spoof.ResponseChecker,
	desc string,
	resolvable bool,
	opts ...interface{},
) (*spoof.Response, error)

func CleanupOnInterrupt

func CleanupOnInterrupt(f func(), log logFunc)

CleanupOnInterrupt will execute the function if an interrupt signal is caught Deprecated - use OnInterrupt

func ClusterRoleBinding

func ClusterRoleBinding(name string, namespace string, serviceAccount string, role string) *rbacv1.ClusterRoleBinding

ClusterRoleBinding returns ClusterRoleBinding for given subject and role

func CoreV1ObjectReference

func CoreV1ObjectReference(kind, apiversion, name string) *corev1.ObjectReference

CoreV1ObjectReference returns a corev1.ObjectReference for the given name, kind and apiversion

func CreatePod

func CreatePod(ctx context.Context, client kubernetes.Interface, pod *corev1.Pod) (*corev1.Pod, error)

CreatePod will create a Pod

func DeploymentScaledToZeroFunc

func DeploymentScaledToZeroFunc() func(d *appsv1.Deployment) (bool, error)

DeploymentScaledToZeroFunc returns a func that evaluates if a deployment has scaled to 0 pods

func GetConfigMap

func GetConfigMap(client kubernetes.Interface, namespace string) k8styped.ConfigMapInterface

GetConfigMap gets the configmaps for a given namespace

func GetEndpointAddresses

func GetEndpointAddresses(ctx context.Context, client kubernetes.Interface, svcName, svcNamespace string) ([]string, error)

GetEndpointAddresses returns addresses of endpoints for the given service.

func ImagePath

func ImagePath(name string) string

ImagePath is a helper function to transform an image name into an image reference that can be pulled.

func NewSpoofingClient

func NewSpoofingClient(ctx context.Context, client kubernetes.Interface, logf logging.FormatLogger,
	domain string, resolvable bool, opts ...spoof.TransportOption) (*spoof.SpoofingClient, error)

NewSpoofingClient returns a spoofing client to make requests

func NginxPod

func NginxPod(namespace string) *corev1.Pod

NginxPod returns nginx pod defined in given namespace

func OnInterrupt

func OnInterrupt(cleanupFunc func())

OnInterrupt registers a cleanup function to run if an interrupt signal is caught

func PodLogs

func PodLogs(ctx context.Context, client kubernetes.Interface, podName, containerName, namespace string) ([]byte, error)

PodLogs returns Pod logs for given Pod and Container in the namespace

func ServiceAccount

func ServiceAccount(name, namespace string) *corev1.ServiceAccount

ServiceAccount returns ServiceAccount object in given namespace

func SetupLoggingFlags

func SetupLoggingFlags()

SetupLoggingFlags initializes a logger for tests. TODO(coryrc): Remove once other repos are moved to call logging.InitializeLogger() directly

func UpdateConfigMap

func UpdateConfigMap(ctx context.Context, client kubernetes.Interface, name string, configName string, values map[string]string) error

UpdateConfigMap updates the config map for specified @name with values

func WaitForAllPodsRunning

func WaitForAllPodsRunning(ctx context.Context, client kubernetes.Interface, namespace string) error

WaitForAllPodsRunning waits for all the pods to be in running state

func WaitForChangedEndpoints

func WaitForChangedEndpoints(ctx context.Context, client kubernetes.Interface, svcName, svcNamespace string, origEndpoints []string) error

WaitForChangedEndpoints waits until the endpoints for the given service differ from origEndpoints.

func WaitForDeploymentScale

func WaitForDeploymentScale(ctx context.Context, client kubernetes.Interface, name, namespace string, scale int) error

WaitForDeploymentScale waits until the given deployment has the expected scale.

func WaitForDeploymentState

func WaitForDeploymentState(ctx context.Context, client kubernetes.Interface, name string, inState func(d *appsv1.Deployment) (bool, error), desc string, namespace string, timeout time.Duration) error

WaitForDeploymentState polls the status of the Deployment called name from client every interval until inState returns `true` indicating it is done, returns an error or timeout. desc will be used to name the metric that is emitted to track how long it took for name to get into the state checked by inState.

func WaitForEndpointState

func WaitForEndpointState(
	ctx context.Context,
	kubeClient kubernetes.Interface,
	logf logging.FormatLogger,
	url *url.URL,
	inState spoof.ResponseChecker,
	desc string,
	resolvable bool,
	opts ...interface{}) (*spoof.Response, error)

WaitForEndpointState will poll an endpoint until inState indicates the state is achieved, or default timeout is reached. If resolvableDomain is false, it will use kubeClientset to look up the ingress and spoof the domain in the request headers, otherwise it will make the request directly to domain. desc will be used to name the metric that is emitted to track how long it took for the domain to get into the state checked by inState. Commas in `desc` must be escaped.

func WaitForEndpointStateWithTimeout

func WaitForEndpointStateWithTimeout(
	ctx context.Context,
	kubeClient kubernetes.Interface,
	logf logging.FormatLogger,
	url *url.URL,
	inState spoof.ResponseChecker,
	desc string,
	resolvable bool,
	timeout time.Duration,
	opts ...interface{}) (*spoof.Response, error)

WaitForEndpointStateWithTimeout will poll an endpoint until inState indicates the state is achieved or the provided timeout is achieved. If resolvableDomain is false, it will use kubeClientset to look up the ingress and spoof the domain in the request headers, otherwise it will make the request directly to domain. desc will be used to name the metric that is emitted to track how long it took for the domain to get into the state checked by inState. Commas in `desc` must be escaped.

func WaitForLogContent

func WaitForLogContent(ctx context.Context, client kubernetes.Interface, podName, containerName, namespace, content string) error

WaitForLogContent waits until logs for given Pod/Container include the given content. If the content is not present within timeout it returns error.

func WaitForPodDeleted

func WaitForPodDeleted(ctx context.Context, client kubernetes.Interface, name, namespace string) error

WaitForPodDeleted waits for the given pod to disappear from the given namespace.

func WaitForPodListState

func WaitForPodListState(ctx context.Context, client kubernetes.Interface, inState func(p *corev1.PodList) (bool, error), desc string, namespace string) error

WaitForPodListState polls the status of the PodList from client every interval until inState returns `true` indicating it is done, returns an error or timeout. desc will be used to name the metric that is emitted to track how long it took to get into the state checked by inState.

func WaitForPodRunning

func WaitForPodRunning(ctx context.Context, client kubernetes.Interface, name string, namespace string) error

WaitForPodRunning waits for the given pod to be in running state

func WaitForPodState

func WaitForPodState(ctx context.Context, client kubernetes.Interface, inState func(p *corev1.Pod) (bool, error), name string, namespace string) error

WaitForPodState polls the status of the specified Pod from client every interval until inState returns `true` indicating it is done, returns an error or timeout. desc will be used to name the metric that is emitted to track how long it took to get into the state checked by inState.

func WaitForServiceEndpoints

func WaitForServiceEndpoints(ctx context.Context, client kubernetes.Interface, svcName string, svcNamespace string, numOfEndpoints int) error

WaitForServiceEndpoints polls the status of the specified Service from client every interval until number of service endpoints = numOfEndpoints

Types

type EnvironmentFlags

type EnvironmentFlags struct {
	env.ClientConfig
	testenv.TestClientConfig
}

EnvironmentFlags define the flags that are needed to run the e2e tests. Deprecated: use test/flags.Flags() or injection.Flags()

type RequestOption

type RequestOption = spoof.RequestOption

RequestOption enables configuration of requests when polling for endpoint states.

type T

type T interface {
	Name() string
	Helper()
	SkipNow()
	Cleanup(func())
	Log(args ...interface{})
	Error(args ...interface{})
}

T is an interface mimicking *testing.T. Deprecated: Do not use this. Define your own interface.

type TLegacy

type TLegacy interface {
	T
	Logf(fmt string, args ...interface{}) // It gets passed to things in logstream
	Fatal(args ...interface{})
}

TLegacy is an interface mimicking *testing.T. Deprecated: Do not use this. Define your own interface.

Directories

Path Synopsis
gcs
gke
Helper functions for running interactive CLI sessions from Go
Helper functions for running interactive CLI sessions from Go
Package logstream lets end-to-end tests incorporate controller logs into the error output of tests.
Package logstream lets end-to-end tests incorporate controller logs into the error output of tests.
v2
Package monitoring provides common methods for all the monitoring components used in the tests
Package monitoring provides common methods for all the monitoring components used in the tests
vegeta
Package zipkin adds Zipkin tracing support that can be used in conjunction with SpoofingClient to log zipkin traces for requests that have encountered server errors i.e HTTP request that have HTTP status between 500 to 600.
Package zipkin adds Zipkin tracing support that can be used in conjunction with SpoofingClient to log zipkin traces for requests that have encountered server errors i.e HTTP request that have HTTP status between 500 to 600.

Jump to

Keyboard shortcuts

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