Skip to content

Commit

Permalink
disruptors/tests: test commands are correctly generated with the corr…
Browse files Browse the repository at this point in the history
…esponding podIP
  • Loading branch information
roobre committed Jul 6, 2023
1 parent 86dccd1 commit a9b0805
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
2 changes: 2 additions & 0 deletions pkg/disruptors/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ func Test_InjectAgent(t *testing.T) {
pods: []*corev1.Pod{
builders.NewPodBuilder("pod1").
WithNamespace("test-ns").
WithIP("192.0.2.6").
Build(),
builders.NewPodBuilder("pod2").
WithNamespace("test-ns").
WithIP("192.0.2.6").
Build(),
},
timeout: -1,
Expand Down
53 changes: 42 additions & 11 deletions pkg/disruptors/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func buildPodWithPort(name string, portName string, port int32) *corev1.Pod {
pod := builders.NewPodBuilder(name).
WithNamespace("test-ns").
WithContainer(*container).
WithIP("192.0.2.6").
Build()

return pod
Expand Down Expand Up @@ -109,7 +110,7 @@ func Test_PodHTTPFaultInjection(t *testing.T) {
},
opts: HTTPDisruptionOptions{},
duration: 60 * time.Second,
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 -r 0.1 -e 500",
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 -r 0.1 -e 500 --upstream-host 192.0.2.6",
expectError: false,
cmdError: nil,
},
Expand All @@ -123,8 +124,10 @@ func Test_PodHTTPFaultInjection(t *testing.T) {
},
},
},
target: buildPodWithPort("my-app-pod", "http", 80),
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 -r 0.1 -e 500 -b {\"error\": 500}",
target: buildPodWithPort("my-app-pod", "http", 80),
// Lack of quotes on the -b arg is okay. Argument is returned as []string, and it is compared as string
// solely to make the test more readable.
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 -r 0.1 -e 500 -b {\"error\": 500} --upstream-host 192.0.2.6",
expectError: false,
cmdError: nil,
fault: HTTPFault{
Expand All @@ -147,7 +150,7 @@ func Test_PodHTTPFaultInjection(t *testing.T) {
},
},
target: buildPodWithPort("my-app-pod", "http", 80),
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 -a 100ms -v 0ms",
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 -a 100ms -v 0ms --upstream-host 192.0.2.6",
expectError: false,
cmdError: nil,
fault: HTTPFault{
Expand All @@ -168,7 +171,7 @@ func Test_PodHTTPFaultInjection(t *testing.T) {
},
},
target: buildPodWithPort("my-app-pod", "http", 80),
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 -x /path1,/path2",
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 -x /path1,/path2 --upstream-host 192.0.2.6",
expectError: false,
cmdError: nil,
fault: HTTPFault{
Expand All @@ -189,7 +192,7 @@ func Test_PodHTTPFaultInjection(t *testing.T) {
},
},
target: buildPodWithPort("my-app-pod", "http", 80),
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80",
expectedCmd: "xk6-disruptor-agent http -d 60s -t 80 --upstream-host 192.0.2.6",
expectError: true,
cmdError: fmt.Errorf("error executing command"),
fault: HTTPFault{
Expand All @@ -216,6 +219,34 @@ func Test_PodHTTPFaultInjection(t *testing.T) {
opts: HTTPDisruptionOptions{},
duration: 60,
},
{
title: "Pod without PodIP",
selector: PodSelector{
Namespace: "testns",
Select: PodAttributes{
Labels: map[string]string{
"app": "myapp",
},
},
},
target: builders.NewPodBuilder("noip").
WithNamespace("test-ns").
WithLabels(map[string]string{
"app": "myapp",
}).
WithContainer(
*builders.NewContainerBuilder("noip").
WithPort("http", 80).
Build(),
).
Build(),
expectError: true,
fault: HTTPFault{
Port: 80,
},
opts: HTTPDisruptionOptions{},
duration: 60,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -292,7 +323,7 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
},
opts: GrpcDisruptionOptions{},
duration: 60 * time.Second,
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 -r 0.1 -s 14",
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 -r 0.1 -s 14 --upstream-host 192.0.2.6",
expectError: false,
cmdError: nil,
},
Expand All @@ -315,7 +346,7 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
},
opts: GrpcDisruptionOptions{},
duration: 60 * time.Second,
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 -r 0.1 -s 14 -m internal error",
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 -r 0.1 -s 14 -m internal error --upstream-host 192.0.2.6",
expectError: false,
cmdError: nil,
},
Expand All @@ -336,7 +367,7 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
},
opts: GrpcDisruptionOptions{},
duration: 60 * time.Second,
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 -a 100ms -v 0ms",
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 -a 100ms -v 0ms --upstream-host 192.0.2.6",
expectError: false,
cmdError: nil,
},
Expand All @@ -357,7 +388,7 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
},
opts: GrpcDisruptionOptions{},
duration: 60 * time.Second,
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 -x service1,service2",
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 -x service1,service2 --upstream-host 192.0.2.6",
expectError: false,
cmdError: nil,
},
Expand All @@ -375,7 +406,7 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
fault: GrpcFault{},
opts: GrpcDisruptionOptions{},
duration: 60 * time.Second,
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000",
expectedCmd: "xk6-disruptor-agent grpc -d 60s -t 3000 --upstream-host 192.0.2.6",
expectError: true,
cmdError: fmt.Errorf("error executing command"),
},
Expand Down
6 changes: 5 additions & 1 deletion pkg/disruptors/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/grafana/xk6-disruptor/pkg/testutils/kubernetes/builders"
)

// TODO: Refactor tests so they include the generated command.
// Currently we do not have tests covering command generation logic for ServiceDisruptor.
func Test_NewServiceDisruptor(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -49,7 +51,9 @@ func Test_NewServiceDisruptor(t *testing.T) {
WithNamespace("test-ns").
WithLabels(map[string]string{
"app": "test",
}).Build(),
}).
WithIP("192.0.2.6").
Build(),
},
endpoints: []*corev1.Endpoints{
builders.NewEndPointsBuilder("test-svc").
Expand Down

0 comments on commit a9b0805

Please sign in to comment.