harness

package
v0.2.13 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

Package harness starts the envtest control plane the suites under test/envtest share: one kube-apiserver and one etcd per package, and nothing else. No controller runs there -- nothing reconciles, garbage-collects or schedules -- so what a suite proves against it is the API server's own verdict: schemas, CEL, admission policies and webhooks.

Index

Constants

View Source
const (
	// AssetsVariable names the directory holding kube-apiserver, etcd and
	// kubectl. `make test-envtest` fills it from the pinned setup-envtest.
	AssetsVariable = "KUBEBUILDER_ASSETS"
	// RequireVariable turns a missing control plane from a skip into a
	// failure. `make test-envtest` sets it, so the target CI runs can only pass
	// by running every suite.
	RequireVariable = "PTAH_REQUIRE_ENVTEST"
)

Variables

View Source
var (
	ReleaseName      = "ptah"
	ReleaseNamespace = "ptah-system"
	ManagerDigest    = "sha256:" + strings.Repeat("2", 64)
	ManagerImage     = "ghcr.io/stokaro/ptah-operator@" + ManagerDigest
	ExecutorImage    = "example.invalid/ptah@sha256:" + strings.Repeat("3", 64)
	RunnerImage      = "example.invalid/operator@sha256:" + strings.Repeat("4", 64)
	PtahVersion      = "v0.1.0"
)

The release the suites render. The images are content-addressed and never pulled: nothing in envtest runs a container, and the admission rules read only the references.

View Source
var ErrHelmMissing = errors.New("helm is not on PATH; the admission suites render the chart they test")

ErrHelmMissing reports a machine with no helm on PATH.

Functions

func CRDDirectory

func CRDDirectory() string

CRDDirectory holds the CRDs `make manifests` generates, byte for byte the ones the chart ships and the CRD manager installs.

func DecodeDocuments

func DecodeDocuments(stream []byte) ([]*unstructured.Unstructured, error)

DecodeDocuments splits a YAML stream into objects, dropping empty documents.

func RenderChart

func RenderChart(ctx context.Context, release Release) ([]*unstructured.Unstructured, error)

RenderChart runs `helm template` over charts/ptah-operator and decodes every document it prints. It renders offline, so every lookup the templates make sees an empty cluster, which is what a first install sees.

func RepositoryRoot

func RepositoryRoot() string

RepositoryRoot is the checkout this package was compiled from.

Types

type ControlPlane

type ControlPlane struct {
	Environment *envtest.Environment
	// Config authenticates as the envtest administrator, a member of
	// system:masters. RBAC does not constrain it; admission still does.
	Config *rest.Config
	// contains filtered or unexported fields
}

ControlPlane is the API server a package's tests share.

func New

func New(environment *envtest.Environment) *ControlPlane

New wraps environment. The package's TestMain keeps the result and calls Main on it.

func (*ControlPlane) AwaitRBAC added in v0.2.11

func (plane *ControlPlane) AwaitRBAC(ctx context.Context, namespace string) error

AwaitRBAC returns once the API server's authorizer has seen every Role, RoleBinding, ClusterRole and ClusterRoleBinding written before the call.

The authorizer reads RBAC through the API server's own informers, so a request sent right after its grant is written can be refused, and a refusal read then says nothing about the grant. An informer receives one kind's writes in the order the store made them. So AwaitRBAC writes one grant of each kind, a Role and a RoleBinding in namespace and a ClusterRole and a ClusterRoleBinding, and waits until the authorizer allows what each pair grants. Once it does, every earlier write of those kinds has arrived too, and a review then answers for the RBAC as written, whether it allows or refuses. The fence is removed before AwaitRBAC returns.

That holds for one API server, which is what envtest runs. Behind several, a review can reach a server whose informers have not seen the fence.

func (*ControlPlane) Cleanup

func (plane *ControlPlane) Cleanup(f func())

Cleanup registers f to run once the package's tests are done and before the control plane stops, last registered first: a webhook server has to stop while the API server that calls it is still there to be told.

func (*ControlPlane) Impersonate

func (plane *ControlPlane) Impersonate(username string, groups ...string) *rest.Config

Impersonate returns a copy of the administrator's configuration that acts as username. A ServiceAccount username with no groups gets the groups the API server derives for it -- system:serviceaccounts and the namespace's -- and every non-anonymous identity gets system:authenticated, exactly as a token for that identity would.

func (*ControlPlane) Main

func (plane *ControlPlane) Main(m *testing.M, setup func() error) int

Main starts the environment, runs setup against it, runs the package's tests and stops the control plane again. It returns the exit code for os.Exit.

Without binaries the tests skip, unless RequireVariable is set, in which case the package fails before any test runs: a suite nothing ran is not a suite that passed, and the target CI uses sets the variable.

func (*ControlPlane) Require

func (plane *ControlPlane) Require(t testing.TB)

Require skips the calling test when the control plane was never started because no binaries were available and nothing required them.

func (*ControlPlane) Start

func (plane *ControlPlane) Start(t testing.TB, environment *envtest.Environment) *rest.Config

Start starts another control plane, from the same binaries, for one test that has to leave an API server in a state no other test may see. It stops when the test ends.

type Release

type Release struct {
	Name      string
	Namespace string
	// Values are passed to helm as --set-string pairs, in order.
	Values [][2]string
	// TypedValues use Helm --set for booleans and numbers.
	TypedValues [][2]string
}

Release is the chart release a suite renders.

func DefaultRelease

func DefaultRelease() Release

DefaultRelease renders the chart with its own defaults and the four values it refuses to guess.

Jump to

Keyboard shortcuts

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