Help us shape the future of Go! Take the 2026 Go Developer Survey

testutil

package
v0.47.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package testutil provides reflection-based helpers used by drift-detection tests in the operator. It is intended for test code only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertNoDrift

func AssertNoDrift[Runtime, CRD any](t *testing.T, spec DriftSpec)

AssertNoDrift runs three subtests against spec:

  • CRDFieldsCovered: every CRD leaf is mapped or ignored
  • RuntimeFieldsCovered: every runtime leaf is mapped or ignored
  • MappingTableSanity: tables have no duplicates, empty entries, cross-pollination, or stale leaves

Type parameter ordering is [Runtime, CRD] mirroring how a converter reads: it produces Runtime from CRD. Failure messages reference both type names so a developer who hits a failure can grep directly to the offending field.

testutil.AssertNoDrift[telemetry.Config, v1beta1.MCPTelemetryConfigSpec](
    t,
    testutil.DriftSpec{
        Domain:               "telemetry",
        Mappings:             telemetryFieldMappings,
        IgnoredOnCRDOnly:     telemetryIgnoredOnCRDOnly,
        IgnoredOnRuntimeOnly: telemetryIgnoredOnRuntimeOnly,
    },
)

func FlattenJSONLeafFields

func FlattenJSONLeafFields(t reflect.Type) []string

FlattenJSONLeafFields returns every leaf JSON field path under t as a sorted slice of dot-delimited paths (e.g. "openTelemetry.tracing.enabled").

A leaf is any type that does not produce nested JSON keys at runtime: a primitive, a slice/map of primitives, or a type implementing json.Marshaler (whose MarshalJSON shape is opaque to reflection). Structs, slices/maps of structs, and pointers to either are recursed into. Field names follow encoding/json rules including `,inline` and anonymous-field promotion. Self-referential types stop on revisit so the walk always terminates.

If t is nil or, after dereferencing, is not a struct, an empty slice is returned rather than panicking.

func NewScheme added in v0.30.1

func NewScheme(t *testing.T, extra ...func(*runtime.Scheme) error) *runtime.Scheme

NewScheme builds a runtime.Scheme registered with all built-in Kubernetes types (corev1, appsv1, rbacv1, …) via client-go's scheme, plus both ToolHive operator API versions (v1alpha1 and v1beta1). Any extra AddToScheme functions are applied on top of that default set, in order.

It replaces the per-test boilerplate of calling runtime.NewScheme() followed by individual AddToScheme calls. The built-in superset is harmless for fake clients: registering types a given test does not use has no effect, so almost every caller wants the bare NewScheme(t). Pass extras only when a test needs an API outside the default set, e.g.:

scheme := testutil.NewScheme(t, apiextensionsv1.AddToScheme)

A test that must assert behavior when a type is deliberately unregistered should build its scheme inline with runtime.NewScheme(); that is a rare, self-documenting exception this helper intentionally does not serve.

Types

type DriftSpec

type DriftSpec struct {
	Domain               string
	Mappings             []FieldMapping
	IgnoredOnCRDOnly     map[string]string
	IgnoredOnRuntimeOnly map[string]string
}

DriftSpec declares the bidirectional mapping between a CRD spec type and its runtime config counterpart. Every leaf path on either type must be classified into exactly one of three buckets: a Mappings entry, an IgnoredOnCRDOnly entry (with justification), or an IgnoredOnRuntimeOnly entry (with justification). AssertNoDrift fails the test when a leaf is unclassified, when a table entry is stale, or when the tables contradict themselves.

Domain is used in failure messages and subtest names; pick something that matches the domain folder (e.g. "telemetry", "oidc", "vmcp").

type FieldMapping

type FieldMapping struct {
	CRD     string
	Runtime string
}

FieldMapping pairs a CRD-side leaf path with a runtime-side leaf path. Both sides must be present unless the field is in one of the ignore maps of the enclosing DriftSpec.

Jump to

Keyboard shortcuts

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