testutil

package module
v0.0.0-...-56c9a40 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Package testutil provides utilities for writing functional tests

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestCase

type TestCase struct {
	T testing.TB

	// Name of the test cases
	Name string

	// Environment is a envtest.Environment for integration tests
	EnvironmentFn func(*TestCase) *envtest.Environment

	// Environment is populated by EnvironmentFn
	Environment *envtest.Environment

	// MaskExpectedMetadata will drop all metadata fields except
	// name, namespace, labels, and annotations when doing comparison.
	MaskExpectedMetadata bool

	// Inputs are the input files
	Inputs map[string]string

	// Expected is the contents of the expected output
	Expected string

	// Error is the contents of the expected error if one is expected
	Error string

	// ErrorFilepath is where the error was read from
	ErrorFilepath string

	// ExpectedFilepath is where the expected results were read from
	ExpectedFilepath string

	// ExpectedValues are the expected values read from files configured
	// through ExpectedFiles in the TestCaseParser
	ExpectedValues map[string]string

	// Actual is the actual results observed
	Actual string

	// ActualValues are the actual values to compare to the ExpectedValues read from
	// the ExpectedFiles configured in the TestCaseParser
	ActualValues map[string]string

	// ClientInputsSuffix is a suffix to append to files which
	// should be parsed as inputs for a fake client.Client.
	ClientInputsSuffix string

	// ClearMeta if set to true will clear metadata that shouldn't be set
	// at creation time
	ClearMeta bool

	// ExpectedObjects are the expected objects read from expected_objects.yaml if present
	ExpectedObjects string

	// ExpectedEvents are the expected events read from expected_events.yaml if present
	ExpectedEvents string

	// ExpectedMetrics are the expected metrics to read from expected_metrics.txt if present
	ExpectedMetrics string

	// CompareObjects is set to lists' of object types to read from the apiserver and compare
	// to ExpectedObjects
	CompareObjects []client.ObjectList

	// CompareMetrics is set to a list of metric names to compare the results of
	CompareMetrics []string

	// FakeRecorder is set when requesting a fake recorder and used to compare ExpectedEvents
	FakeRecorder *record.FakeRecorder

	// Client is set when requesting a fake client and used to compare ExpectedObjects
	Client client.Client
}

TestCase contains a test case parsed from a testdata directory

func (TestCase) CreateObjects

func (tc TestCase) CreateObjects()

func (*TestCase) GetFakeClient

func (tc *TestCase) GetFakeClient(s *runtime.Scheme, buildOpts ...TestCaseFakeClientBuilder) (client.Client, error)

GetFakeClient returns a new client.Client populated with Kubernetes parsed from the testdata input files. Defaults to parsing objects from files with suffix `_client_runtime_objects.yaml`

func (*TestCase) GetFakeRecorder

func (tc *TestCase) GetFakeRecorder() *record.FakeRecorder

GetFakeRecorder returns a fake recorder which can be used to compare events

func (TestCase) GetObjects

func (tc TestCase) GetObjects(s *runtime.Scheme) ([]client.Object, []client.ObjectList, error)

func (TestCase) GetObjectsFromFile

func (tc TestCase) GetObjectsFromFile(js *sjson.Serializer, s *runtime.Scheme, filename, data string) ([]client.Object, []client.ObjectList, error)

GetObjectsFromFile parses the input objects from the test case

func (TestCase) UnmarshalInputsStrict

func (tc TestCase) UnmarshalInputsStrict(into map[string]interface{})

UnmarshalInputsStrict Unmarshals the TestCase Inputs (i.e. by filename) into the corresponding objects (e.g. pointers to structs)

func (TestCase) UpdateObjects

func (tc TestCase) UpdateObjects(s *runtime.Scheme, fn func()) error

UpdateObjects will update objects from files prefixed with "input_client_objects_updates" After each set of updates fn is called. Updates are performed in alphanumberic order by filename.

type TestCaseFakeClientBuilder

type TestCaseFakeClientBuilder func(*fake.ClientBuilder) *fake.ClientBuilder

TestCaseFakeClientBuilder is a function that can be used to modify the fake client builder before building the client.

type TestCaseParser

type TestCaseParser struct {
	// EnvironmentFn if specified will create an integration test environment for the test
	EnvironmentFn func(*TestCase) *envtest.Environment

	// EnvironmentStopFn if specified will create an integration test environment for the test
	EnvironmentStopFn func(*TestCase)

	// MaskExpectedMetadata will drop all metadata fields except
	// name, namespace, labels, and annotations when doing comparison.
	MaskExpectedMetadata bool

	// NameSuffix is appended to the test name
	NameSuffix string

	// Subdir is an optional subdirectory
	Subdir string

	// ExpectedFiles is a list of expected files.  The "expected_" prefix is applied to each
	// item when matching files. e.g. ExpectedFiles: [ "values.txt" ] will look for "expected_values.txt".
	// These are compared to the ActualValues map in the TestCase.  e.g. ActualValues[ "values.txt" ]
	// will be compared to the values read from the "expected_values.txt" file in the test directory
	// and is configured by setting ExpectedFiles to [ "values.txt" ] in the TestCaseParser.
	ExpectedFiles []string

	// InputSuffix is a suffix to append to the expected file
	ExpectedSuffix string

	// ClientInputsSuffix is a suffix to append to files which
	// should be parsed as inputs for a fake client.Client.
	ClientInputsSuffix string

	// DirPrefix is the directory prefix to filter on
	DirPrefix string

	// ClearMeta if set to true will clear metadata that shouldn't be set
	// at creation time
	ClearMeta bool

	// CompareObjects is set to a lists' of objects to read from the apiserver and
	// compare to the objects in expected_objects.yaml.
	CompareObjects []client.ObjectList

	// CompareListOptions are list options applied when comparing objects
	CompareListOptions []client.ListOption

	// CompareMetrics is set to a list of metrics to compare the results of
	CompareMetrics []string

	// CompareEvents if set to true will compare the events from the FakeRecorder to
	// the events in expected_events.yaml.
	CompareEvents bool
}

TestCaseParser parses tests cases from testdata directories. Test cases are assumed to be 1 per-subdirectory under "testdata" with the input in a file called "input" and the expected value in a file called "expected". Errors are stored

func (TestCaseParser) GetActualEvents

func (p TestCaseParser) GetActualEvents(t *testing.T, tc *TestCase) string

GetActualEvents reads objects from the FakeRecorder and returns them as yaml

func (TestCaseParser) GetActualMetrics

func (p TestCaseParser) GetActualMetrics(t *testing.T, names []string) string

GetActualMetrics gathers the metrics from the registry and returns a string value

func (TestCaseParser) GetActualObjects

func (p TestCaseParser) GetActualObjects(t *testing.T, tc *TestCase) string

GetActualObjects reads objects from the apiserver and returns them as yaml

func (TestCaseParser) GetTestCases

func (p TestCaseParser) GetTestCases() ([]TestCase, error)

GetTestCases parses the test cases from the testdata dir

func (TestCaseParser) SetupEnvTest

func (p TestCaseParser) SetupEnvTest(t *testing.T, tc *TestCase) func()

SetupEnvTest will setup and run and envtest environment (apiserver and etcd) for integration tests.Returns a function to stop the envtest. Credentials and coordinates will be injected into each webhook.

func (TestCaseParser) TestDir

func (p TestCaseParser) TestDir(t *testing.T, fn func(*TestCase) error)

TestDir invokes fn for each TestCase for in a "testdata" directory and verifies that the actual observed value matches the expected value

func (TestCaseParser) UpdateExpectedDir

func (p TestCaseParser) UpdateExpectedDir(t *testing.T, cases []TestCase, fn func(*TestCase) error)

UpdateExpectedDir invokes fn for each TestCase for in a "testdata" directory and updates the expected data file with the actual observed value

Jump to

Keyboard shortcuts

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