forked from fabric8io/kubernetes-client
/
TemplateTest.java
312 lines (261 loc) · 12 KB
/
TemplateTest.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
312
/**
* 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.openshift.client.server.mock;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.fabric8.kubernetes.api.model.APIGroupListBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServicePort;
import io.fabric8.kubernetes.api.model.ServiceSpec;
import io.fabric8.kubernetes.api.model.StatusBuilder;
import io.fabric8.kubernetes.client.utils.IOHelpers;
import io.fabric8.openshift.api.model.ParameterBuilder;
import io.fabric8.openshift.api.model.Template;
import io.fabric8.openshift.api.model.TemplateBuilder;
import io.fabric8.openshift.api.model.TemplateList;
import io.fabric8.openshift.api.model.TemplateListBuilder;
import io.fabric8.openshift.client.DefaultOpenShiftClient;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.OpenShiftConfigBuilder;
import io.fabric8.openshift.client.ParameterValue;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import static java.util.Collections.singletonMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnableRuleMigrationSupport
public class TemplateTest {
@Rule
public OpenShiftServer server = new OpenShiftServer();
@Test
void testList() {
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates").andReturn(200, new TemplateListBuilder().build()).once();
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/ns1/templates").andReturn(200, new TemplateListBuilder()
.addNewItem().and()
.addNewItem().and().build()).once();
server.expect().withPath("/apis").andReturn(200, new APIGroupListBuilder()
.addNewGroup()
.withApiVersion("v1")
.withName("autoscaling.k8s.io")
.endGroup()
.addNewGroup()
.withApiVersion("v1")
.withName("security.openshift.io")
.endGroup()
.build()).always();
server.expect().withPath("/apis/template.openshift.io/v1/templates").andReturn(200, new TemplateListBuilder()
.addNewItem().and()
.addNewItem().and()
.addNewItem()
.and().build()).once();
OpenShiftClient client = server.getOpenshiftClient();
TemplateList templateList = client.templates().list();
assertNotNull(templateList);
assertEquals(0, templateList.getItems().size());
templateList = client.templates().inNamespace("ns1").list();
assertNotNull(templateList);
assertEquals(2, templateList.getItems().size());
templateList = client.templates().inAnyNamespace().list();
assertNotNull(templateList);
assertEquals(3, templateList.getItems().size());
}
@Test
void testListWithParams() throws IOException {
String json = IOHelpers.readFully(getClass().getResourceAsStream("/template-list-with-number-params.json"));
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates").andReturn(200, json).always();
OpenShiftClient client = server.getOpenshiftClient();
Map<String, String> map = new HashMap<>();
map.put("PORT", "8080");
TemplateList templateList = client.templates().withParameters(map).list();
assertNotNull(templateList);
assertEquals(1, templateList.getItems().size());
}
@Test
void testGet() {
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates/tmpl1").andReturn(200, new TemplateBuilder()
.withNewMetadata().withName("tmpl1").endMetadata()
.build()).once();
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/ns1/templates/tmpl2").andReturn(200, new TemplateBuilder()
.withNewMetadata().withName("tmpl2").endMetadata()
.build()).once();
OpenShiftClient client = server.getOpenshiftClient();
Template template = client.templates().withName("tmpl1").get();
assertNotNull(template);
assertEquals("tmpl1", template.getMetadata().getName());
template = client.templates().withName("tmpl2").get();
assertNull(template);
template = client.templates().inNamespace("ns1").withName("tmpl2").get();
assertNotNull(template);
assertEquals("tmpl2", template.getMetadata().getName());
}
@Test
void testDelete() {
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates/tmpl1").andReturn(200, new TemplateBuilder().build()).once();
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/ns1/templates/tmpl2").andReturn(200, new TemplateBuilder().build()).once();
OpenShiftClient client = server.getOpenshiftClient();
Boolean deleted = client.templates().withName("tmpl1").delete();
assertNotNull(deleted);
deleted = client.templates().withName("tmpl2").delete();
assertFalse(deleted);
deleted = client.templates().inNamespace("ns1").withName("tmpl2").delete();
assertTrue(deleted);
}
@Test
void testCreateWithHandler() {
Template template = new TemplateBuilder()
.editOrNewMetadata()
.withName("tmpl3")
.withNamespace("test")
.endMetadata()
.build();
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates").andReturn(200, template).once();
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates/tmpl3").andReturn(404, new StatusBuilder().withCode(404).build()).once();
OpenShiftClient client = server.getOpenshiftClient();
Template created = client.resource(template).createOrReplace();
assertNotNull(created);
}
@Test
void testProcess() {
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates/tmpl1").andReturn(200, new TemplateBuilder().build()).once();
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/processedtemplates").andReturn(201, new KubernetesListBuilder().build()).once();
OpenShiftClient client = server.getOpenshiftClient();
KubernetesList list = client.templates().withName("tmpl1").process(new ParameterValue("name1", "value1"));
assertNotNull(list);
}
@Test
void shouldLoadTemplateWithNumberParameters() {
OpenShiftClient client = new DefaultOpenShiftClient(new OpenShiftConfigBuilder().build());
Map<String, String> map = new HashMap<>();
map.put("PORT", "8080");
KubernetesList list = client.templates().withParameters(map).load(getClass().getResourceAsStream("/template-with-number-params.yml")).processLocally(map);
assertListIsServiceWithPort8080(list);
}
protected void assertListIsServiceWithPort8080(KubernetesList list) {
assertNotNull(list);
assertListIsServiceWithPort8080(list.getItems());
}
protected static void assertListIsServiceWithPort8080(List<HasMetadata> items) {
assertNotNull(items);
assertEquals(1, items.size());
HasMetadata item = items.get(0);
assertTrue(item instanceof Service);
Service service = (Service) item;
ServiceSpec serviceSpec = service.getSpec();
assertNotNull(serviceSpec);
List<ServicePort> ports = serviceSpec.getPorts();
assertEquals(1, ports.size());
ServicePort port = ports.get(0);
assertEquals(Integer.valueOf(8080), port.getPort());
}
@Test
void testLoadParameterizedNumberTemplate() throws IOException {
String json = IOHelpers.readFully(getClass().getResourceAsStream("/template-with-number-params.json"));
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates/tmpl1").andReturn(200, json).once();
Map<String, String> map = new HashMap<>();
map.put("PORT", "8080");
OpenShiftClient client = server.getOpenshiftClient();
Template template = client.templates().withParameters(map).withName("tmpl1").get();
List<HasMetadata> list = template.getObjects();
assertListIsServiceWithPort8080(list);
}
@Test
void testProcessParameterizedNumberTemplate() throws IOException {
String json = IOHelpers.readFully(getClass().getResourceAsStream("/template-with-number-params.json"));
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates/tmpl1").andReturn(200, json).once();
Map<String, String> map = new HashMap<>();
map.put("PORT", "8080");
OpenShiftClient client = server.getOpenshiftClient();
KubernetesList list = client.templates().withName("tmpl1").processLocally(map);
assertListIsServiceWithPort8080(list);
}
@Test
void testNullParameterMapValueShouldNotThrowNullPointerException() {
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates/tmpl1").andReturn(200, new TemplateBuilder()
.withNewMetadata().withName("tmpl1").endMetadata()
.withParameters(new ParameterBuilder().withName("key").build())
.build()).once();
OpenShiftClient client = server.getOpenshiftClient();
Map<String, String> nullValueMap = singletonMap("key", null);
KubernetesList list = client.templates().withName("tmpl1").processLocally(nullValueMap);
assertNotNull(list);
}
@Test
void testEmptyParameterMapValueShouldNotThrowNullPointerException() {
server.expect().withPath("/apis/template.openshift.io/v1/namespaces/test/templates/tmpl1").andReturn(200, new TemplateBuilder()
.withNewMetadata().withName("tmpl1").endMetadata()
.withParameters(new ParameterBuilder().withName("key").build())
.build()).once();
OpenShiftClient client = server.getOpenshiftClient();
Map<String, String> emptyValueMap = singletonMap("key", "");
KubernetesList list = client.templates().withName("tmpl1").processLocally(emptyValueMap);
assertNotNull(list);
}
@Test
void testCreateOrReplaceOpenShif4() {
// Given
Template template = getTemplateBuilder().build();
server.expect().post().withPath("/apis/template.openshift.io/v1/namespaces/ns1/templates")
.andReturn(HttpURLConnection.HTTP_OK, template)
.once();
OpenShiftClient client = server.getOpenshiftClient();
// When
template = client.templates().inNamespace("ns1").createOrReplace(template);
// Then
assertNotNull(template);
}
private TemplateBuilder getTemplateBuilder() {
Pod pod = new PodBuilder()
.withNewMetadata().withName("redis-master").endMetadata()
.withNewSpec()
.addNewContainer()
.addNewEnv().withName("REDIS_PASSWORD").withValue("${REDIS_PASSWORD}").endEnv()
.withImage("dockerfile/redis")
.addNewPort()
.withContainerPort(6379)
.withProtocol("TCP")
.endPort()
.endContainer()
.endSpec()
.build();
return new TemplateBuilder()
.withNewMetadata()
.withName("redis-template")
.addToAnnotations("description", "Description")
.addToAnnotations("iconClass", "icon-redis")
.addToAnnotations("tags", "database,nosql")
.endMetadata()
.addToObjects(pod)
.addNewParameter()
.withDescription("Password used for Redis authentication")
.withFrom("[A-Z0-9]{8}")
.withGenerate("expression")
.withName("REDIS_PASSWORD")
.endParameter()
.addToLabels("redis", "master");
}
}