2018-08-21 15:45:59 +02:00
import com.sap.piper.JenkinsUtils
2020-09-24 13:47:20 +02:00
import com.sap.piper.Utils
2018-08-31 10:22:43 +02:00
2020-09-24 13:47:20 +02:00
import org.junit.After
2018-08-21 15:45:59 +02:00
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
2019-03-20 11:07:37 +02:00
import groovy.json.JsonSlurper
2018-08-21 15:45:59 +02:00
import util.BasePiperTest
import util.JenkinsDockerExecuteRule
import util.JenkinsLoggingRule
2018-08-31 10:22:43 +02:00
import util.JenkinsReadYamlRule
2018-08-21 15:45:59 +02:00
import util.JenkinsShellCallRule
import util.JenkinsStepRule
import util.PluginMock
import util.Rules
2020-01-29 15:15:12 +02:00
import hudson.AbortException
2018-10-04 17:06:42 +02:00
import static org . hamcrest . Matchers . *
import static org . junit . Assert . assertThat
2018-08-21 15:45:59 +02:00
import static org . junit . Assert . assertTrue
import static org . junit . Assert . assertEquals
import static org . junit . Assert . assertFalse
2020-10-02 12:56:16 +02:00
import static org . junit . Assert . assertNotNull
2020-09-23 13:08:43 +02:00
import static org . junit . Assert . assertNull
2018-08-21 15:45:59 +02:00
class DockerExecuteOnKubernetesTest extends BasePiperTest {
private ExpectedException exception = ExpectedException . none ( )
2019-01-22 10:19:28 +02:00
private JenkinsShellCallRule shellRule = new JenkinsShellCallRule ( this )
2019-01-22 10:22:15 +02:00
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule ( this )
2019-01-22 10:25:42 +02:00
private JenkinsStepRule stepRule = new JenkinsStepRule ( this )
2020-01-29 15:15:12 +02:00
private ExpectedException thrown = new ExpectedException ( )
2018-08-21 15:45:59 +02:00
@Rule
public RuleChain ruleChain = Rules
. getCommonRules ( this )
2018-08-31 10:22:43 +02:00
. around ( new JenkinsReadYamlRule ( this ) )
2018-08-21 15:45:59 +02:00
. around ( exception )
2019-01-22 10:19:28 +02:00
. around ( shellRule )
2019-01-22 10:22:15 +02:00
. around ( loggingRule )
2019-01-22 10:25:42 +02:00
. around ( stepRule )
2020-01-29 15:15:12 +02:00
. around ( thrown )
2018-08-21 15:45:59 +02:00
int whichDockerReturnValue = 0
def bodyExecuted
def dockerImage
def containerMap
def dockerEnvVars
def dockerWorkspace
def podName = ''
def podLabel = ''
2019-06-19 12:26:16 +02:00
def podNodeSelector = ''
2018-08-21 15:45:59 +02:00
def containersList = [ ]
def imageList = [ ]
def containerName = ''
2019-01-08 20:44:28 +02:00
def containerShell = ''
2018-08-21 15:45:59 +02:00
def envList = [ ]
2018-10-04 17:06:42 +02:00
def portList = [ ]
def containerCommands = [ ]
2019-02-06 09:48:33 +02:00
def pullImageMap = [ : ]
2019-03-20 11:07:37 +02:00
def namespace
def securityContext
2020-04-08 19:54:06 +02:00
def inheritFrom
def yamlMergeStrategy
2020-10-02 12:56:16 +02:00
def podSpec
2020-09-23 13:08:43 +02:00
Map resources = [ : ]
2019-08-14 16:44:12 +02:00
List stashList = [ ]
2018-08-21 15:45:59 +02:00
@Before
void init ( ) {
containersList = [ ]
imageList = [ ]
envList = [ ]
2018-10-04 17:06:42 +02:00
portList = [ ]
containerCommands = [ ]
2020-09-23 13:08:43 +02:00
resources = [ : ]
2018-08-21 15:45:59 +02:00
bodyExecuted = false
2020-10-02 12:56:16 +02:00
podSpec = null
2018-08-21 15:45:59 +02:00
JenkinsUtils . metaClass . static . isPluginActive = { def s - > new PluginMock ( s ) . isActive ( ) }
2020-04-17 10:31:04 +02:00
helper . registerAllowedMethod ( 'sh' , [ Map . class ] , { return whichDockerReturnValue } )
helper . registerAllowedMethod ( 'container' , [ Map . class , Closure . class ] , { Map config , Closure body - >
container ( config ) {
body ( )
}
2018-08-21 15:45:59 +02:00
} )
2020-04-08 19:54:06 +02:00
helper . registerAllowedMethod ( 'merge' , [ ] , {
return 'merge'
} )
2018-08-21 15:45:59 +02:00
helper . registerAllowedMethod ( 'podTemplate' , [ Map . class , Closure . class ] , { Map options , Closure body - >
podName = options . name
podLabel = options . label
2019-03-20 11:07:37 +02:00
namespace = options . namespace
2020-04-08 19:54:06 +02:00
inheritFrom = options . inheritFrom
yamlMergeStrategy = options . yamlMergeStrategy
2019-06-19 12:26:16 +02:00
podNodeSelector = options . nodeSelector
2020-10-02 12:56:16 +02:00
podSpec = new JsonSlurper ( ) . parseText ( options . yaml ) // this yaml is actually json
2019-03-20 11:07:37 +02:00
def containers = podSpec . spec . containers
securityContext = podSpec . spec . securityContext
containers . each { container - >
containersList . add ( container . name )
imageList . add ( container . image . toString ( ) )
envList . add ( container . env )
2020-04-17 10:31:04 +02:00
if ( container . ports ) {
2019-03-20 11:07:37 +02:00
portList . add ( container . ports )
}
if ( container . command ) {
containerCommands . add ( container . command )
2018-10-04 17:06:42 +02:00
}
2019-03-20 11:07:37 +02:00
pullImageMap . put ( container . image . toString ( ) , container . imagePullPolicy = = "Always" )
2020-09-23 13:08:43 +02:00
resources . put ( container . name , container . resources )
2018-08-21 15:45:59 +02:00
}
body ( )
} )
2020-04-17 10:31:04 +02:00
helper . registerAllowedMethod ( 'stash' , [ Map . class ] , { m - >
2019-08-14 16:44:12 +02:00
stashList . add ( m )
2019-06-05 14:00:04 +02:00
} )
2020-09-24 13:47:20 +02:00
Utils . metaClass . echo = { def m - > }
}
@After
public void tearDown ( ) {
Utils . metaClass = null
2018-08-21 15:45:59 +02:00
}
@Test
void testRunOnPodNoContainerMapOnlyDockerImage ( ) throws Exception {
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes (
2018-11-05 12:24:25 +02:00
script: nullScript ,
juStabUtils: utils ,
2018-08-21 15:45:59 +02:00
dockerImage: 'maven:3.5-jdk-8-alpine' ,
dockerOptions: '-it' ,
dockerVolumeBind: [ 'my_vol' : '/my_vol' ] ,
2018-10-08 11:54:13 +02:00
dockerEnvVars: [ 'http_proxy' : 'http://proxy:8000' ] , dockerWorkspace: '/home/piper'
2020-04-17 10:31:04 +02:00
) {
2018-10-08 11:54:13 +02:00
bodyExecuted = true
2018-08-21 15:45:59 +02:00
}
2018-10-08 11:54:13 +02:00
assertThat ( containersList , hasItem ( 'container-exec' ) )
assertThat ( imageList , hasItem ( 'maven:3.5-jdk-8-alpine' ) )
assertThat ( envList . toString ( ) , containsString ( 'http_proxy' ) )
assertThat ( envList . toString ( ) , containsString ( 'http://proxy:8000' ) )
assertThat ( envList . toString ( ) , containsString ( '/home/piper' ) )
assertThat ( bodyExecuted , is ( true ) )
2018-10-04 17:06:42 +02:00
assertThat ( containerCommands . size ( ) , is ( 1 ) )
2018-08-21 15:45:59 +02:00
}
2020-04-17 10:31:04 +02:00
@Test
void testDockerExecuteOnKubernetesEmptyContainerMapNoDockerImage ( ) throws Exception {
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
containerMap: [ : ] ,
dockerEnvVars: [ 'customEnvKey' : 'customEnvValue' ] ) {
bodyExecuted = true
}
assertTrue ( bodyExecuted )
}
2018-08-21 15:45:59 +02:00
@Test
void testDockerExecuteOnKubernetesWithCustomContainerMap ( ) throws Exception {
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
2018-08-21 15:45:59 +02:00
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] ) {
container ( name: 'mavenexecute' ) {
bodyExecuted = true
}
}
assertEquals ( 'mavenexecute' , containerName )
assertTrue ( containersList . contains ( 'mavenexecute' ) )
assertTrue ( imageList . contains ( 'maven:3.5-jdk-8-alpine' ) )
assertTrue ( bodyExecuted )
2018-10-04 17:06:42 +02:00
assertThat ( containerCommands . size ( ) , is ( 1 ) )
2018-08-21 15:45:59 +02:00
}
2020-04-08 19:54:06 +02:00
@Test
void testInheritFromPodTemplate ( ) throws Exception {
nullScript . commonPipelineEnvironment . configuration = [ 'general' : [ 'jenkinsKubernetes' : [ 'inheritFrom' : 'default' ] ] ]
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] ) {
container ( name: 'mavenexecute' ) {
bodyExecuted = true
}
}
assertEquals ( inheritFrom , 'default' )
assertEquals ( yamlMergeStrategy , 'merge' )
assertTrue ( bodyExecuted )
}
2018-08-21 15:45:59 +02:00
@Test
void testDockerExecuteOnKubernetesWithCustomJnlpWithContainerMap ( ) throws Exception {
nullScript . commonPipelineEnvironment . configuration = [ 'general' : [ 'jenkinsKubernetes' : [ 'jnlpAgent' : 'myJnalpAgent' ] ] ]
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
2018-08-21 15:45:59 +02:00
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] ) {
container ( name: 'mavenexecute' ) {
bodyExecuted = true
}
}
assertEquals ( 'mavenexecute' , containerName )
assertTrue ( containersList . contains ( 'mavenexecute' ) )
assertTrue ( imageList . contains ( 'maven:3.5-jdk-8-alpine' ) )
assertTrue ( containersList . contains ( 'jnlp' ) )
assertTrue ( imageList . contains ( 'myJnalpAgent' ) )
assertTrue ( bodyExecuted )
}
2020-04-08 19:54:06 +02:00
2018-08-21 15:45:59 +02:00
@Test
void testDockerExecuteOnKubernetesWithCustomJnlpWithDockerImage ( ) throws Exception {
nullScript . commonPipelineEnvironment . configuration = [ 'general' : [ 'jenkinsKubernetes' : [ 'jnlpAgent' : 'myJnalpAgent' ] ] ]
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes (
2018-11-05 12:24:25 +02:00
script: nullScript ,
juStabUtils: utils ,
2018-08-21 15:45:59 +02:00
dockerImage: 'maven:3.5-jdk-8-alpine' ) {
bodyExecuted = true
}
assertEquals ( 'container-exec' , containerName )
assertTrue ( containersList . contains ( 'jnlp' ) )
assertTrue ( containersList . contains ( 'container-exec' ) )
assertTrue ( imageList . contains ( 'myJnalpAgent' ) )
assertTrue ( imageList . contains ( 'maven:3.5-jdk-8-alpine' ) )
assertTrue ( bodyExecuted )
}
@Test
void testDockerExecuteOnKubernetesWithCustomWorkspace ( ) throws Exception {
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
2018-08-21 15:45:59 +02:00
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] ,
dockerWorkspace: '/home/piper' ) {
container ( name: 'mavenexecute' ) {
bodyExecuted = true
}
}
assertTrue ( envList . toString ( ) . contains ( '/home/piper' ) )
assertTrue ( bodyExecuted )
}
@Test
void testDockerExecuteOnKubernetesWithCustomEnv ( ) throws Exception {
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
2018-08-21 15:45:59 +02:00
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] ,
dockerEnvVars: [ 'customEnvKey' : 'customEnvValue' ] ) {
container ( name: 'mavenexecute' ) {
bodyExecuted = true
}
}
assertTrue ( envList . toString ( ) . contains ( 'customEnvKey' ) & & envList . toString ( ) . contains ( 'customEnvValue' ) )
assertTrue ( bodyExecuted )
2020-09-23 13:08:43 +02:00
}
@Test
void testDockerExecuteOnKubernetesNoResourceLimitsOnEmptyResourcesMap ( ) throws Exception {
nullScript . commonPipelineEnvironment . configuration = [ general:
[ jenkinsKubernetes: [
resources: [
DEFAULT: [
requests: [
memory: '1Gi' ,
cpu: '0.25'
] ,
limits: [
memory: '2Gi' ,
cpu: '1'
]
] ,
mavenexecute: [ : ]
]
]
] ]
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] , {
bodyExecuted = true
} )
assertNull ( resources . mavenexecute )
assertTrue ( bodyExecuted )
}
@Test
void testDockerExecuteOnKubernetesWithDefaultResourceLimits ( ) throws Exception {
nullScript . commonPipelineEnvironment . configuration = [ general:
[ jenkinsKubernetes: [
resources: [ DEFAULT: [
requests: [
memory: '1Gi' ,
cpu: '0.25'
] ,
limits: [
memory: '2Gi' ,
cpu: '1'
]
]
]
] ] ]
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] , {
bodyExecuted = true
} )
assertEquals ( requests: [ memory: '1Gi' , cpu: '0.25' ] , limits: [ memory: '2Gi' , cpu: '1' ] , resources . jnlp )
assertEquals ( requests: [ memory: '1Gi' , cpu: '0.25' ] , limits: [ memory: '2Gi' , cpu: '1' ] , resources . mavenexecute )
assertTrue ( bodyExecuted )
}
@Test
void testDockerExecuteOnKubernetesWithSpecificResourcLimitsParametersAreTakingPrecendence ( ) throws Exception {
// the settings here are expected to be overwritten by the parameters provided via signature
nullScript . commonPipelineEnvironment . configuration = [ general:
[ jenkinsKubernetes: [
resources: [
mavenexecute: [
requests: [
memory: '2Gi' ,
cpu: '0.75'
] ,
limits: [
memory: '4Gi' ,
cpu: '2'
]
]
]
] ] ]
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] ,
resources: [
mavenexecute: [
requests: [
memory: '8Gi' ,
cpu: '2'
] ,
limits: [
memory: '16Gi' ,
cpu: '4'
]
]
] ) {
bodyExecuted = true
}
assertEquals ( requests: [ memory: '8Gi' , cpu: '2' ] , limits: [ memory: '16Gi' , cpu: '4' ] , resources . mavenexecute )
assertTrue ( bodyExecuted )
}
@Test
void testDockerExecuteOnKubernetesWithSpecificResourceLimits ( ) throws Exception {
nullScript . commonPipelineEnvironment . configuration = [ general:
[ jenkinsKubernetes: [
resources: [
DEFAULT: [
requests: [
memory: '1Gi' ,
cpu: '0.25'
] ,
limits: [
memory: '2Gi' ,
cpu: '1'
]
] ,
mavenexecute: [
requests: [
memory: '2Gi' ,
cpu: '0.75'
] ,
limits: [
memory: '4Gi' ,
cpu: '2'
]
] ,
jnlp: [
requests: [
memory: '3Gi' ,
cpu: '0.33'
] ,
limits: [
memory: '6Gi' ,
cpu: '3'
]
] ,
mysidecar: [
requests: [
memory: '10Gi' ,
cpu: '5.00'
] ,
limits: [
memory: '20Gi' ,
cpu: '10'
]
]
]
]
] ]
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] ,
sidecarImage: 'ubuntu' ,
sidecarName: 'mysidecar' ) {
bodyExecuted = true
}
2021-04-29 11:39:34 +02:00
2020-09-23 13:08:43 +02:00
assertEquals ( requests: [ memory: '10Gi' , cpu: '5.00' ] , limits: [ memory: '20Gi' , cpu: '10' ] , resources . mysidecar )
assertEquals ( requests: [ memory: '3Gi' , cpu: '0.33' ] , limits: [ memory: '6Gi' , cpu: '3' ] , resources . jnlp )
assertEquals ( requests: [ memory: '2Gi' , cpu: '0.75' ] , limits: [ memory: '4Gi' , cpu: '2' ] , resources . mavenexecute )
assertTrue ( bodyExecuted )
2018-08-21 15:45:59 +02:00
}
@Test
void testDockerExecuteOnKubernetesUpperCaseContainerName ( ) throws Exception {
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes ( script: nullScript ,
2018-08-21 15:45:59 +02:00
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'MAVENEXECUTE' ] ,
dockerEnvVars: [ 'customEnvKey' : 'customEnvValue' ] ) {
container ( name: 'mavenexecute' ) {
bodyExecuted = true
}
}
assertEquals ( 'mavenexecute' , containerName )
assertTrue ( containersList . contains ( 'mavenexecute' ) )
assertTrue ( imageList . contains ( 'maven:3.5-jdk-8-alpine' ) )
assertTrue ( bodyExecuted )
}
2018-10-04 17:06:42 +02:00
@Test
2019-09-12 10:52:05 +02:00
void testSidecarDefaultWithContainerMap ( ) {
2018-10-04 17:06:42 +02:00
List portMapping = [ ]
2020-04-17 10:31:04 +02:00
helper . registerAllowedMethod ( 'portMapping' , [ Map . class ] , { m - >
2018-10-04 17:06:42 +02:00
portMapping . add ( m )
return m
} )
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes (
2018-10-04 17:06:42 +02:00
script: nullScript ,
2018-11-05 12:24:25 +02:00
juStabUtils: utils ,
2018-10-04 17:06:42 +02:00
containerCommands: [ 'selenium/standalone-chrome' : '' ] ,
containerEnvVars: [
'selenium/standalone-chrome' : [ 'customEnvKey' : 'customEnvValue' ]
] ,
containerMap: [
2020-04-17 10:31:04 +02:00
'maven:3.5-jdk-8-alpine' : 'mavenexecute' ,
2018-10-04 17:06:42 +02:00
'selenium/standalone-chrome' : 'selenium'
] ,
containerName: 'mavenexecute' ,
containerPortMappings: [
2019-04-02 14:23:19 +02:00
'selenium/standalone-chrome' : [ [ containerPort: 4444 ] ]
2018-10-04 17:06:42 +02:00
] ,
containerWorkspaces: [
'selenium/standalone-chrome' : ''
] ,
dockerWorkspace: '/home/piper'
) {
bodyExecuted = true
}
assertThat ( bodyExecuted , is ( true ) )
assertThat ( containerName , is ( 'mavenexecute' ) )
assertThat ( containersList , allOf (
hasItem ( 'mavenexecute' ) ,
hasItem ( 'selenium' ) ,
) )
assertThat ( imageList , allOf (
hasItem ( 'maven:3.5-jdk-8-alpine' ) ,
hasItem ( 'selenium/standalone-chrome' ) ,
) )
2019-04-02 14:23:19 +02:00
assertThat ( portList , hasItem ( [ [ name: 'selenium0' , containerPort: 4444 ] ] ) )
2018-10-04 17:06:42 +02:00
assertThat ( containerCommands . size ( ) , is ( 1 ) )
2020-04-17 10:31:04 +02:00
assertThat ( envList , hasItem ( hasItem ( allOf ( hasEntry ( 'name' , 'customEnvKey' ) , hasEntry ( 'value' , 'customEnvValue' ) ) ) ) )
2018-10-04 17:06:42 +02:00
}
2019-09-12 10:52:05 +02:00
@Test
void testSidecarDefaultWithParameters ( ) {
List portMapping = [ ]
2020-04-17 10:31:04 +02:00
helper . registerAllowedMethod ( 'portMapping' , [ Map . class ] , { m - >
2019-09-12 10:52:05 +02:00
portMapping . add ( m )
return m
} )
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ] ,
containerName: 'mavenexecute' ,
dockerOptions: '-it' ,
dockerVolumeBind: [ 'my_vol' : '/my_vol' ] ,
dockerEnvVars: [ 'http_proxy' : 'http://proxy:8000' ] ,
dockerWorkspace: '/home/piper' ,
sidecarEnvVars: [ 'testEnv' : 'testVal' ] ,
2020-03-20 12:36:16 +02:00
sidecarWorkspace: '/home/piper/sidecar' ,
2019-09-12 10:52:05 +02:00
sidecarImage: 'postgres' ,
sidecarName: 'postgres' ,
sidecarReadyCommand: 'pg_isready'
) {
bodyExecuted = true
}
assertThat ( bodyExecuted , is ( true ) )
assertThat ( containersList , allOf ( hasItem ( 'postgres' ) , hasItem ( 'mavenexecute' ) ) )
assertThat ( imageList , allOf ( hasItem ( 'maven:3.5-jdk-8-alpine' ) , hasItem ( 'postgres' ) ) )
2020-03-20 12:36:16 +02:00
2020-04-17 10:31:04 +02:00
assertThat ( envList , hasItem ( hasItem ( allOf ( hasEntry ( 'name' , 'testEnv' ) , hasEntry ( 'value' , 'testVal' ) ) ) ) )
assertThat ( envList , hasItem ( hasItem ( allOf ( hasEntry ( 'name' , 'HOME' ) , hasEntry ( 'value' , '/home/piper/sidecar' ) ) ) ) )
2019-09-12 10:52:05 +02:00
}
2019-01-08 20:44:28 +02:00
@Test
void testDockerExecuteOnKubernetesWithCustomShell ( ) {
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes (
2019-01-08 20:44:28 +02:00
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
containerShell: '/busybox/sh'
) {
//nothing to exeute
}
assertThat ( containerShell , is ( '/busybox/sh' ) )
}
2019-01-14 15:43:07 +02:00
@Test
void testDockerExecuteOnKubernetesWithCustomContainerCommand ( ) {
2019-01-22 10:25:42 +02:00
stepRule . step . dockerExecuteOnKubernetes (
2019-01-14 15:43:07 +02:00
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
containerCommand: '/busybox/tail -f /dev/null'
) {
//nothing to exeute
}
2019-03-20 11:07:37 +02:00
assertThat ( containerCommands , hasItem ( [ '/bin/sh' , '-c' , '/busybox/tail -f /dev/null' ] ) )
2019-01-14 15:43:07 +02:00
}
2019-02-06 09:48:33 +02:00
@Test
void testSkipDockerImagePull ( ) throws Exception {
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
dockerPullImage: false ,
containerMap: [ 'maven:3.5-jdk-8-alpine' : 'mavenexecute' ]
) {
container ( name: 'mavenexecute' ) {
bodyExecuted = true
}
}
assertEquals ( false , pullImageMap . get ( 'maven:3.5-jdk-8-alpine' ) )
assertTrue ( bodyExecuted )
}
@Test
void testSkipSidecarImagePull ( ) throws Exception {
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
containerCommands: [ 'selenium/standalone-chrome' : '' ] ,
containerEnvVars: [
'selenium/standalone-chrome' : [ 'customEnvKey' : 'customEnvValue' ]
] ,
containerMap: [
2020-04-17 10:31:04 +02:00
'maven:3.5-jdk-8-alpine' : 'mavenexecute' ,
2019-02-06 09:48:33 +02:00
'selenium/standalone-chrome' : 'selenium'
] ,
containerName: 'mavenexecute' ,
containerWorkspaces: [
'selenium/standalone-chrome' : ''
] ,
containerPullImageFlags: [
2020-04-17 10:31:04 +02:00
'maven:3.5-jdk-8-alpine' : true ,
2019-02-06 09:48:33 +02:00
'selenium/standalone-chrome' : false
] ,
dockerWorkspace: '/home/piper'
) {
bodyExecuted = true
}
assertEquals ( true , pullImageMap . get ( 'maven:3.5-jdk-8-alpine' ) )
assertEquals ( false , pullImageMap . get ( 'selenium/standalone-chrome' ) )
assertTrue ( bodyExecuted )
}
2018-10-04 17:06:42 +02:00
2019-03-20 11:07:37 +02:00
@Test
void testDockerExecuteOnKubernetesWithCustomNamespace ( ) {
def expectedNamespace = "sandbox"
nullScript . commonPipelineEnvironment . configuration = [ general: [ jenkinsKubernetes: [ namespace: expectedNamespace ] ] ]
stepRule . step . dockerExecuteOnKubernetes (
2020-04-17 10:31:04 +02:00
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
) { bodyExecuted = true }
2019-03-20 11:07:37 +02:00
assertTrue ( bodyExecuted )
assertThat ( namespace , is ( equalTo ( expectedNamespace ) ) )
}
2020-10-02 12:56:16 +02:00
@Test
void testDockerExecuteWithAdditionalPodProperties ( ) {
nullScript . commonPipelineEnvironment . configuration = [ general: [ jenkinsKubernetes: [
additionalPodProperties: [
tolerations: [
[
key: "key1" ,
operator: "Equal" ,
value: "value1" ,
effect: "NoSchedule" ,
] ,
[
key: "key2" ,
operator: "Equal" ,
value: "value2" ,
effect: "NoExecute" ,
] ,
] ,
subdomain: 'foo' ,
shareProcessNamespace: false
]
] ] ]
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
) { bodyExecuted = true }
assertTrue ( bodyExecuted )
assertEquals (
[
[
"key" : "key1" ,
"operator" : "Equal" ,
"value" : "value1" ,
"effect" : "NoSchedule"
] ,
[
"key" : "key2" ,
"operator" : "Equal" ,
"value" : "value2" ,
"effect" : "NoExecute"
]
] , podSpec . spec . tolerations )
assertEquals ( 'foo' , podSpec . spec . subdomain )
assertFalse ( podSpec . spec . shareProcessNamespace )
assertThat ( loggingRule . log , containsString ( 'Additional pod properties found ([tolerations, subdomain, shareProcessNamespace]). Providing additional pod properties is some kind of expert mode. In case of any problems caused by these additional properties only limited support can be provided.' ) )
}
@Test
void testDockerExecuteWithAdditionalPodPropertiesContainerPropertyIsNotOverwritten ( ) {
nullScript . commonPipelineEnvironment . configuration = [ general: [ jenkinsKubernetes: [
additionalPodProperties: [
containers: [
[
some: "stupid" ,
stuff: "here" ,
]
] ,
subdomain: 'foo' ,
shareProcessNamespace: false ,
]
] ] ]
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
) { bodyExecuted = true }
assertTrue ( bodyExecuted )
// This is not contained in the configuration above.
assertNotNull ( podSpec . spec . containers [ 0 ] . name )
// This is contained in the config above.
assertNull ( podSpec . spec . some )
assertNull ( podSpec . spec . stuff )
assertEquals ( 'foo' , podSpec . spec . subdomain )
assertFalse ( podSpec . spec . shareProcessNamespace )
}
2019-03-20 11:07:37 +02:00
@Test
void testDockerExecuteOnKubernetesWithSecurityContext ( ) {
2020-04-17 10:31:04 +02:00
def expectedSecurityContext = [ runAsUser: 1000 , fsGroup: 1000 ]
2019-03-20 11:07:37 +02:00
nullScript . commonPipelineEnvironment . configuration = [ general: [ jenkinsKubernetes: [
2020-04-17 10:31:04 +02:00
securityContext: expectedSecurityContext ] ] ]
2019-03-20 11:07:37 +02:00
stepRule . step . dockerExecuteOnKubernetes (
2020-04-17 10:31:04 +02:00
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
) { bodyExecuted = true }
2019-03-20 11:07:37 +02:00
assertTrue ( bodyExecuted )
assertThat ( securityContext , is ( equalTo ( expectedSecurityContext ) ) )
}
2019-06-05 11:24:32 +02:00
@Test
2019-06-19 12:26:16 +02:00
void testDockerExecuteOnKubernetesCustomNode ( ) {
2019-06-05 11:24:32 +02:00
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
2019-06-19 12:26:16 +02:00
nodeSelector: 'size:big'
2019-06-05 11:24:32 +02:00
) { bodyExecuted = true }
assertTrue ( bodyExecuted )
2019-06-19 12:26:16 +02:00
assertThat ( podNodeSelector , is ( 'size:big' ) )
2019-06-05 11:24:32 +02:00
}
2019-07-17 12:01:24 +02:00
@Test
void testDockerExecuteOnKubernetesCustomJnlpViaEnv ( ) {
2019-08-14 16:44:12 +02:00
nullScript . commonPipelineEnvironment . configuration = [
2019-07-17 12:01:24 +02:00
general: [ jenkinsKubernetes: [ jnlpAgent: 'config/jnlp:latest' ] ]
]
binding . variables . env . JENKINS_JNLP_IMAGE = 'env/jnlp:latest'
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
) { bodyExecuted = true }
assertTrue ( bodyExecuted )
assertThat ( containersList , allOf (
hasItem ( 'jnlp' ) ,
hasItem ( 'container-exec' )
) )
assertThat ( imageList , allOf (
hasItem ( 'env/jnlp:latest' ) ,
hasItem ( 'maven:3.5-jdk-8-alpine' ) ,
) )
}
@Test
void testDockerExecuteOnKubernetesCustomJnlpViaConfig ( ) {
2019-08-14 16:44:12 +02:00
nullScript . commonPipelineEnvironment . configuration = [
2019-07-17 12:01:24 +02:00
general: [ jenkinsKubernetes: [ jnlpAgent: 'config/jnlp:latest' ] ]
]
2019-08-14 16:44:12 +02:00
//binding.variables.env.JENKINS_JNLP_IMAGE = 'config/jnlp:latest'
2019-07-17 12:01:24 +02:00
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
) { bodyExecuted = true }
assertTrue ( bodyExecuted )
assertThat ( containersList , allOf (
hasItem ( 'jnlp' ) ,
hasItem ( 'container-exec' )
) )
assertThat ( imageList , allOf (
hasItem ( 'config/jnlp:latest' ) ,
hasItem ( 'maven:3.5-jdk-8-alpine' ) ,
) )
}
2020-01-29 15:15:12 +02:00
@Test
void testDockerExecuteOnKubernetesExecutionFails ( ) {
thrown . expect ( AbortException )
thrown . expectMessage ( 'Execution failed.' )
nullScript . commonPipelineEnvironment . configuration = [
general: [ jenkinsKubernetes: [ jnlpAgent: 'config/jnlp:latest' ] ]
]
//binding.variables.env.JENKINS_JNLP_IMAGE = 'config/jnlp:latest'
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
) { throw new AbortException ( 'Execution failed.' ) }
}
2019-08-14 16:44:12 +02:00
@Test
2019-12-17 16:10:57 +02:00
void testStashIncludesAndExcludes ( ) {
2019-08-14 16:44:12 +02:00
nullScript . commonPipelineEnvironment . configuration = [
steps: [
dockerExecuteOnKubernetes: [
stashExcludes: [
workspace: 'workspace/exclude.test' ,
stashBack: 'container/exclude.test'
] ,
stashIncludes: [
workspace: 'workspace/include.test' ,
stashBack: 'container/include.test'
]
]
]
]
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
) {
bodyExecuted = true
}
2019-12-17 16:10:57 +02:00
assertThat ( stashList , hasItem ( allOf (
not ( hasEntry ( 'allowEmpty' , true ) ) ,
2020-04-17 10:31:04 +02:00
hasEntry ( 'includes' , 'workspace/include.test' ) ,
hasEntry ( 'excludes' , 'workspace/exclude.test' ) ) ) )
2019-12-17 16:10:57 +02:00
assertThat ( stashList , hasItem ( allOf (
not ( hasEntry ( 'allowEmpty' , true ) ) ,
2020-04-17 10:31:04 +02:00
hasEntry ( 'includes' , 'container/include.test' ) ,
hasEntry ( 'excludes' , 'container/exclude.test' ) ) ) )
2019-08-14 16:44:12 +02:00
}
2021-04-29 11:39:34 +02:00
@Test
void testDockerExecuteWithVolumeProperties ( ) {
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
containerName: 'mycontainer' ,
2021-06-01 09:59:49 +02:00
initContainerImage: 'ppiper/cf-cli' ,
2021-04-29 11:39:34 +02:00
dockerImage: 'maven:3.5-jdk-8-alpine' ,
containerMountPath: '/opt' ,
) { bodyExecuted = true }
def containerSpec = podSpec . spec . containers . find { it . image = = "maven:3.5-jdk-8-alpine" }
2021-06-01 09:59:49 +02:00
def initContainerSpec = podSpec . spec . initContainers [ 0 ]
2021-04-29 11:39:34 +02:00
assertTrue ( bodyExecuted )
assertEquals (
[ [
"name" : "volume" ,
"emptyDir" : [ : ]
] ] , podSpec . spec . volumes )
assertEquals (
[ [
"name" : "volume" ,
"mountPath" : "/opt"
] ] , containerSpec . volumeMounts )
2021-06-01 09:59:49 +02:00
assertEquals (
[ [
"name" : "volume" ,
"mountPath" : "/opt"
] ] , initContainerSpec . volumeMounts )
}
@Test
void testInitContainerDefaultWithParameters ( ) {
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
containerName: 'mycontainer' ,
initContainerImage: 'ppiper/cf-cli@sha256:latest' ,
initContainerCommand: 'cp /usr/local/bin/cf7 /opt/bin/cf' ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
containerMountPath: '/opt' ,
) { bodyExecuted = true }
def initContainer = podSpec . spec . initContainers [ 0 ]
def expectedCommandOutput = [ "sh" , "-c" , "cp /usr/local/bin/cf7 /opt/bin/cf" ]
assertTrue ( bodyExecuted )
assertEquals ( "ppiper-cf-cli-sha256-latest" , initContainer . name )
assertEquals ( "ppiper/cf-cli@sha256:latest" , initContainer . image )
assertThat ( initContainer . command , is ( equalTo ( expectedCommandOutput ) ) )
}
@Test
void testInitContainerWithoutContainerCommand ( ) {
stepRule . step . dockerExecuteOnKubernetes (
script: nullScript ,
juStabUtils: utils ,
containerName: 'mycontainer' ,
initContainerImage: 'ppiper/cf-cli@sha256:latest' ,
dockerImage: 'maven:3.5-jdk-8-alpine' ,
containerMountPath: '/opt' ,
) { bodyExecuted = true }
def initContainer = podSpec . spec . initContainers [ 0 ]
def expectedCommandOutput = [ "/usr/bin/tail" , "-f" , "/dev/null" ]
assertTrue ( bodyExecuted )
assertThat ( initContainer . command , is ( equalTo ( expectedCommandOutput ) ) )
2021-04-29 11:39:34 +02:00
}
2019-03-20 11:07:37 +02:00
2018-08-21 15:45:59 +02:00
private container ( options , body ) {
containerName = options . name
2019-01-08 20:44:28 +02:00
containerShell = options . shell
2018-08-21 15:45:59 +02:00
body ( )
}
}