2019-07-02 13:00:36 +02:00
|
|
|
#!groovy
|
2020-09-24 13:47:20 +02:00
|
|
|
import org.junit.After
|
2019-07-02 13:00:36 +02:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
import util.BasePiperTest
|
|
|
|
import util.JenkinsDockerExecuteRule
|
|
|
|
import util.JenkinsReadYamlRule
|
|
|
|
import util.JenkinsShellCallRule
|
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.Rules
|
|
|
|
|
2020-09-24 13:47:20 +02:00
|
|
|
import com.sap.piper.Utils
|
|
|
|
|
2019-07-02 13:00:36 +02:00
|
|
|
import static org.hamcrest.CoreMatchers.containsString
|
|
|
|
import static org.hamcrest.CoreMatchers.hasItem
|
|
|
|
import static org.hamcrest.CoreMatchers.is
|
|
|
|
import static org.hamcrest.CoreMatchers.nullValue
|
2020-06-04 17:53:06 +02:00
|
|
|
import static org.junit.Assert.assertEquals
|
|
|
|
import static org.junit.Assert.assertFalse
|
2020-09-04 14:45:09 +02:00
|
|
|
import static org.junit.Assert.assertNotNull
|
2019-07-02 13:00:36 +02:00
|
|
|
import static org.junit.Assert.assertThat
|
2020-06-04 17:53:06 +02:00
|
|
|
import static org.junit.Assert.assertTrue
|
2020-09-04 14:45:09 +02:00
|
|
|
import static org.junit.Assert.fail
|
2019-07-02 13:00:36 +02:00
|
|
|
|
|
|
|
class BuildExecuteTest extends BasePiperTest {
|
|
|
|
private ExpectedException exception = ExpectedException.none()
|
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
|
|
|
private JenkinsDockerExecuteRule dockerRule = new JenkinsDockerExecuteRule(this)
|
|
|
|
private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
|
|
|
.around(new JenkinsReadYamlRule(this))
|
|
|
|
.around(exception)
|
|
|
|
.around(shellCallRule)
|
|
|
|
.around(dockerRule)
|
|
|
|
.around(stepRule)
|
|
|
|
|
|
|
|
def dockerMockArgs = [:]
|
|
|
|
class DockerMock {
|
2020-06-04 17:53:06 +02:00
|
|
|
DockerMock(name) {
|
2019-07-02 13:00:36 +02:00
|
|
|
dockerMockArgs.name = name
|
|
|
|
}
|
2020-06-04 17:53:06 +02:00
|
|
|
|
2019-07-02 13:00:36 +02:00
|
|
|
def build(image, options) {
|
|
|
|
return [image: image, options: options]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() {
|
2020-09-24 13:47:20 +02:00
|
|
|
Utils.metaClass.echo = { def m -> }
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
Utils.metaClass = null
|
2019-07-02 13:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testDefaultError() {
|
|
|
|
exception.expectMessage(containsString('buildTool not set and no dockerImage & dockerCommand provided'))
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testDefaultWithDockerImage() {
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
dockerImage: 'path/to/myImage:tag',
|
|
|
|
dockerCommand: 'myTestCommand'
|
|
|
|
)
|
|
|
|
assertThat(dockerRule.dockerParams.dockerImage, is('path/to/myImage:tag'))
|
|
|
|
assertThat(shellCallRule.shell, hasItem('myTestCommand'))
|
|
|
|
}
|
|
|
|
|
2020-09-04 14:45:09 +02:00
|
|
|
@Test
|
|
|
|
void inferBuildToolMaven() {
|
|
|
|
boolean buildToolCalled = false
|
|
|
|
helper.registerAllowedMethod('fileExists', [String.class], { s ->
|
|
|
|
return s == "pom.xml"
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('mavenBuild', [Map.class], { m ->
|
|
|
|
buildToolCalled = true
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: true])
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
)
|
|
|
|
|
|
|
|
assertNotNull(nullScript.commonPipelineEnvironment.getBuildTool())
|
|
|
|
assertEquals('maven', nullScript.commonPipelineEnvironment.getBuildTool())
|
|
|
|
assertTrue(buildToolCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void inferBuildToolNpm() {
|
|
|
|
boolean buildToolCalled = false
|
|
|
|
helper.registerAllowedMethod('fileExists', [String.class], { s ->
|
|
|
|
return s == "package.json"
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
|
|
|
|
buildToolCalled = true
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: true])
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
)
|
|
|
|
|
|
|
|
assertNotNull(nullScript.commonPipelineEnvironment.getBuildTool())
|
|
|
|
assertEquals('npm', nullScript.commonPipelineEnvironment.getBuildTool())
|
|
|
|
assertTrue(buildToolCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void inferBuildToolMTA() {
|
|
|
|
boolean buildToolCalled = false
|
|
|
|
helper.registerAllowedMethod('fileExists', [String.class], { s ->
|
|
|
|
return s == "mta.yaml"
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('mtaBuild', [Map.class], { m ->
|
|
|
|
buildToolCalled = true
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: true])
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
)
|
|
|
|
|
|
|
|
assertNotNull(nullScript.commonPipelineEnvironment.getBuildTool())
|
|
|
|
assertEquals('mta', nullScript.commonPipelineEnvironment.getBuildTool())
|
|
|
|
assertTrue(buildToolCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void 'Do not infer build tool, do not set build tool, with docker dockerImage and dockerCommand, should run docker'() {
|
|
|
|
helper.registerAllowedMethod('fileExists', [String.class], { s ->
|
|
|
|
return s == "package.json"
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
|
|
|
|
fail("Called npmExecuteScripts which should not happen when no buildTool was defined but dockerImage and dockerCommand were.")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Does nothing because feature toggle is not active
|
|
|
|
setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: false])
|
|
|
|
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
dockerImage: 'path/to/myImage:tag',
|
|
|
|
dockerCommand: 'myTestCommand'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(dockerRule.dockerParams.dockerImage, is('path/to/myImage:tag'))
|
|
|
|
assertThat(shellCallRule.shell, hasItem('myTestCommand'))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void 'Do infer build tool, do not set build tool, with docker dockerImage and dockerCommand, should run npm'() {
|
|
|
|
boolean npmCalled = false
|
|
|
|
helper.registerAllowedMethod('fileExists', [String.class], { s ->
|
|
|
|
return s == "package.json"
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
|
|
|
|
npmCalled = true
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
setupCommonPipelineEnvironment.inferBuildTool(nullScript, [inferBuildTool: true])
|
|
|
|
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
dockerImage: 'path/to/myImage:tag',
|
|
|
|
dockerCommand: 'myTestCommand'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertTrue(npmCalled)
|
|
|
|
assertEquals(0, shellCallRule.shell.size())
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:00:36 +02:00
|
|
|
@Test
|
|
|
|
void testMaven() {
|
2020-06-04 17:53:06 +02:00
|
|
|
boolean buildToolCalled = false
|
|
|
|
boolean installOptionSet = false
|
|
|
|
helper.registerAllowedMethod('mavenBuild', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
buildToolCalled = true
|
|
|
|
return
|
|
|
|
})
|
2020-06-04 17:53:06 +02:00
|
|
|
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
|
|
|
|
installOptionSet = m['install']
|
|
|
|
return
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod('fileExists', [String.class], { s ->
|
|
|
|
return s == 'package.json'
|
|
|
|
})
|
2019-07-02 13:00:36 +02:00
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'maven',
|
|
|
|
)
|
2020-06-04 17:53:06 +02:00
|
|
|
assertTrue(buildToolCalled)
|
|
|
|
assertTrue(installOptionSet)
|
2019-07-02 13:00:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testMta() {
|
|
|
|
def buildToolCalled = false
|
2020-06-04 17:53:06 +02:00
|
|
|
helper.registerAllowedMethod('mtaBuild', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
buildToolCalled = true
|
|
|
|
return
|
|
|
|
})
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'mta',
|
|
|
|
)
|
|
|
|
assertThat(buildToolCalled, is(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testNpm() {
|
|
|
|
def buildToolCalled = false
|
2020-06-04 17:53:06 +02:00
|
|
|
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
buildToolCalled = true
|
|
|
|
return
|
|
|
|
})
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'npm',
|
|
|
|
)
|
|
|
|
assertThat(buildToolCalled, is(true))
|
|
|
|
}
|
|
|
|
|
2020-06-04 17:53:06 +02:00
|
|
|
@Test
|
|
|
|
void testNpmWithScripts() {
|
|
|
|
boolean actualValue = false
|
|
|
|
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
|
|
|
|
actualValue = (m['runScripts'][0] == 'foo' && m['runScripts'][1] == 'bar')
|
|
|
|
return
|
|
|
|
})
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'npm',
|
|
|
|
npmRunScripts: ['foo', 'bar']
|
|
|
|
)
|
|
|
|
assertTrue(actualValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testNpmWithInstallFalse() {
|
|
|
|
boolean actualValue = true
|
|
|
|
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
|
|
|
|
actualValue = m['install']
|
|
|
|
return
|
|
|
|
})
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'npm',
|
|
|
|
npmInstall: false
|
|
|
|
)
|
|
|
|
assertFalse(actualValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testNpmWithInstallTrue() {
|
|
|
|
boolean actualValue = false
|
|
|
|
helper.registerAllowedMethod('npmExecuteScripts', [Map.class], { m ->
|
|
|
|
actualValue = m['install']
|
|
|
|
return
|
|
|
|
})
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'npm',
|
|
|
|
npmInstall: true
|
|
|
|
)
|
|
|
|
assertTrue(actualValue)
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:00:36 +02:00
|
|
|
@Test
|
|
|
|
void testDocker() {
|
|
|
|
binding.setVariable('docker', new DockerMock('test'))
|
2020-06-04 17:53:06 +02:00
|
|
|
def pushParams = [:]
|
|
|
|
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
pushParams = m
|
|
|
|
return
|
|
|
|
})
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'docker',
|
|
|
|
dockerImageName: 'path/to/myImage',
|
|
|
|
dockerImageTag: 'myTag',
|
|
|
|
dockerRegistryUrl: 'https://my.registry:55555'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(pushParams.dockerBuildImage.image.toString(), is('path/to/myImage:myTag'))
|
|
|
|
assertThat(pushParams.dockerRegistryUrl.toString(), is('https://my.registry:55555'))
|
|
|
|
assertThat(nullScript.commonPipelineEnvironment.getValue('containerImage').toString(), is('path/to/myImage:myTag'))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testDockerWithEnv() {
|
|
|
|
nullScript.commonPipelineEnvironment.setArtifactVersion('1.0.0')
|
|
|
|
binding.setVariable('docker', new DockerMock('test'))
|
2020-06-04 17:53:06 +02:00
|
|
|
def pushParams = [:]
|
|
|
|
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
pushParams = m
|
|
|
|
return
|
|
|
|
})
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'docker',
|
|
|
|
dockerImageName: 'path/to/myImage',
|
|
|
|
dockerRegistryUrl: 'https://my.registry:55555'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(pushParams.dockerBuildImage.image.toString(), is('path/to/myImage:1.0.0'))
|
|
|
|
assertThat(pushParams.dockerRegistryUrl.toString(), is('https://my.registry:55555'))
|
|
|
|
assertThat(nullScript.commonPipelineEnvironment.getValue('containerImage').toString(), is('path/to/myImage:1.0.0'))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testDockerNoPush() {
|
|
|
|
binding.setVariable('docker', new DockerMock('test'))
|
2020-06-04 17:53:06 +02:00
|
|
|
def pushParams = [:]
|
|
|
|
helper.registerAllowedMethod('containerPushToRegistry', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
pushParams = m
|
|
|
|
return
|
|
|
|
})
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'docker',
|
|
|
|
dockerImageName: 'path/to/myImage',
|
|
|
|
dockerImageTag: 'myTag',
|
|
|
|
dockerRegistryUrl: ''
|
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(pushParams.dockerBuildImage, nullValue())
|
|
|
|
assertThat(pushParams.dockerRegistryUrl, nullValue())
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testKaniko() {
|
|
|
|
def kanikoParams = [:]
|
2020-06-04 17:53:06 +02:00
|
|
|
helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
kanikoParams = m
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'kaniko',
|
|
|
|
dockerImageName: 'path/to/myImage',
|
|
|
|
dockerImageTag: 'myTag',
|
|
|
|
dockerRegistryUrl: 'https://my.registry:55555'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(kanikoParams.containerImageNameAndTag.toString(), is('my.registry:55555/path/to/myImage:myTag'))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testKanikoNoPush() {
|
|
|
|
def kanikoParams = [:]
|
2020-06-04 17:53:06 +02:00
|
|
|
helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
kanikoParams = m
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'kaniko',
|
|
|
|
dockerImageName: 'path/to/myImage',
|
|
|
|
dockerImageTag: 'myTag',
|
|
|
|
dockerRegistryUrl: ''
|
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(kanikoParams.containerImageNameAndTag, is(''))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testSwitchToKaniko() {
|
|
|
|
shellCallRule.setReturnValue('docker ps -q > /dev/null', 1)
|
|
|
|
def kanikoParams = [:]
|
2020-06-04 17:53:06 +02:00
|
|
|
helper.registerAllowedMethod('kanikoExecute', [Map.class], { m ->
|
2019-07-02 13:00:36 +02:00
|
|
|
kanikoParams = m
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
stepRule.step.buildExecute(
|
|
|
|
script: nullScript,
|
|
|
|
buildTool: 'kaniko',
|
|
|
|
dockerImageName: 'path/to/myImage',
|
|
|
|
dockerImageTag: 'myTag',
|
|
|
|
dockerRegistryUrl: 'https://my.registry:55555'
|
|
|
|
)
|
|
|
|
|
|
|
|
assertThat(kanikoParams.containerImageNameAndTag.toString(), is('my.registry:55555/path/to/myImage:myTag'))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|