Skip to content

Commit

Permalink
Fix tests on remote CI
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Dec 11, 2017
1 parent a8a6baa commit 2d3b1d4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
package org.jenkinsci.plugins.kubernetes.credentials;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl;
import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl.FileOnMasterKeyStoreSource;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.FilePath;
import hudson.util.Secret;
import org.apache.commons.compress.utils.IOUtils;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
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.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand All @@ -34,14 +25,7 @@
/**
* @author Max Laverse
*/
public class IntegrationTest {
protected static final String CREDENTIAL_ID = "cred1234";
protected static final String PASSPHRASE = "test";
protected static final String USERNAME = "bob";
protected static final String PASSWORD = "s3cr3t";
protected static final String CA_CERTIFICATE = "-----BEGIN CERTIFICATE-----\na-certificate\n-----END CERTIFICATE-----";
protected static final String SERVER_URL = "https://localhost:6443";
protected static final String KUBECTL_BINARY = "kubectl";
public class IntegrationTest extends KubectlTestBase {

@Rule
public JenkinsRule r = new JenkinsRule();
Expand All @@ -50,8 +34,10 @@ public class IntegrationTest {
public void checkKubectlPresence() throws Exception {
boolean kubectlPresent = Stream.of(System.getenv("PATH").split(Pattern.quote(File.pathSeparator)))
.map(Paths::get)
.anyMatch(path -> Files.exists(path.resolve(KUBECTL_BINARY)) && Files.isExecutable(path.resolve(KUBECTL_BINARY)));
assumeTrue(false);
.map(p -> p.resolve(KUBECTL_BINARY))
.filter(Files::exists)
.anyMatch(Files::isExecutable);
assumeTrue(kubectlPresent);
}

@Test
Expand All @@ -60,7 +46,7 @@ public void testBasicWithCa() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testBasicWithCa");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("kubectlWithCa.groovy"), true));
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithCa.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
Expand All @@ -78,7 +64,7 @@ public void testBasicWithoutCa() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testBasicWithoutCa");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("kubectlWithoutCa.groovy"), true));
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
Expand All @@ -96,7 +82,7 @@ public void testUsernamePasswordCredentials() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testUsernamePasswordCredentials");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("kubectlWithoutCa.groovy"), true));
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
Expand All @@ -114,7 +100,7 @@ public void testSecretCredentials() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secretCredential());

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testSecretCredentials");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("kubectlWithoutCa.groovy"), true));
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
Expand All @@ -131,7 +117,7 @@ public void testCertificateCredentials() throws Exception {
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), certificateCredential());

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testCertificateCredentials");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("kubectlWithoutCa.groovy"), true));
p.setDefinition(new CpsFlowDefinition(loadResource("kubectlWithoutCa.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
Expand Down Expand Up @@ -185,26 +171,4 @@ public void testCertificateCredentials() throws Exception {
"aHVkT1NSQis5YzRYWFNFTzUKLS0tLS1FTkQgUFJJVkFURSBLRVktLS0tLQ=="));

}

protected String loadPipelineScript(String name) {
try {
return new String(IOUtils.toByteArray(getClass().getResourceAsStream(name)));
} catch (Throwable t) {
throw new RuntimeException("Could not read resource:[" + name + "].");
}
}

protected BaseStandardCredentials usernamePasswordCredential() {
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", USERNAME, PASSWORD);
}

protected BaseStandardCredentials secretCredential() {
return new StringCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", Secret.fromString(PASSWORD));
}

protected BaseStandardCredentials certificateCredential() {
String storeFile = getClass().getResource("/org/jenkinsci/plugins/kubernetes/credentials/kubernetes.pkcs12").getFile();
CertificateCredentialsImpl.KeyStoreSource keyStoreSource = new FileOnMasterKeyStoreSource(storeFile);
return new CertificateCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", PASSPHRASE, keyStoreSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,32 @@

import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.Launcher;
import hudson.Proc;
import hudson.model.Result;
import org.apache.commons.compress.utils.IOUtils;
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.FakeLauncher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.PretendSlave;

import java.io.IOException;
import java.io.PrintStream;

import static org.junit.Assert.assertNotNull;

/**
* @author Max Laverse
*/
public class KubectlBuildWrapperStepTest {
protected static final String CREDENTIAL_ID = "cred1234";
protected static final String USERNAME = "bob";
protected static final String PASSWORD = "s3cr3t";
protected static final String KUBECTL_BINARY = "kubectl";
public class KubectlBuildWrapperStepTest extends KubectlTestBase {

@Rule
public JenkinsRule r = new JenkinsRule();

@Before
public void addFakeSlave() throws Exception {
PretendSlave slave = r.createPretendSlave(new FakeLauncher() {
public Proc onLaunch(Launcher.ProcStarter p) throws IOException {
if (p.cmds().get(0).equals(KUBECTL_BINARY)) {
PrintStream ps = new PrintStream(p.stdout());
ps.println("Called stubed: " + String.join(" ", p.cmds()));
ps.close();
return new FinishedProc(0);
}
return r.createLocalLauncher().launch(p);
}
});
slave.setLabelString("mocked-kubectl");
r.jenkins.addNode(slave);
// Add a fake slave
r.jenkins.addNode(getFakeSlave(r));

// For the jobs to run on it
r.jenkins.setNumExecutors(0);
}

Expand All @@ -61,7 +37,7 @@ public void testScopedCredentials() throws Exception {
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());

WorkflowJob p = folder.createProject(WorkflowJob.class, "testScopedCredentials");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("mockedKubectl.groovy"), true));
p.setDefinition(new CpsFlowDefinition(loadResource("mockedKubectl.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
Expand All @@ -73,22 +49,10 @@ public void testMissingScopedCredentials() throws Exception {
CredentialsProvider.lookupStores(folder).iterator().next().addCredentials(Domain.global(), usernamePasswordCredential());

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testMissingScopedCredentials");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("mockedKubectl.groovy"), true));
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);
}

protected String loadPipelineScript(String name) {
try {
return new String(IOUtils.toByteArray(getClass().getResourceAsStream(name)));
} catch (Throwable t) {
throw new RuntimeException("Could not read resource:[" + name + "].");
}
}

protected BaseStandardCredentials usernamePasswordCredential() {
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", USERNAME, PASSWORD);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.jenkinsci.plugins.kubernetes.credentials;

import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.Launcher;
import hudson.Proc;
import hudson.model.Slave;
import hudson.util.Secret;
import org.apache.commons.compress.utils.IOUtils;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.jvnet.hudson.test.FakeLauncher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.PretendSlave;

import java.io.IOException;
import java.io.PrintStream;

/**
* @author Max Laverse
*/
public class KubectlTestBase {
protected static final String CREDENTIAL_ID = "cred1234";
protected static final String PASSPHRASE = "test";
protected static final String USERNAME = "bob";
protected static final String PASSWORD = "s3cr3t";
protected static final String CA_CERTIFICATE = "-----BEGIN CERTIFICATE-----\na-certificate\n-----END CERTIFICATE-----";
protected static final String SERVER_URL = "https://localhost:6443";
protected static final String KUBECTL_BINARY = "kubectl";

protected String loadResource(String name) {
try {
return new String(IOUtils.toByteArray(getClass().getResourceAsStream(name)));
} catch (Throwable t) {
throw new RuntimeException("Could not read resource:[" + name + "].");
}
}

protected String getResourceFile(String name) {
return getClass().getResource(name).getFile();
}

protected Slave getFakeSlave(JenkinsRule r) throws Exception {
PretendSlave slave = r.createPretendSlave(p -> {
if (p.cmds().get(0).equals(KUBECTL_BINARY)) {
PrintStream ps = new PrintStream(p.stdout());
ps.println("Call stubed for: " + String.join(" ", p.cmds()));
ps.close();
return new FakeLauncher.FinishedProc(0);
}
return r.createLocalLauncher().launch(p);
});
slave.setLabelString("mocked-kubectl");
return slave;
}

protected BaseStandardCredentials secretCredential() {
return new StringCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", Secret.fromString(PASSWORD));
}

protected BaseStandardCredentials certificateCredential() {
String storeFile = getResourceFile("/org/jenkinsci/plugins/kubernetes/credentials/kubernetes.pkcs12");
CertificateCredentialsImpl.KeyStoreSource keyStoreSource = new CertificateCredentialsImpl.FileOnMasterKeyStoreSource(storeFile);
return new CertificateCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", PASSPHRASE, keyStoreSource);
}

protected BaseStandardCredentials usernamePasswordCredential() {
return new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", USERNAME, PASSWORD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ node{
label "mocked-kubectl"
stage('Run') {
withKubeConfig([credentialsId: 'cred1234', serverUrl: 'https://localhost:6443']) {
sh 'echo "OK"'
echo "File has been configured ${env.KUBECONFIG}"
}
}
}

0 comments on commit 2d3b1d4

Please sign in to comment.