test

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 33 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

View Source
const (
	NodeConditionDiskPressure   = "DiskPressure"
	NodeConditionMemoryPressure = "MemoryPressure"
	NodeConditionPIDPressure    = "PIDPressure"
	NodeConditionNetworkReady   = "NetworkReady"
)

Constants for node conditions

View Source
const (
	Worker1Name = "worker-1"
	Worker2Name = "worker-2"
)

Constants for worker names

View Source
const (
	MCMDeploymentName = "machine-controller-manager"
	KCMDeploymentName = "kube-controller-manager"
	CADeploymentName  = "cluster-autoscaler"
	DefaultImage      = "registry.k8s.io/pause:3.5"
)

Constants for deployments

View Source
const (
	Node1Name = "node-1"
	Node2Name = "node-2"
	Node3Name = "node-3"
	Node4Name = "node-4"
)

Constants for node names

View Source
const (
	Machine1Name = "machine-1"
	Machine2Name = "machine-2"
	Machine3Name = "machine-3"
	Machine4Name = "machine-4"
)

Constants for machine names

View Source
const DefaultNamespace = "test"

DefaultNamespace is the default namespace used in tests

Variables

This section is empty.

Functions

func CreateTestNamespace

func CreateTestNamespace(ctx context.Context, g *WithT, cli client.Client, name 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 GenerateDeployment added in v1.3.0

func GenerateDeployment(name, namespace, imageName string, replicas int32, annotations map[string]string) *appsv1.Deployment

GenerateDeployment generates a deployment object with the given parameters.

func GenerateMachines added in v1.3.0

func GenerateMachines(machineSpecs []MachineSpec, namespace string) []*v1alpha1.Machine

GenerateMachines generates machines based on the given machine specs.

func GenerateNodeLeases added in v1.3.0

func GenerateNodeLeases(leaseSpecs []NodeLeaseSpec) []*coordinationv1.Lease

GenerateNodeLeases generates leases based on the given lease specs.

func GenerateNodes added in v1.3.0

func GenerateNodes(nodeSpecs []NodeSpec) []*corev1.Node

GenerateNodes generates nodes based on the given node specs.

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 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 ClusterBuilder added in v1.3.0

type ClusterBuilder struct {
	// contains filtered or unexported fields
}

ClusterBuilder is a builder for the cluster resource.

func NewClusterBuilder added in v1.3.0

func NewClusterBuilder() *ClusterBuilder

NewClusterBuilder creates a new instance of ClusterBuilder.

func (*ClusterBuilder) Build added in v1.3.0

Build builds the cluster resource.

func (*ClusterBuilder) WithNodeMonitorGracePeriod added in v1.3.0

func (b *ClusterBuilder) WithNodeMonitorGracePeriod(nodeMonitorGracePeriod *metav1.Duration) *ClusterBuilder

WithNodeMonitorGracePeriod sets the node monitor grace period for the cluster.

func (*ClusterBuilder) WithRawShoot added in v1.3.0

func (b *ClusterBuilder) WithRawShoot(rawShoot bool) *ClusterBuilder

WithRawShoot sets the raw shoot for the cluster.

func (*ClusterBuilder) WithWorkerCount added in v1.3.0

func (b *ClusterBuilder) WithWorkerCount(workerCount int) *ClusterBuilder

WithWorkerCount sets the worker count for the cluster.

func (*ClusterBuilder) WithWorkerNodeConditions added in v1.3.0

func (b *ClusterBuilder) WithWorkerNodeConditions(workerNodeConditions [][]string) *ClusterBuilder

WithWorkerNodeConditions sets the worker node conditions for the cluster.

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.

type MachineSpec added in v1.3.0

type MachineSpec struct {
	Name          string
	Labels        map[string]string
	CurrentStatus v1alpha1.CurrentStatus
	Namespace     string
}

MachineSpec is a specification for a machine.

type NodeLeaseSpec added in v1.3.0

type NodeLeaseSpec struct {
	Name          string
	IsExpired     bool
	IsOwnerRefSet bool
}

NodeLeaseSpec is a specification for a node lease.

type NodeSpec added in v1.3.0

type NodeSpec struct {
	Name        string
	Annotations map[string]string
	Labels      map[string]string
	Conditions  []corev1.NodeCondition
}

NodeSpec is a specification for a node.

Jump to

Keyboard shortcuts

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