From 6a7ea48f22f6ce4ab514bdd8bf837d2500aabaa0 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Wed, 20 Dec 2023 15:05:02 +0100 Subject: [PATCH 1/4] Checking that environment is the one we expect inside container() step --- pom.xml | 6 +++++ .../ContainerExecDecoratorPipelineTest.java | 11 ++++++++ .../containerEnvironmentIsHonored.groovy | 25 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy diff --git a/pom.xml b/pom.xml index 70b8b02e2c..b4153c781a 100644 --- a/pom.xml +++ b/pom.xml @@ -203,6 +203,12 @@ junit test + + org.jenkins-ci.plugins + pipeline-maven + 1362.vee39a_d4b_02b_1 + test + org.jenkins-ci.plugins scm-api 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 b87d69555d..aedc45ee9e 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 @@ -115,4 +115,15 @@ public void envVarDollarSignEscaping() throws Exception { r.assertLogContains("outside container: $string$with$dollars", b); r.assertLogContains("inside container: $string$with$dollars", b); } + + @Test + public void containerEnvironmentIsHonored() throws Exception { + assertNotNull(createJobThenScheduleRun()); + r.waitForCompletion(b); + r.assertLogContains( + "from Groovy: /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + r.assertLogContains("inside container: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + r.assertLogContains( + "inside withMaven in container: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + } } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy new file mode 100644 index 0000000000..566b8bb437 --- /dev/null +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy @@ -0,0 +1,25 @@ +podTemplate(yaml: ''' +spec: + containers: + - name: jnlp + image: jenkins/inbound-agent:3192.v713e3b_039fb_e-1 + # PATH=/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + - name: alpine + image: alpine:3.19.0 + # PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + command: + - sleep + args: + - infinity +''') { + node(POD_LABEL) { + echo "from Groovy: ${env.PATH}" + sh 'echo "outside container: $PATH"' + container('alpine') { + sh 'echo "inside container: $PATH"' + withMaven(publisherStrategy: 'EXPLICIT', traceability: false) { + sh 'echo "inside withMaven in container: $PATH"' + } + } + } +} From 17112b346b307c136458c7afc916909963779081 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Wed, 20 Dec 2023 16:00:01 +0100 Subject: [PATCH 2/4] Update test with failing override inside container --- pom.xml | 6 ------ .../pipeline/ContainerExecDecoratorPipelineTest.java | 8 +++++++- .../pipeline/containerEnvironmentIsHonored.groovy | 7 +++++-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index b4153c781a..70b8b02e2c 100644 --- a/pom.xml +++ b/pom.xml @@ -203,12 +203,6 @@ junit test - - org.jenkins-ci.plugins - pipeline-maven - 1362.vee39a_d4b_02b_1 - test - org.jenkins-ci.plugins scm-api 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 aedc45ee9e..73d64f46dd 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 @@ -122,8 +122,14 @@ public void containerEnvironmentIsHonored() throws Exception { r.waitForCompletion(b); r.assertLogContains( "from Groovy: /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + r.assertLogContains( + "outside container: /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + b); + r.assertLogContains( + "outside container with override: /bar:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + b); r.assertLogContains("inside container: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); r.assertLogContains( - "inside withMaven in container: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + "inside container with override: /bar:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); } } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy index 566b8bb437..98c1634569 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy @@ -15,10 +15,13 @@ spec: node(POD_LABEL) { echo "from Groovy: ${env.PATH}" sh 'echo "outside container: $PATH"' + withEnv(['PATH+foo=/bar']) { + sh 'echo "outside container with override: $PATH"' + } container('alpine') { sh 'echo "inside container: $PATH"' - withMaven(publisherStrategy: 'EXPLICIT', traceability: false) { - sh 'echo "inside withMaven in container: $PATH"' + withEnv(['PATH+foo=/bar']) { + sh 'echo "inside container with override: $PATH"' } } } From ff9005e3559cfb53d9b677087531aef7a5d739b3 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Wed, 20 Dec 2023 17:12:40 +0100 Subject: [PATCH 3/4] Update test --- .../ContainerExecDecoratorPipelineTest.java | 27 +++++++++++++++---- .../containerEnvironmentIsHonored.groovy | 13 +++++---- 2 files changed, 30 insertions(+), 10 deletions(-) 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 73d64f46dd..03e5e011a4 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 @@ -121,15 +121,32 @@ public void containerEnvironmentIsHonored() throws Exception { assertNotNull(createJobThenScheduleRun()); r.waitForCompletion(b); r.assertLogContains( - "from Groovy: /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + "from Groovy outside container: /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + b); + r.assertLogContains( + "from shell outside container: /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + b); r.assertLogContains( - "outside container: /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "from Groovy outside container with override: /bar:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); r.assertLogContains( - "outside container with override: /bar:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "from shell outside container with override: /bar:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); - r.assertLogContains("inside container: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + // When using groovy, the environment relies on the computer's environment. r.assertLogContains( - "inside container with override: /bar:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + "from Groovy inside container: /opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + b); + r.assertLogContains( + "from shell inside container: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); + // When using groovy, the environment relies on the computer's environment, using container step doesn't change + // anything. + r.assertLogContains( + "from Groovy inside container with override: /bar:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + b); + // TODO Currently fails because env override is applied to the computer's environment instead of the container's + // environment. + // r.assertLogContains( + // "from shell inside container with override: + // /bar:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); } } diff --git a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy index 98c1634569..1bc336e0ef 100644 --- a/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy +++ b/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/containerEnvironmentIsHonored.groovy @@ -13,15 +13,18 @@ spec: - infinity ''') { node(POD_LABEL) { - echo "from Groovy: ${env.PATH}" - sh 'echo "outside container: $PATH"' + echo "from Groovy outside container: ${env.PATH}" + sh 'echo "from shell outside container: $PATH"' withEnv(['PATH+foo=/bar']) { - sh 'echo "outside container with override: $PATH"' + echo "from Groovy outside container with override: ${env.PATH}" + sh 'echo "from shell outside container with override: $PATH"' } container('alpine') { - sh 'echo "inside container: $PATH"' + echo "from Groovy inside container: ${env.PATH}" + sh 'echo "from shell inside container: $PATH"' withEnv(['PATH+foo=/bar']) { - sh 'echo "inside container with override: $PATH"' + echo "from Groovy inside container with override: ${env.PATH}" + sh 'echo "from shell inside container with override: $PATH"' } } } From fdb014abc2f6dae2a5927ae89afcaeb381a1829e Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Thu, 21 Dec 2023 09:26:46 +0100 Subject: [PATCH 4/4] Note https://github.com/jenkinsci/kubernetes-plugin/pull/1486#discussion_r1432926821 --- .../pipeline/ContainerExecDecoratorPipelineTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 03e5e011a4..1eb59be3df 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 @@ -138,11 +138,11 @@ public void containerEnvironmentIsHonored() throws Exception { b); r.assertLogContains( "from shell inside container: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", b); - // When using groovy, the environment relies on the computer's environment, using container step doesn't change - // anything. - r.assertLogContains( - "from Groovy inside container with override: /bar:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", - b); + // TODO Using groovy within container, the agent environment is used instead of the container environment. + // r.assertLogContains( + // "from Groovy inside container with override: + // /bar:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + // b); // TODO Currently fails because env override is applied to the computer's environment instead of the container's // environment. // r.assertLogContains(