forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateOrReplaceResourceTest.java
311 lines (258 loc) · 16 KB
/
CreateOrReplaceResourceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.mock;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.DeleteOptions;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.StatusBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.net.HttpURLConnection;
import java.nio.charset.Charset;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnableKubernetesMockClient
class CreateOrReplaceResourceTest {
KubernetesMockServer server;
KubernetesClient client;
@Test
@DisplayName("Should replace an existing resource in Kubernetes Cluster")
void testResourceReplace() {
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).always();
server.expect().get().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).times(2);
server.expect().put().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
Pod pod = client.resource(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()).createOrReplace();
assertNotNull(pod);
assertEquals("12345", pod.getMetadata().getResourceVersion());
}
@Test
@DisplayName("Should create a new resource in Kubernetes Cluster")
void testResourceCreate() {
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
HasMetadata result = client.resource(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()).createOrReplace();
assertNotNull(result);
assertEquals("12345", result.getMetadata().getResourceVersion());
}
@Test
@DisplayName("Should throw Exception on failed create")
void testResourceCreateFailure() {
// Given
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_BAD_REQUEST, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").endMetadata().build()).once();
NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicable<Pod> podOperation = client.resource(new PodBuilder().withNewMetadata().withName("pod123").endMetadata().build());
// When
assertThrows(KubernetesClientException.class, podOperation::createOrReplace);
}
@Test
@DisplayName("Should create a new resource in Kubernetes Cluster")
void testCreate() {
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
Pod pod = client.pods().createOrReplace(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build());
assertNotNull(pod);
assertEquals("12345", pod.getMetadata().getResourceVersion());
}
@Test
@DisplayName("Shoulc replace an existing resource in Kubernetes Cluster")
void testReplace() {
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always();
server.expect().get().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).times(2);
server.expect().put().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
Pod pod = client.pods().createOrReplace(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build());
assertNotNull(pod);
assertEquals("12345", pod.getMetadata().getResourceVersion());
}
@Test
@DisplayName("Should throw exception on failed replace")
void testFailedReplace() {
// Given
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always();
server.expect().get().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).times(2);
server.expect().put().withPath("/api/v1/namespaces/test/pods/pod123").andReturn(HttpURLConnection.HTTP_BAD_REQUEST, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
final Pod toCreate = new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build();
final MixedOperation<Pod, PodList, PodResource<Pod>> pods = client.pods();
// When
assertThrows(KubernetesClientException.class, () -> pods.create(toCreate));
}
@Test
@DisplayName("Should create a new resource in Kubernetes Cluster")
void testResourceCreateFromLoad() throws Exception {
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
List<HasMetadata> result = client.load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")).createOrReplace();
assertNotNull(result);
assertEquals(1, result.size());
Pod pod = (Pod) result.get(0);
assertEquals("12345", pod.getMetadata().getResourceVersion());
RecordedRequest request = server.takeRequest();
assertEquals("/api/v1/namespaces/test/pods", request.getPath());
Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(request.getBody().inputStream());
assertEquals("nginx", requestPod.getMetadata().getName());
}
@Test
@DisplayName("Should replace an existing resource in Kubernetes Cluster")
void testResourceReplaceFromLoad() throws Exception {
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).always();
server.expect().get().withPath("/api/v1/namespaces/test/pods/nginx").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().withNewMetadata().withResourceVersion("12345").and().build()).times(2);
server.expect().put().withPath("/api/v1/namespaces/test/pods/nginx").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
List<HasMetadata> result = client.load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")).createOrReplace();
assertNotNull(result);
assertEquals(1, result.size());
Pod pod = (Pod) result.get(0);
assertEquals("12345", pod.getMetadata().getResourceVersion());
RecordedRequest request = server.getLastRequest();
assertEquals("/api/v1/namespaces/test/pods/nginx", request.getPath());
Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(request.getBody().inputStream());
assertEquals("nginx", requestPod.getMetadata().getName());
}
@Test
@DisplayName("Should create a new resource from yaml")
void testCreateFromLoad() throws Exception {
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
Pod pod = client.pods().load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")).createOrReplace();
assertNotNull(pod);
assertEquals("12345", pod.getMetadata().getResourceVersion());
Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(server.getLastRequest().getBody().inputStream());
assertEquals("nginx", requestPod.getMetadata().getName());
}
@Test
@DisplayName("Should update existing resource")
void testReplaceFromLoad() throws Exception {
server.expect().post().withPath("/api/v1/namespaces/test/pods").andReturn(HttpURLConnection.HTTP_CONFLICT, new PodBuilder().build()).always();
server.expect().get().withPath("/api/v1/namespaces/test/pods/nginx").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder().build()).times(2);
server.expect().put().withPath("/api/v1/namespaces/test/pods/nginx").andReturn(HttpURLConnection.HTTP_OK, new PodBuilder()
.withNewMetadata().withResourceVersion("12345").and().build()).once();
Pod pod = client.pods().load(getClass().getResourceAsStream("/test-pod-create-from-load.yml")).createOrReplace();
assertNotNull(pod);
assertEquals("12345", pod.getMetadata().getResourceVersion());
RecordedRequest request = server.getLastRequest();
assertEquals("/api/v1/namespaces/test/pods/nginx", request.getPath());
Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(request.getBody().inputStream());
assertEquals("nginx", requestPod.getMetadata().getName());
}
@Test
void testReplaceWithItemResourceVersion() throws Exception {
server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1")
.andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder()
.withNewMetadata().withResourceVersion("1001").and().build()).once();
// when you try to replace with a resource version specified, it will try the existing one first
ConfigMap map = client.configMaps().withName("map1").replace(new ConfigMapBuilder()
.withNewMetadata().withName("map1").withResourceVersion("1000").and().build());
assertNotNull(map);
assertEquals("1001", map.getMetadata().getResourceVersion());
ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(server.getLastRequest().getBody().inputStream());
assertEquals("1000", replacedMap.getMetadata().getResourceVersion());
}
@Test
void testReplaceWithItemResourceVersionRetry() throws Exception {
server.expect().get().withPath("/api/v1/namespaces/test/configmaps/map1").andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder()
.withNewMetadata().withResourceVersion("1000").and().build()).always();
server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1")
.andReturn(HttpURLConnection.HTTP_CONFLICT, new StatusBuilder().withCode(HttpURLConnection.HTTP_CONFLICT).build()).once();
server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1")
.andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder()
.withNewMetadata().withResourceVersion("1001").and().build()).once();
// when you try to replace with a resource version specified, it will try the existing one first
ConfigMap map = client.configMaps().withName("map1").replace(new ConfigMapBuilder()
.withNewMetadata().withName("map1").withResourceVersion("999").and().build());
assertNotNull(map);
assertEquals("1001", map.getMetadata().getResourceVersion());
ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(server.getLastRequest().getBody().inputStream());
assertEquals("1000", replacedMap.getMetadata().getResourceVersion());
}
@Test
@DisplayName("Should replace an existing ConfigMap without lock")
void testReplaceWithoutLock() throws Exception {
server.expect().get().withPath("/api/v1/namespaces/test/configmaps/map1").andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder()
.withNewMetadata().withResourceVersion("1000").and().build()).always();
server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1").andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder()
.withNewMetadata().withResourceVersion("1001").and().build()).once();
ConfigMap map = client.configMaps().withName("map1").replace(new ConfigMapBuilder()
.withNewMetadata().withName("map1").and().build());
assertNotNull(map);
assertEquals("1001", map.getMetadata().getResourceVersion());
ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(server.getLastRequest().getBody().inputStream());
assertEquals("1000", replacedMap.getMetadata().getResourceVersion());
}
@Test
@DisplayName("Should replace an existing resource with lock")
void testReplaceWithLock() throws Exception {
server.expect().put().withPath("/api/v1/namespaces/test/configmaps/map1").andReturn(HttpURLConnection.HTTP_OK, new ConfigMapBuilder()
.withNewMetadata().withResourceVersion("1001").and().build()).once();
ConfigMap map = client.configMaps().withName("map1")
.lockResourceVersion("900")
.replace(new ConfigMapBuilder().withNewMetadata().withName("map1").and().build());
assertNotNull(map);
assertEquals("1001", map.getMetadata().getResourceVersion());
ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(server.getLastRequest().getBody().inputStream());
assertEquals("900", replacedMap.getMetadata().getResourceVersion());
}
@Test
@DisplayName("Should delete an existing resource with lock")
void testDeleteWithLock() throws Exception {
server.expect().delete()
.withPath("/api/v1/namespaces/test/configmaps/map1")
.andReturn(HttpURLConnection.HTTP_OK, null)
.once();
Boolean deleted = client.configMaps().withName("map1")
.lockResourceVersion("800")
.delete();
assertTrue(deleted);
DeleteOptions options = new ObjectMapper().readerFor(DeleteOptions.class).readValue(server.getLastRequest().getBody().inputStream());
assertEquals("800", options.getPreconditions().getResourceVersion());
}
@Test
void testReplaceNonExistent() {
Resource<ConfigMap> resource = client.configMaps().withName("map1");
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName("map1").and().build();
final KubernetesClientException result = assertThrows(KubernetesClientException.class,
() -> resource.replace(configMap));
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.getCode());
}
@Test
void testPatchNonExistent() {
Resource<ConfigMap> resource = client.configMaps().withName("map1");
ConfigMap configMap = new ConfigMapBuilder().withNewMetadata().withName("map1").and().build();
final KubernetesClientException result = assertThrows(KubernetesClientException.class,
() -> resource.patch(configMap));
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.getCode());
}
}