testutils

package
v1.14.0 Latest Latest
Warning

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

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

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertErrContains

func AssertErrContains(t *testing.T, got error, contains string, ctx string)

AssertErrContains asserts that an error is non-nil and its message contains the provided substring.

func AssertErrWrappedContains

func AssertErrWrappedContains(t *testing.T, got error, want error, contains string, ctx string)

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

func AssertFileEquals(t *testing.T, dir, path, expected string)

AssertFileEquals compares the file content with the expected string.

func AssertStringContains

func AssertStringContains(t *testing.T, haystack string, subs ...string)

AssertStringContains asserts that the haystack contains all provided substrings.

func AssertStringContainsOneOf

func AssertStringContainsOneOf(t *testing.T, haystack string, options ...string)

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

func ExpectErrorContains(t *testing.T, err error, substr, description string)

ExpectErrorContains fails the test if err is nil or does not contain substr.

func ExpectNoError added in v1.12.3

func ExpectNoError(t *testing.T, err error, description string)

ExpectNoError fails the test if err is not nil.

func ExpectNotNil added in v1.12.3

func ExpectNotNil(t *testing.T, value any, description string)

ExpectNotNil fails the test if value is nil.

func ExpectTrue added in v1.12.3

func ExpectTrue(t *testing.T, condition bool, description string)

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

func NewCommand(t *testing.T) (*cobra.Command, *bytes.Buffer)

NewCommand creates a test command with output buffers.

func RunNameCases

func RunNameCases(t *testing.T, cases []NameCase, run func(t *testing.T, c NameCase))

RunNameCases runs the provided function for each NameCase with parallel subtests.

func RunTestMainWithSnapshotCleanup

func RunTestMainWithSnapshotCleanup(m *testing.M)

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

func SetFlags(t *testing.T, cmd *cobra.Command, values map[string]string)

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

func SetupCommandWithOutput() (*cobra.Command, *bytes.Buffer)

SetupCommandWithOutput returns a new cobra command with its output wired to an in-memory buffer.

func SetupExistingFile

func SetupExistingFile(t *testing.T, filename string) (string, string, string)

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

func SetupValidWorkingDir(t *testing.T) func()

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

func TestCmdExecuteInCleanDir(t *testing.T, cmdFactory func() *cobra.Command, cmdName string)

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

func WriteValidKsailConfig(t *testing.T, dir string)

WriteValidKsailConfig writes a minimal valid KSail configuration into the provided directory.

Types

type NameCase

type NameCase struct {
	Name         string
	InputName    string
	ExpectedName string
}

NameCase represents a common test case shape for commands that accept an optional name and default to a configured name when empty.

func DefaultNameCases

func DefaultNameCases(defaultCfgName string) []NameCase

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

type RecordingTimer struct {
	StartCalls    int
	StartCount    int
	StageCalls    int
	NewStageCount int
}

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

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.

func (*StubProvisioner) Start added in v1.14.0

func (p *StubProvisioner) Start(_ context.Context, name string) error

Start implements clusterprovisioner.ClusterProvisioner.

func (*StubProvisioner) Stop added in v1.14.0

func (p *StubProvisioner) Stop(_ context.Context, name string) error

Stop implements clusterprovisioner.ClusterProvisioner.

Jump to

Keyboard shortcuts

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