From 25b7800ceae49c0ef72c0955490010ed670da2c6 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Tue, 9 Oct 2018 10:20:58 +0200 Subject: [PATCH] Use SemaphoreStep --- .../pipeline/KubernetesPipelineTest.java | 54 +++++++++---------- .../kubernetes/pipeline/runIn2Pods.groovy | 17 +++--- 2 files changed, 35 insertions(+), 36 deletions(-) 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 3c9d47686e..a1af893626 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 @@ -37,6 +37,7 @@ 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.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -111,35 +112,30 @@ public void runIn2Pods() throws Exception { p.setDefinition(new CpsFlowDefinition(loadPipelineScript(name.getMethodName() + ".groovy"), true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); - List templates = cloud.getAllTemplates(); - - PodTemplate template = null; - while ((template = podTemplateWithLabel("mypod2", templates)) == null) { - LOGGER.log(Level.INFO, "Waiting for mypod2 template to be created"); - templates = cloud.getAllTemplates(); - Thread.sleep(1000); - } - - assertNull("mypod2 should not inherit from anything", template.getInheritFrom()); - - Map labels = getLabels(cloud, this); - LOGGER.log(Level.INFO, "Waiting for pods to be created with labels: {0}", labels); - PodList pods = cloud.connect().pods().withLabels(labels).list(); - while (pods.getItems().isEmpty()) { - LOGGER.log(Level.INFO, "Waiting for pods to be created with labels: {0}", labels); - pods = cloud.connect().pods().withLabels(labels).list(); - Thread.sleep(1000); - } - - assertEquals(Integer.MAX_VALUE, template.getInstanceCap()); - assertThat(template.getLabelsMap(), hasEntry("jenkins/mypod2", "true")); - - for (Pod pod : pods.getItems()) { - LOGGER.log(Level.INFO, "{0}", pod); - Map l = pod.getMetadata().getLabels(); - assertThat(l, hasEntry("jenkins", "slave")); - } - + SemaphoreStep.waitForStart("podTemplate1/1", b); + PodTemplate template1 = podTemplateWithLabel("mypod", cloud.getAllTemplates()); + SemaphoreStep.success("podTemplate1/1", null); + assertEquals(Integer.MAX_VALUE, template1.getInstanceCap()); + assertThat(template1.getLabelsMap(), hasEntry("jenkins/mypod", "true")); + SemaphoreStep.waitForStart("pod1/1", b); + Map labels1 = getLabels(cloud, this); + labels1.put("jenkins/mypod", "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 = podTemplateWithLabel("mypod2", cloud.getAllTemplates()); + 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()); + SemaphoreStep.waitForStart("pod2/1", b); + Map labels2 = getLabels(cloud, this); + labels1.put("jenkins/mypod2", "true"); + PodList pods2 = cloud.connect().pods().withLabels(labels2).list(); + assertTrue(!pods2.getItems().isEmpty()); + SemaphoreStep.success("pod2/1", null); r.assertBuildStatusSuccess(r.waitForCompletion(b)); r.assertLogContains("script file contents: ", b); assertFalse("There are pods leftover after test execution, see previous logs", 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 150abb19e1..a607a5a084 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,29 +1,32 @@ podTemplate(label: 'mypod', containers: [ containerTemplate(name: 'busybox', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - + semaphore 'podTemplate1' node ('mypod') { - stage('Run') { - container('busybox') { - sh """ + semaphore 'pod1' + stage('Run') { + container('busybox') { + sh """ ## durable-task plugin generates a script.sh file. ## echo "script file: \$(find ../../.. -iname script.sh))" echo "script file contents: \$(find ../../.. -iname script.sh -exec cat {} \\;)" test -n "\$(cat \$(find ../../.. -iname script.sh))" - """ + """ + } } - } } } podTemplate(label: 'mypod2', containers: [ containerTemplate(name: 'busybox2', image: 'busybox', ttyEnabled: true, command: '/bin/cat'), ]) { - + semaphore 'podTemplate2' node ('mypod2') { + semaphore 'pod2' stage('Run') { container('busybox2') { + sh """ ## durable-task plugin generates a script.sh file. ##