-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
RawCustomResourceIT.java
282 lines (236 loc) · 11.6 KB
/
RawCustomResourceIT.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
/**
* 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;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import io.fabric8.commons.ClusterEntity;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.apache.commons.io.FileUtils;
import org.arquillian.cube.kubernetes.api.Session;
import org.arquillian.cube.kubernetes.impl.requirement.RequiresKubernetes;
import org.arquillian.cube.requirement.ArquillianConditionalRunner;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
@RunWith(ArquillianConditionalRunner.class)
@RequiresKubernetes
public class RawCustomResourceIT {
@ArquillianResource
public KubernetesClient client;
@ArquillianResource
public Session session;
private String currentNamespace;
private CustomResourceDefinitionContext customResourceDefinitionContext;
@BeforeClass
public static void initCrd() {
// Create a Custom Resource Definition Animals:
ClusterEntity.apply(RawCustomResourceIT.class.getResourceAsStream("/test-rawcustomresource-definition.yml"));
}
@Before
public void initCustomResourceDefinitionContextAndNamespace() {
currentNamespace = session.getNamespace();
customResourceDefinitionContext = new CustomResourceDefinitionContext.Builder()
.withName("animals.jungle.example.com")
.withGroup("jungle.example.com")
.withVersion("v1")
.withPlural("animals")
.withScope("Namespaced")
.build();
}
@Test
public void testCrud() throws IOException {
// Test Create via file
Map<String, Object> object = client.customResource(customResourceDefinitionContext).create(currentNamespace, getClass().getResourceAsStream("/test-rawcustomresource.yml"));
assertThat(((HashMap<String, String>)object.get("metadata")).get("name")).isEqualTo("otter");
// Test Create via raw json string
String rawJsonCustomResourceObj = "{\"apiVersion\":\"jungle.example.com/v1\"," +
"\"kind\":\"Animal\",\"metadata\": {\"name\": \"walrus\"}," +
"\"spec\": {\"image\": \"my-awesome-walrus-image\"}}";
object = client.customResource(customResourceDefinitionContext).createOrReplace(currentNamespace, rawJsonCustomResourceObj);
assertAnimal(object, "walrus", "my-awesome-walrus-image");
// Test replace with object
((HashMap<String, String>)object.get("spec")).put("image", "new-walrus-image");
object = client.customResource(customResourceDefinitionContext).createOrReplace(currentNamespace, object);
assertAnimal(object, "walrus", "new-walrus-image");
// Test Get:
object = client.customResource(customResourceDefinitionContext).get(currentNamespace, "otter");
assertThat(((HashMap<String, String>)object.get("metadata")).get("name")).isEqualTo("otter");
// Test List:
Map<String, Object> list = client.customResource(customResourceDefinitionContext).list(currentNamespace);
assertThat(((ArrayList<Object>)list.get("items")).size()).isGreaterThanOrEqualTo(2);
// List with labels:
list = client.customResource(customResourceDefinitionContext).list(currentNamespace, Collections.singletonMap("foo", "bar"));
assertThat(((ArrayList<Object>)list.get("items")).size()).isGreaterThanOrEqualTo(1);
// Test Update
object = client.customResource(customResourceDefinitionContext).get(currentNamespace, "walrus");
((HashMap<String, Object>)object.get("spec")).put("image", "my-updated-awesome-walrus-image");
object = client.customResource(customResourceDefinitionContext).edit(currentNamespace, "walrus", new ObjectMapper().writeValueAsString(object));
assertAnimal(object, "walrus", "my-updated-awesome-walrus-image");
// Test Delete:
client.customResource(customResourceDefinitionContext).delete(currentNamespace, "otter");
client.customResource(customResourceDefinitionContext).delete(currentNamespace);
}
@Test
public void testCrudUsingMap() throws IOException {
// Create
String name = "bison";
Map<String, Object> bison = createNewAnimal(name, "wisent");
bison = client.customResource(customResourceDefinitionContext).create(currentNamespace, bison);
assertAnimal(bison, "bison", "wisent");
// Read
bison = client.customResource(customResourceDefinitionContext).get(currentNamespace, name);
assertAnimal(bison, "bison", "wisent");
// Update
((Map<String, Object>)bison.get("spec")).put("image", "bison-bonasus");
bison = client.customResource(customResourceDefinitionContext).edit(currentNamespace, name, bison);
assertAnimal(bison, "bison", "bison-bonasus");
// Delete
Map<String, Object> deletionStatusMap = client.customResource(customResourceDefinitionContext).delete(currentNamespace, name);
assertDeletionStatus(deletionStatusMap, "Success");
}
@Test
public void testCrudUsingInputStream() throws IOException {
// Create
String name = "hippo";
InputStream hippoInputStream = getClass().getResourceAsStream("/rawcustomresourceit-crud-inputstream.yml");
Map<String, Object> hippoCr = client.customResource(customResourceDefinitionContext).create(currentNamespace, hippoInputStream);
assertAnimal(hippoCr, name, "Hippopotamidae");
// Read
hippoCr = client.customResource(customResourceDefinitionContext).get(currentNamespace, name);
assertAnimal(hippoCr, name, "Hippopotamidae");
// Update
((Map<String, Object>)hippoCr.get("spec")).put("image", "river-hippo");
File updatedHippoManifest = Files.createTempFile("hippo", "yml").toFile();
FileUtils.writeStringToFile(updatedHippoManifest, Serialization.jsonMapper().writeValueAsString(hippoCr));
hippoCr = client.customResource(customResourceDefinitionContext).edit(currentNamespace, name, new FileInputStream(updatedHippoManifest));
assertAnimal(hippoCr, name, "river-hippo");
// Delete
Map<String, Object> deletionStatusMap = client.customResource(customResourceDefinitionContext).delete(currentNamespace, name);
assertDeletionStatus(deletionStatusMap, "Success");
}
@Test
public void testCrudUsingString() throws IOException {
// Create
String name = "rhino";
String rhino = "{\"apiVersion\": \"jungle.example.com/v1\"," +
" \"kind\": \"Animal\"," +
" \"metadata\": {\"name\": \"rhino\"}," +
" \"spec\": {\"image\": \"Rhinocerotidae\"}}";
Map<String, Object> rhinoCr = client.customResource(customResourceDefinitionContext).create(currentNamespace, rhino);
assertAnimal(rhinoCr, name, "Rhinocerotidae");
// Read
rhinoCr = client.customResource(customResourceDefinitionContext).get(currentNamespace, name);
assertAnimal(rhinoCr, name, "Rhinocerotidae");
// Update
((Map<String, Object>)rhinoCr.get("spec")).put("image", "rhinoceros");
String rhinoCrAsStr = Serialization.jsonMapper().writeValueAsString(rhinoCr);
rhinoCr = client.customResource(customResourceDefinitionContext).edit(currentNamespace, name, rhinoCrAsStr);
assertAnimal(rhinoCr, name, "rhinoceros");
// Delete
Map<String, Object> deletionStatusMap = client.customResource(customResourceDefinitionContext).delete(currentNamespace, name);
assertDeletionStatus(deletionStatusMap, "Success");
}
@Test
public void testWatch() throws IOException, InterruptedException {
// Given
String name = "chital";
Map<String, Object> deer = createNewAnimal(name, "spotted-deer");
CountDownLatch creationEventReceived = new CountDownLatch(1);
// When
client.customResource(customResourceDefinitionContext).watch(currentNamespace, new Watcher<String>() {
@Override
public void eventReceived(Action action, String resource) {
if (resource.contains(name)) {
creationEventReceived.countDown();
}
}
@Override
public void onClose(WatcherException cause) { }
});
client.customResource(customResourceDefinitionContext).create(currentNamespace, deer);
// Then
assertTrue(creationEventReceived.await(1, TimeUnit.SECONDS));
}
@Test
public void testUpdateStatus() throws IOException {
// Given
String name = "black-buck";
Map<String, Object> deer = createNewAnimal(name, "indian-antelope");
Map<String, Object> status = new HashMap<>();
status.put("conservationStatus", "endangered");
// When
deer = client.customResource(customResourceDefinitionContext).create(currentNamespace, deer);
await().atMost(5, TimeUnit.SECONDS)
.until(() -> client.customResource(customResourceDefinitionContext).get(currentNamespace, name) != null);
deer.put("status", status);
Map<String, Object> updatedDeer = client.customResource(customResourceDefinitionContext).updateStatus(currentNamespace, name, deer);
// Then
assertNotNull(updatedDeer);
assertEquals("endangered", ((Map<String, Object>)deer.get("status")).get("conservationStatus").toString());
}
@AfterClass
public static void cleanup() {
ClusterEntity.remove(RawCustomResourceIT.class.getResourceAsStream("/test-rawcustomresource-definition.yml"));
}
private void assertAnimal(Map<String, Object> animal, String name, String image) {
assertNotNull(animal);
assertFalse(animal.isEmpty());
assertThat(((HashMap<String, String>)animal.get("metadata")).get("name")).isEqualTo(name);
assertThat(((HashMap<String, String>)animal.get("spec")).get("image")).isEqualTo(image);
}
private void assertDeletionStatus(Map<String, Object> deletionStatusAsMap, String status) {
Status deletionStatus = Serialization.jsonMapper().convertValue(deletionStatusAsMap, Status.class);
assertNotNull(deletionStatus);
assertEquals(status, deletionStatus.getStatus());
}
private Map<String, Object> createNewAnimal(String name, String image) {
Map<String, Object> crAsMap = new HashMap<>();
crAsMap.put("apiVersion", "jungle.example.com/v1");
crAsMap.put("kind", "Animal");
Map<String, Object> crMetadata = new HashMap<>();
crMetadata.put("name", name);
crMetadata.put("labels", Collections.singletonMap("foo", "bar"));
Map<String, Object> crSpec = new HashMap<>();
crSpec.put("image", image);
crAsMap.put("metadata", crMetadata);
crAsMap.put("spec", crSpec);
return crAsMap;
}
}