use ubuntu_plus_user as default test container image

This commit is contained in:
az 2021-03-31 12:09:03 +02:00
parent 4587938e0b
commit 2623eb857f
10 changed files with 85 additions and 51 deletions

View file

@ -1,5 +1,7 @@
buildscript { buildscript {
ext.kotlin_version = '1.4.31' ext.kotlin_version = '1.4.31'
ext.CI_PROJECT_ID = System.env.CI_PROJECT_ID
repositories { jcenter() } repositories { jcenter() }
dependencies { dependencies {
@ -60,9 +62,8 @@ dependencies {
api "ch.qos.logback:logback-classic:1.2.3" api "ch.qos.logback:logback-classic:1.2.3"
api "ch.qos.logback:logback-core:1.2.3" api "ch.qos.logback:logback-core:1.2.3"
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2' testFixturesApi group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
testImplementation "io.mockk:mockk:1.9.3"
} }
@ -151,7 +152,7 @@ publishing {
if (System.getenv("CI_JOB_TOKEN") != null) { if (System.getenv("CI_JOB_TOKEN") != null) {
// see https://docs.gitlab.com/ee/user/packages/maven_repository/index.html // see https://docs.gitlab.com/ee/user/packages/maven_repository/index.html
maven { maven {
url "https://gitlab.com/api/v4/projects/23966425/packages/maven" url "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/packages/maven"
name "GitLab" name "GitLab"
credentials(HttpHeaderCredentials) { credentials(HttpHeaderCredentials) {
name = 'Job-Token' name = 'Job-Token'

View file

@ -8,6 +8,30 @@ import io.provs.platforms.UbuntuProv
import io.provs.processors.ContainerStartMode import io.provs.processors.ContainerStartMode
/**
* Builds a docker image if not yet existing.
*/
fun Prov.dockerProvideImage(image: DockerImage, skipIfExisting: Boolean = true, sudo: Boolean = true) : ProvResult {
if (this is UbuntuProv) {
return this.dockerProvideImagePlatform(image, skipIfExisting, sudo)
} else {
throw RuntimeException("docker not yet supported for " + (this as UbuntuProv).javaClass)
}
}
/**
* Returns true if the specified docker image exists.
*/
fun Prov.dockerImageExists(imageName: String, sudo: Boolean = true) : Boolean {
if (this is UbuntuProv) {
return this.dockerImageExistsPlatform(imageName, sudo)
} else {
throw RuntimeException("docker not yet supported for " + (this as UbuntuProv).javaClass)
}
}
/** /**
* Creates and runs a new container with name _containerName_ for image _imageName_ if not yet existing. * Creates and runs a new container with name _containerName_ for image _imageName_ if not yet existing.
* In case the container already exists, the parameter _startMode_ determines * In case the container already exists, the parameter _startMode_ determines
@ -51,22 +75,6 @@ fun Prov.runContainer(
} }
fun Prov.dockerBuildImage(image: DockerImage, skipIfExisting: Boolean = true, sudo: Boolean = true) : ProvResult {
if (this is UbuntuProv) {
return this.dockerBuildImagePlatform(image, skipIfExisting, sudo)
} else {
throw RuntimeException("docker not yet supported for " + (this as UbuntuProv).javaClass)
}
}
fun Prov.dockerImageExists(imageName: String, sudo: Boolean = true) : Boolean {
if (this is UbuntuProv) {
return this.dockerImageExistsPlatform(imageName, sudo)
} else {
throw RuntimeException("docker not yet supported for " + (this as UbuntuProv).javaClass)
}
}
fun Prov.exitAndRmContainer( fun Prov.exitAndRmContainer(

View file

@ -1,10 +1,13 @@
package io.provs.docker.platforms package io.provs.docker.platforms
import io.provs.* import io.provs.ProvResult
import io.provs.docker.containerRuns import io.provs.docker.containerRuns
import io.provs.docker.dockerImageExists import io.provs.docker.dockerImageExists
import io.provs.docker.exitAndRmContainer import io.provs.docker.exitAndRmContainer
import io.provs.docker.images.DockerImage import io.provs.docker.images.DockerImage
import io.provs.escapeSingleQuote
import io.provs.fileSeparator
import io.provs.hostUserHome
import io.provs.platforms.UbuntuProv import io.provs.platforms.UbuntuProv
import io.provs.processors.ContainerStartMode import io.provs.processors.ContainerStartMode
@ -58,7 +61,7 @@ fun UbuntuProv.containerExecPlatform(containerName: String, cmd: String, sudo: B
} }
fun UbuntuProv.dockerBuildImagePlatform(image: DockerImage, skipIfExisting: Boolean, sudo: Boolean): ProvResult { fun UbuntuProv.dockerProvideImagePlatform(image: DockerImage, skipIfExisting: Boolean, sudo: Boolean): ProvResult {
val dockerCmd = if (sudo) "sudo docker " else "docker " val dockerCmd = if (sudo) "sudo docker " else "docker "
if (skipIfExisting && dockerImageExists(image.imageName())) { if (skipIfExisting && dockerImageExists(image.imageName())) {

View file

@ -1,9 +1,8 @@
package io.provs package io.provs
import io.provs.test.defaultTestContainer import io.provs.test.defaultTestContainer
import io.provs.test.tags.CONTAINERTEST import io.provs.test.tags.ContainerTest
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.EnabledOnOs
import org.junit.jupiter.api.condition.OS import org.junit.jupiter.api.condition.OS
@ -12,7 +11,7 @@ import org.junit.jupiter.api.condition.OS
internal class ContainerProcessorTest { internal class ContainerProcessorTest {
@Test @Test
@Tag(CONTAINERTEST) @ContainerTest
fun cmd_works_with_echo() { fun cmd_works_with_echo() {
// given // given
@ -29,7 +28,7 @@ internal class ContainerProcessorTest {
@Test @Test
@Tag(CONTAINERTEST) @ContainerTest
fun cmdNoLog_works_with_echo() { fun cmdNoLog_works_with_echo() {
// given // given
val prov = defaultTestContainer() val prov = defaultTestContainer()

View file

@ -1,11 +1,10 @@
package io.provs package io.provs
import io.provs.docker.provideContainer import io.provs.docker.provideContainer
import io.provs.test.tags.CONTAINERTEST import io.provs.test.tags.ContainerTest
import io.provs.test.tags.CONTAINER_NON_CI import io.provs.test.tags.ContainerTestNonCi
import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.EnabledOnOs
import org.junit.jupiter.api.condition.OS import org.junit.jupiter.api.condition.OS
@ -36,7 +35,7 @@ internal class ProvTest {
@Test @Test
@EnabledOnOs(OS.LINUX) @EnabledOnOs(OS.LINUX)
@Tag(CONTAINERTEST) @ContainerTest
fun sh_onLinux() { fun sh_onLinux() {
// given // given
val script = """ val script = """
@ -409,7 +408,7 @@ internal class ProvTest {
@Test @Test
@EnabledOnOs(OS.LINUX) @EnabledOnOs(OS.LINUX)
@Tag(CONTAINER_NON_CI) @ContainerTestNonCi
fun inContainer_locally() { fun inContainer_locally() {
// given // given
val containerName = "provs_test" val containerName = "provs_test"

View file

@ -1,9 +1,8 @@
package io.provs package io.provs
import io.provs.test.defaultTestContainer import io.provs.test.defaultTestContainer
import io.provs.test.tags.CONTAINERTEST import io.provs.test.tags.ContainerTest
import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
internal class UtilsTest { internal class UtilsTest {
@ -18,7 +17,7 @@ internal class UtilsTest {
} }
@Test @Test
@Tag(CONTAINERTEST) @ContainerTest
fun test_docker() { fun test_docker() {
// when // when
val res = defaultTestContainer().cmd("echo something") val res = defaultTestContainer().cmd("echo something")

View file

@ -5,9 +5,8 @@ import io.provs.docker.containerRuns
import io.provs.docker.exitAndRmContainer import io.provs.docker.exitAndRmContainer
import io.provs.docker.runContainer import io.provs.docker.runContainer
import io.provs.local import io.provs.local
import io.provs.test.tags.CONTAINER_NON_CI import io.provs.test.tags.ContainerTestNonCi
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.EnabledOnOs
import org.junit.jupiter.api.condition.OS import org.junit.jupiter.api.condition.OS
@ -16,7 +15,7 @@ internal class UbuntuHostDockerKtTest {
@Test @Test
@EnabledOnOs(OS.LINUX) @EnabledOnOs(OS.LINUX)
@Tag(CONTAINER_NON_CI) @ContainerTestNonCi
fun runAndCheckAndExitContainer() { fun runAndCheckAndExitContainer() {
// when // when
val containerName = "testContainer" val containerName = "testContainer"

View file

@ -1,10 +1,9 @@
package io.provs.processors package io.provs.processors
import io.provs.test.DEFAULT_START_MODE_TEST_CONTAINER
import io.provs.platforms.SHELL import io.provs.platforms.SHELL
import io.provs.test.DEFAULT_START_MODE_TEST_CONTAINER
import io.provs.test.tags.ContainerTest
import io.provs.test.testDockerWithSudo import io.provs.test.testDockerWithSudo
import io.provs.test.tags.CONTAINERTEST
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.EnabledOnOs
import org.junit.jupiter.api.condition.OS.LINUX import org.junit.jupiter.api.condition.OS.LINUX
@ -14,7 +13,7 @@ class ContainerUbuntuHostProcessorTest {
@Test @Test
@EnabledOnOs(LINUX) @EnabledOnOs(LINUX)
@Tag(CONTAINERTEST) @ContainerTest
fun test() { fun test() {
val processor = val processor =
ContainerUbuntuHostProcessor("provs_ubuntuhost_test", "ubuntu", DEFAULT_START_MODE_TEST_CONTAINER, sudo = testDockerWithSudo) ContainerUbuntuHostProcessor("provs_ubuntuhost_test", "ubuntu", DEFAULT_START_MODE_TEST_CONTAINER, sudo = testDockerWithSudo)

View file

@ -1,22 +1,32 @@
package io.provs.test package io.provs.test
import io.provs.Prov import io.provs.Prov
import io.provs.docker.dockerImageExists
import io.provs.docker.dockerProvideImage
import io.provs.docker.images.UbuntuPlusUser
import io.provs.local
import io.provs.processors.ContainerStartMode import io.provs.processors.ContainerStartMode
import io.provs.processors.ContainerUbuntuHostProcessor import io.provs.processors.ContainerUbuntuHostProcessor
val DEFAULT_START_MODE_TEST_CONTAINER = ContainerStartMode.USE_RUNNING_ELSE_CREATE val DEFAULT_START_MODE_TEST_CONTAINER = ContainerStartMode.USE_RUNNING_ELSE_CREATE
val testDockerWithSudo = "true" != System.getProperty("testdockerwithoutsudo")?.toLowerCase() val testDockerWithSudo = !"true".equals(System.getProperty("testdockerwithoutsudo")?.toLowerCase())
const val defaultTestContainerName = "provs_test" const val defaultTestContainerName = "provs_test"
fun defaultTestContainer(imageName: String = "ubuntu"): Prov { fun defaultTestContainer(): Prov {
val image = UbuntuPlusUser()
val prov = local()
if (!prov.dockerImageExists(image.imageName(), testDockerWithSudo)) {
prov.dockerProvideImage(image, sudo = testDockerWithSudo)
}
return Prov.newInstance( return Prov.newInstance(
ContainerUbuntuHostProcessor( ContainerUbuntuHostProcessor(
defaultTestContainerName, defaultTestContainerName,
startMode = DEFAULT_START_MODE_TEST_CONTAINER, startMode = DEFAULT_START_MODE_TEST_CONTAINER,
sudo = testDockerWithSudo, sudo = testDockerWithSudo,
dockerImage = imageName dockerImage = image.imageName()
) )
) )
} }

View file

@ -1,4 +1,21 @@
package io.provs.test.tags package io.provs.test.tags
const val CONTAINERTEST = "containertest" import org.junit.jupiter.api.Tag
const val CONTAINER_NON_CI = "containernonci" import org.junit.jupiter.api.Test
private const val CONTAINER_TEST = "containertest"
private const val CONTAINER_TEST_NON_CI = "containernonci"
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@kotlin.annotation.Retention
@Tag(CONTAINER_TEST)
@Test
annotation class ContainerTest
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@kotlin.annotation.Retention
@Tag(CONTAINER_TEST_NON_CI)
@Test
annotation class ContainerTestNonCi