Skip to content

Commit

Permalink
nettest: add tcpkill container and test for redis
Browse files Browse the repository at this point in the history
  • Loading branch information
roobre committed Oct 2, 2023
1 parent cb6e1ac commit da1c081
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/agent/protocol/nettest/containers/tcpkill/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM debian:bookworm

RUN apt update && apt install -y dsniff
119 changes: 119 additions & 0 deletions pkg/agent/protocol/nettest/redis/tcpkill_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package redis_test

import (
"context"
"os"
"path/filepath"
"testing"
"time"

"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/util"
)

func Test_Redis_TCPKill(t *testing.T) {
t.Parallel()

if os.Getenv("NETTEST") == "" {
t.Skip("Skipping network protocol test as NETTEST is not set")
}

ctx := context.TODO()

redis, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ProviderType: testcontainers.ProviderDocker,
ContainerRequest: testcontainers.ContainerRequest{
Image: "redis",
ExposedPorts: []string{"6379/tcp"},
WaitingFor: wait.ForExposedPort(),
},
Started: true,
})
if err != nil {
t.Fatalf("failed to create redis container %v", err)
}

t.Cleanup(func() {
_ = redis.Terminate(ctx)
})

tcpkill, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ProviderType: testcontainers.ProviderDocker,
ContainerRequest: testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Dockerfile: "Dockerfile",
Context: filepath.Join("..", "containers", "tcpkill"),
},
NetworkMode: container.NetworkMode("container:" + redis.GetContainerID()),
Cmd: []string{"/bin/sh", "-c", "echo ready && sleep infinity"},
Privileged: true,
WaitingFor: wait.ForLog("ready"),
},
Started: true,
})
if err != nil {
t.Fatalf("failed to create agent container %v", err)
}

t.Cleanup(func() {
_ = tcpkill.Terminate(ctx)
})

redisGo, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ProviderType: testcontainers.ProviderDocker,
ContainerRequest: testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Dockerfile: "Dockerfile",
Context: filepath.Join("..", "containers", "redis-go"),
},
Cmd: []string{"localhost:6379"},
NetworkMode: container.NetworkMode("container:" + redis.GetContainerID()),
},
Started: true,
})
if err != nil {
t.Fatalf("failed to create agent container %v", err)
}

// 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.FollowOutput(util.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 {
t.Fatal(err)
}
if !redisGoStatus.Running {
t.Fatalf("Redis client container failed")
}

time.Sleep(3 * time.Second)

util.TCExec(t, tcpkill, "tcpkill -i any -5 port 6379")

time.Sleep(7 * time.Second)

redisGoStatus, err = redisGo.State(ctx)
if err != nil {
t.Fatal(err)
}

if !redisGoStatus.Running {
t.Fatalf("Redis client container failed")
}
}

0 comments on commit da1c081

Please sign in to comment.