From f98c2add16f71209a965f7f7859478ba44e4f9e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 15 Apr 2021 17:52:18 +0200 Subject: [PATCH 01/17] docs: document how to run tests filtered by test name --- docs/DEVELOPMENT.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index bfb3a6cbd..df39f87be 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -77,6 +77,10 @@ execute the following from the directory at the root of this project: `./mvnw test -Dtest=DummyStepTests` +To run one single test, execute the following from the directory at the root of +this project, separating test name from test file with an `#`: + +`./mvnw test -Dtest=DummyStepTests#testName` To run tests and print additional debug output to the console, use the `-Pdebug` flag: From 54fe6676811e2e9d2707edf6e84df41a681ab912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 15 Apr 2021 17:59:05 +0200 Subject: [PATCH 02/17] feat: step to check if a docker image exists It will check the Docker host first, and then will try a docker pull. If it fails, it will log an error --- .../groovy/CheckDockerImageStepTests.groovy | 98 +++++++++++++++++++ vars/README.md | 9 ++ vars/checkDockerImage.groovy | 39 ++++++++ vars/checkDockerImage.txt | 7 ++ 4 files changed, 153 insertions(+) create mode 100644 src/test/groovy/CheckDockerImageStepTests.groovy create mode 100644 vars/checkDockerImage.groovy create mode 100644 vars/checkDockerImage.txt diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy new file mode 100644 index 000000000..e867ae7d0 --- /dev/null +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -0,0 +1,98 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import org.junit.Before +import org.junit.Test +import static org.junit.Assert.assertFalse +import static org.junit.Assert.assertTrue + +class CheckDockerImageStepTests extends ApmBasePipelineTest { + + @Override + @Before + void setUp() throws Exception { + super.setUp() + script = loadScript('vars/checkDockerImage.groovy') + } + + @Test + void testLinux_ImageDoesNotExists() throws Exception { + script.call(image: 'hello-world:latest') + printCallStack() + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist: pulling')) + assertJobStatusSuccess() + } + + @Test + void testLinux_ImageExists() throws Exception { + helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) + script.call(image: 'hello-world:latest') + printCallStack() + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) + assertJobStatusSuccess() + } + + @Test + void testLinux_PullError() throws Exception { + helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) + script.call(image: 'hello-world:latest') + printCallStack() + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'Docker pull for hello-world:latest failed')) + assertJobStatusSuccess() + } + + @Test + void testWindows_ImageDoesNotExists() throws Exception { + helper.registerAllowedMethod('isUnix', [], { false }) + script.call(image: 'hello-world:latest') + printCallStack() + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist: pulling')) + assertJobStatusSuccess() + } + + @Test + void testWindows_ImageExists() throws Exception { + helper.registerAllowedMethod('isUnix', [], { false }) + helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) + script.call(image: 'hello-world:latest') + printCallStack() + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) + assertJobStatusSuccess() + } + + @Test + void testWindows_PullError() throws Exception { + helper.registerAllowedMethod('isUnix', [], { false }) + helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) + script.call(image: 'hello-world:latest') + printCallStack() + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'Docker pull for hello-world:latest failed')) + assertJobStatusSuccess() + } +} diff --git a/vars/README.md b/vars/README.md index 6a6e6483e..14c192bf9 100644 --- a/vars/README.md +++ b/vars/README.md @@ -217,6 +217,15 @@ See https://issues.jenkins-ci.org/browse/JENKINS-43353 * maxBuildsToSearch: number of previous builds to be searched and aborted if so. Default to 10. +## checkDockerImage +Checks if the given Docker image exists. + +``` +checkDockerImage(image: 'hello-world:latest') +``` + +* image: Fully qualified name of the image + ## checkGitChanges use git diff to check the changes on a path, then return true or false. diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy new file mode 100644 index 000000000..9fb6ede8a --- /dev/null +++ b/vars/checkDockerImage.groovy @@ -0,0 +1,39 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +/** + Checks if the given Docker image exists. + + checkDockerImage(image: 'hello-world:latest') +*/ +def call(Map args = [:]) { + def image = args.containsKey('image') ? args.image : error('checkDockerImage: image parameter is required') + + def redirectStdout = isUnix() ? '2>/dev/null' : '2>NUL' + if (cmd(returnStatus: true, script: "docker images -q ${image} ${redirectStdout}") == 0) { + log(level: 'DEBUG', text: "${image} exists in the Docker host") + return + } + + log(level: 'DEBUG', text: "${image} does not exist: pulling") + if (cmd(returnStatus: true, script: "docker pull ${image}") == 0) { + return + } + + log(level: 'ERROR', text: "Docker pull for ${image} failed") + return +} diff --git a/vars/checkDockerImage.txt b/vars/checkDockerImage.txt new file mode 100644 index 000000000..ef4a80397 --- /dev/null +++ b/vars/checkDockerImage.txt @@ -0,0 +1,7 @@ +Checks if the given Docker image exists. + +``` +checkDockerImage(image: 'hello-world:latest') +``` + +* image: Full qualified name of the image From d4f0601402d91b24fb2bf9e0127fcb18f69f2bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Thu, 15 Apr 2021 18:15:05 +0200 Subject: [PATCH 03/17] chore: return boolean in the new step --- .../groovy/CheckDockerImageStepTests.groovy | 18 ++++++++++++------ vars/checkDockerImage.groovy | 10 ++++++---- vars/checkDockerImage.txt | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index e867ae7d0..5959787fc 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -31,8 +31,9 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { @Test void testLinux_ImageDoesNotExists() throws Exception { - script.call(image: 'hello-world:latest') + def ret = script.call(image: 'hello-world:latest') printCallStack() + assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist: pulling')) @@ -42,8 +43,9 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { @Test void testLinux_ImageExists() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) - script.call(image: 'hello-world:latest') + def ret = script.call(image: 'hello-world:latest') printCallStack() + assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) @@ -53,8 +55,9 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { @Test void testLinux_PullError() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) - script.call(image: 'hello-world:latest') + def ret = script.call(image: 'hello-world:latest') printCallStack() + assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'Docker pull for hello-world:latest failed')) @@ -64,8 +67,9 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { @Test void testWindows_ImageDoesNotExists() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) - script.call(image: 'hello-world:latest') + def ret = script.call(image: 'hello-world:latest') printCallStack() + assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist: pulling')) @@ -76,8 +80,9 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { void testWindows_ImageExists() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) - script.call(image: 'hello-world:latest') + def ret = script.call(image: 'hello-world:latest') printCallStack() + assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) @@ -88,8 +93,9 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { void testWindows_PullError() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) - script.call(image: 'hello-world:latest') + def ret = script.call(image: 'hello-world:latest') printCallStack() + assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'Docker pull for hello-world:latest failed')) diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy index 9fb6ede8a..b30f147b4 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/checkDockerImage.groovy @@ -18,7 +18,9 @@ /** Checks if the given Docker image exists. - checkDockerImage(image: 'hello-world:latest') + whenTrue(checkDockerImage(image: 'hello-world:latest')) { + ... + } */ def call(Map args = [:]) { def image = args.containsKey('image') ? args.image : error('checkDockerImage: image parameter is required') @@ -26,14 +28,14 @@ def call(Map args = [:]) { def redirectStdout = isUnix() ? '2>/dev/null' : '2>NUL' if (cmd(returnStatus: true, script: "docker images -q ${image} ${redirectStdout}") == 0) { log(level: 'DEBUG', text: "${image} exists in the Docker host") - return + return true } log(level: 'DEBUG', text: "${image} does not exist: pulling") if (cmd(returnStatus: true, script: "docker pull ${image}") == 0) { - return + return true } log(level: 'ERROR', text: "Docker pull for ${image} failed") - return + return false } diff --git a/vars/checkDockerImage.txt b/vars/checkDockerImage.txt index ef4a80397..ec0a5f5f0 100644 --- a/vars/checkDockerImage.txt +++ b/vars/checkDockerImage.txt @@ -4,4 +4,4 @@ Checks if the given Docker image exists. checkDockerImage(image: 'hello-world:latest') ``` -* image: Full qualified name of the image +* image: Fully qualified name of the image From 3ab7372a959a717fc075ae0d9600e601a33e58ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 19 Apr 2021 11:29:50 +0200 Subject: [PATCH 04/17] chore: support passing if the pull must happen --- .../groovy/CheckDockerImageStepTests.groovy | 60 +++++++++++++++++-- vars/README.md | 3 +- vars/checkDockerImage.groovy | 9 ++- vars/checkDockerImage.txt | 1 + 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index 5959787fc..d6d383bc4 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -30,18 +30,29 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } @Test - void testLinux_ImageDoesNotExists() throws Exception { + void testLinux_ImageDoesNotExists_NoPull() throws Exception { def ret = script.call(image: 'hello-world:latest') printCallStack() assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found')) + assertJobStatusSuccess() + } + + @Test + void testLinux_ImageDoesNotExists_Pull() throws Exception { + def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) + printCallStack() + assertFalse(ret) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist: pulling')) assertJobStatusSuccess() } @Test - void testLinux_ImageExists() throws Exception { + void testLinux_ImageExists_NoPull() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) def ret = script.call(image: 'hello-world:latest') printCallStack() @@ -52,10 +63,22 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertJobStatusSuccess() } + @Test + void testLinux_ImageExists_Pull() throws Exception { + helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) + def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) + printCallStack() + assertTrue(ret) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) + assertJobStatusSuccess() + } + @Test void testLinux_PullError() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) - def ret = script.call(image: 'hello-world:latest') + def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) @@ -65,19 +88,31 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } @Test - void testWindows_ImageDoesNotExists() throws Exception { + void testWindows_ImageDoesNotExists_Pull() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) def ret = script.call(image: 'hello-world:latest') printCallStack() assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found')) + assertJobStatusSuccess() + } + + @Test + void testWindows_ImageDoesNotExists_NoPull() throws Exception { + helper.registerAllowedMethod('isUnix', [], { false }) + def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) + printCallStack() + assertFalse(ret) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist: pulling')) assertJobStatusSuccess() } @Test - void testWindows_ImageExists() throws Exception { + void testWindows_ImageExists_NoPull() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) def ret = script.call(image: 'hello-world:latest') @@ -89,11 +124,24 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertJobStatusSuccess() } + @Test + void testWindows_ImageExists_Pull() throws Exception { + helper.registerAllowedMethod('isUnix', [], { false }) + helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) + def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) + printCallStack() + assertTrue(ret) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) + assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) + assertJobStatusSuccess() + } + @Test void testWindows_PullError() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) - def ret = script.call(image: 'hello-world:latest') + def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) diff --git a/vars/README.md b/vars/README.md index 14c192bf9..7bcf20ece 100644 --- a/vars/README.md +++ b/vars/README.md @@ -221,10 +221,11 @@ See https://issues.jenkins-ci.org/browse/JENKINS-43353 Checks if the given Docker image exists. ``` -checkDockerImage(image: 'hello-world:latest') +checkDockerImage(image: 'hello-world:latest', pullIfNotFound: true) ``` * image: Fully qualified name of the image +* pullIfNotFound: Whether to pull the image if it's not found or not ## checkGitChanges use git diff to check the changes on a path, then return true or false. diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy index b30f147b4..036eaf5c6 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/checkDockerImage.groovy @@ -18,12 +18,14 @@ /** Checks if the given Docker image exists. - whenTrue(checkDockerImage(image: 'hello-world:latest')) { + whenTrue(checkDockerImage(image: 'hello-world:latest')) { ... } + whenTrue(checkDockerImage(image: 'hello-world:latest', pullIfNotFound: true)) { ... } */ def call(Map args = [:]) { def image = args.containsKey('image') ? args.image : error('checkDockerImage: image parameter is required') + def pullIfNotFound = args.containsKey('pullIfNotFound') ? args.pullIfNotFound : false def redirectStdout = isUnix() ? '2>/dev/null' : '2>NUL' if (cmd(returnStatus: true, script: "docker images -q ${image} ${redirectStdout}") == 0) { @@ -31,6 +33,11 @@ def call(Map args = [:]) { return true } + if (!pullIfNotFound) { + log(level: 'DEBUG', text: "Not pulling ${image} although it was not found") + return false + } + log(level: 'DEBUG', text: "${image} does not exist: pulling") if (cmd(returnStatus: true, script: "docker pull ${image}") == 0) { return true diff --git a/vars/checkDockerImage.txt b/vars/checkDockerImage.txt index ec0a5f5f0..c9cafb827 100644 --- a/vars/checkDockerImage.txt +++ b/vars/checkDockerImage.txt @@ -5,3 +5,4 @@ checkDockerImage(image: 'hello-world:latest') ``` * image: Fully qualified name of the image +* pullIfNotFound: Whether to pull the image if it's not found or not From dacf11682ab6a446ee5e89a9d860fab4c82861ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 19 Apr 2021 11:48:31 +0200 Subject: [PATCH 05/17] chore: check Docker is installed or error --- src/test/groovy/CheckDockerImageStepTests.groovy | 14 ++++++++++++++ vars/checkDockerImage.groovy | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index d6d383bc4..db987e591 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -26,9 +26,23 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { @Before void setUp() throws Exception { super.setUp() + helper.registerAllowedMethod('isInstalled', [Map.class], { return true }) script = loadScript('vars/checkDockerImage.groovy') } + @Test + void testDockerIsNotInstalled() throws Exception { + helper.registerAllowedMethod('isInstalled', [Map.class], { return false }) + try { + script.call(image: 'hello-world:latest') + } catch(e){ + //NOOP + } + printCallStack() + assertTrue(assertMethodCallContainsPattern('error', 'Docker is not installed')) + assertJobStatusFailure() + } + @Test void testLinux_ImageDoesNotExists_NoPull() throws Exception { def ret = script.call(image: 'hello-world:latest') diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy index 036eaf5c6..8489a98ba 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/checkDockerImage.groovy @@ -24,6 +24,10 @@ } */ def call(Map args = [:]) { + if (!isInstalled(tool: 'docker', flag: '--version')) { + error 'Docker is not installed' + } + def image = args.containsKey('image') ? args.image : error('checkDockerImage: image parameter is required') def pullIfNotFound = args.containsKey('pullIfNotFound') ? args.pullIfNotFound : false From f44fbd177efc68d27b8e7acf0393bd62dfe5a7a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 19 Apr 2021 11:53:35 +0200 Subject: [PATCH 06/17] chore: enrich log message --- src/test/groovy/CheckDockerImageStepTests.groovy | 4 ++-- vars/checkDockerImage.groovy | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index db987e591..9694abb9f 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -50,7 +50,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) - assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found')) + assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found in the Docker host')) assertJobStatusSuccess() } @@ -109,7 +109,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) - assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found')) + assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found in the Docker host')) assertJobStatusSuccess() } diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy index 8489a98ba..746d9dd64 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/checkDockerImage.groovy @@ -38,7 +38,7 @@ def call(Map args = [:]) { } if (!pullIfNotFound) { - log(level: 'DEBUG', text: "Not pulling ${image} although it was not found") + log(level: 'DEBUG', text: "Not pulling ${image} although it was not found in the Docker host") return false } From 346c008c77649f9eb5d03bef9384efe6eabe88bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 19 Apr 2021 12:22:02 +0200 Subject: [PATCH 07/17] chore: use docker manifest instead of docker pull --- .../groovy/CheckDockerImageStepTests.groovy | 54 +++++++++---------- vars/checkDockerImage.groovy | 10 ++-- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index 9694abb9f..22b5cfe41 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -48,8 +48,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found in the Docker host')) assertJobStatusSuccess() } @@ -59,9 +58,10 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) - assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist: pulling')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist')) assertJobStatusSuccess() } @@ -71,8 +71,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) assertJobStatusSuccess() } @@ -83,45 +82,45 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) assertJobStatusSuccess() } @Test - void testLinux_PullError() throws Exception { + void testLinux_Pull_NotExists() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>/dev/null')) - assertTrue(assertMethodCallContainsPattern('log', 'Docker pull for hello-world:latest failed')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >/dev/null')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist')) assertJobStatusSuccess() } @Test - void testWindows_ImageDoesNotExists_Pull() throws Exception { + void testWindows_ImageDoesNotExists_NoPull() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) def ret = script.call(image: 'hello-world:latest') printCallStack() assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found in the Docker host')) assertJobStatusSuccess() } @Test - void testWindows_ImageDoesNotExists_NoPull() throws Exception { + void testWindows_ImageDoesNotExists_Pull() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) - assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist: pulling')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist')) assertJobStatusSuccess() } @@ -132,8 +131,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) assertJobStatusSuccess() } @@ -145,22 +143,22 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) assertJobStatusSuccess() } @Test - void testWindows_PullError() throws Exception { + void testWindows_Pull_NotExists() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest')) - assertTrue(assertMethodCallContainsPattern('cmd', '2>NUL')) - assertTrue(assertMethodCallContainsPattern('log', 'Docker pull for hello-world:latest failed')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >NUL')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist')) assertJobStatusSuccess() } } diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy index 746d9dd64..c56703a6f 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/checkDockerImage.groovy @@ -32,7 +32,7 @@ def call(Map args = [:]) { def pullIfNotFound = args.containsKey('pullIfNotFound') ? args.pullIfNotFound : false def redirectStdout = isUnix() ? '2>/dev/null' : '2>NUL' - if (cmd(returnStatus: true, script: "docker images -q ${image} ${redirectStdout}") == 0) { + if (cmd(returnStatus: true, script: "docker images -q ${image} ${redirectStdout}") == 0) { log(level: 'DEBUG', text: "${image} exists in the Docker host") return true } @@ -42,11 +42,13 @@ def call(Map args = [:]) { return false } - log(level: 'DEBUG', text: "${image} does not exist: pulling") - if (cmd(returnStatus: true, script: "docker pull ${image}") == 0) { + redirectStdout = isUnix() ? '>/dev/null' : '>NUL' + log(level: 'DEBUG', text: "${image} does not exist in the Docker host: checking registry") + if (cmd(returnStatus: true, script: "docker manifest ${image} ${redirectStdout}") == 0) { + log(level: 'DEBUG', text: "${image} exists in the Docker registry") return true } - log(level: 'ERROR', text: "Docker pull for ${image} failed") + log(level: 'ERROR', text: "${image} does not exist") return false } From 8281a688e7bddf1ac5f15eaafbf26373c0309a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 19 Apr 2021 12:55:50 +0200 Subject: [PATCH 08/17] chore: update tests --- .../groovy/CheckDockerImageStepTests.groovy | 24 ++++++++++++++----- vars/checkDockerImage.groovy | 2 +- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index 22b5cfe41..e4a1f5f88 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -55,13 +55,19 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { @Test void testLinux_ImageDoesNotExists_Pull() throws Exception { + helper.registerAllowedMethod('cmd', [Map.class], { m -> + if (m.script.contains('docker manifest')) { + return 0 + } + return 1 + }) def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() - assertFalse(ret) + assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >/dev/null')) - assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker registry')) assertJobStatusSuccess() } @@ -96,7 +102,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >/dev/null')) - assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist at all')) assertJobStatusSuccess() } @@ -114,13 +120,19 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { @Test void testWindows_ImageDoesNotExists_Pull() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) + helper.registerAllowedMethod('cmd', [Map.class], { m -> + if (m.script.contains('docker manifest')) { + return 0 + } + return 1 + }) def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) printCallStack() - assertFalse(ret) + assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >NUL')) - assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker registry')) assertJobStatusSuccess() } @@ -158,7 +170,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >NUL')) - assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist')) + assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist at all')) assertJobStatusSuccess() } } diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy index c56703a6f..81592116d 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/checkDockerImage.groovy @@ -49,6 +49,6 @@ def call(Map args = [:]) { return true } - log(level: 'ERROR', text: "${image} does not exist") + log(level: 'DEBUG', text: "${image} does not exist at all") return false } From 82dbe4c4792142ddcc74320299c7d6ecdbfa7b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 19 Apr 2021 12:56:36 +0200 Subject: [PATCH 09/17] chore: add test for windows --- src/test/groovy/CheckDockerImageStepTests.groovy | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index e4a1f5f88..7371f4b8c 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -31,7 +31,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } @Test - void testDockerIsNotInstalled() throws Exception { + void testLinux_DockerIsNotInstalled() throws Exception { helper.registerAllowedMethod('isInstalled', [Map.class], { return false }) try { script.call(image: 'hello-world:latest') @@ -106,6 +106,20 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertJobStatusSuccess() } + @Test + void testWindows_DockerIsNotInstalled() throws Exception { + helper.registerAllowedMethod('isUnix', [], { false }) + helper.registerAllowedMethod('isInstalled', [Map.class], { return false }) + try { + script.call(image: 'hello-world:latest') + } catch(e){ + //NOOP + } + printCallStack() + assertTrue(assertMethodCallContainsPattern('error', 'Docker is not installed')) + assertJobStatusFailure() + } + @Test void testWindows_ImageDoesNotExists_NoPull() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) From 162cba78ec1b8ed69f7189d00d0dc4dccbcd445d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 19 Apr 2021 13:30:32 +0200 Subject: [PATCH 10/17] chore: remove pull field --- .../groovy/CheckDockerImageStepTests.groovy | 62 +++---------------- vars/README.md | 3 +- vars/checkDockerImage.groovy | 9 +-- vars/checkDockerImage.txt | 1 - 4 files changed, 11 insertions(+), 64 deletions(-) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index 7371f4b8c..b7d68b979 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -44,24 +44,14 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } @Test - void testLinux_ImageDoesNotExists_NoPull() throws Exception { - def ret = script.call(image: 'hello-world:latest') - printCallStack() - assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) - assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found in the Docker host')) - assertJobStatusSuccess() - } - - @Test - void testLinux_ImageDoesNotExists_Pull() throws Exception { + void testLinux_ImageDoesNotExists() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> if (m.script.contains('docker manifest')) { return 0 } return 1 }) - def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) + def ret = script.call(image: 'hello-world:latest') printCallStack() assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) @@ -72,7 +62,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } @Test - void testLinux_ImageExists_NoPull() throws Exception { + void testLinux_ImageExists() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) def ret = script.call(image: 'hello-world:latest') printCallStack() @@ -83,20 +73,9 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } @Test - void testLinux_ImageExists_Pull() throws Exception { - helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) - def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) - printCallStack() - assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) - assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) - assertJobStatusSuccess() - } - - @Test - void testLinux_Pull_NotExists() throws Exception { + void testLinux_NotExists() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) - def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) + def ret = script.call(image: 'hello-world:latest') printCallStack() assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) @@ -120,17 +99,6 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertJobStatusFailure() } - @Test - void testWindows_ImageDoesNotExists_NoPull() throws Exception { - helper.registerAllowedMethod('isUnix', [], { false }) - def ret = script.call(image: 'hello-world:latest') - printCallStack() - assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) - assertTrue(assertMethodCallContainsPattern('log', 'Not pulling hello-world:latest although it was not found in the Docker host')) - assertJobStatusSuccess() - } - @Test void testWindows_ImageDoesNotExists_Pull() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) @@ -140,7 +108,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } return 1 }) - def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) + def ret = script.call(image: 'hello-world:latest') printCallStack() assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) @@ -151,7 +119,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } @Test - void testWindows_ImageExists_NoPull() throws Exception { + void testWindows_ImageExists() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) def ret = script.call(image: 'hello-world:latest') @@ -163,22 +131,10 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { } @Test - void testWindows_ImageExists_Pull() throws Exception { - helper.registerAllowedMethod('isUnix', [], { false }) - helper.registerAllowedMethod('cmd', [Map.class], { m -> 0 }) - def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) - printCallStack() - assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) - assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) - assertJobStatusSuccess() - } - - @Test - void testWindows_Pull_NotExists() throws Exception { + void testWindows_NotExists() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) - def ret = script.call(image: 'hello-world:latest', pullIfNotFound: true) + def ret = script.call(image: 'hello-world:latest') printCallStack() assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) diff --git a/vars/README.md b/vars/README.md index 7bcf20ece..14c192bf9 100644 --- a/vars/README.md +++ b/vars/README.md @@ -221,11 +221,10 @@ See https://issues.jenkins-ci.org/browse/JENKINS-43353 Checks if the given Docker image exists. ``` -checkDockerImage(image: 'hello-world:latest', pullIfNotFound: true) +checkDockerImage(image: 'hello-world:latest') ``` * image: Fully qualified name of the image -* pullIfNotFound: Whether to pull the image if it's not found or not ## checkGitChanges use git diff to check the changes on a path, then return true or false. diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy index 81592116d..224b05317 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/checkDockerImage.groovy @@ -18,8 +18,7 @@ /** Checks if the given Docker image exists. - whenTrue(checkDockerImage(image: 'hello-world:latest')) { ... } - whenTrue(checkDockerImage(image: 'hello-world:latest', pullIfNotFound: true)) { + whenTrue(checkDockerImage(image: 'hello-world:latest')) { ... } */ @@ -29,7 +28,6 @@ def call(Map args = [:]) { } def image = args.containsKey('image') ? args.image : error('checkDockerImage: image parameter is required') - def pullIfNotFound = args.containsKey('pullIfNotFound') ? args.pullIfNotFound : false def redirectStdout = isUnix() ? '2>/dev/null' : '2>NUL' if (cmd(returnStatus: true, script: "docker images -q ${image} ${redirectStdout}") == 0) { @@ -37,11 +35,6 @@ def call(Map args = [:]) { return true } - if (!pullIfNotFound) { - log(level: 'DEBUG', text: "Not pulling ${image} although it was not found in the Docker host") - return false - } - redirectStdout = isUnix() ? '>/dev/null' : '>NUL' log(level: 'DEBUG', text: "${image} does not exist in the Docker host: checking registry") if (cmd(returnStatus: true, script: "docker manifest ${image} ${redirectStdout}") == 0) { diff --git a/vars/checkDockerImage.txt b/vars/checkDockerImage.txt index c9cafb827..ec0a5f5f0 100644 --- a/vars/checkDockerImage.txt +++ b/vars/checkDockerImage.txt @@ -5,4 +5,3 @@ checkDockerImage(image: 'hello-world:latest') ``` * image: Fully qualified name of the image -* pullIfNotFound: Whether to pull the image if it's not found or not From 66d9b1c4c74f8033849ab898d40e62faca345374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 19 Apr 2021 18:29:41 +0200 Subject: [PATCH 11/17] fix: forgot the inspect subcommand --- src/test/groovy/CheckDockerImageStepTests.groovy | 12 ++++++------ vars/checkDockerImage.groovy | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/CheckDockerImageStepTests.groovy index b7d68b979..65fb9ba36 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/CheckDockerImageStepTests.groovy @@ -46,7 +46,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { @Test void testLinux_ImageDoesNotExists() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> - if (m.script.contains('docker manifest')) { + if (m.script.contains('docker manifest inspect')) { return 0 } return 1 @@ -56,7 +56,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker registry')) assertJobStatusSuccess() } @@ -80,7 +80,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist at all')) assertJobStatusSuccess() } @@ -103,7 +103,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { void testWindows_ImageDoesNotExists_Pull() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) helper.registerAllowedMethod('cmd', [Map.class], { m -> - if (m.script.contains('docker manifest')) { + if (m.script.contains('docker manifest inspect')) { return 0 } return 1 @@ -113,7 +113,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker registry')) assertJobStatusSuccess() } @@ -139,7 +139,7 @@ class CheckDockerImageStepTests extends ApmBasePipelineTest { assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest hello-world:latest >NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist at all')) assertJobStatusSuccess() } diff --git a/vars/checkDockerImage.groovy b/vars/checkDockerImage.groovy index 224b05317..36780c379 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/checkDockerImage.groovy @@ -37,7 +37,7 @@ def call(Map args = [:]) { redirectStdout = isUnix() ? '>/dev/null' : '>NUL' log(level: 'DEBUG', text: "${image} does not exist in the Docker host: checking registry") - if (cmd(returnStatus: true, script: "docker manifest ${image} ${redirectStdout}") == 0) { + if (cmd(returnStatus: true, script: "docker manifest inspect ${image} ${redirectStdout}") == 0) { log(level: 'DEBUG', text: "${image} exists in the Docker registry") return true } From e8098791cac93dfde8ea50aa332ad048a850b05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 20 Apr 2021 08:18:04 +0200 Subject: [PATCH 12/17] chore: rename step --- ...roovy => DockerImageExistsStepTests.groovy} | 4 ++-- vars/README.md | 18 +++++++++--------- ...erImage.groovy => dockerImageExists.groovy} | 4 ++-- ...ckDockerImage.txt => dockerImageExists.txt} | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) rename src/test/groovy/{CheckDockerImageStepTests.groovy => DockerImageExistsStepTests.groovy} (98%) rename vars/{checkDockerImage.groovy => dockerImageExists.groovy} (90%) rename vars/{checkDockerImage.txt => dockerImageExists.txt} (66%) diff --git a/src/test/groovy/CheckDockerImageStepTests.groovy b/src/test/groovy/DockerImageExistsStepTests.groovy similarity index 98% rename from src/test/groovy/CheckDockerImageStepTests.groovy rename to src/test/groovy/DockerImageExistsStepTests.groovy index 65fb9ba36..1bb32bfa1 100644 --- a/src/test/groovy/CheckDockerImageStepTests.groovy +++ b/src/test/groovy/DockerImageExistsStepTests.groovy @@ -20,14 +20,14 @@ import org.junit.Test import static org.junit.Assert.assertFalse import static org.junit.Assert.assertTrue -class CheckDockerImageStepTests extends ApmBasePipelineTest { +class DockerImageExistsStepTests extends ApmBasePipelineTest { @Override @Before void setUp() throws Exception { super.setUp() helper.registerAllowedMethod('isInstalled', [Map.class], { return true }) - script = loadScript('vars/checkDockerImage.groovy') + script = loadScript('vars/dockerImageExists.groovy') } @Test diff --git a/vars/README.md b/vars/README.md index 14c192bf9..d82ae179f 100644 --- a/vars/README.md +++ b/vars/README.md @@ -217,15 +217,6 @@ See https://issues.jenkins-ci.org/browse/JENKINS-43353 * maxBuildsToSearch: number of previous builds to be searched and aborted if so. Default to 10. -## checkDockerImage -Checks if the given Docker image exists. - -``` -checkDockerImage(image: 'hello-world:latest') -``` - -* image: Fully qualified name of the image - ## checkGitChanges use git diff to check the changes on a path, then return true or false. @@ -352,6 +343,15 @@ Generate the details URL to be added to the GitHub notifications. When possible * tab: What kind of details links will be used. Enum type: tests, changes, artifacts, pipeline or an ``). Default `pipeline`. * isBlueOcean: Whether to use the BlueOcean URLs. Default `false`. +## dockerImageExists +Checks if the given Docker image exists. + +``` +dockerImageExists(image: 'hello-world:latest') +``` + +* image: Fully qualified name of the image + ## dockerLogin Login to hub.docker.com with an authentication credentials from a Vault secret. The vault secret contains `user` and `password` fields with the authentication details. diff --git a/vars/checkDockerImage.groovy b/vars/dockerImageExists.groovy similarity index 90% rename from vars/checkDockerImage.groovy rename to vars/dockerImageExists.groovy index 36780c379..6c45bb04f 100644 --- a/vars/checkDockerImage.groovy +++ b/vars/dockerImageExists.groovy @@ -18,7 +18,7 @@ /** Checks if the given Docker image exists. - whenTrue(checkDockerImage(image: 'hello-world:latest')) { + whenTrue(dockerImageExists(image: 'hello-world:latest')) { ... } */ @@ -27,7 +27,7 @@ def call(Map args = [:]) { error 'Docker is not installed' } - def image = args.containsKey('image') ? args.image : error('checkDockerImage: image parameter is required') + def image = args.containsKey('image') ? args.image : error('dockerImageExists: image parameter is required') def redirectStdout = isUnix() ? '2>/dev/null' : '2>NUL' if (cmd(returnStatus: true, script: "docker images -q ${image} ${redirectStdout}") == 0) { diff --git a/vars/checkDockerImage.txt b/vars/dockerImageExists.txt similarity index 66% rename from vars/checkDockerImage.txt rename to vars/dockerImageExists.txt index ec0a5f5f0..755981f06 100644 --- a/vars/checkDockerImage.txt +++ b/vars/dockerImageExists.txt @@ -1,7 +1,7 @@ Checks if the given Docker image exists. ``` -checkDockerImage(image: 'hello-world:latest') +dockerImageExists(image: 'hello-world:latest') ``` * image: Fully qualified name of the image From 05ae989f5a65f8a59192677f3bba9453ca733935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 20 Apr 2021 15:57:44 +0200 Subject: [PATCH 13/17] fix: use a more accurate command to check for local images --- src/test/groovy/DockerImageExistsStepTests.groovy | 12 ++++++------ vars/dockerImageExists.groovy | 5 ++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/test/groovy/DockerImageExistsStepTests.groovy b/src/test/groovy/DockerImageExistsStepTests.groovy index 1bb32bfa1..dc2d79106 100644 --- a/src/test/groovy/DockerImageExistsStepTests.groovy +++ b/src/test/groovy/DockerImageExistsStepTests.groovy @@ -54,7 +54,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker registry')) @@ -67,7 +67,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) assertJobStatusSuccess() } @@ -78,7 +78,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >/dev/null')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist at all')) @@ -111,7 +111,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker registry')) @@ -125,7 +125,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertTrue(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker host')) assertJobStatusSuccess() } @@ -137,7 +137,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { def ret = script.call(image: 'hello-world:latest') printCallStack() assertFalse(ret) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker images -q hello-world:latest 2>NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >NUL')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist at all')) diff --git a/vars/dockerImageExists.groovy b/vars/dockerImageExists.groovy index 6c45bb04f..3f3d15782 100644 --- a/vars/dockerImageExists.groovy +++ b/vars/dockerImageExists.groovy @@ -29,13 +29,12 @@ def call(Map args = [:]) { def image = args.containsKey('image') ? args.image : error('dockerImageExists: image parameter is required') - def redirectStdout = isUnix() ? '2>/dev/null' : '2>NUL' - if (cmd(returnStatus: true, script: "docker images -q ${image} ${redirectStdout}") == 0) { + if (cmd(returnStatus: true, script: "docker inspect -f \"{{.Id}}\" ${image}") == 0) { log(level: 'DEBUG', text: "${image} exists in the Docker host") return true } - redirectStdout = isUnix() ? '>/dev/null' : '>NUL' + def redirectStdout = isUnix() ? '>/dev/null' : '>NUL' log(level: 'DEBUG', text: "${image} does not exist in the Docker host: checking registry") if (cmd(returnStatus: true, script: "docker manifest inspect ${image} ${redirectStdout}") == 0) { log(level: 'DEBUG', text: "${image} exists in the Docker registry") From 6d9223afb6412597fff5432a63b4ed30a5727bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 21 Apr 2021 11:04:39 +0200 Subject: [PATCH 14/17] chore: validate image param first --- vars/dockerImageExists.groovy | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vars/dockerImageExists.groovy b/vars/dockerImageExists.groovy index 3f3d15782..03c044978 100644 --- a/vars/dockerImageExists.groovy +++ b/vars/dockerImageExists.groovy @@ -23,12 +23,12 @@ } */ def call(Map args = [:]) { + def image = args.containsKey('image') ? args.image : error('dockerImageExists: image parameter is required') + if (!isInstalled(tool: 'docker', flag: '--version')) { error 'Docker is not installed' } - def image = args.containsKey('image') ? args.image : error('dockerImageExists: image parameter is required') - if (cmd(returnStatus: true, script: "docker inspect -f \"{{.Id}}\" ${image}") == 0) { log(level: 'DEBUG', text: "${image} exists in the Docker host") return true From 1116479b4d7cf317dfbd7f7c02d33520bf5a55e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 21 Apr 2021 11:06:23 +0200 Subject: [PATCH 15/17] chore: enrich error log --- vars/dockerImageExists.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vars/dockerImageExists.groovy b/vars/dockerImageExists.groovy index 03c044978..f3bbcb963 100644 --- a/vars/dockerImageExists.groovy +++ b/vars/dockerImageExists.groovy @@ -26,7 +26,7 @@ def call(Map args = [:]) { def image = args.containsKey('image') ? args.image : error('dockerImageExists: image parameter is required') if (!isInstalled(tool: 'docker', flag: '--version')) { - error 'Docker is not installed' + error 'dockerImageExists: Docker is not installed' } if (cmd(returnStatus: true, script: "docker inspect -f \"{{.Id}}\" ${image}") == 0) { From 51b298ec74d11881e61b6cf8a1543aeb042263d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 21 Apr 2021 11:06:41 +0200 Subject: [PATCH 16/17] chore: add tests for image param validation --- .../groovy/DockerImageExistsStepTests.groovy | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/test/groovy/DockerImageExistsStepTests.groovy b/src/test/groovy/DockerImageExistsStepTests.groovy index dc2d79106..2a4027611 100644 --- a/src/test/groovy/DockerImageExistsStepTests.groovy +++ b/src/test/groovy/DockerImageExistsStepTests.groovy @@ -39,7 +39,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { //NOOP } printCallStack() - assertTrue(assertMethodCallContainsPattern('error', 'Docker is not installed')) + assertTrue(assertMethodCallContainsPattern('error', 'dockerImageExists: Docker is not installed')) assertJobStatusFailure() } @@ -72,6 +72,18 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { assertJobStatusSuccess() } + @Test + void testLinux_ImageIsNotSet() throws Exception { + try { + script.call() + } catch(e){ + //NOOP + } + printCallStack() + assertTrue(assertMethodCallContainsPattern('error', 'dockerImageExists: image parameter is required')) + assertJobStatusFailure() + } + @Test void testLinux_NotExists() throws Exception { helper.registerAllowedMethod('cmd', [Map.class], { m -> 1 }) @@ -95,7 +107,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { //NOOP } printCallStack() - assertTrue(assertMethodCallContainsPattern('error', 'Docker is not installed')) + assertTrue(assertMethodCallContainsPattern('error', 'dockerImageExists: Docker is not installed')) assertJobStatusFailure() } @@ -130,6 +142,19 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { assertJobStatusSuccess() } + @Test + void testWindows_ImageIsNotSet() throws Exception { + helper.registerAllowedMethod('isUnix', [], { false }) + try { + script.call() + } catch(e){ + //NOOP + } + printCallStack() + assertTrue(assertMethodCallContainsPattern('error', 'dockerImageExists: image parameter is required')) + assertJobStatusFailure() + } + @Test void testWindows_NotExists() throws Exception { helper.registerAllowedMethod('isUnix', [], { false }) From 47ffb37bd2167cf9a600dfba41211038105dcaf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 21 Apr 2021 11:09:08 +0200 Subject: [PATCH 17/17] chore: remove stdout redirections --- src/test/groovy/DockerImageExistsStepTests.groovy | 8 ++++---- vars/dockerImageExists.groovy | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/test/groovy/DockerImageExistsStepTests.groovy b/src/test/groovy/DockerImageExistsStepTests.groovy index 2a4027611..08349cc3d 100644 --- a/src/test/groovy/DockerImageExistsStepTests.groovy +++ b/src/test/groovy/DockerImageExistsStepTests.groovy @@ -56,7 +56,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker registry')) assertJobStatusSuccess() } @@ -92,7 +92,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >/dev/null')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist at all')) assertJobStatusSuccess() } @@ -125,7 +125,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { assertTrue(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest exists in the Docker registry')) assertJobStatusSuccess() } @@ -164,7 +164,7 @@ class DockerImageExistsStepTests extends ApmBasePipelineTest { assertFalse(ret) assertTrue(assertMethodCallContainsPattern('cmd', 'docker inspect -f "{{.Id}}" hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist in the Docker host: checking registry')) - assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest >NUL')) + assertTrue(assertMethodCallContainsPattern('cmd', 'docker manifest inspect hello-world:latest')) assertTrue(assertMethodCallContainsPattern('log', 'hello-world:latest does not exist at all')) assertJobStatusSuccess() } diff --git a/vars/dockerImageExists.groovy b/vars/dockerImageExists.groovy index f3bbcb963..b4ece9b0c 100644 --- a/vars/dockerImageExists.groovy +++ b/vars/dockerImageExists.groovy @@ -34,9 +34,8 @@ def call(Map args = [:]) { return true } - def redirectStdout = isUnix() ? '>/dev/null' : '>NUL' log(level: 'DEBUG', text: "${image} does not exist in the Docker host: checking registry") - if (cmd(returnStatus: true, script: "docker manifest inspect ${image} ${redirectStdout}") == 0) { + if (cmd(returnStdout: true, returnStatus: true, script: "docker manifest inspect ${image}") == 0) { log(level: 'DEBUG', text: "${image} exists in the Docker registry") return true }