github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/FioriOnCloudPlatformPipelineTest.groovy (about) 1 import util.CommandLineMatcher 2 import util.JenkinsLockRule 3 import util.JenkinsWithEnvRule 4 import util.JenkinsWriteFileRule 5 6 import static org.hamcrest.Matchers.allOf 7 import static org.hamcrest.Matchers.containsString 8 import static org.hamcrest.Matchers.equalTo 9 import static org.hamcrest.Matchers.hasItem 10 import static org.hamcrest.Matchers.is 11 import static org.hamcrest.Matchers.subString 12 import static org.junit.Assert.assertThat 13 14 import org.hamcrest.Matchers 15 import org.junit.Assert 16 import org.junit.After 17 import org.junit.Before 18 import org.junit.Rule 19 import org.junit.Test 20 import org.junit.rules.ExpectedException 21 import org.junit.rules.RuleChain 22 23 import com.sap.piper.JenkinsUtils 24 import com.sap.piper.Utils 25 26 import util.BasePiperTest 27 import util.JenkinsCredentialsRule 28 import util.JenkinsReadYamlRule 29 import util.JenkinsShellCallRule 30 import util.JenkinsStepRule 31 import util.Rules 32 33 class FioriOnCloudPlatformPipelineTest extends BasePiperTest { 34 35 /* This scenario builds a fiori app and deploys it into an neo account. 36 The build is performed using mta, which delegates to grunt. grunt in 37 turn makes use of the 'sap/grunt-sapui5-bestpractice-build' plugin. 38 The dependencies are resolved via npm. 39 40 In order to run the scenario the project needs to fulfill these 41 prerequisites: 42 43 Build tools: 44 * mta.jar available 45 * npm installed 46 47 Project configuration: 48 * sap registry `@sap:registry=https://npm.sap.com` configured in 49 .npmrc (either in the project or on any other suitable level) 50 * dependency to `@sap/grunt-sapui5-bestpractice-build` declared in 51 package.json 52 * npmTask `@sap/grunt-sapui5-bestpractice-build` loaded inside 53 Gruntfile.js and configure default tasks (e.g. lint, clean, build) 54 * mta.yaml 55 */ 56 private ExpectedException thrown = new ExpectedException().none() 57 58 JenkinsStepRule stepRule = new JenkinsStepRule(this) 59 JenkinsReadYamlRule readYamlRule = new JenkinsReadYamlRule(this) 60 JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this) 61 JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this) 62 private JenkinsLockRule jlr = new JenkinsLockRule(this) 63 64 @Rule 65 public RuleChain ruleChain = Rules 66 .getCommonRules(this) 67 .around(readYamlRule) 68 .around(thrown) 69 .around(stepRule) 70 .around(shellRule) 71 .around(jlr) 72 .around(writeFileRule) 73 .around(new JenkinsWithEnvRule(this)) 74 .around(new JenkinsCredentialsRule(this) 75 .withCredentials('CI_CREDENTIALS_ID', 'foo', 'terceSpot')) 76 77 private writeInfluxMap = [:] 78 def utilsMock 79 80 class JenkinsUtilsMock extends JenkinsUtils { 81 def isJobStartedByUser() { 82 return true 83 } 84 } 85 86 @Before 87 void setup() { 88 // 89 // needed since we have dockerExecute inside neoDeploy 90 JenkinsUtils.metaClass.static.isPluginActive = {def s -> false} 91 92 // 93 // there is a check for the mta.yaml file and for the deployable test.mtar file 94 helper.registerAllowedMethod('fileExists', [String],{ 95 96 it -> 97 98 // called inside neo deploy, this file gets deployed 99 it == 'test.mtar' 100 }) 101 102 helper.registerAllowedMethod("deleteDir",[], null) 103 104 binding.setVariable('scm', null) 105 106 helper.registerAllowedMethod('pwd', [], { return "./" }) 107 108 helper.registerAllowedMethod('mtaBuild', [Map], { 109 m -> m.script.commonPipelineEnvironment.mtarFilePath = 'test.mtar' 110 }) 111 112 helper.registerAllowedMethod('influxWriteData', [Map.class], { m -> 113 writeInfluxMap = m 114 }) 115 116 utilsMock = newUtilsMock() 117 118 UUID.metaClass.static.randomUUID = { -> 1 } 119 } 120 121 @After 122 public void tearDown() { 123 UUID.metaClass = null 124 } 125 126 Utils newUtilsMock() { 127 def utilsMock = new Utils() 128 utilsMock.steps = [ stash : { } ] 129 utilsMock.echo = { def m -> } 130 return utilsMock 131 } 132 133 @Test 134 void straightForwardTestNeo() { 135 136 nullScript 137 .commonPipelineEnvironment 138 .configuration = [steps: 139 [mtaBuild: 140 [ 141 platform: 'NEO' 142 ], 143 neoDeploy: 144 [neo: 145 [ host: 'hana.example.com', 146 account: 'myTestAccount', 147 ] 148 ] 149 ] 150 ] 151 152 stepRule.step.fioriOnCloudPlatformPipeline(script: nullScript, utils: utilsMock) 153 154 // 155 // the deployable is exchanged between the involved steps via this property: 156 // From the presence of this value we can conclude that mtaBuild has been called 157 // this value is set on the commonPipelineEnvironment in the corresponding mock. 158 assertThat(nullScript.commonPipelineEnvironment.getMtarFilePath(), is(equalTo('test.mtar'))) 159 160 // 161 // the neo deploy call: 162 Assert.assertThat(shellRule.shell, 163 new CommandLineMatcher() 164 .hasProlog("neo.sh deploy-mta") 165 .hasSingleQuotedOption('host', 'hana\\.example\\.com') 166 .hasSingleQuotedOption('account', 'myTestAccount') 167 .hasSingleQuotedOption('password', 'terceSpot') 168 .hasSingleQuotedOption('user', 'foo') 169 .hasSingleQuotedOption('source', 'test.mtar') 170 .hasArgument('synchronous')) 171 } 172 173 @Test 174 void straightForwardTestCF() { 175 176 def calledStep 177 helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], { 178 Map parameters, String stepName, 179 String metadataFile, List credentialInfo -> 180 calledStep = stepName 181 }) 182 183 nullScript 184 .commonPipelineEnvironment 185 .configuration = [steps: 186 [mtaBuild: 187 [ 188 platform: 'CF' 189 ], 190 cloudFoundryDeploy: 191 [ deployTool: 'mtaDeployPlugin', 192 cloudFoundry: 193 [ apiEndpoint: 'https://api.cf.hana.example.com', 194 org: 'testOrg', 195 space: 'testSpace', 196 credentialsId: 'CI_CREDENTIALS_ID' 197 ] 198 ] 199 ] 200 ] 201 202 stepRule.step.fioriOnCloudPlatformPipeline(script: nullScript, utils: utilsMock) 203 204 assertThat(calledStep, is('cloudFoundryDeploy')) 205 } 206 207 @Test 208 void errorNoTargetDefined() { 209 210 nullScript 211 .commonPipelineEnvironment 212 .configuration = [steps: 213 [neoDeploy: 214 [neo: 215 [ host: 'hana.example.com', 216 account: 'myTestAccount', 217 ] 218 ] 219 ] 220 ] 221 222 thrown.expect(Exception) 223 thrown.expectMessage('Deployment failed: no valid deployment target defined') 224 225 stepRule.step.fioriOnCloudPlatformPipeline(script: nullScript, utils: utilsMock) 226 227 } 228 229 @Test 230 void errorUnknownTargetDefined() { 231 232 nullScript 233 .commonPipelineEnvironment 234 .configuration = [steps: 235 [mtaBuild: 236 [ 237 platform: 'UNKNOWN' 238 ] 239 ] 240 ] 241 242 thrown.expect(Exception) 243 thrown.expectMessage('Deployment failed: no valid deployment target defined') 244 245 stepRule.step.fioriOnCloudPlatformPipeline(script: nullScript, utils: utilsMock) 246 } 247 }