package testing

import "k8s.io/apiserver/pkg/admission/plugin/webhook/testing"

Index

Functions

func ClockSteppingWebhookHandler

func ClockSteppingWebhookHandler(t testing.TB, fakeClock *testingclock.FakeClock) func(http.ResponseWriter, *http.Request)

ClockSteppingWebhookHandler given a fakeClock returns a request handler that moves time in given clock by an amount specified in the webhook request

func ConvertToMutatingWebhooks

func ConvertToMutatingWebhooks(webhooks []registrationv1.ValidatingWebhook) []registrationv1.MutatingWebhook

ConvertToMutatingWebhooks converts a validating webhook to a mutating one for test purposes.

func NewAttribute

func NewAttribute(namespace string, labels map[string]string, dryRun bool) admission.Attributes

NewAttribute returns static admission Attributes for testing.

func NewAttributeUnstructured

func NewAttributeUnstructured(namespace string, labels map[string]string, dryRun bool) admission.Attributes

NewAttributeUnstructured returns static admission Attributes for testing with custom resources.

func NewAuthenticationInfoResolver

func NewAuthenticationInfoResolver(cacheMisses *int32) webhook.AuthenticationInfoResolver

NewAuthenticationInfoResolver creates a fake AuthenticationInfoResolver that counts cache misses on every call to its methods.

func NewFakeMutatingDataSource

func NewFakeMutatingDataSource(name string, webhooks []registrationv1.MutatingWebhook, stopCh <-chan struct{}) (clientset kubernetes.Interface, factory informers.SharedInformerFactory)

NewFakeMutatingDataSource returns a mock client and informer returning the given webhooks.

func NewFakeValidatingDataSource

func NewFakeValidatingDataSource(name string, webhooks []registrationv1.ValidatingWebhook, stopCh <-chan struct{}) (clientset kubernetes.Interface, factory informers.SharedInformerFactory)

NewFakeValidatingDataSource returns a mock client and informer returning the given webhooks.

func NewObjectInterfacesForTest

func NewObjectInterfacesForTest() admission.ObjectInterfaces

NewObjectInterfacesForTest returns an ObjectInterfaces appropriate for test cases in this file.

func NewPanickingAuthenticationInfoResolver

func NewPanickingAuthenticationInfoResolver(panicMessage string) webhook.AuthenticationInfoResolver

NewPanickingAuthenticationInfoResolver creates a fake AuthenticationInfoResolver that panics

func NewPanickingServiceResolver

func NewPanickingServiceResolver(panicMessage string) webhook.ServiceResolver

NewPanickingServiceResolver returns a static service resolver that panics.

func NewServiceResolver

func NewServiceResolver(base url.URL) webhook.ServiceResolver

NewServiceResolver returns a static service resolve that return the given URL or an error for the failResolve namespace.

func NewTestServer

func NewTestServer(t testing.TB) *httptest.Server

NewTestServer returns a webhook test HTTPS server with fixed webhook test certs.

func NewTestServerWithHandler

func NewTestServerWithHandler(t testing.TB, handler func(http.ResponseWriter, *http.Request)) *httptest.Server

NewTestServerWithHandler returns a webhook test HTTPS server which uses given handler function to handle requests

func Wrapper

Wrapper turns an AuthenticationInfoResolver into a AuthenticationInfoResolverWrapper that unconditionally returns the given AuthenticationInfoResolver.

Types

type CachedTest

type CachedTest struct {
	Name            string
	Webhooks        []registrationv1.ValidatingWebhook
	ExpectAllow     bool
	ExpectCacheMiss bool
}

CachedTest is a test case for the client manager.

func NewCachedClientTestcases

func NewCachedClientTestcases(url *url.URL) []CachedTest

NewCachedClientTestcases returns a set of client manager test cases.

type DurationTest

type DurationTest struct {
	Name                string
	Webhooks            []registrationv1.ValidatingWebhook
	InitContext         bool
	IsDryRun            bool
	ExpectedDurationSum time.Duration
	ExpectedDurationMax time.Duration
}

DurationTest is webhook duration test case, used both in mutating and validating plugin test cases.

func NewValidationDurationTestCases

func NewValidationDurationTestCases(url *url.URL) []DurationTest

NewValidationDurationTestCases returns test cases for webhook duration test

type FakeAttributes

type FakeAttributes struct {
	admission.Attributes
	// contains filtered or unexported fields
}

FakeAttributes decorate admission.Attributes. It's used to trace the added annotations.

func (*FakeAttributes) AddAnnotation

func (f *FakeAttributes) AddAnnotation(k, v string) error

AddAnnotation adds an annotation key value pair to FakeAttributes

func (*FakeAttributes) AddAnnotationWithLevel

func (f *FakeAttributes) AddAnnotationWithLevel(k, v string, _ auditinternal.Level) error

AddAnnotationWithLevel adds an annotation key value pair to FakeAttributes

func (*FakeAttributes) GetAnnotations

func (f *FakeAttributes) GetAnnotations(level auditinternal.Level) map[string]string

GetAnnotations reads annotations from FakeAttributes

type MutatingTest

type MutatingTest struct {
	Name                   string
	Webhooks               []registrationv1.MutatingWebhook
	Path                   string
	IsCRD                  bool
	IsDryRun               bool
	AdditionalLabels       map[string]string
	SkipBenchmark          bool
	ExpectLabels           map[string]string
	ExpectAllow            bool
	ErrorContains          string
	ExpectAnnotations      map[string]string
	ExpectStatusCode       int32
	ExpectReinvokeWebhooks map[string]bool
}

MutatingTest is a mutating webhook test case.

func ConvertToMutatingTestCases

func ConvertToMutatingTestCases(tests []ValidatingTest, configurationName string) []MutatingTest

ConvertToMutatingTestCases converts a validating test case to a mutating one for test purposes.

func NewMutatingTestCases

func NewMutatingTestCases(url *url.URL, configurationName string) []MutatingTest

NewMutatingTestCases returns test cases with a given base url. All test cases in NewMutatingTestCases have Patch set in AdmissionResponse. The test cases are only used by both MutatingAdmissionWebhook.

type ValidatingTest

type ValidatingTest struct {
	Name                   string
	Webhooks               []registrationv1.ValidatingWebhook
	Path                   string
	IsCRD                  bool
	IsDryRun               bool
	AdditionalLabels       map[string]string
	SkipBenchmark          bool
	ExpectLabels           map[string]string
	ExpectAllow            bool
	ErrorContains          string
	ExpectAnnotations      map[string]string
	ExpectStatusCode       int32
	ExpectReinvokeWebhooks map[string]bool
}

ValidatingTest is a validating webhook test case.

func NewNonMutatingPanicTestCases

func NewNonMutatingPanicTestCases(url *url.URL) []ValidatingTest

NewNonMutatingPanicTestCases returns test cases with a given base url. All test cases in NewNonMutatingTestCases have no Patch set in AdmissionResponse. The expected responses are set for panic handling.

func NewNonMutatingTestCases

func NewNonMutatingTestCases(url *url.URL) []ValidatingTest

NewNonMutatingTestCases returns test cases with a given base url. All test cases in NewNonMutatingTestCases have no Patch set in AdmissionResponse. The test cases are used by both MutatingAdmissionWebhook and ValidatingAdmissionWebhook.

Source Files

authentication_info_resolver.go service_resolver.go testcase.go webhook_server.go

Directories

PathSynopsis
pkg/admission/plugin/webhook/testing/main
Version
v0.33.1 (latest)
Published
May 15, 2025
Platform
linux/amd64
Imports
32 packages
Last checked
5 hours ago

Tools for package owners.