-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathKubernetesTest.java
248 lines (222 loc) · 10.4 KB
/
KubernetesTest.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
/*
* The MIT License
*
* Copyright (c) 2016, Carlos Sanchez
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.csanchez.jenkins.plugins.kubernetes;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import hudson.model.Label;
import org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Default;
import org.csanchez.jenkins.plugins.kubernetes.pod.retention.Never;
import org.csanchez.jenkins.plugins.kubernetes.volumes.EmptyDirVolume;
import org.csanchez.jenkins.plugins.kubernetes.volumes.HostPathVolume;
import org.jenkinsci.plugins.kubernetes.credentials.FileSystemServiceAccountCredential;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.recipes.LocalData;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.plugins.git.GitTool;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.tools.ToolLocationNodeProperty;
import hudson.tools.ToolLocationNodeProperty.ToolLocation;
import hudson.util.DescribableList;
import hudson.util.Secret;
/**
* @author Carlos Sanchez
* @since 0.9
*
*/
public class KubernetesTest {
@Rule
public JenkinsRule r = new JenkinsRule();
@Rule
public LoggerRule log = new LoggerRule();
private KubernetesCloud cloud;
@Before
public void before() throws Exception {
cloud = r.jenkins.clouds.get(KubernetesCloud.class);
assertNotNull(cloud);
}
@Test
@LocalData()
public void upgradeFrom_1_17_2() throws Exception {
Map<String, String> labels = cloud.getPodLabelsMap();
assertEquals(2, labels.size());
assertThat(cloud.getPodLabelsMap(), hasEntry("jenkins", "slave"));
assertThat(cloud.getPodLabelsMap(), hasEntry("biff", "johnson"));
PodTemplate pt = cloud.getTemplate(Label.get("java"));
assertNotNull(pt);
for (ContainerTemplate ct : pt.getContainers()) {
// Retain working dir used in previous version
assertEquals("/home/jenkins", ct.getWorkingDir());
}
}
@Test
@LocalData
public void upgradeFrom_1_15_9() {
List<PodTemplate> templates = cloud.getTemplates();
assertPodTemplates(templates);
PodTemplate template = templates.get(0);
assertEquals("blah", template.getYaml());
assertEquals(Collections.singletonList("blah"), template.getYamls());
assertNull(template._getYamls());
}
@Test
@LocalData
public void upgradeFrom_1_15_9_invalid() {
log.record(PodTemplate.class, Level.WARNING).capture(1);
List<PodTemplate> templates = cloud.getTemplates();
assertPodTemplates(templates);
PodTemplate template = templates.get(0);
assertEquals("blah", template.getYaml());
assertEquals(Collections.singletonList("blah"), template.getYamls());
assertNull(template._getYamls());
log.getMessages().stream()
.anyMatch(msg -> msg.contains("Found several persisted YAML fragments in pod template java"));
}
@Test
@LocalData()
@Issue("JENKINS-57116")
public void upgradeFrom_1_15_1() throws Exception {
List<PodTemplate> templates = cloud.getTemplates();
assertPodTemplates(templates);
PodTemplate template = templates.get(0);
assertEquals(Collections.emptyList(), template.getYamls());
assertNull(template.getYaml());
}
@Test
@LocalData()
public void upgradeFrom_1_10() throws Exception {
List<PodTemplate> templates = cloud.getTemplates();
assertPodTemplates(templates);
assertEquals(new Never(), cloud.getPodRetention());
PodTemplate template = templates.get(0);
assertEquals(new Default(), template.getPodRetention());
assertEquals(cloud.DEFAULT_WAIT_FOR_POD_SEC, cloud.getWaitForPodSec());
assertTrue(template.isShowRawYaml());
assertEquals(Collections.emptyList(), template.getYamls());
assertNull(template.getYaml());
}
@Test
@LocalData()
public void upgradeFrom_1_1() throws Exception {
List<Credentials> credentials = SystemCredentialsProvider.getInstance().getCredentials();
assertEquals(3, credentials.size());
UsernamePasswordCredentialsImpl cred0 = (UsernamePasswordCredentialsImpl) credentials.get(0);
assertEquals("token", cred0.getId());
assertEquals("myusername", cred0.getUsername());
FileSystemServiceAccountCredential cred1 = (FileSystemServiceAccountCredential) credentials.get(1);
StringCredentialsImpl cred2 = (StringCredentialsImpl) credentials.get(2);
assertEquals("mytoken", Secret.toString(cred2.getSecret()));
assertThat(cloud.getLabels(), hasEntry("jenkins", "slave"));
assertEquals(cloud.DEFAULT_WAIT_FOR_POD_SEC, cloud.getWaitForPodSec());
}
@Test
@LocalData()
public void upgradeFrom_0_12() throws Exception {
List<PodTemplate> templates = cloud.getTemplates();
assertPodTemplates(templates);
PodTemplate template = templates.get(0);
assertEquals(Arrays.asList(new KeyValueEnvVar("pod_a_key", "pod_a_value"),
new KeyValueEnvVar("pod_b_key", "pod_b_value")), template.getEnvVars());
assertEquals(Collections.emptyList(), template.getYamls());
assertEquals(cloud.DEFAULT_WAIT_FOR_POD_SEC, cloud.getWaitForPodSec());
}
@Test
@LocalData()
public void upgradeFrom_0_10() throws Exception {
List<PodTemplate> templates = cloud.getTemplates();
PodTemplate template = templates.get(0);
DescribableList<NodeProperty<?>, NodePropertyDescriptor> nodeProperties = template.getNodeProperties();
assertEquals(1, nodeProperties.size());
ToolLocationNodeProperty property = (ToolLocationNodeProperty) nodeProperties.get(0);
assertEquals(1, property.getLocations().size());
ToolLocation location = property.getLocations().get(0);
assertEquals("Default", location.getName());
assertEquals("/custom/path", location.getHome());
assertEquals(GitTool.class, location.getType().clazz);
assertEquals(cloud.DEFAULT_WAIT_FOR_POD_SEC, cloud.getWaitForPodSec());
}
@Test
@LocalData()
public void upgradeFrom_0_8() throws Exception {
List<PodTemplate> templates = cloud.getTemplates();
assertPodTemplates(templates);
assertEquals(cloud.DEFAULT_WAIT_FOR_POD_SEC, cloud.getWaitForPodSec());
}
@Test
@LocalData()
@Issue("JENKINS-70287")
public void mixedPrivivilegedPodTemplates() throws Exception {
List<PodTemplate> templates = cloud.getTemplates();
assertPodTemplates(templates, 2, 2);
PodTemplate template = templates.get(0);
assertEquals(2, template.getContainers().size());
assertEquals(true, template.getContainers().get(0).isPrivileged());
assertEquals(false, template.getContainers().get(1).isPrivileged());
PodTemplate baseTemplate = templates.get(1);
assertEquals(1, baseTemplate.getContainers().size());
assertEquals(true, baseTemplate.getContainers().get(0).isPrivileged());
assertEquals(Collections.emptyList(), template.getYamls());
assertNull(template.getYaml());
}
private void assertPodTemplates(List<PodTemplate> templates, Integer... numbers) {
assertEquals(numbers.length > 0 ? numbers[0] : 1, templates.size());
PodTemplate podTemplate = templates.get(0);
assertEquals(Integer.MAX_VALUE, podTemplate.getInstanceCap());
assertEquals(numbers.length > 1 ? numbers[1] : 1, podTemplate.getContainers().size());
ContainerTemplate containerTemplate = podTemplate.getContainers().get(0);
assertEquals("jenkins/inbound-agent", containerTemplate.getImage());
assertEquals("jnlp", containerTemplate.getName());
assertEquals(Arrays.asList(new KeyValueEnvVar("a", "b"), new KeyValueEnvVar("c", "d")),
containerTemplate.getEnvVars());
assertEquals(2, podTemplate.getVolumes().size());
EmptyDirVolume emptyVolume = (EmptyDirVolume) podTemplate.getVolumes().get(0);
assertEquals("/mnt", emptyVolume.getMountPath());
assertFalse(emptyVolume.getMemory());
assertEquals(EmptyDirVolume.class.getName(), emptyVolume.getClass().getName());
HostPathVolume hostPathVolume = (HostPathVolume) podTemplate.getVolumes().get(1);
assertEquals("/host", hostPathVolume.getMountPath());
assertEquals("/mnt/host", hostPathVolume.getHostPath());
assertEquals(HostPathVolume.class.getName(), hostPathVolume.getClass().getName());
assertEquals(0, podTemplate.getActiveDeadlineSeconds());
}
}