testutils

package
v0.0.0-...-4913318 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertHasNoObject

func AssertHasNoObject(t assert.TestingT, c client.Client, o client.Object, msgAndArgs ...interface{}) bool
Example
package main

import (
	"fmt"

	k8stestutils "github.com/adevinta/go-k8s-toolkit/testutils"
	testutils "github.com/adevinta/go-testutils-toolkit"

	v1 "k8s.io/api/core/v1"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func main() {
	// use the real *testing.T from the test
	t := &testutils.FakeTest{Name: "TestMyObjectDoesNotExist"}

	client := fake.NewClientBuilder().Build()
	k8stestutils.AssertHasNoObject(t, client, &v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-namespace",
		},
	})

	// Usually, this is done by the go framework
	fmt.Println(t)
}
Output:

--- PASS: TestMyObjectDoesNotExist

func AssertHasObject

func AssertHasObject(t assert.TestingT, c client.Client, o client.Object, msgAndArgs ...interface{}) bool
Example
package main

import (
	"fmt"

	k8stestutils "github.com/adevinta/go-k8s-toolkit/testutils"
	testutils "github.com/adevinta/go-testutils-toolkit"

	v1 "k8s.io/api/core/v1"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func main() {
	// use the real *testing.T from the test
	t := &testutils.FakeTest{Name: "TestMyObjectExists"}

	client := fake.NewClientBuilder().Build()
	k8stestutils.AssertHasObject(t, client, &v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-namespace",
		},
	})

	// Usually, this is done by the go framework
	fmt.Println(t)
}
Output:

--- FAIL: TestMyObjectExists

func CreateOrUpdateAll

func CreateOrUpdateAll(t require.TestingT, ctx context.Context, c client.Client, objects ...client.Object)
Example
package main

import (
	"context"
	"fmt"

	k8stestutils "github.com/adevinta/go-k8s-toolkit/testutils"
	testutils "github.com/adevinta/go-testutils-toolkit"

	v1 "k8s.io/api/core/v1"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func main() {
	// use the real *testing.T from the test
	t := &testutils.FakeTest{Name: "TestICanCreateObjects"}

	client := fake.NewClientBuilder().Build()

	k8stestutils.CreateOrUpdateAll(t, context.Background(), client, &v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-namespace",
		},
	})

	k8stestutils.AssertHasObject(t, client, &v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-namespace",
		},
	})

	// Usually, this is done by the go framework
	fmt.Println(t)
}
Output:

--- PASS: TestICanCreateObjects

func FilterUnstructuredObjects

func FilterUnstructuredObjects(objects []*unstructured.Unstructured, filters ...Filter) []*unstructured.Unstructured

func RequireHasNoObject

func RequireHasNoObject(t require.TestingT, c client.Client, o client.Object, msgAndArgs ...interface{})
Example
package main

import (
	"fmt"

	k8stestutils "github.com/adevinta/go-k8s-toolkit/testutils"
	testutils "github.com/adevinta/go-testutils-toolkit"

	v1 "k8s.io/api/core/v1"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func main() {
	// use the real *testing.T from the test
	t := &testutils.FakeTest{Name: "TestMyObjectDoesNotExist"}

	client := fake.NewClientBuilder().WithObjects(&v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-namespace",
		},
	}).Build()
	k8stestutils.RequireHasNoObject(t, client, &v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-namespace",
		},
	})

	// Usually, this is done by the go framework
	fmt.Println(t)
}
Output:

--- FAIL: TestMyObjectDoesNotExist

func RequireHasObject

func RequireHasObject(t require.TestingT, c client.Client, o client.Object, msgAndArgs ...interface{})
Example
package main

import (
	"fmt"

	k8stestutils "github.com/adevinta/go-k8s-toolkit/testutils"
	testutils "github.com/adevinta/go-testutils-toolkit"

	v1 "k8s.io/api/core/v1"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func main() {
	// use the real *testing.T from the test
	t := &testutils.FakeTest{Name: "TestMyObjectExists"}

	client := fake.NewClientBuilder().WithObjects(&v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-namespace",
		},
	}).Build()
	k8stestutils.RequireHasObject(t, client, &v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			Name: "my-namespace",
		},
	})

	// Usually, this is done by the go framework
	fmt.Println(t)
}
Output:

--- PASS: TestMyObjectExists

Types

type Filter

type Filter interface {
	Include(*unstructured.Unstructured) bool
	Mutate(*unstructured.Unstructured) *unstructured.Unstructured
}

func ExcludeObject

func ExcludeObject(selector ObjectSelector) Filter

func ExtractObjectField

func ExtractObjectField[T interface{}](t require.TestingT, selector ObjectSelector, dest *T, fields ...string) Filter
Example
package main

import (
	"encoding/json"
	"os"

	k8stestutils "github.com/adevinta/go-k8s-toolkit/testutils"
	testutils "github.com/adevinta/go-testutils-toolkit"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func main() {
	// use the real *testing.T from the test
	t := &testutils.FakeTest{Name: "TestICanExtractObjectFields"}
	labels := map[string]string{}

	myObjects := []*unstructured.Unstructured{
		{
			Object: map[string]interface{}{
				"kind": "ConfigMap",
				"metadata": map[string]interface{}{
					"namespace": "my-namespace",
					"name":      "my-cm",
					"labels": map[string]string{
						"my": "label",
					},
				},
			},
		},
		{
			Object: map[string]interface{}{
				"kind": "ConfigMap",
				"metadata": map[string]interface{}{
					"namespace": "other",
					"name":      "other",
				},
			},
		},
	}

	k8stestutils.FilterUnstructuredObjects(myObjects, k8stestutils.ExtractObjectField(t, k8stestutils.And(k8stestutils.WithKind("ConfigMap"), k8stestutils.WithName("my-cm")), &labels, "metadata", "labels"))

	json.NewEncoder(os.Stdout).Encode(labels)
}
Output:

{"my":"label"}

func ExtractObjectName

func ExtractObjectName(selector ObjectSelector, dest *string) Filter
Example
package main

import (
	"fmt"

	k8stestutils "github.com/adevinta/go-k8s-toolkit/testutils"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func main() {
	name := "unknown"

	myObjects := []*unstructured.Unstructured{
		{
			Object: map[string]interface{}{
				"kind": "ConfigMap",
				"metadata": map[string]interface{}{
					"namespace": "my-namespace",
					"name":      "my-cm",
				},
			},
		},
		{
			Object: map[string]interface{}{
				"kind": "Secret",
				"metadata": map[string]interface{}{
					"namespace": "my-namespace",
					"name":      "my-secret",
				},
			},
		},
	}

	k8stestutils.FilterUnstructuredObjects(myObjects, k8stestutils.ExtractObjectName(k8stestutils.WithKind("ConfigMap"), &name))

	fmt.Println(name)
}
Output:

my-cm

func ExtractObjectNamespace

func ExtractObjectNamespace(selector ObjectSelector, dest *string) Filter
Example
package main

import (
	"fmt"

	k8stestutils "github.com/adevinta/go-k8s-toolkit/testutils"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func main() {
	namespace := "unknown"

	myObjects := []*unstructured.Unstructured{
		{
			Object: map[string]interface{}{
				"kind": "ConfigMap",
				"metadata": map[string]interface{}{
					"namespace": "my-namespace",
					"name":      "my-cm",
				},
			},
		},
		{
			Object: map[string]interface{}{
				"kind": "ConfigMap",
				"metadata": map[string]interface{}{
					"namespace": "other",
					"name":      "other",
				},
			},
		},
	}

	k8stestutils.FilterUnstructuredObjects(myObjects, k8stestutils.ExtractObjectNamespace(k8stestutils.And(k8stestutils.WithKind("ConfigMap"), k8stestutils.WithName("my-cm")), &namespace))

	fmt.Println(namespace)
}
Output:

my-namespace

type IncludeFilterFunc

type IncludeFilterFunc func(*unstructured.Unstructured) bool

func (IncludeFilterFunc) Include

func (IncludeFilterFunc) Mutate

type MutateFilterFunc

type MutateFilterFunc func(*unstructured.Unstructured) *unstructured.Unstructured

func (MutateFilterFunc) Include

func (MutateFilterFunc) Mutate

type ObjectSelector

type ObjectSelector func(object *unstructured.Unstructured) bool

func And

func And(selectors ...ObjectSelector) ObjectSelector

func WithKind

func WithKind(kind string) ObjectSelector

func WithName

func WithName(name string) ObjectSelector

func WithNamespace

func WithNamespace(namespace string) ObjectSelector

Jump to

Keyboard shortcuts

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