-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathKubernetesCloudTest.java
324 lines (282 loc) · 13.7 KB
/
KubernetesCloudTest.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
313
314
315
316
317
318
319
320
321
322
323
324
package org.csanchez.jenkins.plugins.kubernetes;
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.logging.Level;
import java.util.logging.Logger;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlFormUtil;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.Label;
import hudson.slaves.Cloud;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
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.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.recipes.LocalData;
import jenkins.model.JenkinsLocationConfiguration;
import static org.junit.Assert.*;
public class KubernetesCloudTest {
@Rule
public JenkinsRule j = new JenkinsRule();
@Rule
public LoggerRule logs = new LoggerRule().record(Logger.getLogger(KubernetesCloud.class.getPackage().getName()),
Level.ALL);
@After
public void tearDown() {
System.getProperties().remove("KUBERNETES_JENKINS_URL");
}
@Test
public void configRoundTrip() throws Exception {
KubernetesCloud cloud = new KubernetesCloud("kubernetes");
PodTemplate podTemplate = new PodTemplate();
podTemplate.setName("test-template");
podTemplate.setLabel("test");
cloud.addTemplate(podTemplate);
j.jenkins.clouds.add(cloud);
j.jenkins.save();
JenkinsRule.WebClient wc = j.createWebClient();
HtmlPage p = wc.goTo("configureClouds/");
HtmlForm f = p.getFormByName("config");
j.submit(f);
assertEquals("PodTemplate{id='"+podTemplate.getId()+"', name='test-template', label='test'}", podTemplate.toString());
}
@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 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");
ArrayList<String> objectProperties = new ArrayList<>(Arrays.asList("templates", "podRetention", "podLabels", "labels", "serverCertificate"));
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"));
KubernetesCloud copy = new KubernetesCloud("copy", cloud);
assertEquals("copy", copy.name);
assertEquals("Expected cloud from copy constructor to be equal to the source except for name", cloud, copy);
}
@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("configureClouds/");
HtmlForm f = p.getFormByName("config");
HtmlButton buttonExtends = HtmlFormUtil.getButtonByCaption(f, "Pod Templates...");
buttonExtends.click();
HtmlButton buttonAdd = HtmlFormUtil.getButtonByCaption(f, "Add Pod Template");
buttonAdd.click();
HtmlButton buttonDetails = HtmlFormUtil.getButtonByCaption(f, "Pod Template details...");
buttonDetails.click();
DomElement templates = p.getElementByName("templates");
HtmlInput templateName = getInputByName(templates, "_.name");
templateName.setValueAttribute("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
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);
}
@Test
public void maintenanceModeNoProvision() throws Exception {
String testNodeLabel = "test-node";
PodTemplate podTemplate = new PodTemplate("test");
podTemplate.setLabel(testNodeLabel);
KubernetesCloud cloud = new KubernetesCloud("kubernetes");
cloud.addTemplate(podTemplate);
Cloud.CloudState state = new Cloud.CloudState(Label.parseExpression(testNodeLabel), 1);
assertTrue(cloud.canProvision(state));
// disable provision
cloud.setMaintenanceMode(true);
assertFalse(cloud.canProvision(state));
}
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;
}
}