ctrlutil

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 73 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TestEventActionStopWithoutRemove containers.EventAction = "stop_without_remove"

	// Range of ports used for "host port" if container port configuration does not specify a host port.
	// (Docker/Podman assigns a random port in this case)
	// Note that these ports are not actually used during the test (no sockets are bound to them),
	// they are just for verification purposes.
	MinRandomHostPort int = 40001
	MaxRandomHostPort int = 50000
)
View Source
const AutoStartExecutableAnnotation = "test.usvc-dev.developer.microsoft.com/auto-start-executable"
View Source
const InvalidTunnelID uint32 = 0

Variables

View Source
var (
	ContainerNameAttribute string = apiv1.GroupVersion.Group + ".container_name"
)

Functions

func GetApiServerClient

func GetApiServerClient(t *testing.T, serverInfo *ApiServerInfo) *http.Client

func InjectProxyStartFailures

func InjectProxyStartFailures(port int32, failures uint32)

func MakeResourceCleanupRequest

func MakeResourceCleanupRequest(ctx context.Context, serverInfo *ApiServerInfo) (*http.Request, error)

func NewLoggingWriteCloser

func NewLoggingWriteCloser(log logr.Logger, inner io.Writer) usvc_io.WriteSyncerCloser

func NewTestProxy

func NewTestProxy(mode apiv1.PortProtocol, listenAddress string, listenPort int32, lifetimeCtx context.Context, log logr.Logger) proxy.Proxy

func TestFingerprint

Creates a TunnelRequestFingerprint from a proto.TunnelSpec, compatible with ones created from TunnelReq. Used for testing only.

func WaitApiServerStatus

func WaitApiServerStatus(
	ctx context.Context,
	client *http.Client,
	serverInfo *ApiServerInfo,
	expectedStatus apiserver.ApiServerExecutionStatus,
) error

func WaitObjectCount

func WaitObjectCount[T commonapi.ObjectStruct, LT commonapi.ObjectList, PT commonapi.PObjectStruct[T], PLT commonapi.PObjectList[T, LT, PT]](
	t *testing.T,
	ctx context.Context,
	client ctrl_client.Client,
	expectedCount int,
	contextStr string,
	selector func(obj PT) bool,
)

func WaitObjectDeleted

func WaitObjectDeleted[T commonapi.ObjectStruct, PT commonapi.PObjectStruct[T]](
	t *testing.T,
	ctx context.Context,
	client ctrl_client.Client,
	deleted PT,
)

func WaitObjectExists

func WaitObjectExists[T commonapi.ObjectStruct, LT commonapi.ObjectList, PT commonapi.PObjectStruct[T], PLT commonapi.PObjectList[T, LT, PT]](
	t *testing.T,
	ctx context.Context,
	client ctrl_client.Client,
	contextStr string,
	selector func(obj PT) (bool, error),
) PT

Types

type ApiServerFlag

type ApiServerFlag uint32
const (
	ApiServerFlagsNone                    ApiServerFlag = 0
	ApiServerUseTrueContainerOrchestrator ApiServerFlag = 1
)

type ApiServerInfo

type ApiServerInfo struct {

	// Stops the API server process and cleans up associated resources.
	Dispose func()

	// The event that will be set when the API server process exits.
	ApiServerExited *concurrency.AutoResetEvent

	// The event that will be set when the cleanup of the API server and associated resources is complete.
	ApiServerDisposalComplete *concurrency.AutoResetEvent

	// Strongly-typed client for the API server.
	Client ctrl_client.Client

	// REST client for the API server.
	RestClient *clientgorest.RESTClient

	// Configuration for the client used to connect to the API server.
	ClientConfig *clientgorest.Config

	// Container orchestrator used by the API server.
	ContainerOrchestrator containers.ContainerOrchestrator

	// The process ID for the API server process.
	ApiServerPID process.Pid_t
	// contains filtered or unexported fields
}

ApiServerInfo contains data for interacting with DCP API server instance.

func StartApiServer

func StartApiServer(
	testRunCtx context.Context,
	flags ApiServerFlag,
	log logr.Logger,
) (*ApiServerInfo, error)

Starts the API server in a separate process.

type StartFailureData

type StartFailureData struct {
	RemainingFailures uint32
	Attempts          uint32
}

Data structure to keep track of how many times a proxy start should fail, and how many times the ServiceReconciler has attempted to start the proxy for a given port.

func GetStartFailureData

func GetStartFailureData(port int32) (StartFailureData, bool)

type TestContainerOrchestrator

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

func NewTestContainerOrchestrator

func NewTestContainerOrchestrator(
	lifetimeCtx context.Context,
	log logr.Logger,
	opts TestContainerOrchestratorOption,
) (*TestContainerOrchestrator, error)

func (*TestContainerOrchestrator) BuildImage

func (*TestContainerOrchestrator) CaptureContainerLogs

func (to *TestContainerOrchestrator) CaptureContainerLogs(ctx context.Context, containerNameOrId string, stdout usvc_io.WriteSyncerCloser, stderr usvc_io.WriteSyncerCloser, options containers.StreamContainerLogsOptions) error

func (*TestContainerOrchestrator) Close

func (to *TestContainerOrchestrator) Close() error

func (*TestContainerOrchestrator) ConnectNetwork

func (*TestContainerOrchestrator) ContainerHost

func (*TestContainerOrchestrator) ContainerHost() string

func (*TestContainerOrchestrator) CreateContainer

func (*TestContainerOrchestrator) CreateFiles

func (*TestContainerOrchestrator) CreateNetwork

func (*TestContainerOrchestrator) CreateVolume

func (*TestContainerOrchestrator) DefaultNetworkName

func (to *TestContainerOrchestrator) DefaultNetworkName() string

func (*TestContainerOrchestrator) DisconnectNetwork

func (*TestContainerOrchestrator) EnsureBackgroundStatusUpdates

func (to *TestContainerOrchestrator) EnsureBackgroundStatusUpdates(ctx context.Context)

func (*TestContainerOrchestrator) ExecContainer

func (to *TestContainerOrchestrator) ExecContainer(ctx context.Context, options containers.ExecContainerOptions) (<-chan int32, error)

func (*TestContainerOrchestrator) FailMatchingContainers

func (to *TestContainerOrchestrator) FailMatchingContainers(ctx context.Context, name string, exitCode int32, stdErr string)

func (*TestContainerOrchestrator) GetCreatedFiles

func (to *TestContainerOrchestrator) GetCreatedFiles(name string) ([]*containerCreateFile, error)

func (*TestContainerOrchestrator) GetDiagnostics

GetDiagnostics returns an empty diagnostics object, as the test container orchestrator does not support diagnostics.

func (*TestContainerOrchestrator) GetSocketFilePath

func (to *TestContainerOrchestrator) GetSocketFilePath() string

func (*TestContainerOrchestrator) HasImage

func (to *TestContainerOrchestrator) HasImage(id string) bool

func (*TestContainerOrchestrator) ImageCount

func (to *TestContainerOrchestrator) ImageCount() int

func (*TestContainerOrchestrator) InspectContainers

func (*TestContainerOrchestrator) InspectImages

func (*TestContainerOrchestrator) InspectNetworks

func (*TestContainerOrchestrator) InspectVolumes

func (*TestContainerOrchestrator) IsDefault

func (*TestContainerOrchestrator) IsDefault() bool

func (*TestContainerOrchestrator) ListContainers

func (*TestContainerOrchestrator) ListNetworks

func (*TestContainerOrchestrator) Name

func (*TestContainerOrchestrator) PullImage

func (*TestContainerOrchestrator) RemoveContainers

func (*TestContainerOrchestrator) RemoveNetworks

func (*TestContainerOrchestrator) RemoveVolumes

func (*TestContainerOrchestrator) RunContainer

func (*TestContainerOrchestrator) SetHealthcheckMatchingContainers

func (to *TestContainerOrchestrator) SetHealthcheckMatchingContainers(ctx context.Context, name string, healthcheck containers.ContainerHealthcheck)

func (*TestContainerOrchestrator) SetRuntimeHealth

func (to *TestContainerOrchestrator) SetRuntimeHealth(healthy bool)

func (*TestContainerOrchestrator) SimulateContainerExecCommandLogging

func (to *TestContainerOrchestrator) SimulateContainerExecCommandLogging(container string, command string, target apiv1.LogStreamSource, content []byte) error

func (*TestContainerOrchestrator) SimulateContainerExecExit

func (to *TestContainerOrchestrator) SimulateContainerExecExit(ctx context.Context, container string, command string, exitCode int32) error

func (*TestContainerOrchestrator) SimulateContainerExit

func (to *TestContainerOrchestrator) SimulateContainerExit(ctx context.Context, name string, exitCode int32) error

func (*TestContainerOrchestrator) SimulateContainerLogging

func (to *TestContainerOrchestrator) SimulateContainerLogging(containerName string, target apiv1.LogStreamSource, content []byte) error

func (*TestContainerOrchestrator) SimulateContainerRestart

func (to *TestContainerOrchestrator) SimulateContainerRestart(ctx context.Context, name string) error

func (*TestContainerOrchestrator) SimulateContainerStartupLogs

func (to *TestContainerOrchestrator) SimulateContainerStartupLogs(name string, stdout, stderr []byte)

func (*TestContainerOrchestrator) SimulateHealthcheck

func (to *TestContainerOrchestrator) SimulateHealthcheck(ctx context.Context, name string, health *containers.InspectedContainerHealth) error

func (*TestContainerOrchestrator) StartContainers

func (*TestContainerOrchestrator) StopContainers

func (*TestContainerOrchestrator) WatchContainers

func (*TestContainerOrchestrator) WatchNetworks

type TestContainerOrchestratorOption

type TestContainerOrchestratorOption uint32
const (
	TcoOptionNone TestContainerOrchestratorOption = 0

	// Enables communication with the test container orchestrator via a Unix domain socket.
	// Used for tests that involve calls to API server to fetch container logs (the API server is a different process
	// from the the one that runs the container controller and the test container orchestrator).
	TcoOptionEnableSocketListener = 0x01
)

type TestContainerPortConfig

type TestContainerPortConfig struct {
	containers.InspectedContainerHostPortConfig
	UseRandomHostPort bool
}

type TestIdeRun

type TestIdeRun struct {
	ID                 controllers.RunID
	Exe                *apiv1.Executable
	FinishTimestamp    metav1.MicroTime
	ExitCode           *int32
	StartWaitingCalled bool

	ChangeHandler        controllers.RunChangeHandler
	StartWaitingCallback func()
	// contains filtered or unexported fields
}

func (*TestIdeRun) Finished

func (r *TestIdeRun) Finished() bool

func (*TestIdeRun) Running

func (r *TestIdeRun) Running() bool

type TestIdeRunner

type TestIdeRunner struct {
	Runs *syncmap.Map[types.NamespacedName, *TestIdeRun]
	// contains filtered or unexported fields
}

func NewTestIdeRunner

func NewTestIdeRunner(lifetimeCtx context.Context) *TestIdeRunner

func (*TestIdeRunner) FindAll

func (r *TestIdeRunner) FindAll(exePath string, cond func(run TestIdeRun) bool) []TestIdeRun

func (*TestIdeRunner) SimulateFailedRunStart

func (r *TestIdeRunner) SimulateFailedRunStart(exeName types.NamespacedName, startupError error) error

func (*TestIdeRunner) SimulateRunEnd

func (r *TestIdeRunner) SimulateRunEnd(runID controllers.RunID, exitCode int32) error

func (*TestIdeRunner) SimulateRunStart

func (r *TestIdeRunner) SimulateRunStart(
	isDesiredRun func(types.NamespacedName, *TestIdeRun) bool,
	changeToStarted func(*TestIdeRun, *controllers.ExecutableStartResult),
) error

func (*TestIdeRunner) SimulateSuccessfulRunStart

func (r *TestIdeRunner) SimulateSuccessfulRunStart(runID controllers.RunID, pid process.Pid_t) error

func (*TestIdeRunner) StartRun

func (*TestIdeRunner) StopRun

func (r *TestIdeRunner) StopRun(_ context.Context, runID controllers.RunID, _ logr.Logger) error

type TestProcessExecutableRunner added in v0.22.2

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

TestProcessExecutableRunner wraps an ExecutableRunner and allows tests to simulate asynchronous executable startup by delaying the call to the underlying runner.

func NewTestProcessExecutableRunner added in v0.22.2

func NewTestProcessExecutableRunner(pe process.Executor) *TestProcessExecutableRunner

func (*TestProcessExecutableRunner) InstallAutoExecution added in v0.22.2

func (r *TestProcessExecutableRunner) InstallAutoExecution(ae testutil.AutoExecution)

InstallAsyncStartConfig adds an async start configuration. If a configuration with the same condition already exists, it will be replaced.

func (*TestProcessExecutableRunner) RemoveAutoExecution added in v0.22.2

func (e *TestProcessExecutableRunner) RemoveAutoExecution(sc testutil.ProcessSearchCriteria)

func (*TestProcessExecutableRunner) StartRun added in v0.22.2

func (*TestProcessExecutableRunner) StopRun added in v0.22.2

StopRun implements ExecutableRunner by delegating to the underlying runner.

type TestProxy

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

func (*TestProxy) Configure

func (tp *TestProxy) Configure(config proxy.ProxyConfig) error

func (*TestProxy) EffectiveAddress

func (tp *TestProxy) EffectiveAddress() string

func (*TestProxy) EffectivePort

func (tp *TestProxy) EffectivePort() int32

func (*TestProxy) ListenAddress

func (tp *TestProxy) ListenAddress() string

func (*TestProxy) ListenPort

func (tp *TestProxy) ListenPort() int32

func (*TestProxy) Start

func (tp *TestProxy) Start() error

func (*TestProxy) State

func (tp *TestProxy) State() proxy.ProxyState

type TestTunnelControlClient

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

func NewTestTunnelControlClient

func NewTestTunnelControlClient() *TestTunnelControlClient

func (*TestTunnelControlClient) DeleteTunnel

func (*TestTunnelControlClient) DisableTunnel

Disables a tunnel, making the subsequent PrepareTunnel calls for the tunnel's fingerprint fail. Returns true if the tunnel was found and disabled, false otherwise (i.e. it was not previously enabled, or known).

func (*TestTunnelControlClient) EnableTunnel

func (t *TestTunnelControlClient) EnableTunnel(tunnelSpec *dcptunproto.TunnelSpec)

Enables a tunnel, making the subsequent PrepareTunnel calls return the provided tunnel spec.

func (*TestTunnelControlClient) GetTunnelSpec

Gets the tunnel spec for the specified fingerprint. Can be used to verify that a tunnel has been prepared by the ContainerNetworkTunnelProxy controller (by checking if the tunnel ID has been assigned to the tunnel spec).

func (*TestTunnelControlClient) PrepareTunnel

func (*TestTunnelControlClient) Shutdown

Jump to

Keyboard shortcuts

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