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
- func CreateTestNamespace(ctx context.Context, g *WithT, cli client.Client, name string)
- func FileExistsOrFail(filepath string)
- func GenerateDeployment(name, namespace, imageName string, replicas int32, ...) *appsv1.Deployment
- func GenerateMachines(machineSpecs []MachineSpec, namespace string) []*v1alpha1.Machine
- func GenerateNodeLeases(leaseSpecs []NodeLeaseSpec) []*coordinationv1.Lease
- func GenerateNodes(nodeSpecs []NodeSpec) []*corev1.Node
- func GetStructured[T any](filepath string) (*T, error)
- func GetUnstructured(filePath string) (*unstructured.Unstructured, error)
- func ReadFile(filePath string) (*bytes.Buffer, error)
- func TeardownEnv(g *WithT, testEnv *envtest.Environment, cancelFn context.CancelFunc)
- func ValidateIfFileExists(file string, t *testing.T)
- type ClusterBuilder
- func (b *ClusterBuilder) Build() (*gardenerv1alpha1.Cluster, *gardencorev1beta1.Shoot, error)
- func (b *ClusterBuilder) WithNodeMonitorGracePeriod(nodeMonitorGracePeriod *metav1.Duration) *ClusterBuilder
- func (b *ClusterBuilder) WithRawShoot(rawShoot bool) *ClusterBuilder
- func (b *ClusterBuilder) WithWorkerCount(workerCount int) *ClusterBuilder
- func (b *ClusterBuilder) WithWorkerNodeConditions(workerNodeConditions [][]string) *ClusterBuilder
- type ControllerTestEnv
- type KindCluster
- type KindConfig
- type MachineSpec
- type NodeLeaseSpec
- type NodeSpec
Constants ¶
const ( NodeConditionDiskPressure = "DiskPressure" NodeConditionMemoryPressure = "MemoryPressure" NodeConditionPIDPressure = "PIDPressure" NodeConditionNetworkReady = "NetworkReady" )
Constants for node conditions
const ( Worker1Name = "worker-1" Worker2Name = "worker-2" )
Constants for worker names
const ( MCMDeploymentName = "machine-controller-manager" KCMDeploymentName = "kube-controller-manager" CADeploymentName = "cluster-autoscaler" DefaultImage = "registry.k8s.io/pause:3.5" )
Constants for deployments
const ( Node1Name = "node-1" Node2Name = "node-2" Node3Name = "node-3" Node4Name = "node-4" )
Constants for node names
const ( Machine1Name = "machine-1" Machine2Name = "machine-2" Machine3Name = "machine-3" Machine4Name = "machine-4" )
Constants for machine names
const DefaultNamespace = "test"
DefaultNamespace is the default namespace used in tests
Variables ¶
This section is empty.
Functions ¶
func CreateTestNamespace ¶
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
GenerateNodes generates nodes based on the given node specs.
func GetStructured ¶
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 ¶
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 ¶
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
func (b *ClusterBuilder) Build() (*gardenerv1alpha1.Cluster, *gardencorev1beta1.Shoot, error)
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 ¶
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
NodeLeaseSpec is a specification for a node lease.