testcontainer initializationError при запуске набора тестов

У меня есть несколько тестовых классов, использующих одну и ту же команду docker-compose с testcontainer.

Пакет не работает с initializationError, хотя каждый тест проходит, когда выполняется отдельно.

Вот соответствующая часть трассировки стека, происходящая во время второго теста. ./gradlew e2e:test -i

io.foo.e2e.AuthTest > initializationError FAILED
    org.testcontainers.containers.ContainerLaunchException: Container startup failed
        at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:330)
        at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:311)
        at org.testcontainers.containers.DockerComposeContainer.startAmbassadorContainers(DockerComposeContainer.java:331)
        at org.testcontainers.containers.DockerComposeContainer.start(DockerComposeContainer.java:178)
        at io.foo.e2e.bases.BaseE2eTest$Companion.beforeAll$e2e(BaseE2eTest.kt:62)
        at io.foo.e2e.bases.BaseE2eTest.beforeAll$e2e(BaseE2eTest.kt)
       ...

        Caused by:
        org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
            at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:88)
            at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:323)
            ... 83 more

            Caused by:
            org.testcontainers.containers.ContainerLaunchException: Could not create/start container
                at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:497)
                at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:325)
                at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
                ... 84 more

                Caused by:
                org.testcontainers.containers.ContainerLaunchException: Aborting attempt to link to container btraq5fzahac_worker_1 as it is not running
                    at org.testcontainers.containers.GenericContainer.applyConfiguration(GenericContainer.java:779)
                    at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:359)
                    ... 86 more

Мне кажется, что второй тест не ждет, пока первый отключит предыдущие контейнеры.

Здесь базовый класс, от которого наследуются все тесты. Он отвечает за раскрутку контейнеров.

open class BaseE2eTest {

    ...

    companion object {
        const val A = "containera_1"
        const val B = "containerb_1"
        const val C = "containerc_1"

        val dockerCompose: KDockerComposeContainer by lazy {
            defineDockerCompose()
                .withLocalCompose(true)
                .withExposedService(A, 8080, Wait.forListeningPort())
                .withExposedService(B, 8081)
                .withExposedService(C, 5672, Wait.forListeningPort())
        }

        class KDockerComposeContainer(file: File) : DockerComposeContainer<KDockerComposeContainer>(file)

        private fun defineDockerCompose() = KDockerComposeContainer(File("../docker-compose.yml"))

        @BeforeAll
        @JvmStatic
        internal fun beforeAll() {
            dockerCompose.start()
        }

        @AfterAll
        @JvmStatic
        internal fun afterAll() {
            dockerCompose.stop()
        }
    }
}
docker-compose version 1.27.4, build 40524192
testcontainer 1.15.2
testcontainers:junit-jupiter:1.15.2

person aiqency    schedule 18.02.2021    source источник
comment
Вы можете поделиться своим тестовым классом?   -  person Vitaly Chura    schedule 18.02.2021
comment
@VitalyChura Я добавил ту часть, которая должна быть актуальной.   -  person aiqency    schedule 18.02.2021
comment
Я думаю, что если вы хотите, чтобы контейнеры перезапускались каждый раз, вы должны поставить start в @BeforeEach вместо @BeforeAll, то же самое для остановки. Вы пробовали что-то подобное?   -  person Vitaly Chura    schedule 19.02.2021
comment
@VitalyChura, тогда все тесты не пройдут.   -  person aiqency    schedule 19.02.2021


Ответы (1)


После просмотра этого выступления я понял, что мой подход к созданию экземпляров testcontainers с помощью Junit5 был неправильным.

Вот рабочий код:

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
open class BaseE2eTest {

    ...

    val A = "containera_1"
    val B = "containerb_1"
    val C = "containerc_1"

    val dockerCompose: KDockerComposeContainer by lazy {
        defineDockerCompose()
            .withLocalCompose(true)
            .withExposedService(A, 8080, Wait.forListeningPort())
            .withExposedService(B, 8081)
            .withExposedService(C, 5672, Wait.forListeningPort())
    }

    class KDockerComposeContainer(file: File) : DockerComposeContainer<KDockerComposeContainer>(file)

    private fun defineDockerCompose() = KDockerComposeContainer(File("../docker-compose.yml"))

    @BeforeAll
    fun beforeAll() {
        dockerCompose.start()
    }

    @AfterAll
    fun afterAll() {
        dockerCompose.stop()
    }
}

Теперь набор тестов проходит.

person aiqency    schedule 28.02.2021