testutil

package
v0.22.8 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NotFound              = -1
	KilledProcessExitCode = 137 // 128 + SIGKILL (9)
)
View Source
const TestHttpEndpointPath = "/healthz"

Variables

This section is empty.

Functions

func EnsureProcessTree

func EnsureProcessTree(t *testing.T, rootP process.ProcessTreeItem, expectedSize int, timeout time.Duration)

func GetTestToolDir

func GetTestToolDir(exeName string) (string, error)

Returns the folder containing desired test tool (executable).

func ServeHttp

func ServeHttp(lifetimeCtx context.Context, routes []RouteSpec) string

Start a new HTTP test server with the given route specs. The server will run until the lifetimeCtx is cancelled. Returns the URL of the server.

func ServeHttpFrom

func ServeHttpFrom(lifetimeCtx context.Context, address string, port int32, routes []RouteSpec) string

Starts a new HTTP test server on specific address and port with the given route specs. The server will run until the lifetimeCtx is cancelled.

Types

type AutoExecution

type AutoExecution struct {
	// The criteria that needs to be matched for the command to be executed.
	// There is no safeguard against multiple commands matching the criteria.
	Condition ProcessSearchCriteria

	// The RunCommand function is called after the process is "running".
	// It can write to command stdout and stderr. The return value is an exit code for the command.
	// The function should be checking pe.Signal channel to see if any signals were sent to the process.
	// The test convention is that execution should stop when SIGTERM is received.
	RunCommand func(pe *ProcessExecution) int32

	// If not nil, this is the error that will be returned by the Executor from StartProcess() call.
	// RunCommand will not be called in this case.
	StartupError func(*ProcessExecution) error

	// If not nil, the process will fail to stop with the specified error.
	StopError func(*ProcessExecution) error

	// AsynchronousStartupDelay is used to simulate asynchronous process startup.
	// If set to a non-zero value, the process startup will be delayed by the specified duration.
	AsynchronousStartupDelay time.Duration
}

AutoExecution structure is used by clients to automatically and asynchronously complete an execution of a command that matches certain criteria.

type ProcessExecution

type ProcessExecution struct {
	PID                process.Pid_t
	Cmd                *exec.Cmd
	StartWaitingCalled bool

	StartedAt      time.Time
	EndedAt        time.Time
	ExitHandler    process.ProcessExitHandler
	ExitCode       int32
	ExecutionEnded chan struct{}
	Signal         chan syscall.Signal  // Channel to send simulated signals to the process
	Executor       *TestProcessExecutor // The reference to the executor that created this process execution.
	// contains filtered or unexported fields
}

func WaitForCommand

func WaitForCommand(
	executor *TestProcessExecutor,
	ctx context.Context,
	command []string,
	lastArg string,
	cond func(*ProcessExecution) bool,
) (*ProcessExecution, error)

Waits for an execution of a specific command to be issued. The command is identified by path to the executable and a subset of its arguments (the first N arguments). If lastArg parameter is not empty, it is matched against the last argument of the command. Last parameter is a function that is called to verify that the command is the one we are waiting for.

func (*ProcessExecution) Finished

func (pe *ProcessExecution) Finished() bool

func (*ProcessExecution) Running

func (pe *ProcessExecution) Running() bool

type ProcessSearchCriteria

type ProcessSearchCriteria struct {
	Command []string                  // The name/path of the executable and first N arguments, if any.
	LastArg string                    // The last argument of the command that needs to match. Optional.
	Cond    ProcessSearchCriteriaCond // Special condition to match the command. Optional.
}

func (*ProcessSearchCriteria) Equals

func (*ProcessSearchCriteria) Matches

func (sc *ProcessSearchCriteria) Matches(pe *ProcessExecution) bool

func (*ProcessSearchCriteria) MatchesCmd added in v0.22.2

func (sc *ProcessSearchCriteria) MatchesCmd(cmd *exec.Cmd) bool

type ProcessSearchCriteriaCond

type ProcessSearchCriteriaCond func(pe *ProcessExecution) bool

type ResponseSpec

type ResponseSpec struct {
	// StatusCode is the HTTP status code to return.
	StatusCode int

	// Body is the response body to return. If empty, no body will be returned.
	Body []byte

	// Active is set to true if the response should be used for the next request.
	Active *atomic.Bool

	// A function to call when the response is used.
	Observer func(*http.Request)
}

ResponseSpec describes how HTTP test server should reply to a single request.

type RouteSpec

type RouteSpec struct {
	// Pattern (see net/http.ServeMux) to match the request path.
	Pattern string

	// Set of response specs to use for this route.
	// The first response that is "active" will be used.
	// if there is no active response spec, the request will return 404.
	Responses []ResponseSpec
}

The route spec describes how the HTTP test server should route and handle requests.

type TestHttpEndpoint

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

And endpoint for testing HTTP health probes. It returns "success" or "failure" response, depending on the configuration. The configuration can be changed at run time; initially the endpoint responds with "unhealthy" response.

func NewTestHttpEndpoint

func NewTestHttpEndpoint(lifetimeCtx context.Context) *TestHttpEndpoint

func NewTestHttpEndpointWithAddressAndPort

func NewTestHttpEndpointWithAddressAndPort(lifetimeCtx context.Context, address string, port int32) *TestHttpEndpoint

func (*TestHttpEndpoint) AddressAndPort

func (e *TestHttpEndpoint) AddressAndPort() (string, int32, error)

func (*TestHttpEndpoint) SetHealthyResponseObserver

func (e *TestHttpEndpoint) SetHealthyResponseObserver(observer func(*http.Request))

func (*TestHttpEndpoint) SetOutcome

func (e *TestHttpEndpoint) SetOutcome(outcome apiv1.HealthProbeOutcome)

func (*TestHttpEndpoint) SetUnhealthyResponseObserver

func (e *TestHttpEndpoint) SetUnhealthyResponseObserver(observer func(*http.Request))

func (*TestHttpEndpoint) Url

func (e *TestHttpEndpoint) Url() string

type TestProcessExecutor

type TestProcessExecutor struct {
	Executions     []*ProcessExecution
	AutoExecutions []AutoExecution
	// contains filtered or unexported fields
}

func NewTestProcessExecutor

func NewTestProcessExecutor(lifetimeCtx context.Context) *TestProcessExecutor

func (*TestProcessExecutor) ClearHistory

func (e *TestProcessExecutor) ClearHistory()

Clears all execution history

func (*TestProcessExecutor) Close

func (e *TestProcessExecutor) Close() error

func (*TestProcessExecutor) Dispose

func (e *TestProcessExecutor) Dispose()

func (*TestProcessExecutor) FindAll

func (e *TestProcessExecutor) FindAll(
	command []string,
	lastArg string,
	cond ProcessSearchCriteriaCond,
) []*ProcessExecution

Finds all executions of a specific command. The command is identified by path to the executable and a subset of its arguments (the first N arguments). If lastArg parameter is not empty, it is matched against the last argument of the command. Last parameter is a function that is called to verify that the command is the one we are waiting for.

func (*TestProcessExecutor) FindByPid

func (e *TestProcessExecutor) FindByPid(pid process.Pid_t) (*ProcessExecution, bool)

func (*TestProcessExecutor) InstallAutoExecution

func (e *TestProcessExecutor) InstallAutoExecution(autoExecution AutoExecution)

func (*TestProcessExecutor) RemoveAutoExecution

func (e *TestProcessExecutor) RemoveAutoExecution(sc ProcessSearchCriteria)

func (*TestProcessExecutor) SimulateProcessExit

func (e *TestProcessExecutor) SimulateProcessExit(t *testing.T, pid process.Pid_t, exitCode int32)

Called by tests to simulate a process exit with specific exit code.

func (*TestProcessExecutor) StartAndForget

func (*TestProcessExecutor) StartProcess

func (*TestProcessExecutor) StopProcess

func (e *TestProcessExecutor) StopProcess(pid process.Pid_t, processStartTime time.Time) error

Called by the controller (via Executor interface)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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