testutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2025 License: MIT Imports: 8 Imported by: 0

README

testutil

This package contains shared test helpers, fake clients, and fixtures for use across unit and integration tests.

  • All code here should be test-only (never imported by production code).
  • Use the _test.go suffix for all files to ensure they are excluded from production builds.
  • Place generic fakes and golden-file helpers here to avoid duplication across packages.

Kubernetes Testing

For Kubernetes-related tests, use the provided fake client helpers:

  • NewFakeClient(objs ...runtime.Object) *fake.Clientset: creates a fake Kubernetes client pre-loaded with objects.
  • NewFakeKubernetesClientAdapter(clientset *fake.Clientset) TestKubernetesClient: returns a test adapter implementing the minimal interface for use with internal/infra/k8s.K8sHelper tests.

Example usage:

import (
    "yourmodule/internal/testutil"
    "yourmodule/internal/infra/k8s"
)

pod := &corev1.Pod{ /* ... */ }
client := testutil.NewFakeClient(pod)
adapter := testutil.NewFakeKubernetesClientAdapter(client)
// Use adapter in your k8s.K8sHelper tests as a kubernetesClient

Usage

Import as testutil in your test files:

import "yourmodule/internal/testutil"

Documentation

Overview

Package testutil provides assertion helpers for testing, wrapping testify's assert and require.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertPanic

func AssertPanic(t *testing.T, fn func(), msgAndArgs ...any)

AssertPanic asserts that fn panics.

func Contains

func Contains(t *testing.T, s, contains any, msgAndArgs ...any) bool

Contains asserts that s contains contains.

func ElementsMatch

func ElementsMatch(t *testing.T, listA, listB any, msgAndArgs ...any) bool

ElementsMatch asserts that two slices contain the same elements, regardless of order.

func Equal

func Equal(t *testing.T, expected, actual any, msgAndArgs ...any) bool

Equal asserts that expected and actual are equal.

Parameters:

  • t: the testing.T instance
  • expected: the expected value
  • actual: the actual value
  • msgAndArgs: optional message and arguments

Returns:

  • bool: true if equal, false otherwise

func GreaterOrEqual

func GreaterOrEqual(t *testing.T, e1, e2 any, msgAndArgs ...any) bool

GreaterOrEqual asserts that e1 is greater than or equal to e2.

func NewFakeClient

func NewFakeClient(objs ...runtime.Object) *fake.Clientset

NewFakeClient returns a fake Kubernetes Clientset pre-loaded with the given objects.

func NotNil

func NotNil(t *testing.T, object any, msgAndArgs ...any) bool

NotNil asserts that object is not nil.

func RequireContains

func RequireContains(t *testing.T, s, contains any, msgAndArgs ...any)

RequireContains requires that s contains contains.

func RequireElementsMatch

func RequireElementsMatch(t *testing.T, listA, listB any, msgAndArgs ...any)

RequireElementsMatch requires that two slices contain the same elements, regardless of order.

func RequireEqual

func RequireEqual(t *testing.T, expected, actual any, msgAndArgs ...any)

RequireEqual requires that expected and actual are equal.

func RequireError

func RequireError(t *testing.T, err error, msgAndArgs ...any)

RequireError requires that err is not nil.

func RequireNoError

func RequireNoError(t *testing.T, err error, msgAndArgs ...any)

RequireNoError requires that err is nil.

func RequireNotNil

func RequireNotNil(t *testing.T, object any, msgAndArgs ...any)

RequireNotNil requires that object is not nil.

Types

type TestKubernetesClient

type TestKubernetesClient interface {
	CoreV1NodesList(ctx context.Context, opts v1.ListOptions) ([]corev1.Node, error)
	CoreV1PodsList(ctx context.Context, namespace string, opts v1.ListOptions) ([]corev1.Pod, error)
}

TestKubernetesClient is a minimal interface for test fakes.

func NewFakeKubernetesClientAdapter

func NewFakeKubernetesClientAdapter(clientset *fake.Clientset) TestKubernetesClient

NewFakeKubernetesClientAdapter returns a TestKubernetesClient for testing.

Jump to

Keyboard shortcuts

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