mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-14 11:03:09 +02:00
5bb6d59753
This step should serve as generic entry point in pipelines for building artifacts. Build principle: build once. Purpose of the step: - build using a defined build technology - store build result for future use in testing etc.
98 lines
3.4 KiB
Groovy
98 lines
3.4 KiB
Groovy
package com.sap.piper
|
|
|
|
import hudson.AbortException
|
|
import org.junit.Before
|
|
import org.junit.Ignore
|
|
import org.junit.Rule
|
|
import org.junit.Test
|
|
import org.junit.rules.ExpectedException
|
|
import org.junit.rules.RuleChain
|
|
import util.BasePiperTest
|
|
import util.JenkinsCredentialsRule
|
|
import util.JenkinsShellCallRule
|
|
import util.Rules
|
|
|
|
import static org.hamcrest.CoreMatchers.hasItem
|
|
import static org.hamcrest.CoreMatchers.is
|
|
import static org.junit.Assert.assertThat
|
|
|
|
class DockerUtilsTest extends BasePiperTest {
|
|
|
|
public ExpectedException exception = ExpectedException.none()
|
|
public JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
|
|
|
|
def dockerMockArgs = [:]
|
|
class DockerMock {
|
|
def withRegistry(paramRegistry, paramClosure){
|
|
dockerMockArgs.paramRegistryAnonymous = paramRegistry.toString()
|
|
return paramClosure()
|
|
}
|
|
}
|
|
|
|
@Rule
|
|
public RuleChain ruleChain = Rules.getCommonRules(this)
|
|
.around(shellCallRule)
|
|
.around(exception)
|
|
.around(new JenkinsCredentialsRule(this)
|
|
.withCredentials('testCredentialsId', 'registryUser', '********')
|
|
)
|
|
@Before
|
|
void init() {
|
|
nullScript.binding.setVariable('docker', new DockerMock())
|
|
}
|
|
|
|
@Test
|
|
void testWithDockerDaemon() {
|
|
DockerUtils dockerUtils = new DockerUtils(nullScript)
|
|
assertThat(dockerUtils.withDockerDaemon(), is(true))
|
|
}
|
|
|
|
@Test
|
|
void testWithoutDockerDaemon() {
|
|
shellCallRule.setReturnValue('docker ps -q > /dev/null', 1)
|
|
DockerUtils dockerUtils = new DockerUtils(nullScript)
|
|
assertThat(dockerUtils.withDockerDaemon(), is(false))
|
|
}
|
|
|
|
@Test
|
|
void testOnKubernetes() {
|
|
nullScript.env.ON_K8S = 'true'
|
|
DockerUtils dockerUtils = new DockerUtils(nullScript)
|
|
assertThat(dockerUtils.onKubernetes(), is(true))
|
|
}
|
|
|
|
@Test
|
|
void testMoveImageKubernetes() {
|
|
shellCallRule.setReturnValue('docker ps -q > /dev/null', 1)
|
|
DockerUtils dockerUtils = new DockerUtils(nullScript)
|
|
dockerUtils.moveImage(
|
|
[
|
|
registryUrl: 'https://my.source.registry:44444',
|
|
image: 'sourceImage:sourceTag'
|
|
],
|
|
[
|
|
registryUrl: 'https://my.registry:55555',
|
|
image: 'testImage:tag',
|
|
credentialsId: 'testCredentialsId'
|
|
]
|
|
)
|
|
|
|
assertThat(shellCallRule.shell, hasItem('skopeo copy --src-tls-verify=false --dest-tls-verify=false --dest-creds=\'registryUser\':\'********\' docker://my.source.registry:44444/sourceImage:sourceTag docker://my.registry:55555/testImage:tag'))
|
|
}
|
|
|
|
@Test
|
|
void testGetRegistryFromUrl() {
|
|
DockerUtils dockerUtils = new DockerUtils(nullScript)
|
|
assertThat(dockerUtils.getRegistryFromUrl('https://my.registry.com:55555'), is('my.registry.com:55555'))
|
|
assertThat(dockerUtils.getRegistryFromUrl('http://my.registry.com:55555'), is('my.registry.com:55555'))
|
|
assertThat(dockerUtils.getRegistryFromUrl('https://my.registry.com'), is('my.registry.com'))
|
|
}
|
|
|
|
@Test
|
|
void testGetProtocolFromUrl() {
|
|
DockerUtils dockerUtils = new DockerUtils(nullScript)
|
|
assertThat(dockerUtils.getProtocolFromUrl('https://my.registry.com:55555'), is('https'))
|
|
assertThat(dockerUtils.getProtocolFromUrl('http://my.registry.com:55555'), is('http'))
|
|
}
|
|
}
|