1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/test/groovy/XsDeployTest.groovy

277 lines
10 KiB
Groovy
Raw Normal View History

import static org.hamcrest.Matchers.allOf
import static org.hamcrest.Matchers.contains
import static org.hamcrest.Matchers.containsInAnyOrder
import static org.hamcrest.Matchers.containsString
2019-12-13 17:05:55 +02:00
import static org.hamcrest.Matchers.equalTo
import static org.hamcrest.Matchers.is
2019-12-13 17:05:55 +02:00
import static org.hamcrest.Matchers.not
import static org.hamcrest.Matchers.nullValue
import static org.junit.Assert.assertThat
2019-12-13 17:05:55 +02:00
import org.hamcrest.Matchers
import org.hamcrest.core.IsNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
2019-12-13 17:05:55 +02:00
import com.sap.piper.PiperGoUtils
import hudson.AbortException
import util.BasePiperTest
import util.CommandLineMatcher
import util.JenkinsCredentialsRule
import util.JenkinsDockerExecuteRule
import util.JenkinsLockRule
2019-12-13 17:05:55 +02:00
import util.JenkinsReadJsonRule
import util.JenkinsReadYamlRule
import util.JenkinsShellCallRule
import util.JenkinsStepRule
2019-12-13 17:05:55 +02:00
import util.JenkinsWriteFileRule
import util.Rules
class XsDeployTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException.none()
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this)
private JenkinsLockRule lockRule = new JenkinsLockRule(this)
2019-12-13 17:05:55 +02:00
private JenkinsDockerExecuteRule dockerRule = new JenkinsDockerExecuteRule(this)
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
List env
@Rule
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
2019-12-13 17:05:55 +02:00
.around(new JenkinsReadJsonRule(this))
.around(stepRule)
2019-12-13 17:05:55 +02:00
.around(dockerRule)
.around(writeFileRule)
.around(new JenkinsCredentialsRule(this)
.withCredentials('myCreds', 'cred_xs', 'topSecret'))
.around(lockRule)
.around(shellRule)
.around(thrown)
2019-12-13 17:05:55 +02:00
private PiperGoUtils goUtils = new PiperGoUtils(null) {
void unstashPiperBin() {
}
}
2019-12-13 17:05:55 +02:00
@Before
public void init() {
helper.registerAllowedMethod('withEnv', [List, Closure], {l, c -> env = l; c()})
2019-12-20 15:41:17 +02:00
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.*getConfig.*--contextConfig.*', '{"dockerImage": "xs", "dockerPullImage": false, "credentialsId":"myCreds"}')
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, 'getConfig.* (?!--contextConfig)', '{"mode": "BG_DEPLOY", "action": "NONE", "apiUrl": "https://example.org/xs", "org": "myOrg", "space": "mySpace"}')
2019-12-13 17:05:55 +02:00
nullScript.commonPipelineEnvironment.xsDeploymentId = null
}
@Test
2019-12-13 17:05:55 +02:00
public void testDeployFailed() {
thrown.expect(AbortException)
2019-12-13 17:05:55 +02:00
thrown.expectMessage('script returned exit code 1')
2019-12-13 17:05:55 +02:00
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.*xsDeploy .*', { throw new AbortException('script returned exit code 1')})
stepRule.step.xsDeploy(
script: nullScript,
2019-12-13 17:05:55 +02:00
piperGoUtils: goUtils,
)
}
@Test
2019-12-13 17:05:55 +02:00
public void testInvalidDeploymentModeProvided() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage('No enum constant')
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, 'getConfig.* (?!--contextConfig)', '{"mode": "DOES_NOT_EXIST", "action": "NONE", "apiUrl": "https://example.org/xs", "org": "myOrg", "space": "mySpace"}')
2019-12-13 17:05:55 +02:00
stepRule.step.xsDeploy(
script: nullScript,
2019-12-13 17:05:55 +02:00
piperGoUtils: goUtils,
)
}
@Test
2019-12-13 17:05:55 +02:00
public void testParametersViaSignature() {
2019-12-13 17:05:55 +02:00
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.*xsDeploy .*', '{"operationId": "1234"}')
stepRule.step.xsDeploy(
script: nullScript,
apiUrl: 'https://example.org/xs',
org: 'myOrg',
space: 'mySpace',
credentialsId: 'myCreds',
deployOpts: '-t 60',
mtaPath: 'myApp.mta',
2019-12-13 17:05:55 +02:00
mode: 'DEPLOY',
action: 'NONE',
piperGoUtils: goUtils
)
2019-12-13 17:05:55 +02:00
// nota bene: script and piperGoUtils are not contained in the json below.
assertThat(env*.toString(), contains('PIPER_parametersJSON={"apiUrl":"https://example.org/xs","org":"myOrg","space":"mySpace","credentialsId":"myCreds","deployOpts":"-t 60","mtaPath":"myApp.mta","mode":"DEPLOY","action":"NONE"}'))
}
@Test
2019-12-13 17:05:55 +02:00
public void testBlueGreenDeployInit() {
2019-12-13 17:05:55 +02:00
//
// Only difference between bg deploy and standard deploy is in the config.
// The surrounding behavior is the same. Hence there is no dedicated test here
// in the groovy layer for standard deploy
//
2019-12-13 17:05:55 +02:00
boolean unstashCalled
2019-12-13 17:05:55 +02:00
assertThat(nullScript.commonPipelineEnvironment.xsDeploymentId, nullValue())
2019-12-13 17:05:55 +02:00
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.*xsDeploy .*', '{"operationId": "1234"}')
2019-12-13 17:05:55 +02:00
goUtils = new PiperGoUtils(null) {
void unstashPiperBin() {
unstashCalled = true
}
}
stepRule.step.xsDeploy(
script: nullScript,
2019-12-13 17:05:55 +02:00
piperGoUtils: goUtils
)
2019-12-13 17:05:55 +02:00
assertThat(unstashCalled, equalTo(true))
assertThat(nullScript.commonPipelineEnvironment.xsDeploymentId, is('1234'))
assertThat(writeFileRule.files.keySet(), containsInAnyOrder(
'.pipeline/additionalConfigs/default_pipeline_environment.yml',
'.pipeline/metadata/xsDeploy.yaml',
))
2019-12-13 17:05:55 +02:00
assertThat(dockerRule.dockerParams.dockerImage, equalTo('xs'))
assertThat(dockerRule.dockerParams.dockerPullImage, equalTo(false))
assertThat(shellRule.shell,
allOf(
new CommandLineMatcher()
2019-12-13 17:05:55 +02:00
.hasProlog('./piper version'),
new CommandLineMatcher()
.hasProlog('./piper getConfig')
.hasArgument('--contextConfig'),
new CommandLineMatcher()
.hasProlog('./piper getConfig --stepMetadata \'.pipeline/metadata/xsDeploy.yaml\''),
new CommandLineMatcher()
.hasProlog('#!/bin/bash ./piper xsDeploy --defaultConfig ".pipeline/additionalConfigs/default_pipeline_environment.yml" --user \\$\\{USERNAME\\} --password \\$\\{PASSWORD\\}'),
2019-12-13 17:05:55 +02:00
not(new CommandLineMatcher()
.hasProlog('#!/bin/bash ./piper xsDeploy')
.hasOption('operationId', '1234'))
)
)
assertThat(lockRule.getLockResources(), contains('xsDeploy:https://example.org/xs:myOrg:mySpace'))
}
@Test
2019-12-13 17:05:55 +02:00
public void testBlueGreenDeployResume() {
2019-12-13 17:05:55 +02:00
nullScript.commonPipelineEnvironment.xsDeploymentId = '1234'
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, 'getConfig.* (?!--contextConfig)', '{"mode": "BG_DEPLOY", "action": "RESUME", "apiUrl": "https://example.org/xs", "org": "myOrg", "space": "mySpace"}')
stepRule.step.xsDeploy(
script: nullScript,
2019-12-13 17:05:55 +02:00
piperGoUtils: goUtils
)
2019-12-13 17:05:55 +02:00
assertThat(shellRule.shell,
new CommandLineMatcher()
.hasProlog('#!/bin/bash ./piper xsDeploy')
.hasOption('operationId', '1234')
)
2019-12-13 17:05:55 +02:00
assertThat(lockRule.getLockResources(), contains('xsDeploy:https://example.org/xs:myOrg:mySpace'))
}
@Test
2019-12-13 17:05:55 +02:00
public void testBlueGreenDeployResumeWithoutDeploymentId() {
2019-12-13 17:05:55 +02:00
// this happens in case we would like to complete a deployment without having a (successful) deployments before.
2019-12-13 17:05:55 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage(
allOf(
containsString('No operationId provided'),
containsString('Was there a deployment before?')))
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, 'getConfig.* (?!--contextConfig)', '{"mode": "BG_DEPLOY", "action": "RESUME", "apiUrl": "https://example.org/xs", "org": "myOrg", "space": "mySpace"}')
2019-12-13 17:05:55 +02:00
assertThat(nullScript.commonPipelineEnvironment.xsDeploymentId, nullValue())
stepRule.step.xsDeploy(
script: nullScript,
2019-12-13 17:05:55 +02:00
piperGoUtils: goUtils,
failOnError: true,
)
}
@Test
public void testAdditionalCustomConfigLayers() {
def resources = ['a.yml': '- x: y}', 'b.yml' : '- a: b}']
helper.registerAllowedMethod('libraryResource', [String], {
r ->
def resource = resources[r]
if(resource) return resource
File res = new File(new File('resources'), r)
if (res.exists()) {
return res.getText()
}
throw new RuntimeException("Resource '${r}' not found.")
})
assertThat(nullScript.commonPipelineEnvironment.xsDeploymentId, nullValue())
shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, '.*xsDeploy .*', '{"operationId": "1234"}')
nullScript.commonPipelineEnvironment = ['reset': {}, 'getCustomDefaults': {['a.yml', 'b.yml']}]
goUtils = new PiperGoUtils(null) {
void unstashPiperBin() {
}
}
stepRule.step.xsDeploy(
script: nullScript,
piperGoUtils: goUtils
)
assertThat(nullScript.commonPipelineEnvironment.xsDeploymentId, is('1234'))
assertThat(writeFileRule.files.keySet(), containsInAnyOrder(
'.pipeline/additionalConfigs/a.yml',
'.pipeline/additionalConfigs/b.yml',
'.pipeline/additionalConfigs/default_pipeline_environment.yml',
'.pipeline/metadata/xsDeploy.yaml',
))
assertThat(shellRule.shell,
allOf(
new CommandLineMatcher()
.hasProlog('./piper getConfig')
.hasArgument('--contextConfig')
.hasArgument('--defaultConfig ".pipeline/additionalConfigs/b.yml" ".pipeline/additionalConfigs/a.yml" ".pipeline/additionalConfigs/default_pipeline_environment.yml"'),
new CommandLineMatcher()
.hasProlog('./piper getConfig --stepMetadata \'.pipeline/metadata/xsDeploy.yaml\''),
)
)
}
}