diff --git a/src/test/groovy/GsutilStepTests.groovy b/src/test/groovy/GsutilStepTests.groovy index c07033891..cd71d0c26 100644 --- a/src/test/groovy/GsutilStepTests.groovy +++ b/src/test/groovy/GsutilStepTests.groovy @@ -95,11 +95,22 @@ class GsutilStepTests extends ApmBasePipelineTest { } @Test - void test_without_gh_installed_by_default_no_wget() throws Exception { + void test_without_gh_installed_by_default_no_wget_no_curl() throws Exception { helper.registerAllowedMethod('isInstalled', [Map.class], { return false }) script.call(command: 'cp', credentialsId: 'foo') printCallStack() assertFalse(assertMethodCallContainsPattern('sh', 'wget -q -O')) + assertFalse(assertMethodCallContainsPattern('sh', 'curl')) + assertJobStatusSuccess() + } + + @Test + void test_without_gh_installed_by_default_no_wget() throws Exception { + helper.registerAllowedMethod('isInstalled', [Map.class], { m -> return !(m.tool.equals('wget') || m.tool.equals('gsutil'))}) + script.call(command: 'cp', credentialsId: 'foo') + printCallStack() + assertFalse(assertMethodCallContainsPattern('sh', 'wget -q -O gsutil.tar.gz')) + assertTrue(assertMethodCallContainsPattern('sh', 'curl -sSLo gsutil.tar.gz --retry 3 --retry-delay 2 --max-time 10')) assertJobStatusSuccess() } diff --git a/vars/gsutil.groovy b/vars/gsutil.groovy index 3a66203e7..2296fd277 100644 --- a/vars/gsutil.groovy +++ b/vars/gsutil.groovy @@ -46,16 +46,33 @@ def call(Map args = [:]) { def downloadInstaller(where) { def url = googleCloudSdkURL() def tarball = "gsutil.${isUnix() ? 'tar.gz' : 'zip'}" + + dir(where) { + if (!downloadWithWget(tarball, url)) { + downloadWithCurl(tarball, url) + } + uncompress(tarball) + } +} + +def downloadWithWget(tarball, url) { if(isInstalled(tool: 'wget', flag: '--version')) { - dir(where) { - retryWithSleep(retries: 3, seconds: 5, backoff: true) { - cmd(label: 'download gsutil', script: "wget -q -O ${tarball} ${url}") - uncompress(tarball) - } + retryWithSleep(retries: 3, seconds: 5, backoff: true) { + cmd(label: 'download gsutil', script: "wget -q -O ${tarball} ${url}") } + return true } else { log(level: 'WARN', text: 'gsutil: wget is not available. gsutil will not be installed then.') } + return false +} + +def downloadWithCurl(tarball, url) { + if(isInstalled(tool: 'curl', flag: '--version')) { + cmd(label: 'download gsutil', script: "curl -sSLo ${tarball} --retry 3 --retry-delay 2 --max-time 10 ${url}") + } else { + log(level: 'WARN', text: 'gsutil: curl is not available. gsutil will not be installed then.') + } } def googleCloudSdkURL() {