forked from maxlaverse/kubernetes-cli-plugin
-
Notifications
You must be signed in to change notification settings - Fork 47
/
KubectlBuildStepTest.java
93 lines (78 loc) · 4.1 KB
/
KubectlBuildStepTest.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
package org.jenkinsci.plugins.kubernetes.cli;
import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;
import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import static org.junit.Assert.assertNotNull;
/**
* @author Max Laverse
*/
public class KubectlBuildStepTest 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 testScopedCredentials() throws Exception {
Folder folder = new Folder(r.jenkins.getItemGroup(), "test-folder");
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential(CREDENTIAL_ID));
WorkflowJob p = folder.createProject(WorkflowJob.class, "testScopedCredentials");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
}
@Test
public void testMissingScopedCredentials() throws Exception {
Folder folder = new Folder(r.jenkins.getItemGroup(), "test-folder");
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential(CREDENTIAL_ID));
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testMissingScopedCredentials");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatus(Result.FAILURE, r.waitForCompletion(b));
r.assertLogContains("ERROR: No credentials found for id \"cred1234\"", b);
}
@Test
public void testSecretWithSpace() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredentialWithSpace(CREDENTIAL_ID));
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testSecretWithSpace");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogNotContains("with-space", b);
r.assertLogNotContains("s3cr3t", b);
}
@Test
public void testUsernamePasswordWithSpace() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredentialWithSpace(CREDENTIAL_ID));
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testUsernamePasswordWithSpace");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogNotContains("with-passwordspace", b);
r.assertLogNotContains("s3cr3t", b);
}
@Test
public void testKubeConfigDisposed() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredentialWithSpace(CREDENTIAL_ID));
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testUsernamePasswordWithSpace");
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("kubectl configuration cleaned up", b);
}
}