From da1c081b2a9511f0f1b37b1ee2eb2c21a7d53e53 Mon Sep 17 00:00:00 2001 From: Roberto Santalla Date: Mon, 2 Oct 2023 12:35:54 +0200 Subject: [PATCH] nettest: add tcpkill container and test for redis --- .../nettest/containers/tcpkill/Dockerfile | 3 + .../protocol/nettest/redis/tcpkill_test.go | 119 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 pkg/agent/protocol/nettest/containers/tcpkill/Dockerfile create mode 100644 pkg/agent/protocol/nettest/redis/tcpkill_test.go diff --git a/pkg/agent/protocol/nettest/containers/tcpkill/Dockerfile b/pkg/agent/protocol/nettest/containers/tcpkill/Dockerfile new file mode 100644 index 00000000..e5c6503e --- /dev/null +++ b/pkg/agent/protocol/nettest/containers/tcpkill/Dockerfile @@ -0,0 +1,3 @@ +FROM debian:bookworm + +RUN apt update && apt install -y dsniff diff --git a/pkg/agent/protocol/nettest/redis/tcpkill_test.go b/pkg/agent/protocol/nettest/redis/tcpkill_test.go new file mode 100644 index 00000000..0061800b --- /dev/null +++ b/pkg/agent/protocol/nettest/redis/tcpkill_test.go @@ -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") + } +}