forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CustomResourceCrudTest.java
244 lines (190 loc) · 9.98 KB
/
CustomResourceCrudTest.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
/**
* 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 io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.internal.RawCustomResourceOperationsImpl;
import io.fabric8.kubernetes.client.mock.crd.CronTab;
import io.fabric8.kubernetes.client.mock.crd.CronTabSpec;
import io.fabric8.kubernetes.client.mock.crd.CronTabStatus;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.internal.KubernetesDeserializer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnableKubernetesMockClient(crud = true)
class CustomResourceCrudTest {
KubernetesClient client;
private CustomResourceDefinition cronTabCrd;
@BeforeEach
void setUp() {
KubernetesDeserializer.registerCustomKind("stable.example.com/v1", "CronTab", CronTab.class);
cronTabCrd = client
.apiextensions().v1()
.customResourceDefinitions()
.load(getClass().getResourceAsStream("/crontab-crd.yml"))
.get();
client.apiextensions().v1().customResourceDefinitions().create(cronTabCrd);
}
@Test
void testCrud() {
CronTab cronTab1 = createCronTab("my-new-cron-object", "* * * * */5", 3, "my-awesome-cron-image");
CronTab cronTab2 = createCronTab("my-second-cron-object", "* * * * */4", 2, "my-second-cron-image");
CronTab cronTab3 = createCronTab("my-third-cron-object", "* * * * */3", 1, "my-third-cron-image");
MixedOperation<CronTab, KubernetesResourceList<CronTab>, Resource<CronTab>> cronTabClient = client.customResources(CronTab.class);
cronTabClient.inNamespace("test-ns").create(cronTab1);
cronTabClient.inNamespace("test-ns").create(cronTab2);
cronTabClient.inNamespace("test-ns").create(cronTab3);
KubernetesResourceList<CronTab> cronTabList = cronTabClient.inNamespace("test-ns").list();
assertNotNull(cronTabList);
assertEquals(3, cronTabList.getItems().size());
CronTab fromServerCronTab = cronTabClient.inNamespace("test-ns").withName("my-new-cron-object").get();
assertNotNull(fromServerCronTab);
assertCronTab(fromServerCronTab, "my-new-cron-object", "* * * * */5", 3, "my-awesome-cron-image");
CronTab fromServerCronTab2 = cronTabClient.inNamespace("test-ns").withName("my-second-cron-object").get();
assertNotNull(fromServerCronTab2);
assertCronTab(fromServerCronTab2, "my-second-cron-object", "* * * * */4", 2, "my-second-cron-image");
CronTab fromServerCronTab3 = cronTabClient.inNamespace("test-ns").withName("my-third-cron-object").get();
assertNotNull(fromServerCronTab3);
assertCronTab(fromServerCronTab3, "my-third-cron-object", "* * * * */3", 1, "my-third-cron-image");
cronTabClient.inNamespace("test-ns").withName("my-third-cron-object").delete();
cronTabList = cronTabClient.inNamespace("test-ns").list();
assertEquals(2, cronTabList.getItems().size());
}
@Test
void testCreateOrReplaceRaw() throws IOException {
RawCustomResourceOperationsImpl raw = client.customResource(CustomResourceDefinitionContext.fromCrd(cronTabCrd));
Map<String, Object> object = new HashMap<>();
Map<String, Object> metadata = new HashMap<>();
metadata.put("name", "foo");
object.put("apiVersion", "stable.example.com/v1");
object.put("kind", "CronTab");
object.put("metadata", metadata);
object.put("spec", new HashMap<>());
Map<String, Object> created = raw.createOrReplace(object);
assertEquals(Collections.emptyMap(), created.get("spec"));
assertTrue(((Map)created.get("metadata")).entrySet().containsAll(metadata.entrySet()));
object.put("spec", Collections.singletonMap("image", "value"));
Map<String, Object> updated = raw.createOrReplace(object);
assertNotEquals(created, updated);
assertEquals(Collections.singletonMap("image", "value"), updated.get("spec"));
}
@Test
void testCrudWithDashSymbolInCRDName() {
CronTab cronTab1 = createCronTab("my-new-cron-object", "* * * * */5", 3, "my-awesome-cron-image");
CronTab cronTab2 = createCronTab("my-second-cron-object", "* * * * */4", 2, "my-second-cron-image");
CronTab cronTab3 = createCronTab("my-third-cron-object", "* * * * */3", 1, "my-third-cron-image");
MixedOperation<CronTab, KubernetesResourceList<CronTab>, Resource<CronTab>> cronTabClient = client
.customResources(CronTab.class);
cronTabClient.inNamespace("test-ns").create(cronTab1);
cronTabClient.inNamespace("test-ns").create(cronTab2);
cronTabClient.inNamespace("test-ns").create(cronTab3);
KubernetesResourceList<CronTab> cronTabList = cronTabClient.inNamespace("test-ns").list();
assertNotNull(cronTabList);
assertEquals(3, cronTabList.getItems().size());
CronTab fromServerCronTab = cronTabClient.inNamespace("test-ns").withName("my-new-cron-object").get();
assertNotNull(fromServerCronTab);
assertCronTab(fromServerCronTab, "my-new-cron-object", "* * * * */5", 3, "my-awesome-cron-image");
CronTab fromServerCronTab2 = cronTabClient.inNamespace("test-ns").withName("my-second-cron-object").get();
assertNotNull(fromServerCronTab2);
assertCronTab(fromServerCronTab2, "my-second-cron-object", "* * * * */4", 2, "my-second-cron-image");
CronTab fromServerCronTab3 = cronTabClient.inNamespace("test-ns").withName("my-third-cron-object").get();
assertNotNull(fromServerCronTab3);
assertCronTab(fromServerCronTab3, "my-third-cron-object", "* * * * */3", 1, "my-third-cron-image");
cronTabClient.inNamespace("test-ns").withName("my-third-cron-object").delete();
cronTabList = cronTabClient.inNamespace("test-ns").list();
assertEquals(2, cronTabList.getItems().size());
}
@Test
void testStatusSubresourceHandling() {
CronTab cronTab = createCronTab("my-new-cron-object", "* * * * */5", 3, "my-awesome-cron-image");
CronTabStatus status = new CronTabStatus();
status.setReplicas(1);
cronTab.setStatus(status);
NonNamespaceOperation<CronTab, KubernetesResourceList<CronTab>, Resource<CronTab>> cronTabClient = client
.customResources(CronTab.class).inNamespace("test-ns");
CronTab result = cronTabClient.create(cronTab);
// should be null after create
assertNull(result.getStatus());
Map<String, String> labels = new HashMap<>();
labels.put("app", "core");
cronTab.getMetadata().setLabels(labels);
result = cronTabClient.replace(cronTab);
String originalUid = result.getMetadata().getUid();
// should be null after replace
assertNull(result.getStatus());
assertNotNull(cronTabClient.updateStatus(cronTab).getStatus());
labels.put("other", "label");
cronTab.setStatus(null);
result = cronTabClient.replace(cronTab);
// should retain the existing
assertNotNull(result.getStatus());
labels.put("another", "label");
result = cronTabClient.withName(cronTab.getMetadata().getName()).patch(cronTab);
// should retain the existing
assertNotNull(result.getStatus());
// should have accumulated all labels
assertEquals(new HashSet<String>(Arrays.asList("app", "other", "another")), result.getMetadata().getLabels().keySet());
assertEquals(originalUid, result.getMetadata().getUid());
}
@Test
void testStatusPatch() {
CronTab cronTab = createCronTab("my-new-cron-object", "* * * * */5", 3, "my-awesome-cron-image");
CronTabStatus status = new CronTabStatus();
status.setReplicas(1);
cronTab.setStatus(status);
NonNamespaceOperation<CronTab, KubernetesResourceList<CronTab>, Resource<CronTab>> cronTabClient = client
.resources(CronTab.class).inNamespace("test-ns");
CronTab result = cronTabClient.create(cronTab);
// should be null after create
assertNull(result.getStatus());
result.setStatus(status);
result = cronTabClient.withName(cronTab.getMetadata().getName()).patchStatus(result);
assertNotNull(result.getStatus());
}
void assertCronTab(CronTab cronTab, String name, String cronTabSpec, int replicas, String image) {
assertEquals(name, cronTab.getMetadata().getName());
assertEquals(cronTabSpec, cronTab.getSpec().getCronSpec());
assertEquals(replicas, cronTab.getSpec().getReplicas());
assertEquals(image, cronTab.getSpec().getImage());
}
CronTab createCronTab(String name, String cronTabSpecStr, int replicas, String image) {
CronTab cronTab = new CronTab();
cronTab.setMetadata(new ObjectMetaBuilder().withName(name).build());
CronTabSpec cronTabSpec = new CronTabSpec();
cronTabSpec.setCronSpec(cronTabSpecStr);
cronTabSpec.setImage(image);
cronTabSpec.setReplicas(replicas);
cronTab.setSpec(cronTabSpec);
return cronTab;
}
}