CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-fabric8--kubernetes-server-mock

A JUnit 5 testing library that provides a Kubernetes mock server for testing Kubernetes client applications

Pending
Overview
Eval results
Files

mock-server-management.mddocs/

Mock Server Management

Core mock server lifecycle management including initialization, configuration, and client creation. Essential for all testing scenarios.

Capabilities

KubernetesMockServer Class

Main mock server implementation providing full Kubernetes API mocking capabilities.

/**
 * Main mock server implementation for Kubernetes API testing
 * Extends DefaultMockServer and provides Kubernetes-specific functionality
 */
public class KubernetesMockServer extends DefaultMockServer 
    implements Resetable, CustomResourceAware {
    
    /** Default constructor using HTTPS */
    public KubernetesMockServer();
    
    /** 
     * Constructor with HTTPS option
     * @param useHttps whether to use HTTPS protocol
     */
    public KubernetesMockServer(boolean useHttps);
    
    /**
     * Constructor with full configuration
     * @param server MockWebServer instance
     * @param responses Map of request-response expectations
     * @param useHttps whether to use HTTPS protocol
     */
    public KubernetesMockServer(MockWebServer server, 
        Map<ServerRequest, Queue<ServerResponse>> responses, boolean useHttps);
    
    /**
     * Constructor with context configuration
     * @param context Serialization context
     * @param server MockWebServer instance
     * @param responses Map of request-response expectations
     * @param useHttps whether to use HTTPS protocol
     */
    public KubernetesMockServer(Context context, MockWebServer server,
        Map<ServerRequest, Queue<ServerResponse>> responses, boolean useHttps);
    
    /**
     * Constructor with dispatcher configuration
     * @param context Serialization context
     * @param server MockWebServer instance
     * @param responses Map of request-response expectations
     * @param dispatcher Custom request dispatcher
     * @param useHttps whether to use HTTPS protocol
     */
    public KubernetesMockServer(Context context, MockWebServer server,
        Map<ServerRequest, Queue<ServerResponse>> responses,
        Dispatcher dispatcher, boolean useHttps);
    
    /**
     * Full constructor with version info
     * @param context Serialization context
     * @param server MockWebServer instance
     * @param responses Map of request-response expectations
     * @param dispatcher Custom request dispatcher
     * @param useHttps whether to use HTTPS protocol
     * @param versionInfo Kubernetes version information
     */
    public KubernetesMockServer(Context context, MockWebServer server,
        Map<ServerRequest, Queue<ServerResponse>> responses,
        Dispatcher dispatcher, boolean useHttps, VersionInfo versionInfo);
}

Server Lifecycle Management

Methods for controlling the mock server lifecycle.

/**
 * Initialize and start the server on a random port
 * Sets up default API endpoints and version information
 */
public void init();

/**
 * Initialize and start the server on specific address and port
 * @param address InetAddress to bind to
 * @param port Port number to bind to
 */
public void init(InetAddress address, int port);

/**
 * Called when the server starts
 * Sets up default Kubernetes API endpoints and version information
 * Override to customize server initialization behavior
 */
@Override
public void onStart();

/**
 * Shutdown the server and clean up resources
 * Equivalent to shutdown() from parent class
 */
public void destroy();

/**
 * Reset the server to its initial state
 * Clears all expectations and resets internal state
 */
public void reset();

Usage Examples:

// Basic server setup
KubernetesMockServer server = new KubernetesMockServer();
server.init();

try {
    // Server is now running and ready for requests
    // Use server.createClient() to get a configured client
} finally {
    server.destroy();
}

// Server with specific configuration
KubernetesMockServer server = new KubernetesMockServer(false); // HTTP only
server.init(InetAddress.getLocalHost(), 8080);

// Reset server state between tests
server.reset(); // Clears all expectations and data

Client Creation

Methods for creating properly configured Kubernetes clients that connect to the mock server.

/**
 * Create a Kubernetes client configured for this mock server
 * Uses default configuration with trust certificates enabled
 * @return NamespacedKubernetesClient instance
 */
public NamespacedKubernetesClient createClient();

/**
 * Create a Kubernetes client with custom HTTP client factory
 * @param factory HttpClient.Factory for custom HTTP configuration
 * @return NamespacedKubernetesClient instance
 */
public NamespacedKubernetesClient createClient(HttpClient.Factory factory);

/**
 * Create a Kubernetes client with custom builder configuration
 * Allows full customization of the KubernetesClientBuilder
 * @param kubernetesClientBuilderCustomizer Consumer to customize the builder
 * @return NamespacedKubernetesClient instance
 */
public NamespacedKubernetesClient createClient(
    Consumer<KubernetesClientBuilder> kubernetesClientBuilderCustomizer);

Usage Examples:

// Basic client creation
KubernetesClient client = server.createClient();

// Client with custom HTTP factory
HttpClient.Factory customFactory = new MyCustomHttpClientFactory();
KubernetesClient client = server.createClient(customFactory);

// Client with full customization
KubernetesClient client = server.createClient(builder -> {
    builder.withRequestTimeout(30000)
           .withConnectionTimeout(10000)
           .withNamespace("my-test-namespace");
});

// Using in try-with-resources
try (KubernetesClient client = server.createClient()) {
    // Perform operations
    client.pods().list();
}

Server Configuration

Methods for configuring server behavior and supported APIs.

/**
 * Set the Kubernetes version information returned by /version endpoint
 * @param versionInfo VersionInfo object with major/minor version
 */
public void setVersionInfo(VersionInfo versionInfo);

/**
 * Configure API groups to exclude from support
 * Affects client behavior for hasApiGroup and supports methods
 * @param unsupported Array of API group patterns to exclude
 */
public void setUnsupported(String... unsupported);

/**
 * Remove all recorded expectations
 * Useful for cleaning state between test methods
 */
public void clearExpectations();

/**
 * Get the root paths supported by this server
 * @return Array of root API paths
 */
public String[] getRootPaths();

/**
 * Add support for a custom resource
 * Ensures the server responds appropriately to custom resource requests
 * @param rdc CustomResourceDefinitionContext for the custom resource
 */
public void expectCustomResource(CustomResourceDefinitionContext rdc);

Usage Examples:

// Set custom Kubernetes version
VersionInfo version = new VersionInfo.Builder()
    .withMajor("1")
    .withMinor("28")
    .withGitVersion("v1.28.0")
    .build();
server.setVersionInfo(version);

// Exclude OpenShift APIs from support
server.setUnsupported("route.openshift.io", "image.openshift.io/*");

// Add custom resource support
CustomResourceDefinitionContext crdContext = new CustomResourceDefinitionContext.Builder()
    .withGroup("example.com")
    .withVersion("v1")
    .withKind("MyResource")
    .withPlural("myresources")
    .withNamespaceScoped(true)
    .build();
server.expectCustomResource(crdContext);

// Clear expectations between tests
server.clearExpectations();

Configuration Initialization

Internal configuration setup for created clients.

/**
 * Initialize configuration for created clients
 * Sets up default namespace, authentication, and connection settings
 * @return Config object for client initialization
 */
protected Config initConfig();

This method is used internally by the createClient() methods to set up appropriate configuration including:

  • Mock server URL as the master URL
  • Trust certificates enabled for HTTPS
  • Default namespace set to "test"
  • Mock authentication token
  • HTTP/2 disabled for compatibility

Install with Tessl CLI

npx tessl i tessl/maven-io-fabric8--kubernetes-server-mock

docs

attribute-extraction.md

crud-handlers.md

crud-operations.md

custom-resources.md

index.md

junit-integration.md

mock-server-management.md

websocket-operations.md

tile.json