forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KubernetesCrudDispatcherPutTest.java
241 lines (225 loc) · 10.6 KB
/
KubernetesCrudDispatcherPutTest.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
/**
* 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.server.mock.crud;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.rbac.ClusterRole;
import io.fabric8.kubernetes.api.model.rbac.ClusterRoleBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.http.HttpRequest;
import io.fabric8.kubernetes.client.http.HttpResponse;
import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.server.mock.crud.crd.Owl;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.mockwebserver.Context;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import static io.fabric8.kubernetes.client.dsl.internal.OperationSupport.JSON;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class KubernetesCrudDispatcherPutTest {
private KubernetesMockServer server;
private KubernetesClient client;
@BeforeEach
void setUp() {
server = new KubernetesMockServer(new Context(Serialization.jsonMapper()),
new MockWebServer(), new HashMap<>(), new KubernetesCrudDispatcher(), false);
server.start();
client = server.createClient();
}
@AfterEach
void tearDown() {
client.close();
server.shutdown();
}
@Test
@DisplayName("replace, not existing, should return not found")
void replaceNotFound() {
// Given
final Resource<ConfigMap> resource = client.resources(ConfigMap.class)
.resource(new ConfigMapBuilder().withNewMetadata().withName("not-found").endMetadata().build());
// When
final KubernetesClientException result = assertThrows(KubernetesClientException.class, resource::replace);
// Then
assertThat(result)
.hasFieldOrPropertyWithValue("code", 404)
.hasMessageContaining("Not Found");
}
@Test
@DisplayName("HTTP PUT, not existing, should return not found")
void putNotFound() throws Exception {
// Given
final HttpClient httpClient = client.getHttpClient();
final HttpRequest request = httpClient.newHttpRequestBuilder()
.put(JSON, Serialization.asJson(
new ConfigMapBuilder().withNewMetadata().withName("not-found").endMetadata().build()))
.uri(server.url("/api/v1/namespaces/test/not-found"))
.build();
// When
final HttpResponse<String> result = httpClient.sendAsync(request, String.class).get(10, TimeUnit.SECONDS);
// Then
assertThat(result)
.returns(404, HttpResponse::code);
}
@Test
@DisplayName("HTTP PUT, name mismatch, should return bad request")
void putNameMismatch() throws Exception {
// Given
final ConfigMap original = client.resources(ConfigMap.class).resource(
new ConfigMapBuilder().withNewMetadata().withName("mismatched-name").endMetadata().build()).create();
final HttpClient httpClient = client.getHttpClient();
final ConfigMap cm = new ConfigMapBuilder(original).editMetadata().withName("different").endMetadata()
.build();
final HttpRequest request = httpClient.newHttpRequestBuilder()
.put(JSON, Serialization.asJson(cm))
.uri(server.url("/api/v1/namespaces/test/configmaps/mismatched-name"))
.build();
// When
final HttpResponse<String> result = httpClient.sendAsync(request, String.class).get(10, TimeUnit.SECONDS);
// Then
assertThat(result)
.extracting(HttpResponse::code).isEqualTo(400);
assertThat(result)
.extracting(HttpResponse::body).asString()
.contains("the name of the object (different) does not match the name on the URL (mismatched-name)");
}
@Test
@DisplayName("replace, namespaced, updates values incrementing generation preserving creation metadata")
void replaceUpdatesFieldsWithGenerationIncrement() {
// Given
final ConfigMap original = client.resources(ConfigMap.class).inNamespace("a-namespace").resource(
new ConfigMapBuilder().withNewMetadata().withName("name").endMetadata()
.addToData("key", "value").addToData("change", "me").build())
.create();
final ConfigMap replacement = new ConfigMapBuilder(original)
.editMetadata().withNamespace("").withCreationTimestamp(null).withUid(null).endMetadata()
.addToData("change", "changed").build();
// When
final ConfigMap replaced = client.resources(ConfigMap.class).inNamespace("a-namespace").resource(replacement)
.replace();
// Then
assertThat(replaced)
.hasFieldOrPropertyWithValue("metadata.name", "name")
.hasFieldOrPropertyWithValue("metadata.namespace", "a-namespace")
.hasFieldOrPropertyWithValue("metadata.uid", original.getMetadata().getUid())
.hasFieldOrPropertyWithValue("metadata.creationTimestamp",
original.getMetadata().getCreationTimestamp())
.hasFieldOrPropertyWithValue("metadata.resourceVersion", "2")
.hasFieldOrPropertyWithValue("metadata.generation", 2L)
.hasFieldOrPropertyWithValue("data.change", "changed");
}
@Test
@DisplayName("replace, cluster scoped, updates values incrementing generation")
void replaceClusterScopedUpdatesFieldsWithGenerationIncrement() {
// Given
final ClusterRole original = client.resources(ClusterRole.class).resource(
new ClusterRoleBuilder().withNewMetadata().withName("name").endMetadata()
.addNewRule().withResourceNames("Resource").endRule().build())
.create();
final ClusterRole replacement = new ClusterRoleBuilder(original)
.addNewRule().withResourceNames("Other").endRule().build();
// When
final ClusterRole replaced = client.resources(ClusterRole.class).resource(replacement).replace();
// Then
assertThat(replaced)
.hasFieldOrPropertyWithValue("metadata.resourceVersion", "2")
.hasFieldOrPropertyWithValue("metadata.generation", 2L)
.extracting(ClusterRole::getRules).asList().flatExtracting("resourceNames")
.containsExactlyInAnyOrder("Resource", "Other");
}
@Test
@DisplayName("replace, with same values, leaves resource unchanged and keeps resourceVersion and generation")
void replaceWithSameLeavesResourceUnchanged() {
// Given
final ConfigMap original = client.resources(ConfigMap.class).resource(
new ConfigMapBuilder().withNewMetadata().withName("name").endMetadata()
.addToData("key", "value").addToData("change", "me").build())
.create();
final ConfigMap replacement = new ConfigMapBuilder(original).build();
// When
final ConfigMap replaced = client.resources(ConfigMap.class).resource(replacement).replace();
// Then
assertThat(replaced)
.hasFieldOrPropertyWithValue("metadata.resourceVersion", "1")
.hasFieldOrPropertyWithValue("metadata.generation", 1L)
.hasFieldOrPropertyWithValue("data.change", "me");
}
@Test
@DisplayName("replaceStatus, updates status values incrementing resourceVersion, generation remains unchanged")
void replaceStatusUpdatesFieldsWithResourceVersionIncrement() {
// Given
final Pod original = client.resources(Pod.class).resource(
new PodBuilder().withNewMetadata().withName("name").endMetadata().build()).create();
final Pod replacement = new PodBuilder(original).editOrNewStatus().withMessage("new-message").endStatus().build();
// When
final Pod replaced = client.resources(Pod.class).resource(replacement).replaceStatus();
// Then
assertThat(replaced)
.hasFieldOrPropertyWithValue("metadata.resourceVersion", "2")
.hasFieldOrPropertyWithValue("metadata.generation", 1L)
.hasFieldOrPropertyWithValue("status.message", "new-message");
}
@Test
@DisplayName("replace, custom resource with status, ignores changes to the status stanza")
// PUT requests to the custom resource ignore changes to the status stanza.
void replaceCrIgnoreStatusChanges() {
// Given
client.apiextensions().v1().customResourceDefinitions().resource(Owl.toCrd()).create();
final Owl owl = new Owl();
owl.setMetadata(new ObjectMetaBuilder().withName("lincoln-march").build());
owl.getSpec().setSpecies("Barn owl");
client.resources(Owl.class).resource(owl).create();
owl.getSpec().setSpecies("Snowy owl");
owl.getStatus().setNotes("updated");
// When
final Owl result = client.resources(Owl.class).resource(owl).replace();
// Then
assertThat(result)
.hasFieldOrPropertyWithValue("spec.species", "Snowy owl")
.hasFieldOrPropertyWithValue("status.notes", null);
}
@Test
@DisplayName("replaceStatus, custom resource with status, ignores all changes except the status stanza")
// PUT requests to the /status subresource take a custom resource object and ignore changes to anything except the status stanza.
void replaceStatusCrIgnoreAllChangesExceptStatus() {
// Given
client.apiextensions().v1().customResourceDefinitions().resource(Owl.toCrd()).create();
final Owl owl = new Owl();
owl.setMetadata(new ObjectMetaBuilder().withName("lincoln-march").build());
owl.getSpec().setSpecies("Barn owl");
client.resources(Owl.class).resource(owl).create();
owl.getSpec().setSpecies("Snowy owl");
owl.getStatus().setNotes("updated");
// When
final Owl result = client.resources(Owl.class).resource(owl).replaceStatus();
// Then
assertThat(result)
.hasFieldOrPropertyWithValue("spec.species", "Barn owl")
.hasFieldOrPropertyWithValue("status.notes", "updated");
}
}