framework

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package framework provides shared infrastructure for E2E tests.

Index

Constants

View Source
const DefaultPolling = 2 * time.Second

DefaultPolling is the default polling interval for Eventually assertions.

View Source
const DefaultTimeout = 120 * time.Second

DefaultTimeout is the default timeout for Eventually assertions in E2E tests.

Variables

View Source
var (
	// GroupVersion is the group version used to register the TestApp types.
	GroupVersion = schema.GroupVersion{Group: "e2e.ocf.io", Version: "v1alpha1"}

	// SchemeBuilder is used to add Go types to the GroupVersionKind scheme.
	SchemeBuilder = runtime.NewSchemeBuilder(func(scheme *runtime.Scheme) error {
		scheme.AddKnownTypes(GroupVersion,
			&TestApp{}, &TestAppList{},
			&ClusterTestApp{}, &ClusterTestAppList{},
		)
		metav1.AddToGroupVersion(scheme, GroupVersion)
		return nil
	})

	// AddToScheme adds the E2E types to the given scheme.
	AddToScheme = SchemeBuilder.AddToScheme

	// TestAppCRD is the programmatic CRD definition for the namespace-scoped TestApp.
	TestAppCRD = &apiextensionsv1.CustomResourceDefinition{
		ObjectMeta: metav1.ObjectMeta{
			Name: "testapps.e2e.ocf.io",
		},
		Spec: apiextensionsv1.CustomResourceDefinitionSpec{
			Group: "e2e.ocf.io",
			Names: apiextensionsv1.CustomResourceDefinitionNames{
				Kind:     "TestApp",
				ListKind: "TestAppList",
				Plural:   "testapps",
				Singular: "testapp",
			},
			Scope: apiextensionsv1.NamespaceScoped,
			Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
				{
					Name:    "v1alpha1",
					Served:  true,
					Storage: true,
					Schema: &apiextensionsv1.CustomResourceValidation{
						OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
							Type: "object",
							Properties: map[string]apiextensionsv1.JSONSchemaProps{
								"spec": {
									Type: "object",
									Properties: map[string]apiextensionsv1.JSONSchemaProps{
										"suspended": {Type: "boolean"},
									},
								},
								"status": {
									Type: "object",
									Properties: map[string]apiextensionsv1.JSONSchemaProps{
										"conditions": conditionSchema,
									},
								},
							},
						},
					},
					Subresources: &apiextensionsv1.CustomResourceSubresources{
						Status: &apiextensionsv1.CustomResourceSubresourceStatus{},
					},
				},
			},
		},
	}

	// ClusterTestAppCRD is the programmatic CRD definition for the cluster-scoped ClusterTestApp.
	ClusterTestAppCRD = &apiextensionsv1.CustomResourceDefinition{
		ObjectMeta: metav1.ObjectMeta{
			Name: "clustertestapps.e2e.ocf.io",
		},
		Spec: apiextensionsv1.CustomResourceDefinitionSpec{
			Group: "e2e.ocf.io",
			Names: apiextensionsv1.CustomResourceDefinitionNames{
				Kind:     "ClusterTestApp",
				ListKind: "ClusterTestAppList",
				Plural:   "clustertestapps",
				Singular: "clustertestapp",
			},
			Scope: apiextensionsv1.ClusterScoped,
			Versions: []apiextensionsv1.CustomResourceDefinitionVersion{
				{
					Name:    "v1alpha1",
					Served:  true,
					Storage: true,
					Schema: &apiextensionsv1.CustomResourceValidation{
						OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{
							Type: "object",
							Properties: map[string]apiextensionsv1.JSONSchemaProps{
								"spec": {
									Type: "object",
									Properties: map[string]apiextensionsv1.JSONSchemaProps{
										"suspended": {Type: "boolean"},
									},
								},
								"status": {
									Type: "object",
									Properties: map[string]apiextensionsv1.JSONSchemaProps{
										"conditions": conditionSchema,
									},
								},
							},
						},
					},
					Subresources: &apiextensionsv1.CustomResourceSubresources{
						Status: &apiextensionsv1.CustomResourceSubresourceStatus{},
					},
				},
			},
		},
	}
)

Functions

func CreateTestNamespace

func CreateTestNamespace(ctx context.Context, c client.Client, prefix string) string

CreateTestNamespace creates a namespace with GenerateName and returns its name.

func DeleteClusterTestApp

func DeleteClusterTestApp(ctx context.Context, c client.Client, name string)

DeleteClusterTestApp deletes the named ClusterTestApp from the cluster. It is intended for AfterEach cleanup since cluster-scoped resources are not garbage-collected by namespace deletion.

func GetClusterCondition

func GetClusterCondition(
	ctx context.Context, c client.Client,
	name string, conditionType string,
) func(gomega.Gomega) *metav1.Condition

GetClusterCondition returns a closure that fetches a fresh ClusterTestApp from the cluster and returns the named condition. Suitable for use with gomega.Eventually.

func GetCondition

func GetCondition(
	ctx context.Context, c client.Client,
	key types.NamespacedName, conditionType string,
) func(gomega.Gomega) *metav1.Condition

GetCondition returns a closure that fetches a fresh TestApp from the cluster and returns the named condition. Suitable for use with gomega.Eventually.

Usage:

Eventually(framework.GetCondition(ctx, c, key, "E2EReady")).
    Should(framework.HaveConditionStatus(metav1.ConditionTrue, "Healthy"))

func HaveConditionStatus

func HaveConditionStatus(status metav1.ConditionStatus, reason string) gomegatypes.GomegaMatcher

HaveConditionStatus returns a GomegaMatcher that checks a *metav1.Condition has the expected Status and Reason fields.

func InstallCRDs

func InstallCRDs(cfg *rest.Config) error

InstallCRDs applies both the TestApp and ClusterTestApp CRD definitions and waits until they are established.

func UpdateClusterTestApp added in v0.6.0

func UpdateClusterTestApp(ctx context.Context, c client.Client, name string, mutate func(*ClusterTestApp))

UpdateClusterTestApp performs a read-modify-write on the named ClusterTestApp, retrying automatically on conflict. The mutate function receives the latest version of the object and should apply the desired spec or annotation changes.

Types

type ClusterComponentFactory

type ClusterComponentFactory func(owner *ClusterTestApp) (*component.Component, error)

ClusterComponentFactory builds a full *component.Component from the owner ClusterTestApp. Use this for multi-resource component tests where you need full control over the component configuration.

type ClusterE2EReconciler

type ClusterE2EReconciler struct {
	client.Client
	Scheme   *runtime.Scheme
	Recorder record.EventRecorder
	Metrics  component.Recorder
	// contains filtered or unexported fields
}

ClusterE2EReconciler is a controller-runtime reconciler that delegates component construction to per-test factories registered by name key. It reconciles cluster-scoped ClusterTestApp resources for testing cluster-scoped primitives.

func NewClusterE2EReconciler

func NewClusterE2EReconciler(
	c client.Client,
	scheme *runtime.Scheme,
	recorder record.EventRecorder,
	metrics component.Recorder,
) *ClusterE2EReconciler

NewClusterE2EReconciler creates a new ClusterE2EReconciler.

func (*ClusterE2EReconciler) Reconcile

Reconcile implements reconcile.Reconciler. It fetches the ClusterTestApp, looks up the registered factory, builds the component, and reconciles it.

func (*ClusterE2EReconciler) RegisterComponent

func (r *ClusterE2EReconciler) RegisterComponent(name string, factory ClusterComponentFactory)

RegisterComponent registers a ClusterComponentFactory for a specific ClusterTestApp identified by its name. The factory is called on each reconciliation to build the full Component directly.

func (*ClusterE2EReconciler) RegisterResource

func (r *ClusterE2EReconciler) RegisterResource(name string, factory ClusterResourceFactory)

RegisterResource registers a ClusterResourceFactory for a specific ClusterTestApp identified by its name. The factory is called on each reconciliation to build the resource, which is wrapped in a single-resource Component.

func (*ClusterE2EReconciler) Unregister

func (r *ClusterE2EReconciler) Unregister(name string)

Unregister removes any registered factory for the given name.

type ClusterResourceFactory

type ClusterResourceFactory func(owner *ClusterTestApp) (component.Resource, error)

ClusterResourceFactory builds a single component.Resource from the owner ClusterTestApp. The reconciler wraps the returned resource in a single-resource Component with condition type "E2EReady".

type ClusterTestApp

type ClusterTestApp struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   TestAppSpec   `json:"spec,omitempty"`
	Status TestAppStatus `json:"status,omitempty"`
}

ClusterTestApp is the cluster-scoped CRD used as the owner object in E2E tests for cluster-scoped primitives (ClusterRole, ClusterRoleBinding, PersistentVolume).

func NewClusterTestApp

func NewClusterTestApp(ctx context.Context, c client.Client, name string) *ClusterTestApp

NewClusterTestApp creates a cluster-scoped ClusterTestApp CR and returns it.

func (*ClusterTestApp) DeepCopy

func (t *ClusterTestApp) DeepCopy() *ClusterTestApp

DeepCopy returns a deep copy of the ClusterTestApp.

func (*ClusterTestApp) DeepCopyInto

func (t *ClusterTestApp) DeepCopyInto(out *ClusterTestApp)

DeepCopyInto copies all properties into another ClusterTestApp.

func (*ClusterTestApp) DeepCopyObject

func (t *ClusterTestApp) DeepCopyObject() runtime.Object

DeepCopyObject implements runtime.Object.

func (*ClusterTestApp) GetKind

func (t *ClusterTestApp) GetKind() string

GetKind returns the kind of the object.

func (*ClusterTestApp) GetStatusConditions

func (t *ClusterTestApp) GetStatusConditions() *[]metav1.Condition

GetStatusConditions returns a pointer to the slice of status conditions.

type ClusterTestAppList

type ClusterTestAppList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`

	Items []ClusterTestApp `json:"items"`
}

ClusterTestAppList contains a list of ClusterTestApp.

func (*ClusterTestAppList) DeepCopy

func (t *ClusterTestAppList) DeepCopy() *ClusterTestAppList

DeepCopy returns a deep copy of the ClusterTestAppList.

func (*ClusterTestAppList) DeepCopyInto

func (t *ClusterTestAppList) DeepCopyInto(out *ClusterTestAppList)

DeepCopyInto copies all properties into another ClusterTestAppList.

func (*ClusterTestAppList) DeepCopyObject

func (t *ClusterTestAppList) DeepCopyObject() runtime.Object

DeepCopyObject implements runtime.Object.

type ComponentFactory

type ComponentFactory func(owner *TestApp) (*component.Component, error)

ComponentFactory builds a full *component.Component from the owner TestApp. Use this for multi-resource component tests where you need full control over the component configuration.

type E2EReconciler

type E2EReconciler struct {
	client.Client
	Scheme   *runtime.Scheme
	Recorder record.EventRecorder
	Metrics  component.Recorder
	// contains filtered or unexported fields
}

E2EReconciler is a controller-runtime reconciler that delegates component construction to per-test factories registered by namespace/name key.

func NewE2EReconciler

func NewE2EReconciler(
	c client.Client,
	scheme *runtime.Scheme,
	recorder record.EventRecorder,
	metrics component.Recorder,
) *E2EReconciler

NewE2EReconciler creates a new E2EReconciler.

func (*E2EReconciler) Reconcile

func (r *E2EReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error)

Reconcile implements reconcile.Reconciler. It fetches the TestApp, looks up the registered factory, builds the component, and reconciles it.

func (*E2EReconciler) RegisterComponent

func (r *E2EReconciler) RegisterComponent(key types.NamespacedName, factory ComponentFactory)

RegisterComponent registers a ComponentFactory for a specific TestApp identified by its namespaced name. The factory is called on each reconciliation to build the full Component directly.

func (*E2EReconciler) RegisterResource

func (r *E2EReconciler) RegisterResource(key types.NamespacedName, factory ResourceFactory)

RegisterResource registers a ResourceFactory for a specific TestApp identified by its namespaced name. The factory is called on each reconciliation to build the resource, which is wrapped in a single-resource Component.

func (*E2EReconciler) Unregister

func (r *E2EReconciler) Unregister(key types.NamespacedName)

Unregister removes any registered factory for the given namespaced name.

type ResourceFactory

type ResourceFactory func(owner *TestApp) (component.Resource, error)

ResourceFactory builds a single component.Resource from the owner TestApp. The reconciler wraps the returned resource in a single-resource Component with condition type "E2EReady".

type TestApp

type TestApp struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   TestAppSpec   `json:"spec,omitempty"`
	Status TestAppStatus `json:"status,omitempty"`
}

TestApp is the namespace-scoped CRD used as the owner object in E2E tests.

func NewTestApp

func NewTestApp(ctx context.Context, c client.Client, namespace, name string) *TestApp

NewTestApp creates a TestApp CR in the given namespace and returns it.

func (*TestApp) DeepCopy

func (t *TestApp) DeepCopy() *TestApp

DeepCopy returns a deep copy of the TestApp.

func (*TestApp) DeepCopyInto

func (t *TestApp) DeepCopyInto(out *TestApp)

DeepCopyInto copies all properties into another TestApp.

func (*TestApp) DeepCopyObject

func (t *TestApp) DeepCopyObject() runtime.Object

DeepCopyObject implements runtime.Object.

func (*TestApp) GetKind

func (t *TestApp) GetKind() string

GetKind returns the kind of the object.

func (*TestApp) GetStatusConditions

func (t *TestApp) GetStatusConditions() *[]metav1.Condition

GetStatusConditions returns a pointer to the slice of status conditions.

type TestAppList

type TestAppList struct {
	metav1.TypeMeta `json:",inline"`
	metav1.ListMeta `json:"metadata,omitempty"`

	Items []TestApp `json:"items"`
}

TestAppList contains a list of TestApp.

func (*TestAppList) DeepCopy

func (t *TestAppList) DeepCopy() *TestAppList

DeepCopy returns a deep copy of the TestAppList.

func (*TestAppList) DeepCopyInto

func (t *TestAppList) DeepCopyInto(out *TestAppList)

DeepCopyInto copies all properties into another TestAppList.

func (*TestAppList) DeepCopyObject

func (t *TestAppList) DeepCopyObject() runtime.Object

DeepCopyObject implements runtime.Object.

type TestAppSpec

type TestAppSpec struct {
	// Suspended determines whether the application is suspended.
	Suspended bool `json:"suspended,omitempty"`
}

TestAppSpec defines the desired state of TestApp.

type TestAppStatus

type TestAppStatus struct {
	// Conditions store the status of the application's components.
	Conditions []metav1.Condition `json:"conditions,omitempty"`
}

TestAppStatus defines the observed state of TestApp.

Jump to

Keyboard shortcuts

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