Skip to content

Commit

Permalink
nettest: rename and widen tlog package to util
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamoe committed Oct 2, 2023
1 parent 2a63093 commit bb2bca2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 29 deletions.
8 changes: 4 additions & 4 deletions pkg/agent/protocol/nettest/redis/iptables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") == "" {
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 0 additions & 25 deletions pkg/agent/protocol/nettest/tlog/tlog.go

This file was deleted.

58 changes: 58 additions & 0 deletions pkg/agent/protocol/nettest/util/util.go
Original file line number Diff line number Diff line change
@@ -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))
}
}()
}

0 comments on commit bb2bca2

Please sign in to comment.