k8sctx

package
v0.0.0-...-fb730ff Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0, MIT Imports: 11 Imported by: 0

Documentation

Overview

Package k8sctx is used to manage the lifecycle of a Kubernetes test or benchmark running in one or more Kubernetes clusters. It is used to control the behavior of Kubernetes-based tests and benchmarks at runtime and to abstract away how the Kubernetes test clusters are created and managed from the test code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetContextConstructor

func SetContextConstructor(fn func(context.Context) (KubernetesContext, error))

SetContextConstructor sets the global Kubernetes context constructor.

func TestMain

func TestMain(m *testing.M, testFuncs map[string]TestFunc)

TestMain is a helper to write the TestMain function of tests.

Types

type KubernetesContext

type KubernetesContext interface {
	// TestMain should be called inside tests' `TestMain` function, after having
	// registered all tests with `RegisterTest`.
	TestMain(m *testing.M)

	// RegisterTest registers a test.
	// It should be called for every `Test*(*testing.T)` function in the test.
	// Note that the `k8sctx.TestMain` helper function below will call this for
	// you given a map of tests.
	RegisterTest(name string, fn TestFunc)

	// AcquireCluster returns a single cluster for the test or benchmark to use.
	// The cluster is guaranteed to not be in use by other tests or benchmarks
	// until the `ReleaseCluster` method is called.
	// This method should block if there are no available clusters.
	AcquireCluster(ctx context.Context, t *testing.T) *testcluster.TestCluster

	// ReleaseCluster unlocks the given cluster for use by other tests or
	// benchmarks.
	ReleaseCluster(ctx context.Context, t *testing.T, cluster *testcluster.TestCluster)

	// ForEachCluster reserves as many test clusters as are available, calls
	// `fn` on each of them, and releases each of them when `fn` finishes.
	ForEachCluster(ctx context.Context, t *testing.T, fn func(cluster *testcluster.TestCluster))

	// ResolveImage resolves a container image name (possibly with a label)
	// to a fully-qualified image name. It can also return an `image:label`
	// string if the Kubernetes cluster the test runs in will resolve it on
	// its own.
	ResolveImage(ctx context.Context, imageName string) (string, error)
}

KubernetesContext represents the Kubernetes execution context. It is used to keep track of available Kubernetes clusters to test on. Tests are expected to call `RegisterTest` for every of their test function, then `TestMain`.

func Context

func Context(ctx context.Context) (KubernetesContext, error)

Context gets the global Kubernetes context. It must be called after SetContext has already been called.

type TestFunc

type TestFunc func(t *testing.T)

TestFunc is a test function that is expected to call `Context` and run a test or benchmark within a Kubernetes context.

Jump to

Keyboard shortcuts

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