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 ¶
- func Field(obj interface{}, path string) (interface{}, bool)
- func FieldExists(obj *unstructured.Unstructured, path string) bool
- func FieldInt(obj *unstructured.Unstructured, path string) int64
- func FieldString(obj *unstructured.Unstructured, path string) string
- func FieldValue(obj *unstructured.Unstructured, path string) interface{}
- func KubeConfigFake() *clientcmdapi.Config
- func KubeconfigFile(t *testing.T, kubeconfig *api.Config) string
- func McpInitRequest() mcp.InitializeRequest
- func Must[T any](v T, err error) T
- func RandomPortAddress() (*net.TCPAddr, error)
- func ReadFile(path ...string) string
- func RestoreEnv(originalEnv []string)
- func WaitForCondition(timeout time.Duration, condition func() bool) error
- func WaitForHealthz(tcpAddr *net.TCPAddr) error
- func WaitForServer(tcpAddr *net.TCPAddr) error
- func WriteObject(w http.ResponseWriter, obj runtime.Object)
- type DiscoveryClientHandler
- type InOpenShiftHandler
- type McpClient
- type MockServer
- type StreamContext
- type StreamOptions
- type TokenReviewHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Field ¶ added in v0.0.55
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 McpInitRequest ¶ added in v0.0.54
func McpInitRequest() mcp.InitializeRequest
func RandomPortAddress ¶ added in v0.0.54
func RestoreEnv ¶ added in v0.0.54
func RestoreEnv(originalEnv []string)
func WaitForCondition ¶ added in v0.0.55
func WaitForHealthz ¶ added in v0.0.55
WaitForHealthz waits for the /healthz endpoint to return a non-404 response
func WaitForServer ¶ added in v0.0.54
func WriteObject ¶
func WriteObject(w http.ResponseWriter, obj runtime.Object)
Types ¶
type DiscoveryClientHandler ¶ added in v0.0.55
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
func NewMcpClient ¶ added in v0.0.51
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 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)