0
# Mock Server Management
1
2
Core mock server lifecycle management including initialization, configuration, and client creation. Essential for all testing scenarios.
3
4
## Capabilities
5
6
### KubernetesMockServer Class
7
8
Main mock server implementation providing full Kubernetes API mocking capabilities.
9
10
```java { .api }
11
/**
12
* Main mock server implementation for Kubernetes API testing
13
* Extends DefaultMockServer and provides Kubernetes-specific functionality
14
*/
15
public class KubernetesMockServer extends DefaultMockServer
16
implements Resetable, CustomResourceAware {
17
18
/** Default constructor using HTTPS */
19
public KubernetesMockServer();
20
21
/**
22
* Constructor with HTTPS option
23
* @param useHttps whether to use HTTPS protocol
24
*/
25
public KubernetesMockServer(boolean useHttps);
26
27
/**
28
* Constructor with full configuration
29
* @param server MockWebServer instance
30
* @param responses Map of request-response expectations
31
* @param useHttps whether to use HTTPS protocol
32
*/
33
public KubernetesMockServer(MockWebServer server,
34
Map<ServerRequest, Queue<ServerResponse>> responses, boolean useHttps);
35
36
/**
37
* Constructor with context configuration
38
* @param context Serialization context
39
* @param server MockWebServer instance
40
* @param responses Map of request-response expectations
41
* @param useHttps whether to use HTTPS protocol
42
*/
43
public KubernetesMockServer(Context context, MockWebServer server,
44
Map<ServerRequest, Queue<ServerResponse>> responses, boolean useHttps);
45
46
/**
47
* Constructor with dispatcher configuration
48
* @param context Serialization context
49
* @param server MockWebServer instance
50
* @param responses Map of request-response expectations
51
* @param dispatcher Custom request dispatcher
52
* @param useHttps whether to use HTTPS protocol
53
*/
54
public KubernetesMockServer(Context context, MockWebServer server,
55
Map<ServerRequest, Queue<ServerResponse>> responses,
56
Dispatcher dispatcher, boolean useHttps);
57
58
/**
59
* Full constructor with version info
60
* @param context Serialization context
61
* @param server MockWebServer instance
62
* @param responses Map of request-response expectations
63
* @param dispatcher Custom request dispatcher
64
* @param useHttps whether to use HTTPS protocol
65
* @param versionInfo Kubernetes version information
66
*/
67
public KubernetesMockServer(Context context, MockWebServer server,
68
Map<ServerRequest, Queue<ServerResponse>> responses,
69
Dispatcher dispatcher, boolean useHttps, VersionInfo versionInfo);
70
}
71
```
72
73
### Server Lifecycle Management
74
75
Methods for controlling the mock server lifecycle.
76
77
```java { .api }
78
/**
79
* Initialize and start the server on a random port
80
* Sets up default API endpoints and version information
81
*/
82
public void init();
83
84
/**
85
* Initialize and start the server on specific address and port
86
* @param address InetAddress to bind to
87
* @param port Port number to bind to
88
*/
89
public void init(InetAddress address, int port);
90
91
/**
92
* Called when the server starts
93
* Sets up default Kubernetes API endpoints and version information
94
* Override to customize server initialization behavior
95
*/
96
@Override
97
public void onStart();
98
99
/**
100
* Shutdown the server and clean up resources
101
* Equivalent to shutdown() from parent class
102
*/
103
public void destroy();
104
105
/**
106
* Reset the server to its initial state
107
* Clears all expectations and resets internal state
108
*/
109
public void reset();
110
```
111
112
**Usage Examples:**
113
114
```java
115
// Basic server setup
116
KubernetesMockServer server = new KubernetesMockServer();
117
server.init();
118
119
try {
120
// Server is now running and ready for requests
121
// Use server.createClient() to get a configured client
122
} finally {
123
server.destroy();
124
}
125
126
// Server with specific configuration
127
KubernetesMockServer server = new KubernetesMockServer(false); // HTTP only
128
server.init(InetAddress.getLocalHost(), 8080);
129
130
// Reset server state between tests
131
server.reset(); // Clears all expectations and data
132
```
133
134
### Client Creation
135
136
Methods for creating properly configured Kubernetes clients that connect to the mock server.
137
138
```java { .api }
139
/**
140
* Create a Kubernetes client configured for this mock server
141
* Uses default configuration with trust certificates enabled
142
* @return NamespacedKubernetesClient instance
143
*/
144
public NamespacedKubernetesClient createClient();
145
146
/**
147
* Create a Kubernetes client with custom HTTP client factory
148
* @param factory HttpClient.Factory for custom HTTP configuration
149
* @return NamespacedKubernetesClient instance
150
*/
151
public NamespacedKubernetesClient createClient(HttpClient.Factory factory);
152
153
/**
154
* Create a Kubernetes client with custom builder configuration
155
* Allows full customization of the KubernetesClientBuilder
156
* @param kubernetesClientBuilderCustomizer Consumer to customize the builder
157
* @return NamespacedKubernetesClient instance
158
*/
159
public NamespacedKubernetesClient createClient(
160
Consumer<KubernetesClientBuilder> kubernetesClientBuilderCustomizer);
161
```
162
163
**Usage Examples:**
164
165
```java
166
// Basic client creation
167
KubernetesClient client = server.createClient();
168
169
// Client with custom HTTP factory
170
HttpClient.Factory customFactory = new MyCustomHttpClientFactory();
171
KubernetesClient client = server.createClient(customFactory);
172
173
// Client with full customization
174
KubernetesClient client = server.createClient(builder -> {
175
builder.withRequestTimeout(30000)
176
.withConnectionTimeout(10000)
177
.withNamespace("my-test-namespace");
178
});
179
180
// Using in try-with-resources
181
try (KubernetesClient client = server.createClient()) {
182
// Perform operations
183
client.pods().list();
184
}
185
```
186
187
### Server Configuration
188
189
Methods for configuring server behavior and supported APIs.
190
191
```java { .api }
192
/**
193
* Set the Kubernetes version information returned by /version endpoint
194
* @param versionInfo VersionInfo object with major/minor version
195
*/
196
public void setVersionInfo(VersionInfo versionInfo);
197
198
/**
199
* Configure API groups to exclude from support
200
* Affects client behavior for hasApiGroup and supports methods
201
* @param unsupported Array of API group patterns to exclude
202
*/
203
public void setUnsupported(String... unsupported);
204
205
/**
206
* Remove all recorded expectations
207
* Useful for cleaning state between test methods
208
*/
209
public void clearExpectations();
210
211
/**
212
* Get the root paths supported by this server
213
* @return Array of root API paths
214
*/
215
public String[] getRootPaths();
216
217
/**
218
* Add support for a custom resource
219
* Ensures the server responds appropriately to custom resource requests
220
* @param rdc CustomResourceDefinitionContext for the custom resource
221
*/
222
public void expectCustomResource(CustomResourceDefinitionContext rdc);
223
```
224
225
**Usage Examples:**
226
227
```java
228
// Set custom Kubernetes version
229
VersionInfo version = new VersionInfo.Builder()
230
.withMajor("1")
231
.withMinor("28")
232
.withGitVersion("v1.28.0")
233
.build();
234
server.setVersionInfo(version);
235
236
// Exclude OpenShift APIs from support
237
server.setUnsupported("route.openshift.io", "image.openshift.io/*");
238
239
// Add custom resource support
240
CustomResourceDefinitionContext crdContext = new CustomResourceDefinitionContext.Builder()
241
.withGroup("example.com")
242
.withVersion("v1")
243
.withKind("MyResource")
244
.withPlural("myresources")
245
.withNamespaceScoped(true)
246
.build();
247
server.expectCustomResource(crdContext);
248
249
// Clear expectations between tests
250
server.clearExpectations();
251
```
252
253
### Configuration Initialization
254
255
Internal configuration setup for created clients.
256
257
```java { .api }
258
/**
259
* Initialize configuration for created clients
260
* Sets up default namespace, authentication, and connection settings
261
* @return Config object for client initialization
262
*/
263
protected Config initConfig();
264
```
265
266
This method is used internally by the `createClient()` methods to set up appropriate configuration including:
267
- Mock server URL as the master URL
268
- Trust certificates enabled for HTTPS
269
- Default namespace set to "test"
270
- Mock authentication token
271
- HTTP/2 disabled for compatibility