testutils

package
v3.15.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConstantRetry = wait.Backoff{
	Steps:    3000,
	Duration: 10 * time.Millisecond,
}

ConstantRetry makes 3,000 attempts at a rate of 100 per second. Since this is a test instance and not a "real" cluster, this is fine and there's no need to increase the wait time each iteration.

Functions

func CreateGatekeeperNamespace

func CreateGatekeeperNamespace(cfg *rest.Config) error

CreateGatekeeperNamespace bootstraps the gatekeeper-system namespace for use in tests.

func CreateThenCleanup

func CreateThenCleanup(ctx context.Context, t *testing.T, c client.Client, obj client.Object)

CreateThenCleanup creates obj in Client, and then registers obj to be deleted at the end of the test. The passed obj is safely deepcopied before being passed to client.Create, so it is not mutated by this call.

func DeleteObject

func DeleteObject(t *testing.T, c client.Client, original client.Object) func()

DeleteObject deletes obj from c. Succeeds on success or if obj already did not exist in c. Fails the test if calling Delete() returns an error other than NotFound.

Does not ensure the object is actually deleted, just sends a delete request via the passed Client. See #1596 for why this is non-trivial to guarantee.

Tests should not rely on objects being deleted from the API Server which were used in other tests; they should instead use unique object names and namespaces to eliminate cross-talk. More explicitly: if you are writing a test which must make use of this cleanup function, you are risking cross-talk between tests and should instead modify your test.

func DeleteObjectAndConfirm

func DeleteObjectAndConfirm(ctx context.Context, t *testing.T, c client.Client, obj client.Object) func()

DeleteObjectAndConfirm returns a callback which deletes obj from the passed Client. Does result in mutations to obj. The callback includes a cached copy of all information required to delete obj in the callback, so it is safe to mutate obj afterwards. Similarly - client.Delete mutates its input, but the callback does not call client.Delete on obj. Instead, it creates a single-purpose Unstructured for this purpose. Thus, obj is not mutated after the callback is run.

func NewLogger

func NewLogger(t *testing.T) logr.Logger

NewLogger creates a logger specifically for t which logs directly to the test. Use test-specific loggers so that when tests fail, only the log messages from the offending test are printed rather than log messages for every test in the package.

func Setenv

func Setenv(t *testing.T, key, value string)

Setenv sets os environment variable key to value. Registers the environment variable to be set to its original value at the end of the test.

This prevents cross-talk between tests, as some may implicitly come to rely on other tests running before them in order to succeed.

func SetupDataClient added in v3.15.0

func SetupDataClient(t *testing.T) *constraintclient.Client

func SetupManager

func SetupManager(t *testing.T, cfg *rest.Config) (manager.Manager, *watch.Manager)

SetupManager sets up a controller-runtime manager with registered watch manager.

func SetupTestReconcile added in v3.15.0

func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, *sync.Map)

SetupTestReconcile returns a reconcile.Reconcile implementation that delegates to inner and writes the request to requests after Reconcile is finished.

func StartControlPlane

func StartControlPlane(m *testing.M, cfg **rest.Config, testerDepth int)

func StartManager

func StartManager(ctx context.Context, t *testing.T, mgr manager.Manager)

StartManager starts mgr. Registers a cleanup function to stop the manager at the completion of the test.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

func NewTestWriter

func NewTestWriter(t *testing.T) *Writer

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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