Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed May 8, 2024
1 parent c38bbc1 commit f4991ba
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions modules/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kafka_test

import (
"context"
"io"
"strings"
"testing"

Expand All @@ -28,6 +29,8 @@ func TestKafka(t *testing.T) {
}
})

assertAdvertisedListeners(t, kafkaContainer)

if !strings.EqualFold(kafkaContainer.ClusterID, "kraftCluster") {
t.Fatalf("expected clusterID to be %s, got %s", "kraftCluster", kafkaContainer.ClusterID)
}
Expand Down Expand Up @@ -93,3 +96,32 @@ func TestKafka_invalidVersion(t *testing.T) {
t.Fatal(err)
}
}

// assertAdvertisedListeners checks that the advertised listeners are set correctly:
// - The BROKER:// protocol is using the hostname of the Kafka container
func assertAdvertisedListeners(t *testing.T, container testcontainers.Container) {
inspect, err := container.Inspect(context.Background())
if err != nil {
t.Fatal(err)
}

hostname := inspect.Config.Hostname

code, r, err := container.Exec(context.Background(), []string{"cat", "/usr/sbin/testcontainers_start.sh"})
if err != nil {
t.Fatal(err)
}

if code != 0 {
t.Fatalf("expected exit code to be 0, got %d", code)
}

bs, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}

if !strings.Contains(string(bs), "BROKER://"+hostname+":9092") {
t.Fatalf("expected advertised listeners to contain %s, got %s", "BROKER://"+hostname+":9092", string(bs))
}
}

0 comments on commit f4991ba

Please sign in to comment.