test

package
v0.0.0-...-87426b5 Latest Latest
Warning

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

Go to latest
Published: May 13, 2020 License: Apache-2.0 Imports: 25 Imported by: 0

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(
    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.

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.
	Flags = initializeFlags()
)

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 CleanupOnInterrupt

func CleanupOnInterrupt(cleanup func(), logf logging.FormatLogger)

CleanupOnInterrupt will execute the function cleanup if an interrupt signal is caught

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 DeploymentScaledToZeroFunc

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

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

func EventuallyMatchesBody

func EventuallyMatchesBody(expected string) spoof.ResponseChecker

EventuallyMatchesBody checks that the response body *eventually* matches the expected body. TODO(#1178): Delete me. We don't want to need this; we should be waiting for an appropriate Status instead.

func GetConfigMap

func GetConfigMap(client *KubeClient, namespace string) k8styped.ConfigMapInterface

GetConfigMap gets the configmaps for a given namespace

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 IsOneOfStatusCodes

func IsOneOfStatusCodes(codes ...int) spoof.ResponseChecker

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

func IsStatusOK

func IsStatusOK(resp *spoof.Response) (bool, error)

IsStatusOK checks that the response code is a 200.

func MatchesAllOf

func MatchesAllOf(checkers ...spoof.ResponseChecker) spoof.ResponseChecker

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.

func MatchesBody

func MatchesBody(expected string) spoof.ResponseChecker

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

func NewSpoofingClient

func NewSpoofingClient(client *KubeClient, 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 PodRunning

func PodRunning(pod *corev1.Pod) bool

PodRunning will check the status conditions of the pod and return true if it's Running

func PodsRunning

func PodsRunning(podList *corev1.PodList) (bool, error)

PodsRunning will check the status conditions of the pod list and return true all pods are Running

func Retrying

func Retrying(rc spoof.ResponseChecker, codes ...int) spoof.ResponseChecker

Retrying modifies a ResponseChecker to retry certain response codes.

func ServiceAccount

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

ServiceAccount returns ServiceAccount object in given namespace

func SetupLoggingFlags

func SetupLoggingFlags()

TODO(coryrc): Remove once other repos are moved to call logging.InitializeLogger() directly

func WaitForAllPodsRunning

func WaitForAllPodsRunning(client *KubeClient, namespace string) error

WaitForAllPodsRunning waits for all the pods to be in running state

func WaitForDeploymentState

func WaitForDeploymentState(client *KubeClient, 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(
	kubeClient *KubeClient,
	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(
	kubeClient *KubeClient,
	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(client *KubeClient, 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 WaitForPodListState

func WaitForPodListState(client *KubeClient, 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(client *KubeClient, name string, namespace string) error

WaitForPodRunning waits for the given pod to be in running state

func WaitForPodState

func WaitForPodState(client *KubeClient, 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(client *KubeClient, svcName string, svcNamespace string, numOfEndpoints int) error

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

Types

type EnvironmentFlags

type EnvironmentFlags struct {
	Cluster         string // K8s cluster (defaults to cluster in kubeconfig)
	Kubeconfig      string // Path to kubeconfig (defaults to ./kube/config)
	Namespace       string // K8s namespace (blank by default, to be overwritten by test suite)
	IngressEndpoint string // Host to use for ingress endpoint
	ImageTemplate   string // Template to build the image reference (defaults to {{.Repository}}/{{.Name}}:{{.Tag}})
	DockerRepo      string // Docker repo (defaults to $KO_DOCKER_REPO)
	Tag             string // Tag for test images
}

EnvironmentFlags define the flags that are needed to run the e2e tests.

type KubeClient

type KubeClient struct {
	Kube *kubernetes.Clientset
}

KubeClient holds instances of interfaces for making requests to kubernetes client.

func NewKubeClient

func NewKubeClient(configPath string, clusterName string) (*KubeClient, error)

NewKubeClient instantiates and returns several clientsets required for making request to the kube client specified by the combination of clusterName and configPath. Clients can make requests within namespace.

func (*KubeClient) CreatePod

func (client *KubeClient) CreatePod(pod *corev1.Pod) (*corev1.Pod, error)

CreatePod will create a Pod

func (*KubeClient) GetConfigMap

func (client *KubeClient) GetConfigMap(name string) k8styped.ConfigMapInterface

GetConfigMap gets the knative serving config map.

func (*KubeClient) PodLogs

func (client *KubeClient) PodLogs(podName, containerName, namespace string) ([]byte, error)

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

func (*KubeClient) UpdateConfigMap

func (client *KubeClient) UpdateConfigMap(name string, configName string, values map[string]string) error

UpdateConfigMap updates the config map for specified @name with values

type RequestOption

type RequestOption func(*http.Request)

RequestOption enables configuration of requests when polling for endpoint states.

func WithHeader

func WithHeader(header http.Header) RequestOption

WithHeader will add the provided headers to the request.

type T

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

type TLegacy

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

Directories

Path Synopsis
gke
Package logging assists setting up test logging and using leveled logging in tests.
Package logging assists setting up test logging and using leveled logging in tests.
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.
Package monitoring provides common methods for all the monitoring components used in the tests This package exposes following methods: CheckPortAvailability(port int) error Checks if the given port is available GetPods(kubeClientset *kubernetes.Clientset, app string) (*v1.PodList, error) Gets the list of pods that satisfy the lable selector app=<app> Cleanup(pid int) error Kill the current port forwarding process running in the background PortForward(logf logging.FormatLogger, podList *v1.PodList, localPort, remotePort int) (int, error) Create a background process that will port forward the first pod from the local to remote port It returns the process id for the background process created.
Package monitoring provides common methods for all the monitoring components used in the tests This package exposes following methods: CheckPortAvailability(port int) error Checks if the given port is available GetPods(kubeClientset *kubernetes.Clientset, app string) (*v1.PodList, error) Gets the list of pods that satisfy the lable selector app=<app> Cleanup(pid int) error Kill the current port forwarding process running in the background PortForward(logf logging.FormatLogger, podList *v1.PodList, localPort, remotePort int) (int, error) Create a background process that will port forward the first pod from the local to remote port It returns the process id for the background process created.
vegeta
webhook-apicoverage
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