-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from cloudogu/feature/85_bats_test
Feature/85 bats test
- Loading branch information
Showing
6 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.cloudogu.ces.cesbuildlib | ||
|
||
/** | ||
* Bats provides functions to easily execute bats tests (bash scripting tests) | ||
*/ | ||
class Bats { | ||
private script | ||
private docker | ||
|
||
private static String bats_base_image = "bats_base_image" | ||
private static String bats_custom_image = "bats_custom_image" | ||
private static String bats_tag = "bats_tag" | ||
def defaultSetupConfig = [ | ||
bats_base_image : "bats/bats", | ||
bats_custom_image: "cloudogu/bats", | ||
bats_tag : "1.2.1" | ||
] | ||
|
||
Bats(script, docker) { | ||
this.script = script | ||
this.docker = docker | ||
} | ||
|
||
void checkAndExecuteTests(config = [:]) { | ||
// Merge default config with the one passed as parameter | ||
config = defaultSetupConfig << config | ||
|
||
script.echo "Executing bats tests with config:" | ||
script.echo "${config}" | ||
def batsImage = docker.build("${config[bats_custom_image]}:${config[bats_tag]}", "--build-arg=BATS_BASE_IMAGE=${config[bats_base_image]} --build-arg=BATS_TAG=${config[bats_tag]} ./build/make/bats") | ||
try { | ||
script.sh "mkdir -p target" | ||
script.sh "mkdir -p testdir" | ||
|
||
batsImage.inside("--entrypoint='' -v ${script.env.WORKSPACE}:/workspace -v ${script.env.WORKSPACE}/testdir:/usr/share/webapps") { | ||
script.sh "make unit-test-shell-ci" | ||
} | ||
} finally { | ||
script.junit allowEmptyResults: true, testResults: 'target/shell_test_reports/*.xml' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.cloudogu.ces.cesbuildlib | ||
|
||
import org.junit.Test | ||
import org.mockito.invocation.InvocationOnMock | ||
import org.mockito.stubbing.Answer | ||
|
||
import static org.assertj.core.api.Assertions.assertThat | ||
import static org.mockito.ArgumentMatchers.any | ||
import static org.mockito.ArgumentMatchers.anyString | ||
import static org.mockito.ArgumentMatchers.eq | ||
import static org.mockito.Mockito.mock | ||
import static org.mockito.Mockito.verify | ||
import static org.mockito.Mockito.when | ||
|
||
class BatsTest extends GroovyTestCase { | ||
|
||
ScriptMock scriptMock = new ScriptMock() | ||
|
||
@Test | ||
void test_Constructor() { | ||
// given | ||
Docker dockerMock = mock(Docker.class) | ||
Docker.Image imageMock = mock(Docker.Image.class) | ||
when(dockerMock.build(anyString(), anyString())).thenReturn(imageMock) | ||
|
||
// when | ||
Bats bats = new Bats(scriptMock, dockerMock) | ||
|
||
// then | ||
assertNotNull(bats) | ||
} | ||
|
||
@Test | ||
void test_checkAndExecuteTests() { | ||
// given | ||
Docker dockerMock = mock(Docker.class) | ||
Docker.Image imageMock = mock(Docker.Image.class) | ||
when(dockerMock.build("cloudogu/bats:1.2.1", "--build-arg=BATS_BASE_IMAGE=bats/bats --build-arg=BATS_TAG=1.2.1 ./build/make/bats")).thenReturn(imageMock) | ||
when(imageMock.inside(anyString(), any())).thenAnswer(new Answer<Object>() { | ||
@Override | ||
Object answer(InvocationOnMock invocation) throws Throwable { | ||
Closure closure = invocation.getArgument(1) | ||
closure.call() | ||
} | ||
}) | ||
|
||
Bats bats = new Bats(scriptMock, dockerMock) | ||
|
||
// when | ||
bats.checkAndExecuteTests() | ||
|
||
// then | ||
assertThat(scriptMock.actualEcho[0].trim()).contains("Executing bats tests with config:") | ||
assertThat(scriptMock.actualEcho[1].trim()).contains("[bats_base_image:bats/bats, bats_custom_image:cloudogu/bats, bats_tag:1.2.1]") | ||
|
||
verify(dockerMock).build("cloudogu/bats:1.2.1", "--build-arg=BATS_BASE_IMAGE=bats/bats --build-arg=BATS_TAG=1.2.1 ./build/make/bats") | ||
verify(imageMock).inside(eq("--entrypoint='' -v :/workspace -v /testdir:/usr/share/webapps"), any()) | ||
|
||
assertEquals("true", scriptMock.actualJUnitFlags["allowEmptyResults"].toString()) | ||
assertEquals("target/shell_test_reports/*.xml", scriptMock.actualJUnitFlags["testResults"].toString()) | ||
} | ||
|
||
@Test | ||
void test_checkAndExecuteTests_with_custom_config() { | ||
// given | ||
def defaultSetupConfig = [ | ||
bats_custom_image: "myimage/bats", | ||
bats_tag : "1.4.1" | ||
] | ||
|
||
Docker dockerMock = mock(Docker.class) | ||
Docker.Image imageMock = mock(Docker.Image.class) | ||
when(dockerMock.build("myimage/bats:1.4.1", "--build-arg=BATS_BASE_IMAGE=bats/bats --build-arg=BATS_TAG=1.4.1 ./build/make/bats")).thenReturn(imageMock) | ||
when(imageMock.inside(anyString(), any())).thenAnswer(new Answer<Object>() { | ||
@Override | ||
Object answer(InvocationOnMock invocation) throws Throwable { | ||
Closure closure = invocation.getArgument(1) | ||
closure.call() | ||
} | ||
}) | ||
|
||
Bats bats = new Bats(scriptMock, dockerMock) | ||
|
||
// when | ||
bats.checkAndExecuteTests(defaultSetupConfig) | ||
|
||
// then | ||
assertThat(scriptMock.actualEcho[0].trim()).contains("Executing bats tests with config:") | ||
assertThat(scriptMock.actualEcho[1].trim()).contains("[bats_base_image:bats/bats, bats_custom_image:myimage/bats, bats_tag:1.4.1]") | ||
|
||
verify(dockerMock).build("myimage/bats:1.4.1", "--build-arg=BATS_BASE_IMAGE=bats/bats --build-arg=BATS_TAG=1.4.1 ./build/make/bats") | ||
verify(imageMock).inside(eq("--entrypoint='' -v :/workspace -v /testdir:/usr/share/webapps"), any()) | ||
|
||
assertEquals("true", scriptMock.actualJUnitFlags["allowEmptyResults"].toString()) | ||
assertEquals("target/shell_test_reports/*.xml", scriptMock.actualJUnitFlags["testResults"].toString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters