testing

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateNameReactor

func GenerateNameReactor(action ktesting.Action) (bool, runtime.Object, error)

GenerateNameReactor sets the metav1.Name of an object, if metav1.GenerateName was used. It returns "handled" == false, so the test client can continue to the next ReactionFunc.

Example
package main

import (
	"context"
	"fmt"

	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes/fake"
)

var ctx = context.Background()

func main() {
	mockStringRandFunc() // only for testing
	clientset := fake.NewSimpleClientset()
	clientset.PrependReactor("create", "pods", GenerateNameReactor)

	pod := &v1.Pod{
		ObjectMeta: metav1.ObjectMeta{
			Namespace:    "default",
			GenerateName: "testpod-",
		},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:  "nginx",
					Image: "nginx",
				},
			},
		},
	}

	createdPod, _ := clientset.CoreV1().Pods(pod.Namespace).Create(ctx, pod, metav1.CreateOptions{})

	fmt.Printf("Name: %s", createdPod.Name) //remember: the pods Name field was not set, only GenerateName

	unmockStringRandFunc()
}

var stringRandOrigFunc = stringRandFunc
var stringRandMockFunc = func(n int) string {
	return "hm7shz2o"
}

func mockStringRandFunc() {
	stringRandFunc = stringRandMockFunc
}

func unmockStringRandFunc() {
	stringRandMockFunc = stringRandOrigFunc
}
Output:

Name: testpod-hm7shz2o

func SecretDataReactor

func SecretDataReactor(action ktesting.Action) (bool, runtime.Object, error)

SecretDataReactor sets the secret.Data field based on the values from secret.StringData

Example
clientset := fake.NewSimpleClientset()
clientset.PrependReactor("create", "secrets", SecretDataReactor)

secret := &v1.Secret{
	ObjectMeta: metav1.ObjectMeta{
		Namespace: "default",
		Name:      "my-secret",
	},
	StringData: map[string]string{
		"my-key": "my-value",
	},
	Type: v1.SecretTypeOpaque,
}

createdSecret, _ := clientset.CoreV1().Secrets(secret.Namespace).Create(ctx, secret, metav1.CreateOptions{})

fmt.Printf("secret.Data[\"my-key\"]: %s", string(createdSecret.Data["my-key"])) //remember: the secrets StringData field was set, not the Data field
Output:

secret.Data["my-key"]: my-value

Types

This section is empty.

Jump to

Keyboard shortcuts

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