Skip to content

[Bug]: KafkaContainer networkAliases not available anymore after update to 1.19.2 #7823

@chriskn

Description

@chriskn

Module

Kafka

Testcontainers version

1.19.2

Using the latest Testcontainers version?

Yes

Host OS

Windows

Host Arch

x86

Docker version

Client:
 Cloud integration: v1.0.29
 Version:           20.10.22
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        3a2c30b
 Built:             Thu Dec 15 22:36:18 2022
 OS/Arch:           windows/amd64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.16.3 (96739)
 Engine:
  Version:          20.10.22
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.9
  Git commit:       42c8b31
  Built:            Thu Dec 15 22:26:14 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.14
  GitCommit:        9ba4b250366a5ddde94bb7c9d1def331423aa323
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

What happened?

Disclaimer: The error described in this issue can easily be avoided. I am just creating this issue to inform you about a regression issue, which was not expected from a minor version update.

We are using KafkaContainer together with a custom SchemaRegistryContainer for our Spring Boot Integration-Tests.

In order to connect both, we define a Network for the KafkaContainer and configure the SchemaRegistryContainer to use the same network with a network alias set as SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS url:

@Testcontainers
class KafkaContainerInitializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
    companion object {
        private const val CONFLUENT_VERSION = "confluentinc/cp-kafka:7.0.10"

        @Container
        val kafkaContainer: KafkaContainer = KafkaContainer(DockerImageName.parse(CONFLUENT_VERSION))
            .withNetwork(Network.newNetwork())

        @Container
        val schemaRegistryContainer: SchemaRegistryContainer =
            SchemaRegistryContainer().withKafka(kafkaContainer)
    }

    override fun initialize(applicationContext: ConfigurableApplicationContext) {
        kafkaContainer.start()
        schemaRegistryContainer.start()
    }
}
class SchemaRegistryContainer : GenericContainer<SchemaRegistryContainer>(
    "confluentinc/cp-schema-registry:7.0.10"
) {

    init {
        withExposedPorts(8081)
    }

    fun withKafka(kafka: KafkaContainer): SchemaRegistryContainer {
        return withKafka(kafka.network!!,  "${kafka.networkAliases[0]}:9092")
    }

    private fun withKafka(network: Network, bootstrapServers: String): SchemaRegistryContainer {
        withNetwork(network)
        withEnv("SCHEMA_REGISTRY_HOST_NAME", "schema-registry")
        withEnv("SCHEMA_REGISTRY_LISTENERS", "http://0.0.0.0:8081")
        withEnv("SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS", "PLAINTEXT://$bootstrapServers")
        return this
    }
}

After the update to version 1.19.2 kafka.networkAliases[0] throws a IndexOutOfBoundsException because no network alias is configured. This is likely related to this commit: #7748

It can easily be fixed by setting an alias explicitly using the withNetworkAliases method but, as pointed out, such a regression issue was not expected by a minor version update. Therefore, and to make sure no other issues occurred with the change, I'm opening this bug.

Relevant log output

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions