From 2623eb857fc17d07bc19711588577a67ceee4bce Mon Sep 17 00:00:00 2001 From: az Date: Wed, 31 Mar 2021 12:09:03 +0200 Subject: [PATCH] use ubuntu_plus_user as default test container image --- build.gradle | 7 ++-- src/main/kotlin/io/provs/docker/HostDocker.kt | 40 +++++++++++-------- .../docker/platforms/UbuntuHostDocker.kt | 7 +++- .../kotlin/io/provs/ContainerProcessorTest.kt | 7 ++-- src/test/kotlin/io/provs/ProvTest.kt | 9 ++--- src/test/kotlin/io/provs/UtilsTest.kt | 5 +-- .../platforms/UbuntuHostDockerKtTest.kt | 5 +-- .../ContainerUbuntuHostProcessorTest.kt | 7 ++-- .../kotlin/io/provs/test/TestSetup.kt | 28 ++++++++----- .../kotlin/io/provs/test/tags/Tags.kt | 21 +++++++++- 10 files changed, 85 insertions(+), 51 deletions(-) diff --git a/build.gradle b/build.gradle index db2699b..aaf7902 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,7 @@ buildscript { ext.kotlin_version = '1.4.31' + ext.CI_PROJECT_ID = System.env.CI_PROJECT_ID + repositories { jcenter() } dependencies { @@ -60,9 +62,8 @@ dependencies { api "ch.qos.logback:logback-classic: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' - testImplementation "io.mockk:mockk:1.9.3" } @@ -151,7 +152,7 @@ publishing { if (System.getenv("CI_JOB_TOKEN") != null) { // see https://docs.gitlab.com/ee/user/packages/maven_repository/index.html 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" credentials(HttpHeaderCredentials) { name = 'Job-Token' diff --git a/src/main/kotlin/io/provs/docker/HostDocker.kt b/src/main/kotlin/io/provs/docker/HostDocker.kt index 1f63e55..6dc2c5e 100644 --- a/src/main/kotlin/io/provs/docker/HostDocker.kt +++ b/src/main/kotlin/io/provs/docker/HostDocker.kt @@ -8,6 +8,30 @@ import io.provs.platforms.UbuntuProv 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. * 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( diff --git a/src/main/kotlin/io/provs/docker/platforms/UbuntuHostDocker.kt b/src/main/kotlin/io/provs/docker/platforms/UbuntuHostDocker.kt index 11c99f1..7c206e2 100644 --- a/src/main/kotlin/io/provs/docker/platforms/UbuntuHostDocker.kt +++ b/src/main/kotlin/io/provs/docker/platforms/UbuntuHostDocker.kt @@ -1,10 +1,13 @@ package io.provs.docker.platforms -import io.provs.* +import io.provs.ProvResult import io.provs.docker.containerRuns import io.provs.docker.dockerImageExists import io.provs.docker.exitAndRmContainer 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.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 " if (skipIfExisting && dockerImageExists(image.imageName())) { diff --git a/src/test/kotlin/io/provs/ContainerProcessorTest.kt b/src/test/kotlin/io/provs/ContainerProcessorTest.kt index 50c40b0..36520a6 100644 --- a/src/test/kotlin/io/provs/ContainerProcessorTest.kt +++ b/src/test/kotlin/io/provs/ContainerProcessorTest.kt @@ -1,9 +1,8 @@ package io.provs 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.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.OS @@ -12,7 +11,7 @@ import org.junit.jupiter.api.condition.OS internal class ContainerProcessorTest { @Test - @Tag(CONTAINERTEST) + @ContainerTest fun cmd_works_with_echo() { // given @@ -29,7 +28,7 @@ internal class ContainerProcessorTest { @Test - @Tag(CONTAINERTEST) + @ContainerTest fun cmdNoLog_works_with_echo() { // given val prov = defaultTestContainer() diff --git a/src/test/kotlin/io/provs/ProvTest.kt b/src/test/kotlin/io/provs/ProvTest.kt index 9d62ea8..4a092ae 100644 --- a/src/test/kotlin/io/provs/ProvTest.kt +++ b/src/test/kotlin/io/provs/ProvTest.kt @@ -1,11 +1,10 @@ package io.provs import io.provs.docker.provideContainer -import io.provs.test.tags.CONTAINERTEST -import io.provs.test.tags.CONTAINER_NON_CI +import io.provs.test.tags.ContainerTest +import io.provs.test.tags.ContainerTestNonCi import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Disabled -import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.OS @@ -36,7 +35,7 @@ internal class ProvTest { @Test @EnabledOnOs(OS.LINUX) - @Tag(CONTAINERTEST) + @ContainerTest fun sh_onLinux() { // given val script = """ @@ -409,7 +408,7 @@ internal class ProvTest { @Test @EnabledOnOs(OS.LINUX) - @Tag(CONTAINER_NON_CI) + @ContainerTestNonCi fun inContainer_locally() { // given val containerName = "provs_test" diff --git a/src/test/kotlin/io/provs/UtilsTest.kt b/src/test/kotlin/io/provs/UtilsTest.kt index 0d4cfa5..c0aa9b1 100644 --- a/src/test/kotlin/io/provs/UtilsTest.kt +++ b/src/test/kotlin/io/provs/UtilsTest.kt @@ -1,9 +1,8 @@ package io.provs 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.Tag import org.junit.jupiter.api.Test internal class UtilsTest { @@ -18,7 +17,7 @@ internal class UtilsTest { } @Test - @Tag(CONTAINERTEST) + @ContainerTest fun test_docker() { // when val res = defaultTestContainer().cmd("echo something") diff --git a/src/test/kotlin/io/provs/docker/platforms/UbuntuHostDockerKtTest.kt b/src/test/kotlin/io/provs/docker/platforms/UbuntuHostDockerKtTest.kt index 1b6d6b2..573215b 100644 --- a/src/test/kotlin/io/provs/docker/platforms/UbuntuHostDockerKtTest.kt +++ b/src/test/kotlin/io/provs/docker/platforms/UbuntuHostDockerKtTest.kt @@ -5,9 +5,8 @@ import io.provs.docker.containerRuns import io.provs.docker.exitAndRmContainer import io.provs.docker.runContainer 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.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.OS @@ -16,7 +15,7 @@ internal class UbuntuHostDockerKtTest { @Test @EnabledOnOs(OS.LINUX) - @Tag(CONTAINER_NON_CI) + @ContainerTestNonCi fun runAndCheckAndExitContainer() { // when val containerName = "testContainer" diff --git a/src/test/kotlin/io/provs/processors/ContainerUbuntuHostProcessorTest.kt b/src/test/kotlin/io/provs/processors/ContainerUbuntuHostProcessorTest.kt index fc159a6..96ab8ea 100644 --- a/src/test/kotlin/io/provs/processors/ContainerUbuntuHostProcessorTest.kt +++ b/src/test/kotlin/io/provs/processors/ContainerUbuntuHostProcessorTest.kt @@ -1,10 +1,9 @@ package io.provs.processors -import io.provs.test.DEFAULT_START_MODE_TEST_CONTAINER 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.tags.CONTAINERTEST -import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.condition.EnabledOnOs import org.junit.jupiter.api.condition.OS.LINUX @@ -14,7 +13,7 @@ class ContainerUbuntuHostProcessorTest { @Test @EnabledOnOs(LINUX) - @Tag(CONTAINERTEST) + @ContainerTest fun test() { val processor = ContainerUbuntuHostProcessor("provs_ubuntuhost_test", "ubuntu", DEFAULT_START_MODE_TEST_CONTAINER, sudo = testDockerWithSudo) diff --git a/src/testFixtures/kotlin/io/provs/test/TestSetup.kt b/src/testFixtures/kotlin/io/provs/test/TestSetup.kt index 29edab0..6e86654 100644 --- a/src/testFixtures/kotlin/io/provs/test/TestSetup.kt +++ b/src/testFixtures/kotlin/io/provs/test/TestSetup.kt @@ -1,22 +1,32 @@ package io.provs.test 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.ContainerUbuntuHostProcessor 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" -fun defaultTestContainer(imageName: String = "ubuntu"): Prov { - return Prov.newInstance( - ContainerUbuntuHostProcessor( - defaultTestContainerName, - startMode = DEFAULT_START_MODE_TEST_CONTAINER, - sudo = testDockerWithSudo, - dockerImage = imageName - ) +fun defaultTestContainer(): Prov { + val image = UbuntuPlusUser() + val prov = local() + if (!prov.dockerImageExists(image.imageName(), testDockerWithSudo)) { + prov.dockerProvideImage(image, sudo = testDockerWithSudo) + } + + return Prov.newInstance( + ContainerUbuntuHostProcessor( + defaultTestContainerName, + startMode = DEFAULT_START_MODE_TEST_CONTAINER, + sudo = testDockerWithSudo, + dockerImage = image.imageName() ) + ) } diff --git a/src/testFixtures/kotlin/io/provs/test/tags/Tags.kt b/src/testFixtures/kotlin/io/provs/test/tags/Tags.kt index 0ac75e9..54982fe 100644 --- a/src/testFixtures/kotlin/io/provs/test/tags/Tags.kt +++ b/src/testFixtures/kotlin/io/provs/test/tags/Tags.kt @@ -1,4 +1,21 @@ package io.provs.test.tags -const val CONTAINERTEST = "containertest" -const val CONTAINER_NON_CI = "containernonci" \ No newline at end of file +import org.junit.jupiter.api.Tag +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 \ No newline at end of file