testenv

package
v0.0.0-...-9dd34b9 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 44 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testenv runs command tests against a real API server: one envtest (kube-apiserver and etcd) per test package with the Gateway API and kgateway CRDs, behind fault-injecting proxies (design §10).

A package using it calls Run from TestMain and New from each test. Tests using it never call t.Parallel.

Index

Constants

View Source
const (
	// KindAcquire is a classic LIST, or a WATCH with sendInitialEvents=true.
	KindAcquire = "acquire"
	// KindWatch is a WATCH without sendInitialEvents=true.
	KindWatch = "watch"
	// KindDiscovery is a GET under /api, /apis or /version that names no resource.
	KindDiscovery = "discovery"
)

Request kinds a Match selects.

Variables

This section is empty.

Functions

func ClassicWatch

func ClassicWatch(t testing.TB)

ClassicWatch turns the WatchListClient feature gate off, so informers use a classic LIST then WATCH, and restores the previous gates at cleanup. Call it before starting any informer.

func ContextName

func ContextName(i int) string

ContextName returns the kubeconfig context name of Env.Proxies[i].

func ProxyPodSelector

func ProxyPodSelector(reg *extension.Registry) string

ProxyPodSelector returns the label selector the cache sends on its proxy Pod requests: that of the first registered detector that selects proxy Pods, or "" when none does. Use it as Match.LabelSelector.

func Run

func Run(m *testing.M) int

Run runs the package's tests and then stops its envtest. Call it from TestMain: os.Exit(testenv.Run(m)). It silences klog first, as Execute does, so no test's watch goroutine can race a later Execute.

Types

type Action

type Action struct {
	Delay time.Duration
	// Until, if set, holds the request until the channel is closed.
	Until      <-chan struct{}
	Status     int
	RetryAfter int // seconds, sent with Status
	// HoldWatchThenStatus keeps a plain watch open until the matching LIST has
	// completed plus 200ms, then answers with this status.
	HoldWatchThenStatus int
}

Action is the fault applied to a matching request. Delay runs first, then Until; then Status, if set, answers instead of envtest.

type Env

type Env struct {
	// Kubeconfig is the path of a kubeconfig with one context per proxy:
	// ContextName(i) points at Proxies[i], and ContextName(0) is current.
	Kubeconfig string
	Proxies    []*Proxy
	// Admin talks to envtest directly, bypassing the proxies.
	Admin *rest.Config
}

Env is one test's view of the package's envtest.

func New

func New(t testing.TB, opts Options) *Env

New returns an Env with opts.Contexts proxies in front of the package's envtest and opts.Fixtures applied. At cleanup it fails the test for every rule that never matched, closes the proxies, and deletes every object it created, waiting until each is gone. Namespaces are created if absent and never deleted: envtest has no namespace controller to finish a deletion.

func (*Env) AsUser

func (e *Env) AsUser(t testing.TB, name string) string

AsUser returns a kubeconfig whose one context reaches envtest as the named user, through a new proxy. The user may do nothing until a binding grants it.

type Match

type Match struct {
	// Kind is KindAcquire, KindWatch or KindDiscovery; it is ignored when Subresource is set.
	Kind string
	// Resource is "pods" for the core group and "<resource>.<group>" otherwise.
	// For discovery it is "*" for every request, or the group version ("v1",
	// "gateway.networking.k8s.io/v1").
	Resource string
	// LabelSelector is compared as a parsed selector; "" matches any.
	LabelSelector string
	// Subresource, if set, selects requests of any verb to an object's
	// subresource, such as "proxy" for pods/<name>/proxy/... or a POST to
	// "portforward"; Kind and LabelSelector are then ignored.
	Subresource string
}

Match selects requests by kind, resource and label selector, or a named object's subresource.

type Options

type Options struct {
	// Fixtures are YAML files applied as admin, relative to the test's working
	// directory. A status in a fixture is written through the status subresource.
	Fixtures []string
	// Contexts is the number of proxies, one kubeconfig context each; 0 means 1.
	Contexts int
	// CRDDirs are CRD files or directories installed in addition to the
	// Gateway API and kgateway CRDs.
	CRDDirs []string
}

Options configures one test's Env.

type Proxy

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

Proxy is an httptest reverse proxy in front of envtest that injects faults. Requests reach envtest as the admin user.

func (*Proxy) OpenConns

func (p *Proxy) OpenConns() int

OpenConns returns the number of client connections not yet closed or hijacked.

func (*Proxy) OpenWatches

func (p *Proxy) OpenWatches() int

OpenWatches returns the number of watch requests currently open.

func (*Proxy) Requests

func (p *Proxy) Requests() []string

Requests returns every request seen so far, as "METHOD /path?query".

func (*Proxy) Rule

func (p *Proxy) Rule(m Match, a Action) *RuleHits

Rule adds a fault. The first matching rule in the order added applies.

func (*Proxy) URL

func (p *Proxy) URL() string

URL returns the proxy's base URL.

func (*Proxy) WatchesSeen

func (p *Proxy) WatchesSeen() int

WatchesSeen returns the number of watch requests received so far.

type RuleHits

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

RuleHits counts the requests a rule applied to.

func (*RuleHits) ClientGone

func (h *RuleHits) ClientGone() <-chan struct{}

ClientGone is closed once a client went away while the rule held its request.

func (*RuleHits) N

func (h *RuleHits) N() int

N returns the number of requests the rule applied to so far.

Jump to

Keyboard shortcuts

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