Documentation
¶
Overview ¶
Package testutils provides testing utilities to aid error handling in tests.
This package contains generic file-related test utilities, testing helpers shared across packages, and various utilities for error handling and test setup/teardown.
Index ¶
- func AssertErrContains(t *testing.T, got error, contains string, ctx string)
- func AssertErrWrappedContains(t *testing.T, got error, want error, contains string, ctx string)
- func AssertFileEquals(t *testing.T, dir, path, expected string)
- func AssertStringContains(t *testing.T, haystack string, subs ...string)
- func AssertStringContainsOneOf(t *testing.T, haystack string, options ...string)
- func CreateConfigManager(t *testing.T, writer io.Writer) *ksailconfigmanager.ConfigManager
- func CreateConfigManagerWithFieldSelectors(writer io.Writer, ...) *configmanager.ConfigManager
- func CreateDefaultConfigManager() *configmanager.ConfigManager
- func ExpectErrorContains(t *testing.T, err error, substr, description string)
- func ExpectNoError(t *testing.T, err error, description string)
- func ExpectNotNil(t *testing.T, value any, description string)
- func ExpectTrue(t *testing.T, condition bool, description string)
- func MustMarshal[T any](t *testing.T, m marshaller.Marshaller[T], v T) string
- func MustUnmarshal[T any](t *testing.T, m marshaller.Marshaller[T], data []byte, out *T)
- func MustUnmarshalString[T any](t *testing.T, m marshaller.Marshaller[T], data string, out *T)
- func NewCommand(t *testing.T) (*cobra.Command, *bytes.Buffer)
- func RunNameCases(t *testing.T, cases []NameCase, run func(t *testing.T, c NameCase))
- func RunTestMainWithSnapshotCleanup(m *testing.M)
- func RunValidationErrorTest(t *testing.T, commandFactory func() *cobra.Command, ...)
- func SetFlags(t *testing.T, cmd *cobra.Command, values map[string]string)
- func SetupCommandWithOutput() (*cobra.Command, *bytes.Buffer)
- func SetupExistingFile(t *testing.T, filename string) (string, string, string)
- func SetupValidWorkingDir(t *testing.T) func()
- func TestCmdExecuteInCleanDir(t *testing.T, cmdFactory func() *cobra.Command, cmdName string)
- func TestSimpleCommandCreation(t *testing.T, data SimpleCommandTestData)
- func TestSimpleCommandExecution(t *testing.T, data SimpleCommandTestData)
- func TestSimpleCommandHelp(t *testing.T, data SimpleCommandTestData)
- func WriteValidKsailConfig(t *testing.T, dir string)
- type NameCase
- type RecordingTimer
- type SimpleCommandTestData
- type StubFactory
- type StubProvisioner
- func (p *StubProvisioner) Create(_ context.Context, name string) error
- func (p *StubProvisioner) Delete(_ context.Context, name string) error
- func (p *StubProvisioner) Exists(context.Context, string) (bool, error)
- func (p *StubProvisioner) List(context.Context) ([]string, error)
- func (p *StubProvisioner) Start(_ context.Context, name string) error
- func (p *StubProvisioner) Stop(_ context.Context, name string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertErrContains ¶
AssertErrContains asserts that an error is non-nil and its message contains the provided substring.
func AssertErrWrappedContains ¶
AssertErrWrappedContains verifies that an error exists, wraps a target error, and optionally contains a given substring in its message. "ctx" describes the calling context.
func AssertFileEquals ¶
AssertFileEquals compares the file content with the expected string.
func AssertStringContains ¶
AssertStringContains asserts that the haystack contains all provided substrings.
func AssertStringContainsOneOf ¶
AssertStringContainsOneOf asserts that the haystack contains at least one of the provided options.
func CreateConfigManager ¶ added in v1.14.0
func CreateConfigManager(t *testing.T, writer io.Writer) *ksailconfigmanager.ConfigManager
CreateConfigManager creates a config manager for testing with a valid ksail config.
func CreateConfigManagerWithFieldSelectors ¶ added in v1.14.0
func CreateConfigManagerWithFieldSelectors( writer io.Writer, fieldSelectors ...configmanager.FieldSelector[v1alpha1.Cluster], ) *configmanager.ConfigManager
CreateConfigManagerWithFieldSelectors builds a config manager populated with the provided field selectors.
func CreateDefaultConfigManager ¶ added in v1.14.0
func CreateDefaultConfigManager() *configmanager.ConfigManager
CreateDefaultConfigManager returns a config manager configured with the standard KSail defaults used in tests.
func ExpectErrorContains ¶ added in v1.12.3
ExpectErrorContains fails the test if err is nil or does not contain substr.
func ExpectNoError ¶ added in v1.12.3
ExpectNoError fails the test if err is not nil.
func ExpectNotNil ¶ added in v1.12.3
ExpectNotNil fails the test if value is nil.
func ExpectTrue ¶ added in v1.12.3
ExpectTrue fails the test if condition is false.
func MustMarshal ¶
func MustMarshal[T any](t *testing.T, m marshaller.Marshaller[T], v T) string
MustMarshal marshals a value or fails the test.
func MustUnmarshal ¶
func MustUnmarshal[T any](t *testing.T, m marshaller.Marshaller[T], data []byte, out *T)
MustUnmarshal unmarshals bytes into a value or fails the test.
func MustUnmarshalString ¶
func MustUnmarshalString[T any](t *testing.T, m marshaller.Marshaller[T], data string, out *T)
MustUnmarshalString unmarshals a string into a value or fails the test.
func NewCommand ¶ added in v1.14.0
NewCommand creates a test command with output buffers.
func RunNameCases ¶
RunNameCases runs the provided function for each NameCase with parallel subtests.
func RunTestMainWithSnapshotCleanup ¶
RunTestMainWithSnapshotCleanup runs the standard TestMain pattern with snapshot cleanup. Shared across packages that only need snapshot cleanup (non command-specific logic).
func RunValidationErrorTest ¶ added in v1.14.0
func RunValidationErrorTest( t *testing.T, commandFactory func() *cobra.Command, expectedSubstrings ...string, )
RunValidationErrorTest executes the provided command factory in an empty directory and validates error output.
func SetFlags ¶ added in v1.14.0
SetFlags sets multiple flags on a Cobra command, failing the test on first error. Keeps tests concise and ensures consistent error handling.
func SetupCommandWithOutput ¶ added in v1.14.0
SetupCommandWithOutput returns a new cobra command with its output wired to an in-memory buffer.
func SetupExistingFile ¶
SetupExistingFile creates a temporary directory and an existing config file with default placeholder content, returning the directory, file path, and content string.
func SetupValidWorkingDir ¶ added in v1.14.0
SetupValidWorkingDir creates a valid KSail configuration in a temporary directory and switches to it. The returned cleanup function restores the original working directory.
func TestCmdExecuteInCleanDir ¶ added in v1.14.0
TestCmdExecuteInCleanDir ensures commands fail validation when executed without a ksail.yaml.
func TestSimpleCommandCreation ¶ added in v1.14.0
func TestSimpleCommandCreation(t *testing.T, data SimpleCommandTestData)
TestSimpleCommandCreation validates the basic metadata of a command.
func TestSimpleCommandExecution ¶ added in v1.14.0
func TestSimpleCommandExecution(t *testing.T, data SimpleCommandTestData)
TestSimpleCommandExecution executes a command and snapshots its standard output.
func TestSimpleCommandHelp ¶ added in v1.14.0
func TestSimpleCommandHelp(t *testing.T, data SimpleCommandTestData)
TestSimpleCommandHelp runs a command with --help and snapshots the output.
func WriteValidKsailConfig ¶ added in v1.14.0
WriteValidKsailConfig writes a minimal valid KSail configuration into the provided directory.
Types ¶
type NameCase ¶
NameCase represents a common test case shape for commands that accept an optional name and default to a configured name when empty.
func DefaultNameCases ¶
DefaultNameCases returns the standard two cases used throughout the repo: 1) explicit name; 2) empty name falls back to default/cfg name.
type RecordingTimer ¶ added in v1.14.0
RecordingTimer is a lightweight test implementation of the timer.Timer interface capturing the number of Start() and NewStage() calls and returning a fixed duration for deterministic snapshot output.
func NewRecordingTimer ¶ added in v1.14.0
func NewRecordingTimer() *RecordingTimer
NewRecordingTimer constructs a RecordingTimer instance.
func (*RecordingTimer) GetTiming ¶ added in v1.14.0
func (r *RecordingTimer) GetTiming() (time.Duration, time.Duration)
GetTiming returns deterministic durations for snapshot-friendly assertions.
func (*RecordingTimer) NewStage ¶ added in v1.14.0
func (r *RecordingTimer) NewStage()
NewStage records a NewStage invocation and increments counters.
func (*RecordingTimer) Start ¶ added in v1.14.0
func (r *RecordingTimer) Start()
Start records a Start invocation and increments counters.
func (*RecordingTimer) Stop ¶ added in v1.14.0
func (r *RecordingTimer) Stop()
Stop implements timer.Timer without additional behaviour for tests.
type SimpleCommandTestData ¶ added in v1.14.0
type SimpleCommandTestData struct {
CommandName string
NewCommand func() *cobra.Command
ExpectedUse string
ExpectedShort string
}
SimpleCommandTestData describes expectations for reusable command creation tests.
type StubFactory ¶ added in v1.14.0
type StubFactory struct {
Provisioner clusterprovisioner.ClusterProvisioner
DistributionConfig any
Err error
CallCount int
}
StubFactory is a test double for clusterprovisioner.Factory.
func (*StubFactory) Create ¶ added in v1.14.0
func (s *StubFactory) Create( _ context.Context, _ *v1alpha1.Cluster, ) (clusterprovisioner.ClusterProvisioner, any, error)
Create implements clusterprovisioner.Factory.
type StubProvisioner ¶ added in v1.14.0
type StubProvisioner struct {
CreateErr error
CreateCalls int
DeleteErr error
DeleteCalls int
StartErr error
StartCalls int
StopErr error
StopCalls int
ReceivedNames []string
}
StubProvisioner is a test double for clusterprovisioner.ClusterProvisioner.
func (*StubProvisioner) Create ¶ added in v1.14.0
func (p *StubProvisioner) Create(_ context.Context, name string) error
Create implements clusterprovisioner.ClusterProvisioner.
func (*StubProvisioner) Delete ¶ added in v1.14.0
func (p *StubProvisioner) Delete(_ context.Context, name string) error
Delete implements clusterprovisioner.ClusterProvisioner.
func (*StubProvisioner) Exists ¶ added in v1.14.0
Exists implements clusterprovisioner.ClusterProvisioner.
func (*StubProvisioner) List ¶ added in v1.14.0
func (p *StubProvisioner) List(context.Context) ([]string, error)
List implements clusterprovisioner.ClusterProvisioner.