From bb2bca2ce8b4b877b5926a76c080230fcf7c68c6 Mon Sep 17 00:00:00 2001 From: Roberto Santalla Date: Mon, 2 Oct 2023 12:35:39 +0200 Subject: [PATCH] nettest: rename and widen tlog package to util --- .../protocol/nettest/redis/iptables_test.go | 8 +-- pkg/agent/protocol/nettest/tlog/tlog.go | 25 -------- pkg/agent/protocol/nettest/util/util.go | 58 +++++++++++++++++++ 3 files changed, 62 insertions(+), 29 deletions(-) delete mode 100644 pkg/agent/protocol/nettest/tlog/tlog.go create mode 100644 pkg/agent/protocol/nettest/util/util.go diff --git a/pkg/agent/protocol/nettest/redis/iptables_test.go b/pkg/agent/protocol/nettest/redis/iptables_test.go index 8cb68623..d40e8f08 100644 --- a/pkg/agent/protocol/nettest/redis/iptables_test.go +++ b/pkg/agent/protocol/nettest/redis/iptables_test.go @@ -11,12 +11,12 @@ import ( "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" - "github.com/grafana/xk6-disruptor/pkg/agent/protocol/nettest/tlog" + "github.com/grafana/xk6-disruptor/pkg/agent/protocol/nettest/util" ) const iptablesRule = "INPUT -p tcp --dport 6379 -j REJECT --reject-with tcp-reset" -func Test_Redis(t *testing.T) { +func Test_Redis_Iptables(t *testing.T) { t.Parallel() if os.Getenv("NETTEST") == "" { @@ -83,10 +83,10 @@ func Test_Redis(t *testing.T) { // TODO: Calling terminate with a log attached makes the test hang. // See: https://github.com/testcontainers/testcontainers-go/issues/1669 // t.Cleanup(func() { - // _ = redisGo.Terminate(ctx) + // _ = redisGo.Terminate(ctx) // }) - redisGo.FollowOutput(tlog.Mirror{T: t, Name: "redis-go"}) + redisGo.FollowOutput(util.Mirror{T: t, Name: "redis-go"}) err = redisGo.StartLogProducer(ctx) if err != nil { t.Fatal(err) diff --git a/pkg/agent/protocol/nettest/tlog/tlog.go b/pkg/agent/protocol/nettest/tlog/tlog.go deleted file mode 100644 index 7ec06e0c..00000000 --- a/pkg/agent/protocol/nettest/tlog/tlog.go +++ /dev/null @@ -1,25 +0,0 @@ -// Package tlog implements a testcontainers log handler that mirrors logs to a test logger. -package tlog - -import ( - "testing" - - "github.com/testcontainers/testcontainers-go" -) - -// Mirror is a testcontainers log adapter that mirrors container output to testing.T.Log. -type Mirror struct { - T *testing.T - Name string -} - -// Accept implements the testcontainers adapter interface by writing received output to the test logger. -func (m Mirror) Accept(log testcontainers.Log) { - prefix := "" - if m.Name != "" { - prefix += m.Name + "/" - } - prefix += log.LogType - - m.T.Logf("%s: %s", prefix, log.Content) -} diff --git a/pkg/agent/protocol/nettest/util/util.go b/pkg/agent/protocol/nettest/util/util.go new file mode 100644 index 00000000..da0cc706 --- /dev/null +++ b/pkg/agent/protocol/nettest/util/util.go @@ -0,0 +1,58 @@ +// Package util implements misc utilities for network tests +package util + +import ( + "bufio" + "context" + "strings" + "testing" + + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/exec" +) + +// Mirror is a testcontainers log adapter that mirrors container output to testing.T.Log. +type Mirror struct { + T *testing.T + Name string +} + +// Accept implements the testcontainers adapter interface by writing received output to the test logger. +func (m Mirror) Accept(log testcontainers.Log) { + prefix := "" + if m.Name != "" { + prefix += m.Name + "/" + } + prefix += log.LogType + + m.T.Logf("%s: %s", prefix, log.Content) +} + +// TCExec runs a command on a container in a shell, echoing the output and failing the test if it cannot be run. +func TCExec(t *testing.T, c testcontainers.Container, shellcmd string) { + t.Helper() + + cmd := []string{"/bin/sh", "-c", shellcmd} + + t.Logf("%s: running %q", c.GetContainerID(), shellcmd) + rc, out, err := c.Exec(context.TODO(), cmd, exec.Multiplexed()) + if err != nil { + t.Fatalf("running command on %s: %v", c.GetContainerID(), err) + } + + if rc != 0 { + t.Errorf("%s:%s exited with %d", c.GetContainerID(), cmd, rc) + } + + go func() { + buf := bufio.NewReader(out) + for { + line, err := buf.ReadString('\n') + if err != nil { + return + } + + t.Logf("%s:%s: %s", c.GetContainerID(), cmd, strings.TrimSpace(line)) + } + }() +}