test

package
v0.0.55 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package test provides utilities for testing unstructured Kubernetes objects.

The primary functionality is JSONPath-like field access for unstructured.Unstructured objects, making test assertions more readable and maintainable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Field added in v0.0.55

func Field(obj interface{}, path string) (interface{}, bool)

Field is the core helper that traverses an unstructured object using JSONPath-like notation. It supports both dot notation (foo.bar) and array indexing (foo[0].bar). Returns (nil, false) if any intermediate field is nil, as we cannot traverse through nil.

func FieldExists added in v0.0.55

func FieldExists(obj *unstructured.Unstructured, path string) bool

FieldExists checks if a field exists at the given JSONPath-like path.

func FieldInt added in v0.0.55

func FieldInt(obj *unstructured.Unstructured, path string) int64

FieldInt retrieves an integer field from an unstructured object using JSONPath-like notation. Returns the integer value (int64), or 0 if not found or not an integer type (int, int64, int32).

IMPORTANT: This function cannot distinguish between "field doesn't exist", "field is nil", and "field exists with value 0". When asserting zero values (0), you should also verify the field exists using FieldExists:

s.True(test.FieldExists(obj, "spec.zeroValue"), "field should exist")
s.Equal(int64(0), test.FieldInt(obj, "spec.zeroValue"), "field should be 0")

Examples:

  • "spec.replicas"
  • "spec.ports[0].containerPort"

func FieldString added in v0.0.55

func FieldString(obj *unstructured.Unstructured, path string) string

FieldString retrieves a string field from an unstructured object using JSONPath-like notation. Returns the string value, or empty string if not found or not a string.

IMPORTANT: This function cannot distinguish between "field doesn't exist", "field is nil", and "field exists with empty string value". When asserting empty string values (""), you should also verify the field exists using FieldExists:

s.True(test.FieldExists(obj, "spec.emptyField"), "field should exist")
s.Equal("", test.FieldString(obj, "spec.emptyField"), "field should be empty string")

Examples:

  • "spec.runStrategy"
  • "spec.template.spec.volumes[0].containerDisk.image"
  • "spec.dataVolumeTemplates[0].spec.sourceRef.kind"

func FieldValue added in v0.0.55

func FieldValue(obj *unstructured.Unstructured, path string) interface{}

FieldValue retrieves any field value from an unstructured object using JSONPath-like notation. Returns nil if the field is not found. This is useful when you need the raw value without type conversion. Examples:

  • "spec.template.spec.containers[0]" - returns map[string]interface{}
  • "metadata.labels" - returns map[string]interface{}

func KubeConfigFake added in v0.0.51

func KubeConfigFake() *clientcmdapi.Config

func KubeconfigFile added in v0.0.53

func KubeconfigFile(t *testing.T, kubeconfig *api.Config) string

func McpInitRequest added in v0.0.54

func McpInitRequest() mcp.InitializeRequest

func Must added in v0.0.51

func Must[T any](v T, err error) T

func RandomPortAddress added in v0.0.54

func RandomPortAddress() (*net.TCPAddr, error)

func ReadFile added in v0.0.52

func ReadFile(path ...string) string

func RestoreEnv added in v0.0.54

func RestoreEnv(originalEnv []string)

func WaitForCondition added in v0.0.55

func WaitForCondition(timeout time.Duration, condition func() bool) error

func WaitForHealthz added in v0.0.55

func WaitForHealthz(tcpAddr *net.TCPAddr) error

WaitForHealthz waits for the /healthz endpoint to return a non-404 response

func WaitForServer added in v0.0.54

func WaitForServer(tcpAddr *net.TCPAddr) error

func WriteObject

func WriteObject(w http.ResponseWriter, obj runtime.Object)

Types

type DiscoveryClientHandler added in v0.0.55

type DiscoveryClientHandler struct {
	V1Resources []string
	Groups      []string
}

func (*DiscoveryClientHandler) ServeHTTP added in v0.0.55

func (h *DiscoveryClientHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

type InOpenShiftHandler added in v0.0.51

type InOpenShiftHandler struct {
}

func (*InOpenShiftHandler) ServeHTTP added in v0.0.51

func (h *InOpenShiftHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

type McpClient added in v0.0.51

type McpClient struct {
	*client.Client
	// contains filtered or unexported fields
}

func NewMcpClient added in v0.0.51

func NewMcpClient(t *testing.T, mcpHttpServer http.Handler, options ...transport.StreamableHTTPCOption) *McpClient

func (*McpClient) CallTool added in v0.0.51

func (m *McpClient) CallTool(name string, args map[string]interface{}) (*mcp.CallToolResult, error)

CallTool helper function to call a tool by name with arguments

func (*McpClient) Close added in v0.0.51

func (m *McpClient) Close()

type MockServer

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

func NewMockServer

func NewMockServer() *MockServer

func (*MockServer) Close

func (m *MockServer) Close()

func (*MockServer) Config

func (m *MockServer) Config() *rest.Config

func (*MockServer) Handle

func (m *MockServer) Handle(handler http.Handler)

func (*MockServer) Kubeconfig added in v0.0.51

func (m *MockServer) Kubeconfig() *api.Config

func (*MockServer) KubeconfigFile added in v0.0.51

func (m *MockServer) KubeconfigFile(t *testing.T) string

func (*MockServer) ResetHandlers added in v0.0.54

func (m *MockServer) ResetHandlers()

type StreamContext

type StreamContext struct {
	Closer       io.Closer
	StdinStream  io.ReadCloser
	StdoutStream io.WriteCloser
	StderrStream io.WriteCloser
	// contains filtered or unexported fields
}

func CreateHTTPStreams

func CreateHTTPStreams(w http.ResponseWriter, req *http.Request, opts *StreamOptions) (*StreamContext, error)

type StreamOptions

type StreamOptions struct {
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
}

type TokenReviewHandler added in v0.0.54

type TokenReviewHandler struct {
	DiscoveryClientHandler
	TokenReviewed bool
}

func NewTokenReviewHandler added in v0.0.55

func NewTokenReviewHandler() *TokenReviewHandler

func (*TokenReviewHandler) ServeHTTP added in v0.0.54

func (h *TokenReviewHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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