diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesTestUtil.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesTestUtil.java index f714b7bb5c..2b576caf3a 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesTestUtil.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesTestUtil.java @@ -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; @@ -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; @@ -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 { @@ -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 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 providedEnv) { + Map 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 + "]."); + } + } } \ No newline at end of file diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java index 1e851e393d..dfd30396d9 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java @@ -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; @@ -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 .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 .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 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); @@ -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) { diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorPipelineTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorPipelineTest.java index 9408c45287..a7af2714b2 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorPipelineTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorPipelineTest.java @@ -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; @@ -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. @@ -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); diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerLogStepTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerLogStepTest.java index 8013243b8e..c0862e688f 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerLogStepTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerLogStepTest.java @@ -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; @@ -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); diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesDeclarativeAgentTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesDeclarativeAgentTest.java index 986fc25acb..6d88c78c3a 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesDeclarativeAgentTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesDeclarativeAgentTest.java @@ -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; @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineOverridenNamespaceTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineOverridenNamespaceTest.java index 4b4cce9b85..e375454015 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineOverridenNamespaceTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineOverridenNamespaceTest.java @@ -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 @@ -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); @@ -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 env = new HashMap<>(); + env.put("OVERRIDDEN_NAMESPACE", stepNamespace); + assertNotNull(createJobThenScheduleRun(env)); r.assertBuildStatusSuccess(r.waitForCompletion(b)); r.assertLogContains(stepNamespace, b); } diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java index 647831897b..402883b53b 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java @@ -39,8 +39,6 @@ import io.fabric8.kubernetes.api.model.PodList; import org.csanchez.jenkins.plugins.kubernetes.PodAnnotation; import org.csanchez.jenkins.plugins.kubernetes.PodTemplate; -import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep; import org.junit.Before; @@ -52,7 +50,6 @@ import hudson.model.Result; import java.util.Locale; -import org.jvnet.hudson.test.Issue; /** * @author Carlos Sanchez @@ -64,18 +61,11 @@ public class KubernetesPipelineTest extends AbstractKubernetesPipelineTest { @Rule public TemporaryFolder tmp = new TemporaryFolder(); - WorkflowJob p; - - WorkflowRun b; - @Before public void setUp() throws Exception { deletePods(cloud.connect(), getLabels(cloud, this, name), false); logs.capture(1000); - p = r.jenkins.createProject(WorkflowJob.class, getProjectName()); - p.setDefinition(new CpsFlowDefinition(loadPipelineScript(name.getMethodName() + ".groovy"), true)); - b = p.scheduleBuild2(0).waitForStart(); - assertNotNull(b); + assertNotNull(createJobThenScheduleRun()); } @Test @@ -137,26 +127,28 @@ public void runInPod() throws Exception { @Test public void runIn2Pods() throws Exception { SemaphoreStep.waitForStart("podTemplate1/1", b); - PodTemplate template1 = podTemplatesWithLabel("mypod", cloud.getAllTemplates()).get(0); + String label1 = name.getMethodName() + "-1"; + PodTemplate template1 = podTemplatesWithLabel(label1, cloud.getAllTemplates()).get(0); SemaphoreStep.success("podTemplate1/1", null); assertEquals(Integer.MAX_VALUE, template1.getInstanceCap()); - assertThat(template1.getLabelsMap(), hasEntry("jenkins/mypod", "true")); + assertThat(template1.getLabelsMap(), hasEntry("jenkins/" + label1, "true")); SemaphoreStep.waitForStart("pod1/1", b); Map labels1 = getLabels(cloud, this, name); - labels1.put("jenkins/mypod", "true"); + labels1.put("jenkins/"+label1, "true"); PodList pods = cloud.connect().pods().withLabels(labels1).list(); assertTrue(!pods.getItems().isEmpty()); SemaphoreStep.success("pod1/1", null); SemaphoreStep.waitForStart("podTemplate2/1", b); - PodTemplate template2 = podTemplatesWithLabel("mypod2", cloud.getAllTemplates()).get(0); + String label2 = name.getMethodName() + "-2"; + PodTemplate template2 = podTemplatesWithLabel(label2, cloud.getAllTemplates()).get(0); SemaphoreStep.success("podTemplate2/1", null); assertEquals(Integer.MAX_VALUE, template2.getInstanceCap()); - assertThat(template2.getLabelsMap(), hasEntry("jenkins/mypod2", "true")); - assertNull("mypod2 should not inherit from anything", template2.getInheritFrom()); + assertThat(template2.getLabelsMap(), hasEntry("jenkins/" + label2, "true")); + assertNull(label2 + " should not inherit from anything", template2.getInheritFrom()); SemaphoreStep.waitForStart("pod2/1", b); Map labels2 = getLabels(cloud, this, name); - labels1.put("jenkins/mypod2", "true"); + labels1.put("jenkins/" + label2, "true"); PodList pods2 = cloud.connect().pods().withLabels(labels2).list(); assertTrue(!pods2.getItems().isEmpty()); SemaphoreStep.success("pod2/1", null); diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStepExecutionTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStepExecutionTest.java index c477011021..16555369b8 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStepExecutionTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStepExecutionTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.*; import org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud; +import org.csanchez.jenkins.plugins.kubernetes.KubernetesTestUtil; import org.csanchez.jenkins.plugins.kubernetes.Messages; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; @@ -52,7 +53,7 @@ public void configureCloud() throws Exception { } private String loadPipelineScript(String name) { - return AbstractKubernetesPipelineTest.loadPipelineScript(getClass(), name); + return KubernetesTestUtil.loadPipelineScript(getClass(), name); } @Test diff --git a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/RestartPipelineTest.java b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/RestartPipelineTest.java index 40314eec22..301ebf88e4 100644 --- a/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/RestartPipelineTest.java +++ b/src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/RestartPipelineTest.java @@ -27,9 +27,12 @@ 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.Collections; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; @@ -42,7 +45,6 @@ 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.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.BeforeClass; @@ -52,6 +54,7 @@ import org.junit.rules.TemporaryFolder; import org.junit.rules.TestName; import org.jvnet.hudson.test.BuildWatcher; +import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.LoggerRule; import org.jvnet.hudson.test.RestartableJenkinsNonLocalhostRule; @@ -147,66 +150,67 @@ protected String loadPipelineScript(String name) { @Test public void runInPodWithRestartWithMultipleContainerCalls() throws Exception { + AtomicReference projectName = new AtomicReference<>(); story.then(r -> { configureCloud(); r.jenkins.addNode(new DumbSlave("slave", "dummy", tmp.newFolder("remoteFS").getPath(), "1", Node.Mode.NORMAL, "", new JNLPLauncher(), RetentionStrategy.NOOP, Collections.>emptyList())); // TODO JENKINS-26398 clumsy - WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); - p.setDefinition(new CpsFlowDefinition(loadPipelineScript("runInPodWithRestartWithMultipleContainerCalls.groovy") - , true)); - WorkflowRun b = p.scheduleBuild2(0).waitForStart(); + WorkflowRun b = getPipelineJobThenScheduleRun(r); + projectName.set(b.getParent().getFullName()); // we need to wait until we are sure that the sh // step has started... r.waitForMessage("+ sleep 5", b); }); story.then(r -> { - WorkflowRun b = r.jenkins.getItemByFullName("p", WorkflowJob.class).getBuildByNumber(1); + WorkflowRun b = r.jenkins.getItemByFullName(projectName.get(), WorkflowJob.class).getBuildByNumber(1); r.assertLogContains("finished the test!", r.assertBuildStatusSuccess(r.waitForCompletion(b))); }); } @Test - public void runInPodWithRestart() throws Exception { + public void runInPodWithRestartWithLongSleep() throws Exception { + AtomicReference projectName = new AtomicReference<>(); story.then(r -> { configureCloud(); r.jenkins.addNode(new DumbSlave("slave", "dummy", tmp.newFolder("remoteFS").getPath(), "1", Node.Mode.NORMAL, "", new JNLPLauncher(), RetentionStrategy.NOOP, Collections.>emptyList())); // TODO JENKINS-26398 clumsy - WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); - p.setDefinition(new CpsFlowDefinition(loadPipelineScript("runInPodWithRestartWithLongSleep.groovy") - , true)); - WorkflowRun b = p.scheduleBuild2(0).waitForStart(); + WorkflowRun b = getPipelineJobThenScheduleRun(r); + projectName.set(b.getParent().getFullName()); // we need to wait until we are sure that the sh // step has started... r.waitForMessage("+ sleep 5", b); }); story.then(r -> { - WorkflowRun b = r.jenkins.getItemByFullName("p", WorkflowJob.class).getBuildByNumber(1); + WorkflowRun b = r.jenkins.getItemByFullName(projectName.get(), WorkflowJob.class).getBuildByNumber(1); r.assertLogContains("finished the test!", r.assertBuildStatusSuccess(r.waitForCompletion(b))); }); } @Test public void getContainerLogWithRestart() throws Exception { + AtomicReference projectName = new AtomicReference<>(); story.then(r -> { configureCloud(); r.jenkins.addNode(new DumbSlave("slave", "dummy", tmp.newFolder("remoteFS").getPath(), "1", Node.Mode.NORMAL, "", new JNLPLauncher(), RetentionStrategy.NOOP, Collections.>emptyList())); // TODO JENKINS-26398 clumsy - WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); - p.setDefinition(new CpsFlowDefinition(loadPipelineScript("getContainerLogWithRestart.groovy") - , true)); - WorkflowRun b = p.scheduleBuild2(0).waitForStart(); + WorkflowRun b = getPipelineJobThenScheduleRun(r); + projectName.set(b.getParent().getFullName()); // we need to wait until we are sure that the sh // step has started... r.waitForMessage("+ sleep 5", b); }); story.then(r -> { - WorkflowRun b = r.jenkins.getItemByFullName("p", WorkflowJob.class).getBuildByNumber(1); + WorkflowRun b = r.jenkins.getItemByFullName(projectName.get(), WorkflowJob.class).getBuildByNumber(1); r.assertBuildStatusSuccess(r.waitForCompletion(b)); r.assertLogContains("[Pipeline] containerLog", b); r.assertLogContains("[Pipeline] End of Pipeline", b); }); } + + private WorkflowRun getPipelineJobThenScheduleRun(JenkinsRule r) throws InterruptedException, ExecutionException, IOException { + return createPipelineJobThenScheduleRun(r, getClass(), name.getMethodName()); + } } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badcontainername.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badcontainername.groovy index cca421ae3d..b0f4229bfa 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badcontainername.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badcontainername.groovy @@ -1,8 +1,8 @@ -podTemplate(label: 'badcontainername', containers: [ +podTemplate(label: '$NAME', containers: [ containerTemplate(name: 'badcontainerName_!', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('badcontainername') { + node ('$NAME') { stage('Run') { container('busybox') { sh """ diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badcontainernameyaml.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badcontainernameyaml.groovy index c59c8f9178..350fb71d28 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badcontainernameyaml.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badcontainernameyaml.groovy @@ -1,4 +1,4 @@ -podTemplate(label: 'badcontainernameyaml', yaml: """ +podTemplate(label: '$NAME', yaml: """ apiVersion: v1 kind: Pod metadata: @@ -19,7 +19,7 @@ spec: """ ) { - node ('badcontainernameyaml') { + node ('$NAME') { stage('Run') { container('busybox') { sh """ diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badlabel.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badlabel.groovy index 9deb69e0f5..47281362f2 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badlabel.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/badlabel.groovy @@ -14,7 +14,7 @@ spec: """ ) { - node ('badlabel') { + node ('$NAME') { stage('Run') { container('busybox') { sh """ diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarative.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarative.groovy index 09f1f7b2cb..cc24b9ab19 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarative.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarative.groovy @@ -1,7 +1,7 @@ pipeline { agent { kubernetes { - label 'declarative' + label '$NAME' containerTemplate { name 'maven' image 'maven:3.3.9-jdk-8-alpine' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeCustomWorkspace.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeCustomWorkspace.groovy index c0f6fbed48..8e5ad84241 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeCustomWorkspace.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeCustomWorkspace.groovy @@ -1,7 +1,7 @@ pipeline { agent { kubernetes { - label 'declarative-custom-workspace-pod' + label '$NAME' customWorkspace 'some/other/path' defaultContainer 'maven' yaml """ diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeFromYaml.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeFromYaml.groovy index f49f7841bb..3468eb25b3 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeFromYaml.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeFromYaml.groovy @@ -1,7 +1,7 @@ pipeline { agent { kubernetes { - label 'declarativeFromYaml' + label '$NAME' yaml """ metadata: labels: diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeFromYamlFile.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeFromYamlFile.groovy index 9b6cae8eb5..097f1ce3a0 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeFromYamlFile.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeFromYamlFile.groovy @@ -1,7 +1,7 @@ pipeline { agent { kubernetes { - label 'declarativeFromYamlFile' + label '$NAME' yamlFile 'declarativeYamlFile.yml' } } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeWithNamespaceFromYaml.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeWithNamespaceFromYaml.groovy index 496fe0047c..b6779d159a 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeWithNamespaceFromYaml.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeWithNamespaceFromYaml.groovy @@ -1,7 +1,7 @@ pipeline { agent { kubernetes { - label 'declarativefromyaml-pod' + label '$NAME' yaml """ metadata: namespace: kubernetes-plugin-test-overridden-namespace diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeWithNestedExplicitInheritance.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeWithNestedExplicitInheritance.groovy index 2b6ead8f80..6618092e5f 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeWithNestedExplicitInheritance.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/declarativeWithNestedExplicitInheritance.groovy @@ -1,7 +1,7 @@ pipeline { agent { kubernetes { - label 'parent-pod' + label '$NAME-parent' yaml """ spec: containers: @@ -17,7 +17,7 @@ spec: stage('Run maven') { agent { kubernetes { - label 'nested-pod' + label '$NAME-nested' yaml """ spec: containers: diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/docker.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/docker.groovy index a177817113..91a59833c1 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/docker.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/docker.groovy @@ -1,7 +1,7 @@ pipeline { agent { kubernetes { - label 'docker' + label '$NAME' containerTemplate { name 'docker' image 'docker:1.11' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/getContainerLog.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/getContainerLog.groovy index e09c2a1fd5..3b7d387d3a 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/getContainerLog.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/getContainerLog.groovy @@ -1,6 +1,6 @@ //noinspection GrPackage -podTemplate(label: 'getContainerLog') { - node ('getContainerLog') { +podTemplate(label: '$NAME') { + node ('$NAME') { stage('container log') { containerLog 'jnlp' } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/getContainerLogWithRestart.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/getContainerLogWithRestart.groovy index f90337b697..cc9c213c25 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/getContainerLogWithRestart.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/getContainerLogWithRestart.groovy @@ -1,7 +1,7 @@ //noinspection GrPackage -podTemplate(label: 'getContainerLogWithRestart') +podTemplate(label: '$NAME') { - node ('getContainerLogWithRestart') { + node ('$NAME') { stage('container log') { sh 'for i in `seq 1 5`; do echo $i; sleep 5; done' containerLog 'jnlp' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runDirContext.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runDirContext.groovy index 23cba80d5e..befcaa5082 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runDirContext.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runDirContext.groovy @@ -1,8 +1,8 @@ -podTemplate(label: 'runDirContext', containers: [ +podTemplate(label: '$NAME', containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('runDirContext') { + node ('$NAME') { stage('Run') { container('busybox') { sh 'mkdir hz' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runIn2Pods.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runIn2Pods.groovy index 0c7b058836..6a3c41179e 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runIn2Pods.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runIn2Pods.groovy @@ -1,8 +1,8 @@ -podTemplate(label: 'mypod', containers: [ +podTemplate(label: '$NAME-1', containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { semaphore 'podTemplate1' - node ('mypod') { + node ('$NAME-1') { semaphore 'pod1' stage('Run') { container('busybox') { @@ -18,11 +18,11 @@ podTemplate(label: 'mypod', containers: [ } } -podTemplate(label: 'mypod2', containers: [ +podTemplate(label: '$NAME-2', containers: [ containerTemplate(name: 'busybox2', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { semaphore 'podTemplate2' - node ('mypod2') { + node ('$NAME-2') { semaphore 'pod2' stage('Run') { container('busybox2') { diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPod.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPod.groovy index 3c480c5639..4c52a975b2 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPod.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPod.groovy @@ -1,8 +1,8 @@ -podTemplate(label: 'runInPod', containers: [ +podTemplate(label: '$NAME', containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { semaphore 'podTemplate' - node ('runInPod') { + node ('$NAME') { semaphore 'pod' stage('Run') { container('busybox') { diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodFromYaml.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodFromYaml.groovy index 9426495c58..6e7066f59e 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodFromYaml.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodFromYaml.groovy @@ -1,4 +1,4 @@ -podTemplate(label: 'runInPodFromYaml', yaml: """ +podTemplate(label: '$NAME', yaml: """ apiVersion: v1 kind: Pod metadata: @@ -22,7 +22,7 @@ spec: """ ) { - node ('runInPodFromYaml') { + node ('$NAME') { stage('Run') { container('busybox') { sh '''set +x diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodNested.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodNested.groovy index 2d9cda0717..354fae2379 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodNested.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodNested.groovy @@ -1,12 +1,12 @@ -podTemplate(label: 'runInPodNested1', containers: [ +podTemplate(label: '$NAME-parent', containers: [ containerTemplate(name: 'golang', image: 'golang:1.6.3-alpine', ttyEnabled: true, command: '/bin/cat'), ]) { - podTemplate(label: 'runInPodNested', containers: [ + podTemplate(label: '$NAME', containers: [ containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('runInPodNested') { + node ('$NAME') { stage('Nested') { container('maven') { sh "mvn -version" diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodNestedExplicitInherit.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodNestedExplicitInherit.groovy index 71bab058ca..c52609563a 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodNestedExplicitInherit.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodNestedExplicitInherit.groovy @@ -1,12 +1,12 @@ -podTemplate(label: 'runInPodNestedExplicitInherit1', containers: [ +podTemplate(label: '$NAME-parent', containers: [ containerTemplate(name: 'golang', image: 'golang:1.6.3-alpine', ttyEnabled: true, command: '/bin/cat'), ]) { - podTemplate(label: 'runInPodNestedExplicitInherit', inheritFrom: '', containers: [ + podTemplate(label: '$NAME', inheritFrom: '', containers: [ containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('runInPodNestedExplicitInherit') { + node ('$NAME') { stage('Nested') { container('maven') { sh "mvn -version" diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithDifferentShell.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithDifferentShell.groovy index c2fadce7ed..5275a1b36a 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithDifferentShell.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithDifferentShell.groovy @@ -1,8 +1,8 @@ -podTemplate(label: 'runInPodWithDifferentShell', containers: [ +podTemplate(label: '$NAME', containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('runInPodWithDifferentShell') { + node ('$NAME') { stage('Run') { container(name:'busybox', shell: '/bin/bash') { sh """ diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithLivenessProbe.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithLivenessProbe.groovy index 3ec7ca779f..a7cfcb776b 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithLivenessProbe.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithLivenessProbe.groovy @@ -1,9 +1,9 @@ -podTemplate(label: 'runInPodWithLivenessProbe', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [ +podTemplate(label: '$NAME', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [ containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}'), containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: 'cat', livenessProbe: containerLivenessProbe( execArgs: 'uname -a', initialDelaySeconds: 5, timeoutSeconds: 1, failureThreshold: 3, periodSeconds: 10, successThreshold: 1)) ]) { - node ('runInPodWithLivenessProbe') { + node ('$NAME') { stage('Wait for Liveness Probe') { container('busybox') { sh 'sleep 6 && echo "Still alive"' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithMultipleContainers.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithMultipleContainers.groovy index 3bcaf700ea..3b89dc2b7c 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithMultipleContainers.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithMultipleContainers.groovy @@ -1,10 +1,10 @@ -podTemplate(label: 'runInPodWithMultipleContainers', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [ +podTemplate(label: '$NAME', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [ containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}'), containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'), containerTemplate(name: 'golang', image: 'golang:1.6.3-alpine', ttyEnabled: true, command: 'cat') ]) { - node ('runInPodWithMultipleContainers') { + node ('$NAME') { sh "echo My Kubernetes Pipeline" sh "ls /" diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestart.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestart.groovy index 2ad710a656..9d12e5d94b 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestart.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestart.groovy @@ -1,10 +1,10 @@ package org.csanchez.jenkins.plugins.kubernetes.pipeline -podTemplate(label: 'runInPodWithRestart', containers: [ +podTemplate(label: '$NAME', containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('runInPodWithRestart') { + node ('$NAME') { stage('Run') { container('busybox') { sh 'mkdir hz' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestartWithLongSleep.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestartWithLongSleep.groovy index b3d1f8178f..a8e28d0645 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestartWithLongSleep.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestartWithLongSleep.groovy @@ -1,10 +1,10 @@ package org.csanchez.jenkins.plugins.kubernetes.pipeline -podTemplate(label: 'runInPodWithRestartWithLongSleep', containers: [ +podTemplate(label: '$NAME', containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('runInPodWithRestartWithLongSleep') { + node ('$NAME') { stage('Run') { container('busybox') { sh 'for i in `seq 1 10`; do echo $i; sleep 5; done' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestartWithMultipleContainerCalls.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestartWithMultipleContainerCalls.groovy index eb7c33a24c..87aad8d295 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestartWithMultipleContainerCalls.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRestartWithMultipleContainerCalls.groovy @@ -1,10 +1,10 @@ package org.csanchez.jenkins.plugins.kubernetes.pipeline -podTemplate(label: 'runInPodWithRestartWithMultipleContainerCalls', idleMinutes: 0, containers: [ +podTemplate(label: '$NAME', idleMinutes: 0, containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('runInPodWithRestartWithMultipleContainerCalls') { + node ('$NAME') { stage('Run') { container('busybox') { sh 'for i in `seq 1 10`; do echo $i; sleep 5; done' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRetention.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRetention.groovy index 575357f4c8..f909aa1456 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRetention.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runInPodWithRetention.groovy @@ -1,8 +1,8 @@ -podTemplate(label: 'runInPodWithRetention', podRetention: always(), containers: [ +podTemplate(label: '$NAME', podRetention: always(), containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - node ('runInPodWithRetention') { + node ('$NAME') { stage('Run') { container('busybox') { sh """ diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithActiveDeadlineSeconds.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithActiveDeadlineSeconds.groovy index 7385797db6..e4171bfba8 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithActiveDeadlineSeconds.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithActiveDeadlineSeconds.groovy @@ -1,8 +1,8 @@ -podTemplate(label: 'runWithActiveDeadlineSeconds', activeDeadlineSeconds: 10, containers: [ +podTemplate(label: '$NAME', activeDeadlineSeconds: 10, containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { semaphore 'podTemplate' - node ('runWithActiveDeadlineSeconds') { + node ('$NAME') { stage('Run') { container('busybox') { sh """ diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithCloudOverriddenNamespace.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithCloudOverriddenNamespace.groovy index 71697a9164..4135e59325 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithCloudOverriddenNamespace.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithCloudOverriddenNamespace.groovy @@ -1,8 +1,8 @@ -podTemplate(label: 'runWithOverriddenNamespace', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [ +podTemplate(label: '$NAME', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [ containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}') ]) { - node ('runWithOverriddenNamespace') { + node ('$NAME') { container(name: 'jnlp') { sh "cat /var/run/secrets/kubernetes.io/serviceaccount/namespace" } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVariables.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVariables.groovy index dbc8ea84d1..4367a84548 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVariables.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVariables.groovy @@ -1,4 +1,4 @@ -podTemplate(label: 'runWithEnvVars', +podTemplate(label: '$NAME', envVars: [ envVar(key: 'POD_ENV_VAR', value: 'pod-env-var-value'), secretEnvVar(key: 'POD_ENV_VAR_FROM_SECRET', secretName: 'pod-secret', secretKey: 'password') @@ -15,7 +15,7 @@ podTemplate(label: 'runWithEnvVars', containerTemplate(name: 'java8', image: 'openjdk:8u151-jre-alpine', ttyEnabled: true, command: '/bin/cat') ]) { - node ('runWithEnvVars') { + node ('$NAME') { sh '''set +x echo OUTSIDE_CONTAINER_BUILD_NUMBER = $BUILD_NUMBER diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVariablesInContext.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVariablesInContext.groovy index 56cf2945b7..9285fea3a3 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVariablesInContext.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithEnvVariablesInContext.groovy @@ -1,5 +1,5 @@ //noinspection GrPackage -podTemplate(label: 'runWithEnvVarsFromContext', +podTemplate(label: '$NAME', envVars: [ envVar(key: 'POD_ENV_VAR', value: 'pod-env-var-value'), ], @@ -9,7 +9,7 @@ podTemplate(label: 'runWithEnvVarsFromContext', //we should expect outer environment variables to show up here. env.FROM_ENV_DEFINITION = "ABC" - node ('runWithEnvVarsFromContext') { + node ('$NAME') { stage('Run busybox') { sh 'echo before withEnv' sh ''' diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithOverriddenEnvVariables.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithOverriddenEnvVariables.groovy index d7bbc49c48..94fa83110c 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithOverriddenEnvVariables.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithOverriddenEnvVariables.groovy @@ -1,4 +1,4 @@ -podTemplate(label: 'runWithOverriddenEnvVars', +podTemplate(label: '$NAME', envVars: [ envVar(key: 'POD_ENV_VAR', value: 'pod-env-var-value-first'), envVar(key: 'POD_ENV_VAR', value: 'pod-env-var-value') @@ -13,7 +13,7 @@ podTemplate(label: 'runWithOverriddenEnvVars', ), ]) { - node ('runWithOverriddenEnvVars') { + node ('$NAME') { sh ''' echo OUTSIDE_CONTAINER_HOME_ENV_VAR = $HOME echo OUTSIDE_CONTAINER_POD_ENV_VAR = $POD_ENV_VAR diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithStepOverriddenNamespace.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithStepOverriddenNamespace.groovy index 6b701b1ed1..1fb2a4c4b1 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithStepOverriddenNamespace.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/runWithStepOverriddenNamespace.groovy @@ -1,12 +1,12 @@ // Step namespace should have priority over anything else. podTemplate( - namespace: 'OVERRIDDEN_NAMESPACE', label: 'runWithStepOverriddenNamespace', + namespace: '$OVERRIDDEN_NAMESPACE', label: '$NAME', volumes: [emptyDirVolume(mountPath: '/my-mount')], containers: [ containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-slave:3.10-1-alpine', args: '${computer.jnlpmac} ${computer.name}') ]) { - node ('runWithStepOverriddenNamespace') { + node ('$NAME') { container(name: 'jnlp') { sh "cat /var/run/secrets/kubernetes.io/serviceaccount/namespace" } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/sshagent.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/sshagent.groovy index 49c9fba59a..f90983c4d1 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/sshagent.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/sshagent.groovy @@ -1,7 +1,7 @@ -podTemplate(label: 'sshagent', containers: [ +podTemplate(label: '$NAME', containers: [ containerTemplate(name: 'ssh-client', image: 'kroniak/ssh-client:3.6', ttyEnabled: true, command: 'cat') ]) { - node ('sshagent') { + node ('$NAME') { stage('container log') { container('ssh-client') { sshagent (credentials: ['ContainerExecDecoratorPipelineTest-sshagent']) { diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/supportComputerEnvVars.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/supportComputerEnvVars.groovy index a336535f4d..780ff7b59a 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/supportComputerEnvVars.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/supportComputerEnvVars.groovy @@ -2,7 +2,7 @@ pipeline { agent { kubernetes{ - label 'buildPropertyVars' + label '$NAME' containerTemplate{ name 'openjdk' image 'openjdk'