forked from maxlaverse/kubernetes-cli-plugin
-
Notifications
You must be signed in to change notification settings - Fork 47
/
KubectlBuildWrapperTest.java
83 lines (64 loc) · 2.92 KB
/
KubectlBuildWrapperTest.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
package org.jenkinsci.plugins.kubernetes.cli;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.domains.Domain;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.util.ListBoxModel;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Max Laverse
*/
public class KubectlBuildWrapperTest extends KubectlTestBase {
@Rule
public JenkinsRule r = new JenkinsRule();
@Before
public void addFakeSlave() throws Exception {
r.jenkins.addNode(getFakeSlave(r));
r.jenkins.setNumExecutors(0);
}
@Test
public void testEnvVariablePresent() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredential(CREDENTIAL_ID));
FreeStyleProject p = r.createFreeStyleProject();
KubectlBuildWrapper bw = new KubectlBuildWrapper();
bw.credentialsId = CREDENTIAL_ID;
p.getBuildWrappersList().add(bw);
FreeStyleBuild b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatus(Result.SUCCESS, r.waitForCompletion(b));
r.assertLogContains("KUBECONFIG=" + b.getWorkspace() + File.separator + ".kube", b);
}
@Test
public void testKubeConfigDisposed() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredential(CREDENTIAL_ID));
FreeStyleProject p = r.createFreeStyleProject();
KubectlBuildWrapper bw = new KubectlBuildWrapper();
bw.credentialsId = CREDENTIAL_ID;
p.getBuildWrappersList().add(bw);
FreeStyleBuild b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatus(Result.SUCCESS, r.waitForCompletion(b));
r.assertLogContains("kubectl configuration cleaned up", b);
}
@Test
public void testListedCredentials() throws Exception {
CredentialsStore store = CredentialsProvider.lookupStores(r.jenkins).iterator().next();
store.addCredentials(Domain.global(), usernamePasswordCredential("1"));
store.addCredentials(Domain.global(), secretCredential("2"));
store.addCredentials(Domain.global(), fileCredential("3"));
store.addCredentials(Domain.global(), certificateCredential("4"));
store.addCredentials(Domain.global(), tokenCredential("5"));
KubectlBuildWrapper.DescriptorImpl d = new KubectlBuildWrapper.DescriptorImpl();
FreeStyleProject p = r.createFreeStyleProject();
ListBoxModel s = d.doFillCredentialsIdItems(p.asItem(), "");
assertEquals(6, s.size());
}
}