Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose list of excluded endpoints as a string slice #187

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func Test_JsPodDisruptor(t *testing.T) {
averageDelay: "100ms",
delayVariation: "10ms",
errorBody: '',
exclude: "",
exclude: [],
port: 80
}

Expand All @@ -229,7 +229,7 @@ func Test_JsPodDisruptor(t *testing.T) {
averageDelay: "100ms",
delayVariation: "10ms",
errorBody: '',
exclude: "",
exclude: [],
port: 80
}

Expand All @@ -246,7 +246,7 @@ func Test_JsPodDisruptor(t *testing.T) {
averageDelay: "100ms",
delayVariation: "10ms",
errorBody: '',
exclude: "",
exclude: [],
port: 80
}

Expand All @@ -263,7 +263,7 @@ func Test_JsPodDisruptor(t *testing.T) {
averageDelay: "100ms",
delayVariation: "10ms",
errorBody: '',
exclude: "",
exclude: [],
port: 80
}

Expand All @@ -283,6 +283,17 @@ func Test_JsPodDisruptor(t *testing.T) {
`,
expectError: true,
},
{
description: "inject HTTP Fault with exclude as a string",
script: `
const fault = {
exclude: "foo,bar",
}

d.injectHTTPFaults(fault, "1s")
`,
expectError: false,
},
{
description: "inject Grpc Fault with full arguments",
script: `
Expand All @@ -292,7 +303,7 @@ func Test_JsPodDisruptor(t *testing.T) {
statusMessage: '',
averageDelay: "100ms",
delayVariation: "10ms",
exclude: "",
exclude: [],
port: 80
}

Expand All @@ -314,7 +325,7 @@ func Test_JsPodDisruptor(t *testing.T) {
statusMessage: '',
averageDelay: "100ms",
delayVariation: "10ms",
exclude: "",
exclude: [],
port: 80
}

Expand All @@ -331,7 +342,7 @@ func Test_JsPodDisruptor(t *testing.T) {
statusMessage: '',
averageDelay: "100ms",
delayVariation: "10ms",
exclude: "",
exclude: [],
port: 80
}

Expand All @@ -353,7 +364,7 @@ func Test_JsPodDisruptor(t *testing.T) {
statusMessage: '',
averageDelay: "100ms",
delayVariation: "10ms",
exclude: "",
exclude: [],
port: 80
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/api/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"reflect"
"strings"
"time"
)

Expand Down Expand Up @@ -88,6 +89,18 @@ func convertMap(value interface{}, target interface{}) error {
func convertSlice(value interface{}, target interface{}) error {
targetValue := reflect.ValueOf(target).Elem()

// Hack: Allow to convert a comma-separated string into a slice of strings.
// This block is to be removed in 1.0.
valueStr, isString := value.(string)
if isString && reflect.TypeOf(target).Elem().Elem().Kind() == reflect.String {
// TODO: Log warning.
var iSlice []interface{}
for _, item := range strings.Split(valueStr, ",") {
iSlice = append(iSlice, item)
}
value = iSlice
}

valueSlice, ok := value.([]interface{})
if !ok {
return fmt.Errorf("expected value []interface{} got %s", reflect.TypeOf(value))
Expand Down
5 changes: 3 additions & 2 deletions pkg/disruptors/cmd_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package disruptors

import (
"fmt"
"strings"
"time"

"github.com/grafana/xk6-disruptor/pkg/utils"
Expand Down Expand Up @@ -43,7 +44,7 @@ func buildGrpcFaultCmd(fault GrpcFault, duration time.Duration, options GrpcDisr
}

if len(fault.Exclude) > 0 {
cmd = append(cmd, "-x", fault.Exclude)
cmd = append(cmd, "-x", strings.Join(fault.Exclude, ","))
}

if options.ProxyPort != 0 {
Expand Down Expand Up @@ -93,7 +94,7 @@ func buildHTTPFaultCmd(fault HTTPFault, duration time.Duration, options HTTPDisr
}

if len(fault.Exclude) > 0 {
cmd = append(cmd, "-x", fault.Exclude)
cmd = append(cmd, "-x", strings.Join(fault.Exclude, ","))
}

if options.ProxyPort != 0 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/disruptors/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func Test_PodHTTPFaultInjection(t *testing.T) {
expectError: false,
cmdError: nil,
fault: HTTPFault{
Exclude: "/path1,/path2",
Exclude: []string{"/path1", "/path2"},
},
opts: HTTPDisruptionOptions{},
duration: 60 * time.Second,
Expand Down Expand Up @@ -292,7 +292,7 @@ func Test_PodGrpcPFaultInjection(t *testing.T) {
},
targets: []string{"my-app-pod"},
fault: GrpcFault{
Exclude: "service1,service2",
Exclude: []string{"service1", "service2"},
},
opts: GrpcDisruptionOptions{},
duration: 60 * time.Second,
Expand Down
6 changes: 3 additions & 3 deletions pkg/disruptors/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ type HTTPFault struct {
ErrorCode uint `js:"errorCode"`
// Body to be returned when an error is injected
ErrorBody string `js:"errorBody"`
// Comma-separated list of url paths to be excluded from disruptions
Exclude string
// List of url paths to be excluded from disruptions
Exclude []string `js:"exclude"`
}

// GrpcFault specifies a fault to be injected in grpc requests
Expand All @@ -64,5 +64,5 @@ type GrpcFault struct {
// Status message to be returned in requests selected to return an error
StatusMessage string `js:"statusMessage"`
// List of grpc services to be excluded from disruptions
Exclude string `js:"exclude"`
Exclude []string `js:"exclude"`
}