Skip to content

Commit

Permalink
Adding TestDockerCopy.groovy (#2792)
Browse files Browse the repository at this point in the history
* Add TestDockerCopy.groovy

Signed-off-by: Jeff Lu <[email protected]>

* update TestDockerCopy.groovy

Signed-off-by: Jeff Lu <[email protected]>

* update TestDoickerCopy.groovy test cases

Signed-off-by: Jeff Lu <[email protected]>

* update code comment

Signed-off-by: Jeff Lu <[email protected]>

Signed-off-by: Jeff Lu <[email protected]>
  • Loading branch information
jordarlu authored Oct 26, 2022
1 parent 9d8969c commit 71becf2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/jenkins/TestDockerCopy.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 TestDockerCopy extends BuildPipelineTest {

String sourceImageRegistry = 'opensearchstaging'
String sourceImage = 'opensearch:1.3.2'
String destinationImageRegistry = 'opensearchproject'
String destinationImage = 'opensearch:1.3.2'

@Override
@Before
void setUp() {

super.setUp()

// Variables
binding.setVariable('SOURCE_IMAGE_REGISTRY', sourceImageRegistry)
binding.setVariable('SOURCE_IMAGE', sourceImage)
binding.setVariable('DESTINATION_IMAGE_REGISTRY', destinationImageRegistry)
binding.setVariable('DESTINATION_IMAGE', destinationImage)

}

@Test
void DockerCopyRegression() {
super.testPipeline('jenkins/docker/docker-copy.jenkinsfile',
'tests/jenkins/jenkinsjob-regression-files/docker/docker-copy.jenkinsfile')
}

@Test
public void DockerCopyExecuteWithoutErrors() {
runScript("jenkins/docker/docker-copy.jenkinsfile")

assertJobStatusSuccess()

// Ensure the gcrane is executed in an external shell script exactely once
def copyContainerCommand = getCommands('docker').findAll {
shCommand -> shCommand.contains('gcrane cp opensearchstaging/opensearch:1.3.2 opensearchproject/opensearch:1.3.2; docker logout')
}
assertThat(copyContainerCommand.size(), equalTo(1))

// Validating docker-copy.jenkinsfile does run copyContainer.groovy
assertCallStack().contains("docker-copy.copyContainer")

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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
docker-copy.run()
docker-copy.modernSCM({$class=GitSCMSource, remote=https://github.com/opensearch-project/opensearch-build-libraries.git})
docker-copy.library({[email protected], retriever=null})
docker-copy.pipeline(groovy.lang.Closure)
docker-copy.timeout({time=30})
docker-copy.echo(Executing on agent [label:none])
docker-copy.stage(Parameters Check, groovy.lang.Closure)
docker-copy.script(groovy.lang.Closure)
docker-copy.stage(Copy Image to ECR/DockerHub, groovy.lang.Closure)
docker-copy.script(groovy.lang.Closure)
docker-copy.copyContainer({sourceImage=opensearch:1.3.2, sourceRegistry=opensearchstaging, destinationImage=opensearch:1.3.2, destinationRegistry=opensearchproject})
copyContainer.usernamePassword({credentialsId=jenkins-production-dockerhub-credential, usernameVariable=DOCKER_USERNAME, passwordVariable=DOCKER_PASSWORD})
copyContainer.withCredentials([[DOCKER_USERNAME, DOCKER_PASSWORD]], groovy.lang.Closure)
copyContainer.sh({returnStdout=true, script=echo DOCKER_PASSWORD | docker login --username DOCKER_USERNAME --password-stdin})
copyContainer.sh(gcrane cp opensearchstaging/opensearch:1.3.2 opensearchproject/opensearch:1.3.2; docker logout)
docker-copy.script(groovy.lang.Closure)
docker-copy.postCleanup()
postCleanup.cleanWs({disableDeferredWipeout=true, deleteDirs=true})
docker-copy.sh(docker logout opensearchproject && docker image prune -f --all)

0 comments on commit 71becf2

Please sign in to comment.