helpers

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: Apache-2.0 Imports: 21 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEnvVarValue

func GetEnvVarValue(envs []corev1.EnvVar, name string, defaultValue string) string

GetEnvVarValue returns the value of the EnvVar based on the name of the Var or return the defaultValue if the list does not have EnvVar with the given name

Types

type ConditionGetterFunc

type ConditionGetterFunc func(name types.NamespacedName) condition.Conditions

ConditionGetterFunc recieves custom condition getters for operators specific needs

func (ConditionGetterFunc) GetConditions

GetConditions implements conditions getter for operators specific needs

type TestHelper

type TestHelper struct {
	K8sClient client.Client
	Ctx       context.Context
	Timeout   time.Duration
	Interval  time.Duration
	Logger    logr.Logger
}

TestHelper is a collection EnvTest helpers writing test to code that interacts with the k8s resource. If you need to handle openstack-k8s-operators specific resource then you should use the extended TestHelper from modules/test-operator

func NewTestHelper

func NewTestHelper(
	ctx context.Context,
	k8sClient client.Client,
	timeout time.Duration,
	interval time.Duration,
	logger logr.Logger,
) *TestHelper

NewTestHelper returns a TestHelper

func (*TestHelper) AssertConfigMapDoesNotExist

func (tc *TestHelper) AssertConfigMapDoesNotExist(name types.NamespacedName)

AssertConfigMapDoesNotExist ensures the ConfigMap resource does not exist in a k8s cluster.

func (*TestHelper) AssertDeploymentDoesNotExist

func (tc *TestHelper) AssertDeploymentDoesNotExist(name types.NamespacedName)

AssertDeploymentDoesNotExist ensures the Deployment resource does not exist in a k8s cluster.

func (*TestHelper) AssertJobDoesNotExist

func (tc *TestHelper) AssertJobDoesNotExist(name types.NamespacedName)

AssertJobDoesNotExist ensures the Job resource does not exist in a k8s cluster.

func (*TestHelper) AssertRoleBindingDoesNotExist

func (tc *TestHelper) AssertRoleBindingDoesNotExist(name types.NamespacedName)

AssertRoleBindingDoesNotExist ensures the RoleBinding resource does not exist in a k8s cluster.

func (*TestHelper) AssertRouteDoesNotExist

func (tc *TestHelper) AssertRouteDoesNotExist(name types.NamespacedName)

AssertRouteDoesNotExist ensures the Route resource does not exist in a k8s cluster.

func (*TestHelper) AssertRouteExists

func (tc *TestHelper) AssertRouteExists(name types.NamespacedName) *routev1.Route

AssertRouteExists fetches a Route resource and asserts that the operation is successful.

Example usage:

th.AssertRouteExists(types.NamespacedName{Namespace: novaNames.APIName.Namespace, Name: "nova-public"})

func (*TestHelper) AssertRouteNotExists

func (tc *TestHelper) AssertRouteNotExists(name types.NamespacedName) *routev1.Route

AssertRouteNotExists fetch a Route resource and asserts that the resource does not exist.

Example usage:

th.AssertRouteNotExists(types.NamespacedName{Name: "test-route", Namespace: "test-namespace"})

func (*TestHelper) AssertSecretDoesNotExist

func (tc *TestHelper) AssertSecretDoesNotExist(name types.NamespacedName)

AssertSecretDoesNotExist ensures the Secret resource does not exist in a k8s cluster.

func (*TestHelper) AssertServiceDoesNotExist

func (tc *TestHelper) AssertServiceDoesNotExist(name types.NamespacedName)

AssertServiceDoesNotExist ensures the Service resource does not exist in a k8s cluster.

func (*TestHelper) AssertServiceExists

func (tc *TestHelper) AssertServiceExists(name types.NamespacedName) *corev1.Service

AssertServiceExists - asserts the existence of a Service resource in the Kubernetes cluster.

Example usage:

th.AssertServiceExists(types.NamespacedName{Name: "neutron-public, Namespace: namespace})

func (*TestHelper) AssertStatefulSetDoesNotExist

func (tc *TestHelper) AssertStatefulSetDoesNotExist(name types.NamespacedName)

AssertStatefulSetDoesNotExist ensures the StatefulSet resource does not exist in a k8s cluster.

func (*TestHelper) CreateConfigMap

func (tc *TestHelper) CreateConfigMap(name types.NamespacedName, data map[string]interface{}) client.Object

CreateConfigMap creates a new ConfigMap resource with the provided data.

Example usage:

data := map[string]interface{}{"key": "value"}
cm := th.CreateConfigMap(types.NamespacedName{Namespace: "default", Name: "example-configmap"}, data)

func (*TestHelper) CreateEmptySecret

func (tc *TestHelper) CreateEmptySecret(name types.NamespacedName) *corev1.Secret

CreateEmptySecret creates a new empty Secret resource .

Example usage:

secret := th.CreateSecret(types.NamespacedName{Name: "test-secret", Namespace: "test-namespace"})

func (*TestHelper) CreateNamespace

func (tc *TestHelper) CreateNamespace(name string) *corev1.Namespace

CreateNamespace creates a Kubernetes Namespace resource.

Example usage:

th.CreateNamespace("test-namespace")

Note: the namespace should be unique and not be already present in the cluster, otherwise, the function will fail.

func (*TestHelper) CreateNetworkAttachmentDefinition

func (tc *TestHelper) CreateNetworkAttachmentDefinition(name types.NamespacedName) client.Object

CreateNetworkAttachmentDefinition creates a new NetworkAttachmentDefinition resource.

Example usage:

internalAPINADName := types.NamespacedName{Namespace: "testname", Name: "internalapi"}
nad := th.CreateNetworkAttachmentDefinition(internalAPINADName)

func (*TestHelper) CreateSecret

func (tc *TestHelper) CreateSecret(name types.NamespacedName, data map[string][]byte) *corev1.Secret

CreateSecret creates a new Secret resource with provided data.

Example usage:

secret := th.CreateSecret(types.NamespacedName{Name: "test-secret", Namespace: "test-namespace"}, map[string][]byte{"key": []byte("value")})

func (*TestHelper) CreateUnstructured

func (tc *TestHelper) CreateUnstructured(rawObj map[string]interface{}) *unstructured.Unstructured

CreateUnstructured creates an unstructured Kubernetes object from a map of key-value pairs.

Example usage:

  rawObj := map[string]interface{}{
    "apiVersion": "nova.openstack.org/v1beta1",
		"kind":       "NovaAPI",
		"metadata": map[string]interface{}{
			"name":      name.Name,
			"namespace": name.Namespace,
		},
		"spec": spec,
    },
    ...
  }
  unstructuredObj := tc.CreateUnstructured(rawObj)

func (*TestHelper) DeleteConfigMap

func (tc *TestHelper) DeleteConfigMap(name types.NamespacedName)

DeleteConfigMap deletes a ConfigMap resource from a Kubernetes cluster.

Example usage:

th.DeleteConfigMap(types.NamespacedName{Namespace: "default", Name: "example-configmap"})

or

DeferCleanup(th.DeleteConfigMap, inventoryName)

func (*TestHelper) DeleteInstance

func (tc *TestHelper) DeleteInstance(instance client.Object, opts ...client.DeleteOption)

DeleteInstance deletes a specified resource and waits until it's fully removed from the cluster.

Example usage:

DeferCleanup(th.DeleteInstance, metadata_instance)

func (*TestHelper) DeleteNamespace

func (tc *TestHelper) DeleteNamespace(name string)

DeleteNamespace deletes a Kubernetes Namespace resource.

Example usage:

th.DeleteNamespace("test-namespace")

or

DeferCleanup(th.DeleteNamespace, namespace)

Note: the namespace should exist in the cluster, otherwise, the function will fail.

func (*TestHelper) DeleteSecret

func (tc *TestHelper) DeleteSecret(name types.NamespacedName)

DeleteSecret deletes a Secret resource

Example usage:

CreateNovaExternalComputeSSHSecret(sshSecretName)
DeferCleanup(th.DeleteSecret, sshSecretName)

func (*TestHelper) DeleteService

func (tc *TestHelper) DeleteService(name types.NamespacedName)

DeleteService - deletes a Service resource from the Kubernetes cluster.

Example usage:

th.DeleteService(types.NamespacedName{Name: "test-service", Namespace: "test-namespace"})

func (*TestHelper) ExpectCondition

func (tc *TestHelper) ExpectCondition(
	name types.NamespacedName,
	getter conditionsGetter,
	conditionType condition.Type,
	expectedStatus corev1.ConditionStatus,
)

ExpectCondition - used to assert that a specific condition on a k8s resource matches an expected status.

Example usage:

th.ExpectCondition(
	novaNames.NovaName,
	ConditionGetterFunc(NovaConditionGetter),
	condition.ReadyCondition,
	corev1.ConditionFalse,
)

func (*TestHelper) ExpectConditionWithDetails

func (tc *TestHelper) ExpectConditionWithDetails(
	name types.NamespacedName,
	getter conditionsGetter,
	conditionType condition.Type,
	expectedStatus corev1.ConditionStatus,
	expectedReason condition.Reason,
	expecteMessage string,
)

ExpectConditionWithDetails used to assert that a specific condition on a k8s resource matches an expected status, reason, and message.

Example usage:

th.ExpectConditionWithDetails(
	novaNames.NovaName,
	ConditionGetterFunc(NovaConditionGetter),
	novav1.NovaAllCellsReadyCondition,
	corev1.ConditionFalse,
	condition.ErrorReason,
	"NovaCell creation failed for cell0(missing cell0 specification from Spec.CellTemplates)",
 )

func (*TestHelper) GetConfigMap

func (tc *TestHelper) GetConfigMap(name types.NamespacedName) *corev1.ConfigMap

GetConfigMap retrieves a ConfigMap resource from a k8s cluster.

Example usage: cm := th.GetConfigMap(types.NamespacedName{Namespace: "default", Name: "example-configmap"})

func (*TestHelper) GetDeployment

func (tc *TestHelper) GetDeployment(name types.NamespacedName) *appsv1.Deployment

GetDeployment - retrieves a Deployment resource from cluster. The function uses the Gomega library's Eventually function to repeatedly attempt to get the Deployment until it is successful or the test's timeout is reached.

The function returns a pointer to the retrieved Deployment. If the function cannot find the Deployment within the timeout, it will cause the test to fail.

Example usage:

  deployment := th.GetDeployment(
				types.NamespacedName{
					Namespace: neutronAPIName.Namespace,
					Name:      "neutron",
				},
			)

func (*TestHelper) GetJob

func (tc *TestHelper) GetJob(name types.NamespacedName) *batchv1.Job

GetJob retrieves a specified Job resource from the cluster.

Example usage:

job := th.GetJob(types.NamespacedName{Name: "cell-name", Namespace: "default"})

func (*TestHelper) GetName

func (tc *TestHelper) GetName(obj client.Object) types.NamespacedName

GetName function is used only in lib-common

func (*TestHelper) GetRole

func (tc *TestHelper) GetRole(name types.NamespacedName) *rbacv1.Role

GetRole fetches a Role resource.

Example usage:

th.GetRole(types.NamespacedName{Name: "test-role", Namespace: "test-namespace"})

func (*TestHelper) GetRoleBinding

func (tc *TestHelper) GetRoleBinding(name types.NamespacedName) *rbacv1.RoleBinding

GetRoleBinding - fetches a RoleBinding resource

Example usage:

th.GetRoleBinding(types.NamespacedName{Name: "test-rolebinding", Namespace: "test-namespace"})

func (*TestHelper) GetSecret

func (tc *TestHelper) GetSecret(name types.NamespacedName) corev1.Secret

GetSecret fetches a Secret resource

Example usage:

secret := th.GetSecret(types.NamespacedName{Name: "test-secret", Namespace: "test-namespace"})

func (*TestHelper) GetService

func (tc *TestHelper) GetService(name types.NamespacedName) *corev1.Service

GetService retrieves a Service resource.

Example usage:

th.GetService(types.NamespacedName{Name: "test-service", Namespace: "test-namespace"})

func (*TestHelper) GetServiceAccount

func (tc *TestHelper) GetServiceAccount(name types.NamespacedName) *corev1.ServiceAccount

GetServiceAccount fetches a ServiceAccount resource

Example usage:

th.GetServiceAccount(types.NamespacedName{Name: "test-service-account", Namespace: "test-namespace"})

func (*TestHelper) GetStatefulSet

func (tc *TestHelper) GetStatefulSet(name types.NamespacedName) *appsv1.StatefulSet

GetStatefulSet - retrieves a StatefulSet resource.

example usage:

th.GetStatefulSet(types.NamespacedName{Name: "test-statefulset", Namespace: "test-namespace"})

func (*TestHelper) ListConfigMaps

func (tc *TestHelper) ListConfigMaps(namespace string) *corev1.ConfigMapList

ListConfigMaps retrieves a list of ConfigMap resources from a specific namespace

Example usage:

cms := th.ListConfigMaps(novaNames.MetadataName.Name)

func (*TestHelper) ListJobs

func (tc *TestHelper) ListJobs(namespace string) *batchv1.JobList

ListJobs retrieves a list of all job resources within a specified namespace.

Example usage:

jobs := th.ListJobs("some-name").Items

func (*TestHelper) SimulateDeploymentReadyWithPods

func (tc *TestHelper) SimulateDeploymentReadyWithPods(name types.NamespacedName, networkIPs map[string][]string)

SimulateDeploymentReadyWithPods simulates a Deployment with ready replicas by creating and updating the corresponding Pods.

Example:

    th.SimulateDeploymentReadyWithPods(
				manilaTest.Instance,
				map[string][]string{manilaName.Namespace + "/internalapi": {"10.0.0.1"}},
			)

func (*TestHelper) SimulateDeploymentReplicaReady

func (tc *TestHelper) SimulateDeploymentReplicaReady(name types.NamespacedName)

SimulateDeploymentReplicaReady function retrieves the Deployment resource and simulate that replicas are ready Example usage:

th.SimulateDeploymentReplicaReady(ironicNames.INAName)

func (*TestHelper) SimulateJobFailure

func (tc *TestHelper) SimulateJobFailure(name types.NamespacedName)

SimulateJobFailure function retrieves the Job and simulates the failure of a Kubernetes Job resource.

Example usage:

th.SimulateJobFailure(types.NamespacedName{Name: "test-job", Namespace: "default"})

func (*TestHelper) SimulateJobSuccess

func (tc *TestHelper) SimulateJobSuccess(name types.NamespacedName)

SimulateJobSuccess retrieves the Job and simulates the success of a Kubernetes Job resource.

Note: In a real environment where the mariadb-operator is deployed, this function may not be necessary as the Job could automatically run successfully if the database user is manually registered in the DB service.

Example usage:

th.SimulateJobSuccess(types.NamespacedName{Name: "test-job", Namespace: "default"})

func (*TestHelper) SimulateStatefulSetReplicaReady

func (tc *TestHelper) SimulateStatefulSetReplicaReady(name types.NamespacedName)

SimulateStatefulSetReplicaReady retrieves the StatefulSet and simulates a ready state for a StatefulSet's replica in a Kubernetes cluster.

example usage:

th.SimulateStatefulSetReplicaReady(types.NamespacedName{Name: "test-statefulset", Namespace: "test-namespace"})

func (*TestHelper) SimulateStatefulSetReplicaReadyWithPods

func (tc *TestHelper) SimulateStatefulSetReplicaReadyWithPods(name types.NamespacedName, networkIPs map[string][]string)

SimulateStatefulSetReplicaReadyWithPods simulates a StatefulSet with ready replicas by creating and updating the corresponding Pods.

example usage:

	th.SimulateStatefulSetReplicaReadyWithPods(
 	cell0.ConductorStatefulSetName,
 	map[string][]string{cell0.CellName.Namespace + "/internalapi": {"10.0.0.1"}},
 )

func (*TestHelper) UpdateSecret added in v0.2.0

func (tc *TestHelper) UpdateSecret(secretName types.NamespacedName, key string, newValue []byte)

UpdateSecret adds a new key or updates an existing key in the given Secret with a new value

Jump to

Keyboard shortcuts

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