A JUnit 5 testing library that provides a Kubernetes mock server for testing Kubernetes client applications
—
Low-level HTTP request handlers that implement Kubernetes-compatible CRUD operations. These handlers process individual HTTP requests and manage resource lifecycle operations in CRUD mode.
Core functional interface that defines the contract for handling HTTP requests in CRUD mode.
@FunctionalInterface
public interface KubernetesCrudDispatcherHandler {
// HTTP status codes
int HTTP_UNPROCESSABLE_ENTITY = 422;
String KIND = "kind";
String STATUS = "status";
/**
* Handle an HTTP request and return appropriate response
* @param request - Recorded HTTP request with path, headers, and body
* @return MockResponse with status code and response body
* @throws KubernetesCrudDispatcherException - For validation or processing errors
*/
default MockResponse handle(RecordedRequest request) throws KubernetesCrudDispatcherException;
/**
* Handle an HTTP request with decomposed parameters
* @param path - Request path
* @param contentType - Content-Type header value
* @param requestBody - Request body content
* @return MockResponse with appropriate status and body
* @throws KubernetesCrudDispatcherException - For validation or processing errors
*/
MockResponse handle(String path, String contentType, String requestBody)
throws KubernetesCrudDispatcherException;
/**
* Validate that resource metadata matches the request path
* @param query - Parsed path attributes
* @param updatedResource - Resource JSON node
* @throws KubernetesCrudDispatcherException - If name or namespace mismatch
*/
default void validatePath(AttributeSet query, JsonNode updatedResource)
throws KubernetesCrudDispatcherException;
/**
* Validate resource version for optimistic concurrency control
* @param currentResource - Current resource state
* @param updatedResource - Updated resource with version
* @throws KubernetesCrudDispatcherException - For version conflicts
*/
default void validateResourceVersion(JsonNode currentResource, JsonNode updatedResource)
throws KubernetesCrudDispatcherException;
/**
* Validate and parse request body into a Kubernetes resource
* @param requestBody - JSON request body
* @return Parsed GenericKubernetesResource
* @throws KubernetesCrudDispatcherException - For invalid JSON or missing required fields
*/
default GenericKubernetesResource validateRequestBody(String requestBody)
throws KubernetesCrudDispatcherException;
/**
* Check if the request path targets a status subresource
* @param path - HTTP request path
* @return true if path ends with "/status"
*/
static boolean isStatusPath(String path);
/**
* Set or remove status field from a resource JSON node
* @param source - Resource JSON node to modify
* @param status - Status JSON node to set, or null to remove
*/
static void setStatus(JsonNode source, JsonNode status);
}Handles HTTP POST requests for resource creation, including metadata initialization and name generation.
public class PostHandler implements KubernetesCrudDispatcherHandler {
/**
* Create a new PostHandler with required dependencies
* @param attributeExtractor - For parsing paths and resources
* @param persistence - For storing and retrieving resources
*/
public PostHandler(KubernetesAttributesExtractor attributeExtractor,
KubernetesCrudPersistence persistence);
/**
* Handle resource creation request
* @param path - Creation path (e.g., "/api/v1/namespaces/default/pods")
* @param contentType - Request content type
* @param requestBody - Resource JSON to create
* @return MockResponse with created resource and 201 status
* @throws KubernetesCrudDispatcherException - For validation errors
*/
@Override
public MockResponse handle(String path, String contentType, String requestBody)
throws KubernetesCrudDispatcherException;
}Handles HTTP PUT requests for resource updates and replacements.
public class PutHandler implements KubernetesCrudDispatcherHandler {
/**
* Create a new PutHandler with required dependencies
* @param attributeExtractor - For parsing paths and resources
* @param persistence - For storing and retrieving resources
*/
public PutHandler(KubernetesAttributesExtractor attributeExtractor,
KubernetesCrudPersistence persistence);
/**
* Handle resource update/replacement request
* @param path - Update path (e.g., "/api/v1/namespaces/default/pods/my-pod")
* @param contentType - Request content type
* @param requestBody - Updated resource JSON
* @return MockResponse with updated resource and 200 status
* @throws KubernetesCrudDispatcherException - For validation or conflict errors
*/
@Override
public MockResponse handle(String path, String contentType, String requestBody)
throws KubernetesCrudDispatcherException;
}Handles HTTP PATCH requests supporting both JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7396).
public class PatchHandler implements KubernetesCrudDispatcherHandler {
/**
* Create a new PatchHandler with required dependencies
* @param attributeExtractor - For parsing paths and resources
* @param persistence - For storing and retrieving resources
*/
public PatchHandler(KubernetesAttributesExtractor attributeExtractor,
KubernetesCrudPersistence persistence);
/**
* Handle resource patch request with support for multiple patch formats
* @param path - Patch path (e.g., "/api/v1/namespaces/default/pods/my-pod")
* @param contentType - Patch content type (application/json-patch+json or application/merge-patch+json)
* @param requestBody - Patch operations or merge patch JSON
* @return MockResponse with patched resource and 200 status
* @throws KubernetesCrudDispatcherException - For validation or patch application errors
*/
@Override
public MockResponse handle(String path, String contentType, String requestBody)
throws KubernetesCrudDispatcherException;
}The PostHandler manages resource creation with these features:
metadata.uid, metadata.creationTimestamp, and metadata.resourceVersiongenerateName for automatic unique name creationThe PutHandler handles complete resource replacement:
resourceVersion to prevent conflicting updates/status pathThe PatchHandler supports multiple patch formats:
All handlers use KubernetesCrudDispatcherException for consistent error reporting:
try {
// Handler operation
} catch (KubernetesCrudDispatcherException e) {
// Returns appropriate HTTP status and Kubernetes Status object
return new MockResponse()
.setResponseCode(e.getCode())
.setBody(e.toStatusBody());
}PostHandler handler = new PostHandler(attributeExtractor, persistence);
String podJson = """
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "test-pod",
"namespace": "default"
},
"spec": {
"containers": [{"name": "app", "image": "nginx"}]
}
}
""";
MockResponse response = handler.handle(
"/api/v1/namespaces/default/pods",
"application/json",
podJson);
// Returns 201 Created with initialized metadataPutHandler handler = new PutHandler(attributeExtractor, persistence);
MockResponse response = handler.handle(
"/api/v1/namespaces/default/pods/test-pod",
"application/json",
updatedPodJson);
// Returns 200 OK with updated resourcePatchHandler handler = new PatchHandler(attributeExtractor, persistence);
// JSON Merge Patch
String mergePatch = """
{
"metadata": {
"labels": {
"version": "2.0"
}
}
}
""";
MockResponse response = handler.handle(
"/api/v1/namespaces/default/pods/test-pod",
"application/merge-patch+json",
mergePatch);
// Returns 200 OK with patched resourceInstall with Tessl CLI
npx tessl i tessl/maven-io-fabric8--kubernetes-server-mock