Skip to content

Commit

Permalink
nettest: small changes and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
roobre committed Sep 27, 2023
1 parent 56c650f commit 2a63093
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
21 changes: 16 additions & 5 deletions pkg/agent/protocol/nettest/redis/iptables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"

"github.com/grafana/xk6-disruptor/pkg/agent/protocol/nettest/tlog"
)

const iptablesRule = "INPUT -p tcp --dport 6379 -j REJECT --reject-with tcp-reset"
Expand All @@ -26,7 +28,6 @@ func Test_Redis(t *testing.T) {
redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ProviderType: testcontainers.ProviderDocker,
ContainerRequest: testcontainers.ContainerRequest{
Networks: []string{},
Image: "redis",
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForExposedPort(),
Expand Down Expand Up @@ -79,11 +80,21 @@ func Test_Redis(t *testing.T) {
t.Fatalf("failed to create agent container %v", err)
}

t.Cleanup(func() {
_ = redisGo.Terminate(ctx)
})
// 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)
// })

// TODO:Follow container under test logs. Currently doing so hangs out the test forever.
redisGo.FollowOutput(tlog.Mirror{T: t, Name: "redis-go"})
err = redisGo.StartLogProducer(ctx)
if err != nil {
t.Fatal(err)
}
// TODO: See above.
// t.Cleanup(func() {
// redisGo.StopLogProducer()
// })

redisGoStatus, err := redisGo.State(ctx)
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions pkg/agent/protocol/nettest/tlog/tlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ import (

// Mirror is a testcontainers log adapter that mirrors container output to testing.T.Log.
type Mirror struct {
T *testing.T
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) {
m.T.Logf("%s: %s", log.LogType, log.Content)
prefix := ""
if m.Name != "" {
prefix += m.Name + "/"
}
prefix += log.LogType

m.T.Logf("%s: %s", prefix, log.Content)
}

0 comments on commit 2a63093

Please sign in to comment.