-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathKubernetesCloudTest.java
More file actions
404 lines (358 loc) · 17.1 KB
/
Copy pathKubernetesCloudTest.java
File metadata and controls
404 lines (358 loc) · 17.1 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package org.csanchez.jenkins.plugins.kubernetes;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.AccessDeniedException3;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Always;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.PodRetention;
import org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume;
import org.csanchez.jenkins.plugins.kubernetes.volumes.PodVolume;
import org.csanchez.jenkins.plugins.kubernetes.volumes.workspace.WorkspaceVolume;
import org.htmlunit.html.DomElement;
import org.htmlunit.html.DomNodeList;
import org.htmlunit.html.HtmlElement;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlInput;
import org.htmlunit.html.HtmlPage;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
import org.jvnet.hudson.test.recipes.LocalData;
public class KubernetesCloudTest {
@Rule
public JenkinsRule j = new JenkinsRule();
@Rule
public LoggerRule logs = new LoggerRule()
.record(Logger.getLogger(KubernetesCloud.class.getPackage().getName()), Level.ALL);
@BeforeClass
public static void enableManagePermission() {
// TODO remove when baseline contains https://github.com/jenkinsci/jenkins/pull/23873
Jenkins.MANAGE.setEnabled(true);
}
@After
public void tearDown() {
System.getProperties().remove("KUBERNETES_JENKINS_URL");
}
@Test
public void configRoundTrip() throws Exception {
var cloud = new KubernetesCloud("kubernetes");
var podTemplate = new PodTemplate();
podTemplate.setName("test-template");
podTemplate.setLabel("test");
cloud.addTemplate(podTemplate);
var jenkins = j.jenkins;
jenkins.clouds.add(cloud);
jenkins.save();
j.submit(j.createWebClient().goTo("cloud/kubernetes/configure").getFormByName("config"));
assertEquals(cloud, jenkins.clouds.get(KubernetesCloud.class));
}
@Test
public void testInheritance() {
ContainerTemplate jnlp = new ContainerTemplate("jnlp", "jnlp:1");
ContainerTemplate maven = new ContainerTemplate("maven", "maven:1");
maven.setTtyEnabled(true);
maven.setCommand("cat");
PodVolume podVolume = new EmptyDirVolume("/some/path", true);
PodTemplate parent = new PodTemplate();
parent.setName("parent");
parent.setLabel("parent");
parent.setContainers(Arrays.asList(jnlp));
parent.setVolumes(Arrays.asList(podVolume));
ContainerTemplate maven2 = new ContainerTemplate("maven", "maven:2");
PodTemplate withNewMavenVersion = new PodTemplate();
withNewMavenVersion.setContainers(Arrays.asList(maven2));
PodTemplate result = PodTemplateUtils.combine(parent, withNewMavenVersion);
}
@Test(expected = IllegalStateException.class)
public void getJenkinsUrlOrDie_NoJenkinsUrl() {
JenkinsLocationConfiguration.get().setUrl(null);
KubernetesCloud cloud = new KubernetesCloud("name");
String url = cloud.getJenkinsUrlOrDie();
fail("Should have thrown IllegalStateException at this point but got " + url + " instead.");
}
@Test
public void getJenkinsUrlOrDie_UrlInCloud() {
System.setProperty("KUBERNETES_JENKINS_URL", "http://mylocationinsysprop");
KubernetesCloud cloud = new KubernetesCloud("name");
cloud.setJenkinsUrl("http://mylocation");
assertEquals("http://mylocation/", cloud.getJenkinsUrlOrDie());
}
@Test
public void getJenkinsUrlOrDie_UrlInSysprop() {
System.setProperty("KUBERNETES_JENKINS_URL", "http://mylocation");
KubernetesCloud cloud = new KubernetesCloud("name");
assertEquals("http://mylocation/", cloud.getJenkinsUrlOrDie());
}
@Test
public void getJenkinsUrlOrDie_UrlInLocation() {
JenkinsLocationConfiguration.get().setUrl("http://mylocation");
KubernetesCloud cloud = new KubernetesCloud("name");
assertEquals("http://mylocation/", cloud.getJenkinsUrlOrDie());
}
@Test
public void getJenkinsUrlOrNull_NoJenkinsUrl() {
JenkinsLocationConfiguration.get().setUrl(null);
KubernetesCloud cloud = new KubernetesCloud("name");
String url = cloud.getJenkinsUrlOrNull();
assertNull(url);
}
@Test
public void getJenkinsUrlOrNull_UrlInCloud() {
System.setProperty("KUBERNETES_JENKINS_URL", "http://mylocationinsysprop");
KubernetesCloud cloud = new KubernetesCloud("name");
cloud.setJenkinsUrl("http://mylocation");
assertEquals("http://mylocation/", cloud.getJenkinsUrlOrNull());
}
@Test
public void getJenkinsUrlOrNull_UrlInSysprop() {
System.setProperty("KUBERNETES_JENKINS_URL", "http://mylocation");
KubernetesCloud cloud = new KubernetesCloud("name");
assertEquals("http://mylocation/", cloud.getJenkinsUrlOrNull());
}
@Test
public void getJenkinsUrlOrNull_UrlInLocation() {
JenkinsLocationConfiguration.get().setUrl("http://mylocation");
KubernetesCloud cloud = new KubernetesCloud("name");
assertEquals("http://mylocation/", cloud.getJenkinsUrlOrNull());
}
@Test
public void testKubernetesCloudDefaults() {
KubernetesCloud cloud = new KubernetesCloud("name");
assertEquals(PodRetention.getKubernetesCloudDefault(), cloud.getPodRetention());
}
@Test
public void testPodLabels() {
List<PodLabel> defaultPodLabelsList = PodLabel.fromMap(KubernetesCloud.DEFAULT_POD_LABELS);
KubernetesCloud cloud = new KubernetesCloud("name");
assertEquals(KubernetesCloud.DEFAULT_POD_LABELS, cloud.getPodLabelsMap());
assertEquals(defaultPodLabelsList, cloud.getPodLabels());
assertEquals(cloud.getPodLabelsMap(), cloud.getLabels());
List<PodLabel> labels = PodLabel.listOf("foo", "bar", "cat", "dog");
cloud.setPodLabels(labels);
Map<String, String> expected = new LinkedHashMap<>();
expected.put("foo", "bar");
expected.put("cat", "dog");
assertEquals(expected, cloud.getPodLabelsMap());
assertEquals(cloud.getPodLabelsMap(), cloud.getLabels());
assertEquals(new ArrayList<>(labels), cloud.getPodLabels());
cloud.setPodLabels(null);
assertEquals(KubernetesCloud.DEFAULT_POD_LABELS, cloud.getPodLabelsMap());
assertEquals(defaultPodLabelsList, cloud.getPodLabels());
cloud.setPodLabels(new ArrayList<>());
assertEquals(KubernetesCloud.DEFAULT_POD_LABELS, cloud.getPodLabelsMap());
assertEquals(cloud.getPodLabelsMap(), cloud.getLabels());
assertEquals(defaultPodLabelsList, cloud.getPodLabels());
}
@Test
public void testPodAnnotations() {
KubernetesCloud cloud = new KubernetesCloud("name");
// default is empty list
assertEquals(Collections.emptyList(), cloud.getPodAnnotations());
assertEquals(Collections.emptyMap(), cloud.getPodAnnotationsMap());
// set annotations and verify list and map
List<PodAnnotation> annotations =
Arrays.asList(new PodAnnotation("team", "backend"), new PodAnnotation("cost-center", "platform"));
cloud.setPodAnnotations(annotations);
Map<String, String> expected = new LinkedHashMap<>();
expected.put("team", "backend");
expected.put("cost-center", "platform");
assertEquals(expected, cloud.getPodAnnotationsMap());
assertEquals(annotations, cloud.getPodAnnotations());
// null resets to empty
cloud.setPodAnnotations(null);
assertEquals(Collections.emptyList(), cloud.getPodAnnotations());
assertEquals(Collections.emptyMap(), cloud.getPodAnnotationsMap());
// empty list also results in empty
cloud.setPodAnnotations(new ArrayList<>());
assertEquals(Collections.emptyList(), cloud.getPodAnnotations());
assertEquals(Collections.emptyMap(), cloud.getPodAnnotationsMap());
}
@Test
public void testLabels() {
KubernetesCloud cloud = new KubernetesCloud("name");
List<PodLabel> labels = PodLabel.listOf("foo", "bar", "cat", "dog");
cloud.setPodLabels(labels);
Map<String, String> labelsMap = new LinkedHashMap<>();
for (PodLabel l : labels) {
labelsMap.put(l.getKey(), l.getValue());
}
cloud.setLabels(labelsMap);
assertEquals(new LinkedHashMap<>(labelsMap), cloud.getPodLabelsMap());
assertEquals(labels, cloud.getPodLabels());
cloud.setLabels(null);
assertEquals(Collections.singletonMap("jenkins", "slave"), cloud.getPodLabelsMap());
assertEquals(Collections.singletonMap("jenkins", "slave"), cloud.getLabels());
cloud.setLabels(new LinkedHashMap<>());
assertEquals(Collections.singletonMap("jenkins", "slave"), cloud.getPodLabelsMap());
assertEquals(Collections.singletonMap("jenkins", "slave"), cloud.getLabels());
}
@Test
public void copyConstructor() throws Exception {
PodTemplate pt = new PodTemplate();
pt.setName("podTemplate");
KubernetesCloud cloud = new KubernetesCloud("name");
var objectProperties = Set.of(
"templates",
"podRetention",
"podLabels",
"podAnnotations",
"labels",
"serverCertificate",
"garbageCollection",
"traits");
for (String property : PropertyUtils.describe(cloud).keySet()) {
if (PropertyUtils.isWriteable(cloud, property)) {
Class<?> propertyType = PropertyUtils.getPropertyType(cloud, property);
if (propertyType == String.class) {
if (property.endsWith("Str")) {
// setContainerCapStr
// setMaxRequestsPerHostStr
PropertyUtils.setProperty(cloud, property, RandomStringUtils.randomNumeric(3));
} else {
PropertyUtils.setProperty(cloud, property, RandomStringUtils.randomAlphabetic(10));
}
} else if (propertyType == int.class) {
PropertyUtils.setProperty(cloud, property, RandomUtils.nextInt());
} else if (propertyType == Integer.class) {
PropertyUtils.setProperty(cloud, property, Integer.valueOf(RandomUtils.nextInt()));
} else if (propertyType == boolean.class) {
PropertyUtils.setProperty(cloud, property, RandomUtils.nextBoolean());
} else if (!objectProperties.contains(property)) {
fail("Unhandled field in copy constructor: " + property);
}
}
}
cloud.setServerCertificate("-----BEGIN CERTIFICATE-----");
cloud.setTemplates(Collections.singletonList(pt));
cloud.setPodRetention(new Always());
cloud.setPodLabels(PodLabel.listOf("foo", "bar", "cat", "dog"));
cloud.setLabels(Collections.singletonMap("foo", "bar"));
cloud.setPodAnnotations(Arrays.asList(new PodAnnotation("team", "backend")));
KubernetesCloud copy = new KubernetesCloud("copy", cloud);
assertEquals("copy", copy.name);
assertTrue(
"Expected cloud from copy constructor to be equal to the source except for name",
EqualsBuilder.reflectionEquals(cloud, copy, true, KubernetesCloud.class, "name"));
}
@Test
public void defaultWorkspaceVolume() throws Exception {
KubernetesCloud cloud = new KubernetesCloud("kubernetes");
j.jenkins.clouds.add(cloud);
j.jenkins.save();
JenkinsRule.WebClient wc = j.createWebClient();
HtmlPage p = wc.goTo("cloud/kubernetes/new");
HtmlForm f = p.getFormByName("config");
HtmlInput templateName = getInputByName(f, "_.name");
templateName.setValue("default-workspace-volume");
j.submit(f);
cloud = j.jenkins.clouds.get(KubernetesCloud.class);
PodTemplate podTemplate = cloud.getTemplates().get(0);
assertEquals("default-workspace-volume", podTemplate.getName());
assertEquals(WorkspaceVolume.getDefault(), podTemplate.getWorkspaceVolume());
// test whether we can edit a template
p = wc.goTo("cloud/kubernetes/template/" + podTemplate.getId() + "/");
f = p.getFormByName("config");
templateName = getInputByName(f, "_.name");
templateName.setValue("default-workspace");
j.submit(f);
podTemplate = cloud.getTemplates().get(0);
assertEquals("default-workspace", podTemplate.getName());
p = wc.goTo("cloud/kubernetes/templates");
DomElement row = p.getElementById("template_" + podTemplate.getId());
assertTrue(row != null);
}
@Test
public void minRetentionTimeout() {
KubernetesCloud cloud = new KubernetesCloud("kubernetes");
assertEquals(KubernetesCloud.DEFAULT_RETENTION_TIMEOUT_MINUTES, cloud.getRetentionTimeout());
cloud.setRetentionTimeout(0);
assertEquals(KubernetesCloud.DEFAULT_RETENTION_TIMEOUT_MINUTES, cloud.getRetentionTimeout());
}
@Test
@LocalData
public void emptyKubernetesCloudReadResolve() {
KubernetesCloud cloud = j.jenkins.clouds.get(KubernetesCloud.class);
assertEquals(KubernetesCloud.DEFAULT_RETENTION_TIMEOUT_MINUTES, cloud.getRetentionTimeout());
assertEquals(Integer.MAX_VALUE, cloud.getContainerCap());
assertEquals(KubernetesCloud.DEFAULT_MAX_REQUESTS_PER_HOST, cloud.getMaxRequestsPerHost());
assertEquals(PodRetention.getKubernetesCloudDefault(), cloud.getPodRetention());
assertEquals(KubernetesCloud.DEFAULT_WAIT_FOR_POD_SEC, cloud.getWaitForPodSec());
}
@Test
@LocalData
public void readResolveContainerCapZero() {
KubernetesCloud cloud = j.jenkins.clouds.get(KubernetesCloud.class);
assertEquals(cloud.getContainerCap(), Integer.MAX_VALUE);
}
public HtmlInput getInputByName(DomElement root, String name) {
DomNodeList<HtmlElement> inputs = root.getElementsByTagName("input");
for (HtmlElement input : inputs) {
if (name.equals(input.getAttribute("name"))) {
return (HtmlInput) input;
}
}
return null;
}
@Test
public void authorization() throws Exception {
var securityRealm = j.createDummySecurityRealm();
j.jenkins.setSecurityRealm(securityRealm);
var authorizationStrategy = new MockAuthorizationStrategy();
authorizationStrategy.grant(Jenkins.ADMINISTER).everywhere().to("admin");
authorizationStrategy.grant(Jenkins.MANAGE).everywhere().to("manager");
authorizationStrategy.grant(Jenkins.READ).everywhere().to("user");
j.jenkins.setAuthorizationStrategy(authorizationStrategy);
j.jenkins.clouds.add(new KubernetesCloud("kubernetes"));
var pt1 = new PodTemplate("one");
var pt2 = new PodTemplate("two");
try (var ignored = asUser("admin")) {
j.jenkins.clouds.get(KubernetesCloud.class).addTemplate(pt1);
}
try (var ignored = asUser("user")) {
var expectedMessage = "user is missing the Overall/Manage permission";
var kubernetesCloud = j.jenkins.clouds.get(KubernetesCloud.class);
assertAccessDenied(() -> kubernetesCloud.addTemplate(new PodTemplate()), expectedMessage);
assertAccessDenied(() -> kubernetesCloud.removeTemplate(pt1), expectedMessage);
assertAccessDenied(() -> kubernetesCloud.replaceTemplate(pt1, pt2), expectedMessage);
}
try (var ignored = asUser("manager")) {
j.jenkins.clouds.get(KubernetesCloud.class).addTemplate(pt1);
}
}
private static void assertAccessDenied(ThrowingRunnable throwingRunnable, String expectedMessage) {
assertThat(
assertThrows(AccessDeniedException3.class, throwingRunnable).getMessage(),
containsString(expectedMessage));
}
private static @NonNull ACLContext asUser(String admin) {
return ACL.as2(User.get(admin, true, Map.of()).impersonate2());
}
}