Documentation
¶
Overview ¶
Package testutil provides shared envtest bootstrap helpers for the operator integration suites under cmd/thv-operator/test-integration. Each suite delegates its BeforeSuite/AfterSuite plumbing here and keeps only its per-suite controller registrations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var MalformedDurations = []string{"banana", "1d", "5", "-1s", "2562048h", ""}
MalformedDurations are refused by every Go-duration field in the CRDs. Besides plain garbage they cover the values a looser check would let through: "1d" (accepted by OpenAPI format: duration, rejected by Go), "2562048h" (well-formed but beyond Go's range, so only a CEL duration() rule catches it) and the empty string.
var ServedAPIVersions = []string{mcpv1beta1.GroupVersion.Version, mcpv1alpha1.GroupVersion.Version}
ServedAPIVersions are the served toolhive.stacklok.dev versions whose CRD schemas carry the same validation rules.
var ValidDurations = []string{"90s", "1h30m", "1.5h", "250ms"}
ValidDurations are accepted by every Go-duration field in the CRDs.
Functions ¶
func DescribeDurationField ¶ added in v0.76.0
func DescribeDurationField(f DurationField, env func() (context.Context, client.Client))
DescribeDurationField registers one spec per value: each of ValidDurations and f.Valid must be admitted, and each of MalformedDurations and f.Invalid must be rejected with an error naming the field. env returns the suite's context and client when the spec runs.
func ObjectWithValueAt ¶ added in v0.76.0
func ObjectWithValueAt(gvk schema.GroupVersionKind, obj client.Object, path []any, value any) *unstructured.Unstructured
ObjectWithValueAt renders obj as an unstructured object of kind gvk and sets value at path, whose string elements are object keys and int elements list indices. It round-trips through encoding/json because a typed client always serializes a valid value, and the unstructured converter would skip the custom MarshalJSON of vmcp config durations.
Types ¶
type DurationField ¶ added in v0.76.0
type DurationField struct {
// Name is a short DNS-label identifier, unique within a suite.
Name string
// GVK is the kind and served version the object is created as.
GVK schema.GroupVersionKind
// Base returns a valid object with the given name. Its value at Path is
// overwritten, so Path must already exist in the object's JSON form.
Base func(name string) client.Object
// Path locates the field: string elements are object keys and int
// elements are list indices.
Path []any
// Valid and Invalid extend ValidDurations and MalformedDurations with
// values specific to the field's own bounds.
Valid, Invalid []string
}
DurationField is one Go-duration field under admission test.
type SessionStorageTLSCase ¶ added in v0.52.0
type SessionStorageTLSCase struct {
// Name is a short identifier, unique among the cases, usable in object names.
Name string
// SessionStorage is the spec.sessionStorage under test.
SessionStorage *mcpv1beta1.SessionStorageConfig
// WantErr is a substring of the admission error, or empty when the object
// must be accepted.
WantErr string
}
SessionStorageTLSCase is one admission case for the sessionStorage.tls CEL rules, which MCPServer, MCPRemoteProxy and VirtualMCPServer share.
func SessionStorageTLSCases ¶ added in v0.52.0
func SessionStorageTLSCases() []SessionStorageTLSCase
SessionStorageTLSCases returns the sessionStorage.tls admission cases, so every workload kind is checked against the same rules.
type SuiteEnv ¶
type SuiteEnv struct {
// Cfg is the rest config for the running envtest control plane.
Cfg *rest.Config
// Client is a controller-runtime client wired to the global scheme.
Client client.Client
// Manager is the controller manager. It is nil for StartMinimalSuite and is
// created but not started by StartSuite — call StartManager to launch it.
Manager ctrl.Manager
// Ctx is the suite-scoped context cancelled by Stop. Specs and indexers that
// need a context should use it.
Ctx context.Context
// contains filtered or unexported fields
}
SuiteEnv holds the shared envtest infrastructure for an operator integration suite. It is produced by StartSuite (full bootstrap with a manager) or StartMinimalSuite (envtest + client only) and torn down by Stop.
func StartMinimalSuite ¶
func StartMinimalSuite() *SuiteEnv
StartMinimalSuite bootstraps an envtest control plane with no operator CRDs and no controller manager: just the API server, the apiextensions scheme (so tests can install CRDs on demand), and a client. It exists for the StorageVersionMigrator suite, which installs its own CRDs per-test and drives reconcilers directly. It must be called from within a Ginkgo node.
func StartSuite ¶
func StartSuite(opts SuiteOptions) *SuiteEnv
StartSuite bootstraps a full operator integration suite: it starts an envtest control plane loaded with the operator CRDs, registers the operator API versions plus the built-in Kubernetes types on the global client-go scheme, creates a client and a controller manager (metrics and health probes disabled), and optionally installs the GroupRef field indexers.
The returned manager is not started — callers register their controllers on it and then call StartManager. Pair every StartSuite with a deferred or AfterSuite call to Stop. It must be called from within a Ginkgo node (BeforeSuite or a spec) because it uses Ginkgo/Gomega for logging and assertions.
func (*SuiteEnv) StartManager ¶
func (e *SuiteEnv) StartManager()
StartManager launches the controller manager in a background goroutine. Call it after registering all controllers on Manager. It is a no-op when there is no manager (i.e. for StartMinimalSuite).
func (*SuiteEnv) Stop ¶
func (e *SuiteEnv) Stop()
Stop cancels the suite context and tears down the envtest control plane.
When a manager is running it waits for Manager.Start to return before stopping the control plane. That wait is load-bearing, not politeness: the manager's informers hold long-running watch requests against kube-apiserver, and kube-apiserver's graceful shutdown blocks on draining them. Signalling the API server to stop while watches are still open makes it miss envtest's 20s ControlPlaneStopTimeout, which surfaces as a flaky "timeout waiting for process kube-apiserver to stop" failure in AfterSuite. The suites with fewer specs than Ginkgo procs (MCPTelemetryConfig, MCPGroup) hit it most often: their idle procs cancel the suite context moments after the informers established their watches.
type SuiteOptions ¶
type SuiteOptions struct {
// RegisterGroupRefIndexers installs the spec.groupRef field indexers for
// MCPServer, MCPRemoteProxy, and MCPServerEntry. Suites whose controllers
// list by GroupRef (anything that wires up the MCPGroup or VirtualMCPServer
// controllers) need these; the rest leave it false.
RegisterGroupRefIndexers bool
// EnableAdmissionPlugins lists kube-apiserver admission plugins to enable
// on top of the defaults envtest already runs (--enable-admission-plugins
// only adds plugins; the default-on set stays enabled). Suites that need an
// API server closer to a hardened distribution such as OpenShift use this;
// the rest leave it empty so their API server is unchanged.
EnableAdmissionPlugins []string
}
SuiteOptions configures the envtest bootstrap performed by StartSuite. Add fields only as concrete suites require them.