Skip to content

Commit

Permalink
chore: use testcontainers.RequireContainerExec (#2870)
Browse files Browse the repository at this point in the history
Use testcontainers.RequireContainerExec to simplify tests.

Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored Nov 2, 2024
1 parent 8283ba8 commit ffcb8b6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
13 changes: 4 additions & 9 deletions modules/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kafka_test

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

Expand Down Expand Up @@ -82,15 +81,11 @@ func assertAdvertisedListeners(t *testing.T, container testcontainers.Container)
inspect, err := container.Inspect(context.Background())
require.NoError(t, err)

hostname := inspect.Config.Hostname
brokerURL := "BROKER://" + inspect.Config.Hostname + ":9092"

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

require.Zero(t, code)
ctx := context.Background()

bs, err := io.ReadAll(r)
require.NoError(t, err)
bs := testcontainers.RequireContainerExec(ctx, t, container, []string{"cat", "/usr/sbin/testcontainers_start.sh"})

require.Containsf(t, string(bs), "BROKER://"+hostname+":9092", "expected advertised listeners to contain %s, got %s", "BROKER://"+hostname+":9092", string(bs))
require.Containsf(t, bs, brokerURL, "expected advertised listeners to contain %s, got %s", brokerURL, bs)
}
8 changes: 1 addition & 7 deletions modules/neo4j/neo4j_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package neo4j_test
import (
"context"
"fmt"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -159,12 +158,7 @@ func createDriver(t *testing.T, ctx context.Context, container *neo4j.Neo4jConta

func getContainerEnv(t *testing.T, ctx context.Context, container *neo4j.Neo4jContainer) string {
t.Helper()
exec, reader, err := container.Exec(ctx, []string{"env"})
require.NoErrorf(t, err, "expected env to successfully run but did not")
require.Zerof(t, exec, "expected env to exit with status 0 but exited with: %d", exec)
envVars, err := io.ReadAll(reader)
require.NoErrorf(t, err, "expected to read all bytes from env output but did not")
return string(envVars)
return testcontainers.RequireContainerExec(ctx, t, container, []string{"env"})
}

const logSeparator = "---$$$---"
Expand Down

0 comments on commit ffcb8b6

Please sign in to comment.