Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a generic method to create then run a pipeline based on the current method name #527

Merged
merged 4 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand All @@ -42,7 +43,12 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import hudson.Util;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.rules.TestName;

import com.google.common.collect.ImmutableMap;
Expand All @@ -61,6 +67,7 @@
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
import org.jvnet.hudson.test.JenkinsRule;

public class KubernetesTestUtil {

Expand Down Expand Up @@ -217,4 +224,31 @@ public static void createSecret(KubernetesClient client, String namespace) {
LOGGER.log(Level.INFO, "Created pod secret: {0}", secret);
}

public static String generateProjectName(String name) {
return name.replaceAll("([A-Z])", " $1");
}

public static WorkflowRun createPipelineJobThenScheduleRun(JenkinsRule r, Class cls, String methodName) throws InterruptedException, ExecutionException, IOException {
return createPipelineJobThenScheduleRun(r, cls, methodName, null);
}

public static WorkflowRun createPipelineJobThenScheduleRun(JenkinsRule r, Class cls, String methodName, Map<String, String> env) throws IOException, ExecutionException, InterruptedException {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, generateProjectName(methodName));
p.setDefinition(new CpsFlowDefinition(loadPipelineDefinition(cls, methodName, env), true));
return p.scheduleBuild2(0).waitForStart();
}

public static String loadPipelineDefinition(Class cls, String name, Map<String, String> providedEnv) {
Map<String, String> env = providedEnv == null ? new HashMap<>() : new HashMap<>(providedEnv);
env.put("NAME", name);
return Util.replaceMacro(loadPipelineScript(cls, name + ".groovy"), env);
}

public static String loadPipelineScript(Class<?> clazz, String name) {
try {
return new String(IOUtils.toByteArray(clazz.getResourceAsStream(name)));
} catch (Throwable t) {
throw new RuntimeException("Could not read resource:[" + name + "].");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,24 @@
import static java.util.Arrays.*;
import static org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil.*;

import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.csanchez.jenkins.plugins.kubernetes.ContainerEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.ContainerTemplate;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud;
import org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil;
import org.csanchez.jenkins.plugins.kubernetes.PodTemplate;
import org.csanchez.jenkins.plugins.kubernetes.model.KeyValueEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.model.SecretEnvVar;
import org.csanchez.jenkins.plugins.kubernetes.model.TemplateEnvVar;
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.ClassRule;
Expand Down Expand Up @@ -80,16 +85,59 @@ public static void isKubernetesConfigured() throws Exception {

private String projectName;

protected WorkflowJob p;

protected WorkflowRun b;

@Before
public void defineProjectName() {
// Add spaces before uppercases
this.projectName = name.getMethodName().replaceAll("([A-Z])", " $1");
this.projectName = generateProjectName(name.getMethodName());
}

protected String getProjectName() {
return projectName;
}

/**
* Creates a pipeline job using <methodName>.groovy as pipeline definition,
* then schedule it and wait for it to start.
*
* Resolves $NAME to the method name in order to avoid any hard-coded reference
* to the method name within the pipeline definition.
*
* @return The scheduled pipeline run
* @throws IOException If something gets wrong when creating the pipeline job
* @throws ExecutionException If something went wrong while retrieving the run object
* @throws InterruptedException If the thread gets interrupted while waiting for the run to start
*/
protected final WorkflowRun createJobThenScheduleRun() throws IOException, ExecutionException, InterruptedException {
return createJobThenScheduleRun(null);
}

/**
* Creates a pipeline job using <methodName>.groovy as pipeline definition,
* then schedule it and wait for it to start.
*
* Resolves $NAME to the method name in order to avoid any hard-coded reference
* to the method name within the pipeline definition. Also resolves any reference provided in the given env map.
*
* @param env an environment map to resolve in the pipeline script
* @return The scheduled pipeline run
* @throws IOException If something gets wrong when creating the pipeline job
* @throws ExecutionException If something went wrong while retrieving the run object
* @throws InterruptedException If the thread gets interrupted while waiting for the run to start
*/
protected final WorkflowRun createJobThenScheduleRun(Map<String, String> env) throws IOException, ExecutionException, InterruptedException {
b = createPipelineJobThenScheduleRun(r, getClass(), name.getMethodName(), env);
p = b.getParent();
return b;
}

protected final String loadPipelineDefinition() {
return KubernetesTestUtil.loadPipelineDefinition(getClass(), name.getMethodName(), null);
}

@Before
public void configureCloud() throws Exception {
cloud = setupCloud(this, name);
Expand Down Expand Up @@ -133,15 +181,7 @@ private PodTemplate buildBusyboxTemplate(String label) {
}

protected String loadPipelineScript(String name) {
return loadPipelineScript(getClass(), name);
}

public static String loadPipelineScript(Class<?> clazz, String name) {
try {
return new String(IOUtils.toByteArray(clazz.getResourceAsStream(name)));
} catch (Throwable t) {
throw new RuntimeException("Could not read resource:[" + name + "].");
}
return KubernetesTestUtil.loadPipelineScript(getClass(), name);
}

private static void setEnvVariables(PodTemplate podTemplate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@
import java.util.logging.Logger;

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.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.LoggerRule;

Expand All @@ -53,10 +49,7 @@ public void sshagent() throws Exception {
"ContainerExecDecoratorPipelineTest-sshagent", "bob", source, "secret_passphrase", "test credentials");
SystemCredentialsProvider.getInstance().getCredentials().add(credentials);

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "sshagent");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("sshagent.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
assertNotNull(createJobThenScheduleRun());
r.waitForCompletion(b);
r.assertLogContains("Identity added:", b);
//Assert that ssh-agent provided envVar is now properly contributed and set.
Expand All @@ -73,11 +66,8 @@ public void docker() throws Exception {
"ContainerExecDecoratorPipelineTest-docker", "bob", "myusername", "secret_password");
SystemCredentialsProvider.getInstance().getCredentials().add(credentials);

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "docker");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("docker.groovy"), true));
containerExecLogs.capture(1000);
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
assertNotNull(createJobThenScheduleRun());
r.waitForCompletion(b);
// docker login will fail but we can check that it runs the correct command
r.assertLogContains("Executing command: \"docker\" \"login\" \"-u\" \"myusername\" \"-p\" ******** \"https://index.docker.io/v1/\"", b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@

package org.csanchez.jenkins.plugins.kubernetes.pipeline;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.jvnet.hudson.test.Issue;

import static org.junit.Assert.assertNotNull;
Expand All @@ -30,11 +25,8 @@ public class ContainerLogStepTest extends AbstractKubernetesPipelineTest {

@Issue("JENKINS-46085")
@Test
public void simple() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "containerLog");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("getContainerLog.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
public void getContainerLog() throws Exception {
assertNotNull(createJobThenScheduleRun());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("INFO: Handshaking", b);
r.assertLogContains("INFO: Connected", b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@
import jenkins.plugins.git.GitStep;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jenkinsci.plugins.workflow.actions.ArgumentsAction;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner;
import org.jenkinsci.plugins.workflow.graphanalysis.FlowScanningUtils;
import org.jenkinsci.plugins.workflow.graphanalysis.NodeStepTypePredicate;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand All @@ -53,10 +51,7 @@ public class KubernetesDeclarativeAgentTest extends AbstractKubernetesPipelineTe
@Issue({"JENKINS-41758", "JENKINS-57827"})
@Test
public void declarative() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("declarative.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
assertNotNull(createJobThenScheduleRun());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR = " + CONTAINER_ENV_VAR_VALUE + "\n", b);
Expand All @@ -75,10 +70,7 @@ public void declarative() throws Exception {
@Issue("JENKINS-48135")
@Test
public void declarativeFromYaml() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("declarativeFromYaml.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
assertNotNull(createJobThenScheduleRun());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR = jnlp\n", b);
Expand All @@ -88,11 +80,8 @@ public void declarativeFromYaml() throws Exception {

@Issue("JENKINS-51610")
@Test
public void declarativeFromYamlWithNamespace() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("declarativeWithNamespaceFromYaml.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
public void declarativeWithNamespaceFromYaml() throws Exception {
assertNotNull(createJobThenScheduleRun());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR = jnlp\n", b);
Expand All @@ -104,15 +93,15 @@ public void declarativeFromYamlWithNamespace() throws Exception {
@Test
public void declarativeFromYamlFile() throws Exception {
repoRule.init();
repoRule.write("Jenkinsfile", loadPipelineScript("declarativeFromYamlFile.groovy"));
repoRule.write("Jenkinsfile", loadPipelineDefinition());
repoRule.write("declarativeYamlFile.yml", loadPipelineScript("declarativeYamlFile.yml"));
repoRule.git("add", "Jenkinsfile");
repoRule.git("add", "declarativeYamlFile.yml");
repoRule.git("commit", "--message=files");

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p.setDefinition(new CpsScmFlowDefinition(new GitStep(repoRule.toString()).createSCM(), "Jenkinsfile"));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
Expand All @@ -124,21 +113,18 @@ public void declarativeFromYamlFile() throws Exception {
@Issue("JENKINS-52623")
@Test
public void declarativeSCMVars() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with repo");
p = r.jenkins.createProject(WorkflowJob.class, "job with repo");
// We can't use a local GitSampleRepoRule for this because the repo has to be accessible from within the container.
p.setDefinition(new CpsScmFlowDefinition(new GitStep("https://github.com/abayer/jenkins-52623.git").createSCM(), "Jenkinsfile"));
WorkflowRun b = r.buildAndAssertSuccess(p);
b = r.buildAndAssertSuccess(p);
r.assertLogContains("Outside container: GIT_BRANCH is origin/master", b);
r.assertLogContains("In container: GIT_BRANCH is origin/master", b);
}

@Issue("JENKINS-53817")
@Test
public void declarativeUseCustomWorkspace() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with dir");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("declarativeCustomWorkspace.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
public void declarativeCustomWorkspace() throws Exception {
assertNotNull(createJobThenScheduleRun());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
r.assertLogContains("Workspace dir is", b);
Expand All @@ -147,10 +133,7 @@ public void declarativeUseCustomWorkspace() throws Exception {
@Issue("JENKINS-57548")
@Test
public void declarativeWithNestedExplicitInheritance() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "job with explicit nested inherit");
p.setDefinition(new CpsFlowDefinition(loadPipelineScript("declarativeWithNestedExplicitInheritance.groovy"), true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
assertNotNull(createJobThenScheduleRun());
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains("Apache Maven 3.3.9", b);
r.assertLogNotContains("go version go1.6.3", b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class KubernetesPipelineOverridenNamespaceTest extends AbstractKubernetesPipelineTest {

@Test
Expand All @@ -23,11 +23,7 @@ public void runWithCloudOverriddenNamespace() throws Exception {
new NamespaceBuilder().withNewMetadata().withName(overriddenNamespace).endMetadata().build());
}

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, getProjectName());
p.setDefinition(new CpsFlowDefinition(loadPipelineScript(name.getMethodName()+".groovy"), true));

WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);
assertNotNull(createJobThenScheduleRun());

r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains(overriddenNamespace, b);
Expand All @@ -48,13 +44,9 @@ public void runWithStepOverriddenNamespace() throws Exception {
new NamespaceBuilder().withNewMetadata().withName(stepNamespace).endMetadata().build());
}

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, getProjectName());
p.setDefinition(new CpsFlowDefinition(loadPipelineScript(name.getMethodName()+".groovy")
.replace("OVERRIDDEN_NAMESPACE", stepNamespace), true));

WorkflowRun b = p.scheduleBuild2(0).waitForStart();
assertNotNull(b);

Map<String, String> env = new HashMap<>();
env.put("OVERRIDDEN_NAMESPACE", stepNamespace);
assertNotNull(createJobThenScheduleRun(env));
r.assertBuildStatusSuccess(r.waitForCompletion(b));
r.assertLogContains(stepNamespace, b);
}
Expand Down
Loading