2020-04-24 10:41:49 +02:00
|
|
|
import org.junit.Before
|
2018-01-16 10:33:13 +02:00
|
|
|
import org.junit.Rule
|
2017-12-06 13:03:06 +02:00
|
|
|
import org.junit.Test
|
2020-04-24 10:41:49 +02:00
|
|
|
import org.junit.rules.ExpectedException
|
2018-01-16 16:03:00 +02:00
|
|
|
import org.junit.rules.RuleChain
|
2018-08-31 10:22:43 +02:00
|
|
|
|
2018-06-06 14:42:47 +02:00
|
|
|
import util.BasePiperTest
|
2020-04-24 10:41:49 +02:00
|
|
|
import util.JenkinsCredentialsRule
|
|
|
|
import util.JenkinsFileExistsRule
|
|
|
|
import util.JenkinsMavenExecuteRule
|
|
|
|
import util.JenkinsReadJsonRule
|
2018-08-31 10:22:43 +02:00
|
|
|
import util.JenkinsReadYamlRule
|
2018-06-06 14:42:47 +02:00
|
|
|
import util.JenkinsShellCallRule
|
2018-06-06 11:19:19 +02:00
|
|
|
import util.JenkinsStepRule
|
2020-04-24 10:41:49 +02:00
|
|
|
import util.JenkinsWriteFileRule
|
2018-06-06 14:42:47 +02:00
|
|
|
import util.Rules
|
2018-01-16 18:06:25 +02:00
|
|
|
|
2020-04-24 10:41:49 +02:00
|
|
|
import static org.hamcrest.Matchers.*
|
2018-07-26 16:03:05 +02:00
|
|
|
import static org.junit.Assert.assertThat
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2018-05-30 12:39:01 +02:00
|
|
|
class MavenExecuteTest extends BasePiperTest {
|
2020-04-24 10:41:49 +02:00
|
|
|
private ExpectedException exception = ExpectedException.none()
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2020-04-24 10:41:49 +02:00
|
|
|
private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this)
|
|
|
|
private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
|
2019-01-22 10:25:42 +02:00
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
2020-04-24 10:41:49 +02:00
|
|
|
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
|
|
|
|
private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, [])
|
|
|
|
|
|
|
|
private List withEnvArgs = []
|
2017-12-06 13:03:06 +02:00
|
|
|
|
2018-01-16 10:33:13 +02:00
|
|
|
@Rule
|
2020-04-24 10:41:49 +02:00
|
|
|
public RuleChain rules = Rules
|
2018-06-06 11:19:19 +02:00
|
|
|
.getCommonRules(this)
|
2020-04-24 10:41:49 +02:00
|
|
|
.around(exception)
|
2018-08-31 10:22:43 +02:00
|
|
|
.around(new JenkinsReadYamlRule(this))
|
2020-04-24 10:41:49 +02:00
|
|
|
.around(credentialsRule)
|
|
|
|
.around(new JenkinsReadJsonRule(this))
|
|
|
|
.around(shellCallRule)
|
2019-01-22 10:25:42 +02:00
|
|
|
.around(stepRule)
|
2020-04-24 10:41:49 +02:00
|
|
|
.around(writeFileRule)
|
|
|
|
.around(fileExistsRule)
|
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() {
|
|
|
|
helper.registerAllowedMethod("withEnv", [List, Closure], { arguments, closure ->
|
|
|
|
arguments.each {arg ->
|
|
|
|
withEnvArgs.add(arg.toString())
|
|
|
|
}
|
|
|
|
return closure()
|
|
|
|
})
|
|
|
|
credentialsRule.withCredentials('idOfCxCredential', "admin", "admin123")
|
|
|
|
shellCallRule.setReturnValue(
|
|
|
|
'./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/mavenExecute.yaml\'',
|
|
|
|
'{"credentialsId": "idOfCxCredential", "verbose": false}'
|
|
|
|
)
|
2020-04-24 22:41:34 +02:00
|
|
|
|
|
|
|
helper.registerAllowedMethod('findFiles', [Map.class], {return null})
|
2021-06-15 14:34:56 +02:00
|
|
|
helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
|
|
|
|
helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return })
|
2018-06-06 14:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-04-24 10:41:49 +02:00
|
|
|
void testExecute() {
|
2019-01-22 10:25:42 +02:00
|
|
|
stepRule.step.mavenExecute(
|
2020-04-24 10:41:49 +02:00
|
|
|
juStabUtils: utils,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
testParam: "This is test content",
|
2018-05-30 12:39:01 +02:00
|
|
|
script: nullScript,
|
2020-04-24 10:41:49 +02:00
|
|
|
)
|
|
|
|
// asserts
|
|
|
|
assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenExecute.yaml'], containsString('name: mavenExecute'))
|
|
|
|
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'),
|
|
|
|
containsString('"testParam":"This is test content"')))
|
|
|
|
assertThat(shellCallRule.shell[1], is('./piper mavenExecute'))
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|
2018-03-29 14:13:11 +02:00
|
|
|
|
|
|
|
@Test
|
2020-04-24 10:41:49 +02:00
|
|
|
void testOutputIsReturned() {
|
|
|
|
// init
|
|
|
|
String outputFile = '.pipeline/maven_output.txt'
|
|
|
|
String expectedOutput = 'the output'
|
|
|
|
fileExistsRule.registerExistingFile(outputFile)
|
|
|
|
helper.registerAllowedMethod('readFile', [String], {file ->
|
|
|
|
if (file == outputFile) {
|
|
|
|
return expectedOutput
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
})
|
|
|
|
|
|
|
|
// test
|
|
|
|
String receivedOutput = stepRule.step.mavenExecute(
|
|
|
|
juStabUtils: utils,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
script: nullScript,
|
|
|
|
returnStdout: true,
|
|
|
|
)
|
2018-07-26 16:03:05 +02:00
|
|
|
|
2020-04-24 10:41:49 +02:00
|
|
|
// asserts
|
|
|
|
assertThat(receivedOutput, is(expectedOutput))
|
2018-07-26 16:03:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-04-24 10:41:49 +02:00
|
|
|
void testOutputIsMissing() {
|
|
|
|
// init
|
|
|
|
fileExistsRule.setExistingFiles([])
|
|
|
|
helper.registerAllowedMethod('readFile', [String], {file ->
|
|
|
|
return ''
|
|
|
|
})
|
|
|
|
String errorMessage = ''
|
|
|
|
helper.registerAllowedMethod('error', [String], {message ->
|
|
|
|
errorMessage = message
|
|
|
|
})
|
|
|
|
|
|
|
|
// test
|
|
|
|
stepRule.step.mavenExecute(
|
|
|
|
juStabUtils: utils,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
script: nullScript,
|
|
|
|
returnStdout: true,
|
|
|
|
)
|
2020-01-23 10:31:01 +02:00
|
|
|
|
2020-04-24 10:41:49 +02:00
|
|
|
// asserts
|
|
|
|
assertThat(errorMessage, containsString('Internal error. A text file with the contents of the maven output was expected'))
|
2020-01-23 10:31:01 +02:00
|
|
|
}
|
2017-12-06 13:03:06 +02:00
|
|
|
}
|