github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/PiperGoUtilsTest.groovy (about) 1 package com.sap.piper 2 3 import hudson.AbortException 4 import org.junit.Before 5 import org.junit.Rule 6 import org.junit.Test 7 import org.junit.rules.ExpectedException 8 import org.junit.rules.RuleChain 9 import util.BasePiperTest 10 import util.JenkinsLoggingRule 11 import util.JenkinsShellCallRule 12 import util.Rules 13 14 import static org.hamcrest.Matchers.containsString 15 import static org.hamcrest.Matchers.is 16 import static org.junit.Assert.assertThat 17 18 class PiperGoUtilsTest extends BasePiperTest { 19 20 public ExpectedException exception = ExpectedException.none() 21 public JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this) 22 public JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 23 24 @Rule 25 public RuleChain ruleChain = Rules.getCommonRules(this) 26 .around(shellCallRule) 27 .around(exception) 28 .around(loggingRule) 29 30 @Before 31 void init() { 32 helper.registerAllowedMethod("retry", [Integer, Closure], null) 33 } 34 35 @Test 36 void testUnstashPiperBinAvailable() { 37 38 def piperBinStash = 'piper-bin' 39 40 // this mocks utils.unstash 41 helper.registerAllowedMethod("unstash", [String.class], { stashFileName -> 42 if (stashFileName != piperBinStash) { 43 return [] 44 } 45 return [piperBinStash] 46 }) 47 48 def piperGoUtils = new PiperGoUtils(nullScript, utils) 49 50 piperGoUtils.unstashPiperBin() 51 } 52 53 54 @Test 55 void testUnstashPiperBinMaster() { 56 57 def piperGoUtils = new PiperGoUtils(nullScript, utils) 58 piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'master']]} 59 60 // this mocks utils.unstash - mimic stash not existing 61 helper.registerAllowedMethod("unstash", [String.class], { stashFileName -> 62 return [] 63 }) 64 65 shellCallRule.setReturnValue('[ -x ./piper ]', 1) 66 shellCallRule.setReturnValue('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '200') 67 68 piperGoUtils.unstashPiperBin() 69 assertThat(shellCallRule.shell.size(), is(4)) 70 assertThat(shellCallRule.shell[1].toString(), is('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'')) 71 assertThat(shellCallRule.shell[2].toString(), is('chmod +x piper')) 72 assertThat(shellCallRule.shell[3].toString(), is('./piper version')) 73 } 74 75 @Test 76 void testUnstashPiperBinNonMaster() { 77 78 def piperGoUtils = new PiperGoUtils(nullScript, utils) 79 piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'testTag']]} 80 81 // this mocks utils.unstash - mimic stash not existing 82 helper.registerAllowedMethod("unstash", [String.class], { stashFileName -> 83 return [] 84 }) 85 86 shellCallRule.setReturnValue('[ -x ./piper ]', 1) 87 shellCallRule.setReturnValue('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/testTag/piper\'', '200') 88 89 piperGoUtils.unstashPiperBin() 90 assertThat(shellCallRule.shell.size(), is(4)) 91 assertThat(shellCallRule.shell[1].toString(), is('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/testTag/piper\'')) 92 assertThat(shellCallRule.shell[2].toString(), is('chmod +x piper')) 93 assertThat(shellCallRule.shell[3].toString(), is('./piper version')) 94 } 95 96 @Test 97 void testUnstashPiperBinFallback() { 98 99 def piperGoUtils = new PiperGoUtils(nullScript, utils) 100 piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]} 101 102 shellCallRule.setReturnValue('[ -x ./piper ]', 1) 103 shellCallRule.setReturnValue('./piper version', "1.2.3") 104 shellCallRule.setReturnValue('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'', '404') 105 shellCallRule.setReturnValue('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '200') 106 107 // this mocks utils.unstash - mimic stash not existing 108 helper.registerAllowedMethod("unstash", [String.class], { stashFileName -> 109 return [] 110 }) 111 112 piperGoUtils.unstashPiperBin() 113 assertThat(shellCallRule.shell.size(), is(5)) 114 assertThat(shellCallRule.shell[0].toString(), is('[ -x ./piper ]')) 115 assertThat(shellCallRule.shell[1].toString(), is('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'')) 116 assertThat(shellCallRule.shell[2].toString(), is('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'')) 117 assertThat(shellCallRule.shell[3].toString(), is('chmod +x piper')) 118 assertThat(shellCallRule.shell[4].toString(), is ('./piper version')) 119 } 120 121 @Test 122 void testDownloadFailedWithErrorCode() { 123 def piperGoUtils = new PiperGoUtils(nullScript, utils) 124 piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]} 125 126 shellCallRule.setReturnValue('[ -x ./piper ]', 1) 127 shellCallRule.setReturnValue('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'', '404') 128 shellCallRule.setReturnValue('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '500') 129 130 helper.registerAllowedMethod("unstash", [String.class], { stashFileName -> 131 return [] 132 }) 133 134 exception.expectMessage(containsString('Download of Piper go binary failed')) 135 piperGoUtils.unstashPiperBin() 136 } 137 138 @Test 139 void testDownloadFailedWithHTTPCode() { 140 def piperGoUtils = new PiperGoUtils(nullScript, utils) 141 piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]} 142 143 shellCallRule.setReturnValue('[ -x ./piper ]', 1) 144 shellCallRule.setReturnValue('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/download/notAvailable/piper\'', '404') 145 shellCallRule.setReturnValue('curl --insecure --silent --retry 5 --retry-max-time 240 --location --write-out \'%{http_code}\' --output piper \'https://github.com/SAP/jenkins-library/releases/latest/download/piper_master\'', '500') 146 147 helper.registerAllowedMethod("unstash", [String.class], { stashFileName -> 148 return [] 149 }) 150 151 exception.expectMessage(containsString('Download of Piper go binary failed')) 152 piperGoUtils.unstashPiperBin() 153 } 154 155 @Test 156 void testDownloadFailedWithError() { 157 def piperGoUtils = new PiperGoUtils(nullScript, utils) 158 piperGoUtils.metaClass.getLibrariesInfo = {-> return [[name: 'piper-lib-os', version: 'notAvailable']]} 159 160 shellCallRule.setReturnValue('[ -x ./piper ]', 1) 161 162 helper.registerAllowedMethod("unstash", [String.class], { stashFileName -> 163 return [] 164 }) 165 166 exception.expectMessage(containsString('Download of Piper go binary failed')) 167 piperGoUtils.unstashPiperBin() 168 } 169 }