mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
Remove curl for unstashing binary (#1580)
This commit is contained in:
parent
0857c9a3c6
commit
8b296f7420
@ -21,6 +21,23 @@ static def isPluginActive(pluginId) {
|
||||
return Jenkins.instance.pluginManager.plugins.find { p -> p.isActive() && p.getShortName() == pluginId }
|
||||
}
|
||||
|
||||
@API
|
||||
@NonCPS
|
||||
static void assertPluginIsActive(String pluginName) {
|
||||
if (pluginName == null || pluginName.empty) {
|
||||
throw new RuntimeException("Plugin name cannot be null or empty.")
|
||||
}
|
||||
|
||||
if (!JenkinsUtils.isPluginActive(pluginName)) {
|
||||
String exception = """[ERROR] Plugin '${pluginName}' is not installed or not active.
|
||||
| Please update the Jenkins image to the latest available version.
|
||||
| For more information how to update the image please visit:
|
||||
| https://github.com/SAP/devops-docker-cx-server/blob/master/docs/operations/cx-server-operations-guide.md#update-image
|
||||
| """.stripMargin().stripIndent()
|
||||
throw new RuntimeException(exception)
|
||||
}
|
||||
}
|
||||
|
||||
static boolean hasTestFailures(build){
|
||||
//build: https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html
|
||||
//getRawBuild: https://javadoc.jenkins.io/plugin/workflow-job/org/jenkinsci/plugins/workflow/job/WorkflowRun.html
|
||||
|
@ -26,7 +26,7 @@ class PiperGoUtils implements Serializable {
|
||||
if (steps.env.REPOSITORY_UNDER_TEST && steps.env.LIBRARY_VERSION_UNDER_TEST) {
|
||||
steps.echo("Running in a consumer test, building unit-under-test binary for verification.")
|
||||
steps.dockerExecute(script: steps, dockerImage: 'golang:1.13', dockerOptions: '-u 0', dockerEnvVars: [
|
||||
REPOSITORY_UNDER_TEST: steps.env.REPOSITORY_UNDER_TEST,
|
||||
REPOSITORY_UNDER_TEST : steps.env.REPOSITORY_UNDER_TEST,
|
||||
LIBRARY_VERSION_UNDER_TEST: steps.env.LIBRARY_VERSION_UNDER_TEST
|
||||
]) {
|
||||
steps.sh 'wget https://github.com/$REPOSITORY_UNDER_TEST/archive/$LIBRARY_VERSION_UNDER_TEST.tar.gz'
|
||||
@ -39,19 +39,21 @@ class PiperGoUtils implements Serializable {
|
||||
} else {
|
||||
def libraries = getLibrariesInfo()
|
||||
String version
|
||||
libraries.each {lib ->
|
||||
libraries.each { lib ->
|
||||
if (lib.name == 'piper-lib-os') {
|
||||
version = lib.version
|
||||
}
|
||||
}
|
||||
|
||||
JenkinsUtils.assertPluginIsActive('http_request')
|
||||
|
||||
def fallbackUrl = 'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master'
|
||||
def piperBinUrl = (version == 'master') ? fallbackUrl : "https://github.com/SAP/jenkins-library/releases/download/${version}/piper"
|
||||
|
||||
boolean downloaded = downloadGoBinary(piperBinUrl)
|
||||
if (!downloaded) {
|
||||
//Inform that no Piper binary is available for used library branch
|
||||
steps.echo ("Not able to download go binary of Piper for version ${version}")
|
||||
steps.echo("Not able to download go binary of Piper for version ${version}")
|
||||
//Fallback to master version & throw error in case this fails
|
||||
steps.retry(5) {
|
||||
if (!downloadGoBinary(fallbackUrl)) {
|
||||
@ -76,15 +78,11 @@ class PiperGoUtils implements Serializable {
|
||||
}
|
||||
|
||||
private boolean downloadGoBinary(url) {
|
||||
|
||||
try {
|
||||
def httpStatus = steps.sh(returnStdout: true, script: "curl --insecure --silent --location --write-out '%{http_code}' --output ${piperExecutable} '${url}'")
|
||||
|
||||
if (httpStatus == '200') {
|
||||
steps.sh(script: "chmod +x ${piperExecutable}")
|
||||
return true
|
||||
}
|
||||
} catch(err) {
|
||||
steps.httpRequest ignoreSslErrors: true, outputFile: piperExecutable, url: url, validResponseCodes: '200'
|
||||
steps.sh(script: "chmod +x ${piperExecutable}")
|
||||
return true
|
||||
} catch (err) {
|
||||
//nothing to do since error should just result in downloaded=false
|
||||
steps.echo "Failed downloading Piper go binary with error '${err}'"
|
||||
}
|
||||
|
@ -7,8 +7,10 @@ import org.junit.Test
|
||||
import org.junit.rules.ExpectedException
|
||||
import org.junit.rules.RuleChain
|
||||
import util.BasePiperTest
|
||||
import util.JenkinsHttpRequestRule
|
||||
import util.JenkinsLoggingRule
|
||||
import util.JenkinsShellCallRule
|
||||
import util.PluginMock
|
||||
import util.Rules
|
||||
|
||||
import static org.hamcrest.Matchers.containsString
|
||||
@ -20,16 +22,19 @@ class PiperGoUtilsTest extends BasePiperTest {
|
||||
public ExpectedException exception = ExpectedException.none()
|
||||
public JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
|
||||
public JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
|
||||
public JenkinsHttpRequestRule httpRequestRule = new JenkinsHttpRequestRule(this)
|
||||
|
||||
@Rule
|
||||
public RuleChain ruleChain = Rules.getCommonRules(this)
|
||||
.around(shellCallRule)
|
||||
.around(exception)
|
||||
.around(loggingRule)
|
||||
.around(httpRequestRule)
|
||||
|
||||
@Before
|
||||
void init() {
|
||||
helper.registerAllowedMethod("retry", [Integer, Closure], null)
|
||||
JenkinsUtils.metaClass.static.isPluginActive = { def s -> new PluginMock(s).isActive() }
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -55,88 +60,88 @@ class PiperGoUtilsTest extends BasePiperTest {
|
||||
void testUnstashPiperBinMaster() {
|
||||
|
||||
def piperGoUtils = new PiperGoUtils(nullScript, utils)
|
||||
piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'master']]}
|
||||
piperGoUtils.metaClass.getLibrariesInfo = { -> return [[name: 'piper-lib-os', version: 'master']] }
|
||||
|
||||
// this mocks utils.unstash - mimic stash not existing
|
||||
helper.registerAllowedMethod("unstash", [String.class], { stashFileName ->
|
||||
return []
|
||||
})
|
||||
|
||||
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '200')
|
||||
httpRequestRule.mockUrl("https://github.com/SAP/jenkins-library/releases/latest/download/piper_master", {})
|
||||
|
||||
piperGoUtils.unstashPiperBin()
|
||||
assertThat(shellCallRule.shell.size(), is(3))
|
||||
assertThat(shellCallRule.shell[0].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\''))
|
||||
assertThat(shellCallRule.shell[1].toString(), is('chmod +x piper'))
|
||||
assertThat(shellCallRule.shell[2].toString(), is('./piper version'))
|
||||
assertThat(shellCallRule.shell.size(), is(2))
|
||||
assertThat(shellCallRule.shell[0].toString(), is('chmod +x piper'))
|
||||
assertThat(shellCallRule.shell[1].toString(), is('./piper version'))
|
||||
|
||||
assertThat(httpRequestRule.requests.size(), is(1))
|
||||
assertThat(httpRequestRule.requests[0].url, is('https://github.com/SAP/jenkins-library/releases/latest/download/piper_master'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnstashPiperBinNonMaster() {
|
||||
|
||||
def piperGoUtils = new PiperGoUtils(nullScript, utils)
|
||||
piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'testTag']]}
|
||||
piperGoUtils.metaClass.getLibrariesInfo = { -> return [[name: 'piper-lib-os', version: 'testTag']] }
|
||||
|
||||
// this mocks utils.unstash - mimic stash not existing
|
||||
helper.registerAllowedMethod("unstash", [String.class], { stashFileName ->
|
||||
return []
|
||||
})
|
||||
|
||||
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/testTag/piper\'', '200')
|
||||
|
||||
httpRequestRule.mockUrl("https://github.com/SAP/jenkins-library/releases/download/testTag/piper", {})
|
||||
|
||||
piperGoUtils.unstashPiperBin()
|
||||
assertThat(shellCallRule.shell.size(), is(3))
|
||||
assertThat(shellCallRule.shell[0].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/testTag/piper\''))
|
||||
assertThat(shellCallRule.shell[1].toString(), is('chmod +x piper'))
|
||||
assertThat(shellCallRule.shell[2].toString(), is('./piper version'))
|
||||
|
||||
assertThat(shellCallRule.shell.size(), is(2))
|
||||
assertThat(shellCallRule.shell[0].toString(), is('chmod +x piper'))
|
||||
|
||||
assertThat(httpRequestRule.requests.size(), is(1))
|
||||
assertThat(httpRequestRule.requests[0].url.toString(), is('https://github.com/SAP/jenkins-library/releases/download/testTag/piper'))
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnstashPiperBinFallback() {
|
||||
|
||||
def piperGoUtils = new PiperGoUtils(nullScript, utils)
|
||||
piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]}
|
||||
|
||||
shellCallRule.setReturnValue('./piper version', "1.2.3")
|
||||
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'', '404')
|
||||
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '200')
|
||||
piperGoUtils.metaClass.getLibrariesInfo = { -> return [[name: 'piper-lib-os', version: 'notAvailable']] }
|
||||
|
||||
// this mocks utils.unstash - mimic stash not existing
|
||||
helper.registerAllowedMethod("unstash", [String.class], { stashFileName ->
|
||||
return []
|
||||
})
|
||||
|
||||
httpRequestRule.mockUrl("https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper", {
|
||||
throw new AbortException("Fail: the returned code 404 is not in the accepted range");
|
||||
})
|
||||
httpRequestRule.mockUrl("https://github.com/SAP/jenkins-library/releases/latest/download/piper_master", {})
|
||||
|
||||
shellCallRule.setReturnValue('./piper version', "1.2.3")
|
||||
|
||||
piperGoUtils.unstashPiperBin()
|
||||
assertThat(shellCallRule.shell.size(), is(4))
|
||||
assertThat(shellCallRule.shell[0].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\''))
|
||||
assertThat(shellCallRule.shell[1].toString(), is('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\''))
|
||||
assertThat(shellCallRule.shell[2].toString(), is('chmod +x piper'))
|
||||
assertThat(shellCallRule.shell[3].toString(), is ('./piper version'))
|
||||
|
||||
assertThat(shellCallRule.shell.size(), is(2))
|
||||
assertThat(shellCallRule.shell[0].toString(), is('chmod +x piper'))
|
||||
assertThat(shellCallRule.shell[1].toString(), is ('./piper version'))
|
||||
|
||||
assertThat(httpRequestRule.requests.size(), is(2))
|
||||
assertThat(httpRequestRule.requests[0].url.toString(), is('https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper'))
|
||||
assertThat(httpRequestRule.requests[1].url.toString(), is('https://github.com/SAP/jenkins-library/releases/latest/download/piper_master'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDownloadFailedWithErrorCode() {
|
||||
def piperGoUtils = new PiperGoUtils(nullScript, utils)
|
||||
piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]}
|
||||
piperGoUtils.metaClass.getLibrariesInfo = { -> return [[name: 'piper-lib-os', version: 'notAvailable']] }
|
||||
|
||||
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'', '404')
|
||||
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '500')
|
||||
|
||||
helper.registerAllowedMethod("unstash", [String.class], { stashFileName ->
|
||||
return []
|
||||
httpRequestRule.mockUrl("https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper", {
|
||||
throw new AbortException("Fail: the returned code 404 is not in the accepted range");
|
||||
})
|
||||
httpRequestRule.mockUrl("https://github.com/SAP/jenkins-library/releases/latest/download/piper_master", {
|
||||
throw new AbortException("Fail: the returned code 500 is not in the accepted range");
|
||||
})
|
||||
|
||||
exception.expectMessage(containsString('Download of Piper go binary failed'))
|
||||
piperGoUtils.unstashPiperBin()
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDownloadFailedWithHTTPCode() {
|
||||
def piperGoUtils = new PiperGoUtils(nullScript, utils)
|
||||
piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]}
|
||||
|
||||
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'', '404')
|
||||
shellCallRule.setReturnValue('curl --insecure --silent --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '500')
|
||||
|
||||
helper.registerAllowedMethod("unstash", [String.class], { stashFileName ->
|
||||
return []
|
||||
@ -149,9 +154,9 @@ class PiperGoUtilsTest extends BasePiperTest {
|
||||
@Test
|
||||
void testDownloadFailedWithError() {
|
||||
def piperGoUtils = new PiperGoUtils(nullScript, utils)
|
||||
piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]}
|
||||
piperGoUtils.metaClass.getLibrariesInfo = { -> return [[name: 'piper-lib-os', version: 'notAvailable']] }
|
||||
|
||||
helper.registerAllowedMethod('sh', [Map.class], {m -> throw new AbortException('download failed')})
|
||||
helper.registerAllowedMethod('sh', [Map.class], { m -> throw new AbortException('download failed') })
|
||||
|
||||
helper.registerAllowedMethod("unstash", [String.class], { stashFileName ->
|
||||
return []
|
||||
|
46
test/groovy/util/JenkinsHttpRequestRule.groovy
Normal file
46
test/groovy/util/JenkinsHttpRequestRule.groovy
Normal file
@ -0,0 +1,46 @@
|
||||
package util
|
||||
|
||||
import com.lesfurets.jenkins.unit.BasePipelineTest
|
||||
import org.junit.rules.TestRule
|
||||
import org.junit.runner.Description
|
||||
import org.junit.runners.model.Statement
|
||||
|
||||
class JenkinsHttpRequestRule implements TestRule {
|
||||
|
||||
final BasePipelineTest testInstance
|
||||
|
||||
JenkinsHttpRequestRule(BasePipelineTest testInstance) {
|
||||
this.testInstance = testInstance
|
||||
}
|
||||
|
||||
Map closures = [:]
|
||||
List requests = []
|
||||
|
||||
void mockUrl(String url, Closure body){
|
||||
closures[url] = body
|
||||
}
|
||||
|
||||
@Override
|
||||
Statement apply(Statement base, Description description) {
|
||||
return statement(base)
|
||||
}
|
||||
|
||||
private Statement statement(final Statement base) {
|
||||
return new Statement() {
|
||||
@Override
|
||||
void evaluate() throws Throwable {
|
||||
|
||||
testInstance.helper.helper.registerAllowedMethod('httpRequest', [Map], { p ->
|
||||
requests.add(p)
|
||||
if(p.url && closures.containsKey(p.url).toString()){
|
||||
closures[p.url]()
|
||||
return
|
||||
}
|
||||
throw new RuntimeException("Not implemented")
|
||||
})
|
||||
|
||||
base.evaluate()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user