Skip to content

Commit

Permalink
chore: use require and new CleanupContainer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Nov 21, 2024
1 parent 3429d0b commit 01ba4e9
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions modules/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ func TestKafka_networkConnectivity(t *testing.T) {
}),
)
// }
testcontainers.CleanupContainer(t, kafkaContainer)
require.NoError(t, err, "failed to start kafka container")

kcat, err := runKcatContainer(ctx, Network.Name, "/tmp/msgs.txt")
testcontainers.CleanupContainer(t, kcat)
require.NoError(t, err, "failed to start kcat")

// 4. Copy message to kcat
Expand Down Expand Up @@ -193,10 +195,8 @@ func TestKafka_networkConnectivity(t *testing.T) {
t.Fatal("Empty message")
}

// Assert
if !strings.Contains(string(consumer.message.Value), text_msg) {
t.Error("got wrong string")
}
require.Contains(t, string(consumer.message.Value), text_msg)
require.Contains(t, string(consumer.message.Key), key)
}

func TestKafka_withListener(t *testing.T) {
Expand All @@ -206,6 +206,12 @@ func TestKafka_withListener(t *testing.T) {
rpNetwork, err := network.New(ctx)
require.NoError(t, err)

t.Cleanup(func() {
if err := rpNetwork.Remove(ctx); err != nil {
t.Fatalf("failed to remove network: %s", err)
}
})

// 2. Start Kafka ctr
// withListenerRP {
ctr, err := kafka.Run(ctx,
Expand All @@ -220,13 +226,14 @@ func TestKafka_withListener(t *testing.T) {
}),
)
// }
testcontainers.CleanupContainer(t, ctr)
require.NoError(t, err)

// 3. Start KCat container
// withListenerKcat {
kcat, err := runKcatContainer(ctx, rpNetwork.Name, "/tmp/msgs.txt")
// }

testcontainers.CleanupContainer(t, kcat)
require.NoError(t, err)

// 4. Copy message to kcat
Expand All @@ -245,19 +252,6 @@ func TestKafka_withListener(t *testing.T) {
out, err := kcat.ConsumeMessage(ctx, "kafka:9092", "msgs")
require.NoError(t, err)
require.Contains(t, string(out), "Message produced by kcat")

t.Cleanup(func() {
if err := kcat.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate kcat container: %s", err)
}
if err := ctr.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate Kafka container: %s", err)
}

if err := rpNetwork.Remove(ctx); err != nil {
t.Fatalf("failed to remove network: %s", err)
}
})
}

func TestKafka_restProxyService(t *testing.T) {
Expand Down

0 comments on commit 01ba4e9

Please sign in to comment.