diff --git a/modules/inbucket/inbucket.go b/modules/inbucket/inbucket.go index 09da55648f..7f9049b624 100644 --- a/modules/inbucket/inbucket.go +++ b/modules/inbucket/inbucket.go @@ -3,7 +3,6 @@ package inbucket import ( "context" "fmt" - "net" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" @@ -19,34 +18,14 @@ type InbucketContainer struct { // //nolint:revive,staticcheck //FIXME func (c *InbucketContainer) SmtpConnection(ctx context.Context) (string, error) { - containerPort, err := c.MappedPort(ctx, "2500/tcp") - if err != nil { - return "", err - } - - host, err := c.Host(ctx) - if err != nil { - return "", err - } - - return net.JoinHostPort(host, containerPort.Port()), nil + return c.PortEndpoint(ctx, "2500/tcp", "") } // WebInterface returns the connection string for the web interface server, // using the default 9000 port, and obtaining the host and exposed port from // the container. func (c *InbucketContainer) WebInterface(ctx context.Context) (string, error) { - containerPort, err := c.MappedPort(ctx, "9000/tcp") - if err != nil { - return "", err - } - - host, err := c.Host(ctx) - if err != nil { - return "", err - } - - return "http://" + net.JoinHostPort(host, containerPort.Port()), nil + return c.PortEndpoint(ctx, "9000/tcp", "http") } // Deprecated: use Run instead diff --git a/modules/influxdb/influxdb.go b/modules/influxdb/influxdb.go index 39f77c0485..9a5856a660 100644 --- a/modules/influxdb/influxdb.go +++ b/modules/influxdb/influxdb.go @@ -76,17 +76,7 @@ func (c *InfluxDbContainer) MustConnectionUrl(ctx context.Context) string { //nolint:revive,staticcheck //FIXME func (c *InfluxDbContainer) ConnectionUrl(ctx context.Context) (string, error) { - containerPort, err := c.MappedPort(ctx, "8086/tcp") - if err != nil { - return "", err - } - - host, err := c.Host(ctx) - if err != nil { - return "", err - } - - return fmt.Sprintf("http://%s:%s", host, containerPort.Port()), nil + return c.PortEndpoint(ctx, "8086/tcp", "http") } func WithUsername(username string) testcontainers.CustomizeRequestOption { diff --git a/modules/kafka/kafka.go b/modules/kafka/kafka.go index 384a1d3898..b1342de98f 100644 --- a/modules/kafka/kafka.go +++ b/modules/kafka/kafka.go @@ -22,7 +22,7 @@ const ( // starterScript { starterScriptContent = `#!/bin/bash source /etc/confluent/docker/bash-config -export KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://%s:%d,BROKER://%s:9092 +export KAFKA_ADVERTISED_LISTENERS=%s,BROKER://%s:9092 echo Starting Kafka KRaft mode sed -i '/KAFKA_ZOOKEEPER_CONNECT/d' /etc/confluent/docker/configure echo 'kafka-storage format --ignore-formatted -t "$(kafka-storage random-uuid)" -c /etc/kafka/kafka.properties' >> /etc/confluent/docker/configure @@ -128,9 +128,9 @@ func copyStarterScript(ctx context.Context, c testcontainers.Container) error { return fmt.Errorf("wait for mapped port: %w", err) } - host, err := c.Host(ctx) + endpoint, err := c.PortEndpoint(ctx, publicPort, "PLAINTEXT") if err != nil { - return fmt.Errorf("host: %w", err) + return fmt.Errorf("port endpoint: %w", err) } inspect, err := c.Inspect(ctx) @@ -140,12 +140,7 @@ func copyStarterScript(ctx context.Context, c testcontainers.Container) error { hostname := inspect.Config.Hostname - port, err := c.MappedPort(ctx, publicPort) - if err != nil { - return fmt.Errorf("mapped port: %w", err) - } - - scriptContent := fmt.Sprintf(starterScriptContent, host, port.Int(), hostname) + scriptContent := fmt.Sprintf(starterScriptContent, endpoint, hostname) if err := c.CopyToContainer(ctx, []byte(scriptContent), starterScript, 0o755); err != nil { return fmt.Errorf("copy to container: %w", err) @@ -165,17 +160,12 @@ func WithClusterID(clusterID string) testcontainers.CustomizeRequestOption { // Brokers retrieves the broker connection strings from Kafka with only one entry, // defined by the exposed public port. func (kc *KafkaContainer) Brokers(ctx context.Context) ([]string, error) { - host, err := kc.Host(ctx) - if err != nil { - return nil, err - } - - port, err := kc.MappedPort(ctx, publicPort) + endpoint, err := kc.PortEndpoint(ctx, publicPort, "") if err != nil { return nil, err } - return []string{fmt.Sprintf("%s:%d", host, port.Int())}, nil + return []string{endpoint}, nil } // configureControllerQuorumVoters sets the quorum voters for the controller. For that, it will