integration

package
v0.0.0-...-15d0b39 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: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TestNs = &corev1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "testnamespace",
		},
	}

	TestNs2 = &corev1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "other",
		},
	}

	TestSvc = &unstructured.Unstructured{
		Object: map[string]any{
			"apiVersion": "v1",
			"kind":       "Service",
			"metadata": map[string]any{
				"name":      "test-service",
				"namespace": "default",
			},
			"spec": map[string]any{
				"selector": map[string]any{
					"app": "example",
				},
				"ports": []any{
					map[string]any{
						"protocol":   "TCP",
						"port":       int64(80),
						"targetPort": int64(8080),
					},
				},
				"type": "ClusterIP",
			},
		},
	}

	TestEndpointSlice = &unstructured.Unstructured{
		Object: map[string]any{
			"apiVersion": "discovery.k8s.io/v1",
			"kind":       "EndpointSlice",
			"metadata": map[string]any{
				"name":      "test-endpointslice",
				"namespace": "default",
				"labels": map[string]any{
					"kubernetes.io/service-name": "test-service",
				},
			},
			"addressType": "IPv4",
			"ports": []any{
				map[string]any{
					"name":     "http",
					"port":     int64(80),
					"protocol": "TCP",
				},
			},
			"endpoints": []any{
				map[string]any{
					"addresses": []any{
						"192.0.2.1",
					},
					"conditions": map[string]any{
						"ready": true,
					},
					"hostname": "pod-1",
					"targetRef": map[string]any{
						"kind": "Pod",
						"name": "example-pod-1",
					},
				},
				map[string]any{
					"addresses": []any{
						"192.0.2.2",
					},
					"conditions": map[string]any{
						"ready": true,
					},
					"hostname": "pod-2",
					"targetRef": map[string]any{
						"kind": "Pod",
						"name": "example-pod-2",
					},
				},
			},
		},
	}

	TestConfigMap = &unstructured.Unstructured{
		Object: map[string]any{
			"apiVersion": "v1",
			"kind":       "ConfigMap",
			"metadata": map[string]any{
				"name":      "test-configmap",
				"namespace": "default",
			},
			"data": map[string]any{
				"key1": "value1",
				"key2": "value2",
			},
		},
	}

	TestDeployment = &unstructured.Unstructured{
		Object: map[string]any{
			"apiVersion": "apps/v1",
			"kind":       "Deployment",
			"metadata": map[string]any{
				"name":      "test-deployment",
				"namespace": "default",
				"annotations": map[string]any{
					"dcontroller.io/related-configmap": "test-configmap",
				},
			},
			"spec": map[string]any{
				"replicas": int64(1),
				"selector": map[string]any{
					"matchLabels": map[string]any{
						"app": "test",
					},
				},
				"template": map[string]any{
					"metadata": map[string]any{
						"labels": map[string]any{
							"app": "test",
						},
					},
					"spec": map[string]any{
						"containers": []any{
							map[string]any{
								"name":  "test-container",
								"image": "nginx:latest",
							},
						},
					},
				},
			},
		},
	}

	TestConfigMapDeployment = &unstructured.Unstructured{
		Object: map[string]any{
			"apiVersion": "dcontroller.io/v1alpha1",
			"kind":       "ConfigDeployment",
			"metadata": map[string]any{
				"name":      "dep-config",
				"namespace": "default",
			},
			"spec": map[string]any{
				"configMap":  "test-configmap",
				"deployment": "test-deployment",
			},
		},
	}

	TestPod = &unstructured.Unstructured{
		Object: map[string]any{
			"apiVersion": "v1",
			"kind":       "Pod",
			"metadata": map[string]any{
				"name":      "testpod",
				"namespace": "testnamespace",
			},
			"spec": map[string]any{
				"containers": []any{
					map[string]any{
						"name":  "nginx",
						"image": "nginx",
					},
					map[string]any{
						"name":  "pause",
						"image": "pause",
					},
				},
			},
		},
	}
)

Functions

func GetPort

func GetPort(addr string) int

func MatchRequest

func MatchRequest(req ReconcileRequest, ns, name string, eventType any, gvk schema.GroupVersionKind)

MatchRequest validates that a ReconcileRequest matches the expected values. It checks that the request contains an object snapshot and verifies namespace, name, event type, and GVK fields match the expected values.

This helper should be used in tests to verify reconciler requests without re-fetching objects from the cache, which could introduce TOCTOU races.

Example usage:

req, ok := testutils.TryWatchChan(watcher, interval)
Expect(ok).To(BeTrue())
testutils.MatchRequest(req, "default", "viewname", object.Added, gvk)

func TimestampEncoder

func TimestampEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

func TryWatchEvent

func TryWatchEvent(watcher watch.Interface, timeout time.Duration) (watch.Event, bool)

TryWatchEvent attempts to receive a watch.Event from a watch.Interface within the specified timeout. Returns the event and true if successful, or an empty event and false if timeout occurs.

Types

type ReconcileRequest

type ReconcileRequest interface {
	GetNamespace() string
	GetName() string
	GetEventType() object.DeltaType
	GetGVK() schema.GroupVersionKind
	GetObject() object.Object
}

ReconcileRequest is an interface that abstracts reconciler.Request for testing. This allows testutils to provide helpers without importing the reconciler package.

func TryWatchReq

func TryWatchReq(watcher chan ReconcileRequest, timeout time.Duration) (ReconcileRequest, bool)

TryWatchReq attempts to receive a reconciler.Request from a channel within the specified timeout. Returns the value and true if successful, or a zero value and false if timeout occurs.

Example usage:

req, ok := testutils.TryWatchReq(watcher, 100*time.Millisecond)

type Suite

type Suite struct {
	Timeout, Interval time.Duration
	LogLevel          int8
	Cfg               *rest.Config
	Scheme            *runtime.Scheme
	K8sClient         client.Client
	TestEnv           *envtest.Environment
	Ctx               context.Context
	Cancel            context.CancelFunc
	Log               logr.Logger
}

func NewSuite

func NewSuite(loglevel int8, crdPaths ...string) (*Suite, error)

func (*Suite) Close

func (s *Suite) Close()

Jump to

Keyboard shortcuts

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