Skip to content

Commit 19e5e9c

Browse files
committed
test: test listeners for Redpanda Container
Add test for new WithListener function, validate connectivity and couple of asserts in the construction of the listener Signed-off-by: Santiago Jimenez Giraldo <[email protected]>
1 parent 03168f7 commit 19e5e9c

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

modules/redpanda/redpanda_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,34 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"fmt"
8+
"io"
89
"net/http"
910
"strings"
1011
"testing"
1112
"time"
1213

1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
16+
"github.com/testcontainers/testcontainers-go"
17+
"github.com/testcontainers/testcontainers-go/network"
1518
"github.com/twmb/franz-go/pkg/kadm"
1619
"github.com/twmb/franz-go/pkg/kerr"
1720
"github.com/twmb/franz-go/pkg/kgo"
1821
"github.com/twmb/franz-go/pkg/sasl/scram"
1922
)
2023

24+
// TestContainersLogger is a logger that implements the testcontainers
25+
// logger interface.
26+
type TestContainersLogger struct {
27+
// LogPrefix is a string that is prefixed for every individual log line.
28+
LogPrefix string
29+
}
30+
31+
// Accept prints the log to stdout
32+
func (lc TestContainersLogger) Accept(l testcontainers.Log) {
33+
fmt.Print(lc.LogPrefix + string(l.Content))
34+
}
35+
2136
func TestRedpanda(t *testing.T) {
2237
ctx := context.Background()
2338

@@ -278,6 +293,111 @@ func TestRedpandaWithTLS(t *testing.T) {
278293
require.Error(t, results.FirstErr(), kerr.UnknownTopicOrPartition)
279294
}
280295

296+
func TestRedpandaListener_Simple(t *testing.T) {
297+
ctx := context.Background()
298+
299+
// 1. Create network
300+
RPNetwork, err := network.New(ctx, network.WithCheckDuplicate())
301+
require.NoError(t, err)
302+
303+
// 2. Start Redpanda container
304+
container, err := RunContainer(ctx,
305+
testcontainers.WithImage("redpandadata/redpanda:v23.2.18"),
306+
network.WithNetwork([]string{"redpanda-host"}, RPNetwork),
307+
WithListener("redpanda:29092"), WithAutoCreateTopics(),
308+
)
309+
require.NoError(t, err)
310+
311+
// 3. Start KCat container
312+
kcat, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
313+
ContainerRequest: testcontainers.ContainerRequest{
314+
Image: "confluentinc/cp-kcat:7.4.1",
315+
Networks: []string{
316+
RPNetwork.Name,
317+
},
318+
Entrypoint: []string{
319+
"sh",
320+
},
321+
Cmd: []string{
322+
"-c",
323+
"tail -f /dev/null",
324+
},
325+
},
326+
Started: true,
327+
})
328+
329+
require.NoError(t, err)
330+
331+
// 4. Copy message to kcat
332+
err = kcat.CopyToContainer(ctx, []byte("Message produced by kcat"), "/tmp/msgs.txt", 700)
333+
require.NoError(t, err)
334+
335+
// 5. Produce mesaage to Redpanda
336+
_, _, err = kcat.Exec(ctx, []string{"kcat", "-b", "redpanda:29092", "-t", "msgs", "-P", "-l", "/tmp/msgs.txt"})
337+
338+
require.NoError(t, err)
339+
340+
// 6. Consume message from Redpanda
341+
_, stdout, err := kcat.Exec(ctx, []string{"kcat", "-b", "redpanda:29092", "-C", "-t", "msgs", "-c", "1"})
342+
require.NoError(t, err)
343+
out, err := io.ReadAll(stdout)
344+
require.NoError(t, err)
345+
346+
require.Contains(t, string(out), "Message produced by kcat")
347+
348+
t.Cleanup(func() {
349+
if err := kcat.Terminate(ctx); err != nil {
350+
t.Fatalf("failed to terminate kcat container: %s", err)
351+
}
352+
if err := container.Terminate(ctx); err != nil {
353+
t.Fatalf("failed to terminate redpanda container: %s", err)
354+
}
355+
356+
if err := RPNetwork.Remove(ctx); err != nil {
357+
t.Fatalf("failed to remove network: %s", err)
358+
}
359+
})
360+
}
361+
362+
func TestRedpandaListener_InvalidPort(t *testing.T) {
363+
ctx := context.Background()
364+
365+
// 1. Create network
366+
RPNetwork, err := network.New(ctx, network.WithCheckDuplicate())
367+
require.NoError(t, err)
368+
369+
// 2. Attemp Start Redpanda container
370+
_, err = RunContainer(ctx,
371+
testcontainers.WithImage("redpandadata/redpanda:v23.2.18"),
372+
WithListener("redpanda:99092"),
373+
network.WithNetwork([]string{"redpanda-host"}, RPNetwork),
374+
)
375+
376+
require.Error(t, err)
377+
378+
require.Contains(t, err.Error(), "invalid port on listener redpanda:99092")
379+
380+
t.Cleanup(func() {
381+
if err := RPNetwork.Remove(ctx); err != nil {
382+
t.Fatalf("failed to remove network: %s", err)
383+
}
384+
})
385+
}
386+
387+
func TestRedpandaListener_NoNetwork(t *testing.T) {
388+
ctx := context.Background()
389+
390+
// 1. Attemp Start Redpanda container
391+
_, err := RunContainer(ctx,
392+
testcontainers.WithImage("redpandadata/redpanda:v23.2.18"),
393+
WithListener("redpanda:99092"),
394+
)
395+
396+
require.Error(t, err)
397+
398+
require.Contains(t, err.Error(), "container must be attached to at least one network")
399+
}
400+
281401
// localhostCert is a PEM-encoded TLS cert with SAN IPs
282402
// generated from src/crypto/tls:
283403
// go run generate_cert.go --rsa-bits 2048 --host 127.0.0.1,::1,localhost --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h

0 commit comments

Comments
 (0)