test

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0, BSD-2-Clause, MIT, + 1 more Imports: 31 Imported by: 0

Documentation

Overview

Package test contains test utilities. Following is the guide on how to use it:

Utilities in kind.go: Tests that require a KIND cluster should use utilities inside this file.

To create a KIND cluster use: ```

// to create a KIND test cluster
kindCluster, err := test.CreateKindCluster(test.KindConfig{Name: "<name-of-kind-cluster>"})
if err != nil {
	panic(err) // you can decide how to handle this error in a different way
}

// get the rest.Config for the KIND cluster
restConfig := kindCluster.GetRestConfig()

// currently it has only a few methods to easily create/delete some test k8s resources like namespace, deployment. more methods can be added incrementally and when required
// create a namespace
err = kindCluster.CreateNamespace("bingo-ns")

// create a simple NGINX deployment
err = kindCluster.CreateDeployment("tringo", "bingo-ns", "nginx:1.14.2", 1, map[string]string{"annotation-key":"annotation-value"})

// delete a previously created deployment
err = kindCluster.DeleteDeployment("tringo", "bingo-ns")

// to delete the KIND test cluster
err = kindCluster.Delete()

```

Utilities in testenv.go: Tests that require a controller-runtime envtest should use utilities inside this file ```

// to create a default controller-runtime test environment
ctrlTestEnv, err := test.CreateDefaultControllerTestEnv()

// to create a controller-runtime test environment using custom scheme and crdDirectoryPaths
ctrlTestEnv, err:= test.CreateControllerTestEnv(scheme, crdDirectoryPaths)

// to stop the controller-runtime test environment
ctrlTestEnv.Delete()

// to get client.Client for the test environment
k8sClient := ctrlTestEnv.GetClient()

```

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateClusterResource added in v1.1.0

func CreateClusterResource(numWorkers int, nodeMonitorGracePeriod *metav1.Duration, rawShoot bool) (*gardenerv1alpha1.Cluster, *gardencorev1beta1.Shoot, error)

CreateClusterResource creates a test cluster and shoot resources. This should only be used for unit testing.

func CreateShoot added in v1.2.0

func CreateShoot(seedName string, numWorkers int, nodeMonitorGracePeriod *metav1.Duration) gardencorev1beta1.Shoot

CreateShoot creates a shoot resources. This should only be used for unit testing.

func CreateTestNamespace

func CreateTestNamespace(ctx context.Context, g *WithT, cli client.Client, namePrefix string) string

CreateTestNamespace creates a namespace with the given namePrefix

func FileExistsOrFail

func FileExistsOrFail(filepath string)

FileExistsOrFail checks if the given filepath is valid and returns an error if file is not found or does not exist.

func GetStructured

func GetStructured[T any](filepath string) (*T, error)

GetStructured reads the file present at the given filePath and returns a structured object based on the type T.

func GetUnstructured

func GetUnstructured(filePath string) (*unstructured.Unstructured, error)

GetUnstructured reads the file present at the given filePath and returns an unstructured.Unstructured object from its contents.

func MergeMaps added in v1.1.0

func MergeMaps[T any](oldMap map[string]T, newMaps ...map[string]T) map[string]T

MergeMaps merges newMaps with an oldMap. Keys defined in the new Map which are present in the old Map will be overwritten.

func ReadFile

func ReadFile(filePath string) (*bytes.Buffer, error)

ReadFile reads the file present at the given filePath and returns a byte Buffer containing its contents.

func TeardownEnv added in v1.1.0

func TeardownEnv(g *WithT, testEnv *envtest.Environment, cancelFn context.CancelFunc)

TeardownEnv cancels the context and stops testenv.

func ValidateIfFileExists

func ValidateIfFileExists(file string, t *testing.T)

ValidateIfFileExists validates the existence of a file

Types

type ControllerTestEnv

type ControllerTestEnv interface {
	// GetClient provides access to the kubernetes client.Client to access the Kube ApiServer.
	GetClient() client.Client
	// GetConfig provides access to *rest.Config.
	GetConfig() *rest.Config
	// GetEnv returns the kubernetes test environment.
	GetEnv() *envtest.Environment
	// Delete deletes the resources created as part of testEnv.
	Delete()
}

ControllerTestEnv is a convenience interface to be used by tests to access controller-runtime testEnv.

func CreateControllerTestEnv

func CreateControllerTestEnv(scheme *runtime.Scheme, crdDirectoryPaths []string, apiServerFlags map[string]string) (ControllerTestEnv, error)

CreateControllerTestEnv creates a controller-runtime testEnv using the provided scheme and crdDirectoryPaths and provides access to the convenience interface to interact with it.

func CreateDefaultControllerTestEnv

func CreateDefaultControllerTestEnv(scheme *runtime.Scheme, apiServerFlags map[string]string) (ControllerTestEnv, error)

CreateDefaultControllerTestEnv creates a controller-runtime testEnv and provides access to the convenience interface to interact with it.

type KindCluster

type KindCluster interface {
	// CreateNamespace creates a kubernetes namespace with the give name.
	CreateNamespace(name string) error
	// CreateDeployment creates a kubernetes deployment.
	CreateDeployment(name, namespace, imageName string, replicas int32, annotations map[string]string) error
	// DeleteAllDeployments deletes all kubernetes deployments in a given namespace.
	DeleteAllDeployments(namespace string) error
	// GetRestConfig provides access to *rest.Config.
	GetRestConfig() *rest.Config
	// GetClient provides access to client.Client to connect to the Kube ApiServer of the KIND cluster.
	GetClient() client.Client
	// GetDeployment looks up a kubernetes deployment with a given name and namespace and returns it if it is found else returns an error.
	// The consumer will have to check if the error is NotFoundError and take appropriate action.
	GetDeployment(namespace, name string) (*appsv1.Deployment, error)
	// Delete deletes the KIND cluster.
	Delete() error
}

KindCluster provides a convenient interface to interact with a KIND cluster.

func CreateKindCluster

func CreateKindCluster(config KindConfig) (KindCluster, error)

CreateKindCluster creates a new KIND cluster using the config passed

type KindConfig

type KindConfig struct {
	Name                    string
	NodeImage               string
	ControlPlanReadyTimeout *time.Duration
}

KindConfig holds configuration which will be used when creating a KIND cluster.

Jump to

Keyboard shortcuts

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