github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/DockerUtilsTest.groovy (about) 1 package com.sap.piper 2 3 import hudson.AbortException 4 import org.junit.Before 5 import org.junit.Ignore 6 import org.junit.Rule 7 import org.junit.Test 8 import org.junit.rules.ExpectedException 9 import org.junit.rules.RuleChain 10 import util.BasePiperTest 11 import util.JenkinsCredentialsRule 12 import util.JenkinsShellCallRule 13 import util.Rules 14 15 import static org.hamcrest.CoreMatchers.hasItem 16 import static org.hamcrest.CoreMatchers.is 17 import static org.junit.Assert.assertThat 18 19 class DockerUtilsTest extends BasePiperTest { 20 21 public ExpectedException exception = ExpectedException.none() 22 public JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this) 23 24 def dockerMockArgs = [:] 25 class DockerMock { 26 def withRegistry(paramRegistry, paramClosure){ 27 dockerMockArgs.paramRegistryAnonymous = paramRegistry.toString() 28 return paramClosure() 29 } 30 } 31 32 @Rule 33 public RuleChain ruleChain = Rules.getCommonRules(this) 34 .around(shellCallRule) 35 .around(exception) 36 .around(new JenkinsCredentialsRule(this) 37 .withCredentials('testCredentialsId', 'registryUser', '********') 38 ) 39 @Before 40 void init() { 41 nullScript.binding.setVariable('docker', new DockerMock()) 42 } 43 44 @Test 45 void testWithDockerDaemon() { 46 DockerUtils dockerUtils = new DockerUtils(nullScript) 47 assertThat(dockerUtils.withDockerDaemon(), is(true)) 48 } 49 50 @Test 51 void testWithoutDockerDaemon() { 52 shellCallRule.setReturnValue('docker ps -q > /dev/null', 1) 53 DockerUtils dockerUtils = new DockerUtils(nullScript) 54 assertThat(dockerUtils.withDockerDaemon(), is(false)) 55 } 56 57 @Test 58 void testOnKubernetes() { 59 nullScript.env.ON_K8S = 'true' 60 DockerUtils dockerUtils = new DockerUtils(nullScript) 61 assertThat(dockerUtils.onKubernetes(), is(true)) 62 } 63 64 @Test 65 void testMoveImageKubernetes() { 66 shellCallRule.setReturnValue('docker ps -q > /dev/null', 1) 67 DockerUtils dockerUtils = new DockerUtils(nullScript) 68 dockerUtils.moveImage( 69 [ 70 registryUrl: 'https://my.source.registry:44444', 71 image: 'sourceImage:sourceTag', 72 credentialsId: 'testCredentialsId' 73 ], 74 [ 75 registryUrl: 'https://my.registry:55555', 76 image: 'testImage:tag', 77 credentialsId: 'testCredentialsId' 78 ] 79 ) 80 81 assertThat(shellCallRule.shell, hasItem('skopeo copy --multi-arch=all --src-tls-verify=false --src-creds=\'registryUser\':\'********\' --dest-tls-verify=false --dest-creds=\'registryUser\':\'********\' docker://my.source.registry:44444/sourceImage:sourceTag docker://my.registry:55555/testImage:tag')) 82 } 83 84 @Test 85 void testGetRegistryFromUrl() { 86 DockerUtils dockerUtils = new DockerUtils(nullScript) 87 assertThat(dockerUtils.getRegistryFromUrl('https://my.registry.com:55555'), is('my.registry.com:55555')) 88 assertThat(dockerUtils.getRegistryFromUrl('http://my.registry.com:55555'), is('my.registry.com:55555')) 89 assertThat(dockerUtils.getRegistryFromUrl('https://my.registry.com'), is('my.registry.com')) 90 } 91 92 @Test 93 void testGetProtocolFromUrl() { 94 DockerUtils dockerUtils = new DockerUtils(nullScript) 95 assertThat(dockerUtils.getProtocolFromUrl('https://my.registry.com:55555'), is('https')) 96 assertThat(dockerUtils.getProtocolFromUrl('http://my.registry.com:55555'), is('http')) 97 } 98 }