forked from opensearch-project/opensearch-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestDockerBuild.groovy
71 lines (55 loc) · 2.51 KB
/
TestDockerBuild.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
import static org.hamcrest.CoreMatchers.equalTo
import static org.hamcrest.CoreMatchers.hasItem
import static org.hamcrest.CoreMatchers.hasItems
import static org.hamcrest.CoreMatchers.notNullValue
import static org.hamcrest.MatcherAssert.assertThat
class TestDockerBuild extends BuildPipelineTest {
// Variables
String dockerBuildGitRespository = 'https://github.com/opensearch-project/opensearch-build'
String dockerBuildGitRespositoryReference = 'main'
String dockerBuildScriptwithCommands = 'bash docker/ci/build-image-multi-arch.sh -v <TAG_NAME> -f <DOCKERFILE PATH>'
@Override
@Before
void setUp() {
super.setUp()
binding.setVariable('DOCKER_BUILD_GIT_REPOSITORY', dockerBuildGitRespository)
binding.setVariable('DOCKER_BUILD_GIT_REPOSITORY_REFERENCE', dockerBuildGitRespositoryReference)
binding.setVariable('DOCKER_BUILD_SCRIPT_WITH_COMMANDS', dockerBuildScriptwithCommands)
}
@Test
void DockerBuildRegression() {
super.testPipeline('jenkins/docker/docker-build.jenkinsfile',
'tests/jenkins/jenkinsjob-regression-files/docker/docker-build.jenkinsfile')
}
@Test
public void DockerBuildExecuteWithoutErrors() {
runScript("jenkins/docker/docker-build.jenkinsfile")
assertJobStatusSuccess()
// Ensure the entire docker commanbd is executed in an external shell script exactelly once
def dockerLoginCommand = getCommands('docker').findAll {
shCommand -> shCommand.contains('docker logout && echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin && eval $DOCKER_BUILD_SCRIPT_WITH_COMMANDS')
}
assertThat(dockerLoginCommand.size(), equalTo(1))
// Validate the docker-build.sh is called with correct predefined credential
assertCallStack().contains("docker-build.sh(echo Account: jenkins-staging-dockerhub-credential)")
printCallStack()
}
def getCommands(String commandString) {
def shCurlCommands = helper.callStack.findAll { call ->
call.methodName == 'sh'
}.collect { call ->
callArgsToString(call)
}.findAll { externalCommand ->
externalCommand.contains(commandString)
}
return shCurlCommands
}
}