[go: nahoru, domu]

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

Added support for status code list in HTTP Chaos #545

Merged
46 changes: 37 additions & 9 deletions chaoslib/litmus/http-chaos/lib/statuscode/status-code.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"

http_chaos "github.com/litmuschaos/litmus-go/chaoslib/litmus/http-chaos/lib"
Expand All @@ -15,7 +16,12 @@ import (
"github.com/sirupsen/logrus"
)

var acceptedStatusCodes = []int{200, 201, 202, 204, 300, 301, 302, 304, 307, 400, 401, 403, 404, 500, 501, 502, 503, 504}
var acceptedStatusCodes = []string{
"200", "201", "202", "204",
"300", "301", "302", "304", "307",
"400", "401", "403", "404",
"500", "501", "502", "503", "504",
}

//PodHttpStatusCodeChaos contains the steps to prepare and inject http status code chaos
func PodHttpStatusCodeChaos(experimentsDetails *experimentTypes.ExperimentDetails, clients clients.ClientSets, resultDetails *types.ResultDetails, eventsDetails *types.EventDetails, chaosDetails *types.ChaosDetails) error {
Expand All @@ -29,27 +35,49 @@ func PodHttpStatusCodeChaos(experimentsDetails *experimentTypes.ExperimentDetail
"ModifyResponseBody": experimentsDetails.ModifyResponseBody,
})

args := fmt.Sprintf("-t status_code -a status_code=%d -a modify_response_body=%d", experimentsDetails.StatusCode, stringBoolToInt(experimentsDetails.ModifyResponseBody))
args := fmt.Sprintf("-t status_code -a status_code=%s -a modify_response_body=%d", experimentsDetails.StatusCode, stringBoolToInt(experimentsDetails.ModifyResponseBody))
return http_chaos.PrepareAndInjectChaos(experimentsDetails, clients, resultDetails, eventsDetails, chaosDetails, args)
}

// GetStatusCode performs two functions:
// 1. It checks if the status code is provided or not. If it's not then it selects a random status code from supported list
// 2. It checks if the provided status code is valid or not.
func GetStatusCode(statusCode int) (int, error) {
if statusCode == 0 {
rand.Seed(time.Now().Unix())
func GetStatusCode(statusCode string) (string, error) {

if statusCode == "" {
log.Info("[Info]: No status code provided. Selecting a status code randomly from supported status codes")
return acceptedStatusCodes[1+rand.Intn(len(acceptedStatusCodes))], nil
neelanjan00 marked this conversation as resolved.
Show resolved Hide resolved
}
if checkStatusCode(statusCode) {
return statusCode, nil

statusCodeList := strings.Split(statusCode, ",")
rand.Seed(time.Now().Unix())
if len(statusCodeList) == 1 {
if checkStatusCode(statusCodeList[0], acceptedStatusCodes) {
return statusCodeList[0], nil
}
} else {
acceptedCodes := getAcceptedCodesInList(statusCodeList, acceptedStatusCodes)
if len(acceptedCodes) == 0 {
return "", errors.New("Invalid status code provided")
avaakash marked this conversation as resolved.
Show resolved Hide resolved
}
return acceptedCodes[rand.Intn(len(acceptedCodes))], nil
}
return "", errors.Errorf("status code %d is not supported. \nList of supported status code: %v", statusCode, acceptedStatusCodes)
avaakash marked this conversation as resolved.
Show resolved Hide resolved
}

// getAcceptedCodesInList returns the list of accepted status codes from a list of status codes
func getAcceptedCodesInList(statusCodeList []string, acceptedStatusCodes []string) []string {
var acceptedCodes []string
for _, statusCode := range statusCodeList {
if checkStatusCode(statusCode, acceptedStatusCodes) {
acceptedCodes = append(acceptedCodes, statusCode)
}
}
return 0, errors.Errorf("status code %d is not supported. \nList of supported status code: %v", statusCode, acceptedStatusCodes)
return acceptedCodes
}

// checkStatusCode checks if the provided status code is present in acceptedStatusCode list
func checkStatusCode(statusCode int) bool {
func checkStatusCode(statusCode string, acceptedStatusCodes []string) bool {
for _, code := range acceptedStatusCodes {
if code == statusCode {
return true
Expand Down
2 changes: 1 addition & 1 deletion pkg/generic/http-chaos/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GetENV(experimentDetails *experimentTypes.ExperimentDetails, expName string
case "pod-http-latency":
experimentDetails.Latency, _ = strconv.Atoi(types.Getenv("LATENCY", "6000"))
case "pod-http-status-code":
experimentDetails.StatusCode, _ = strconv.Atoi(types.Getenv("STATUS_CODE", ""))
experimentDetails.StatusCode = types.Getenv("STATUS_CODE", "")
experimentDetails.ModifyResponseBody = types.Getenv("MODIFY_RESPONSE_BODY", "true")
case "pod-http-modify-header":
experimentDetails.HeadersMap = types.Getenv("HEADERS_MAP", "{}")
Expand Down
2 changes: 1 addition & 1 deletion pkg/generic/http-chaos/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ExperimentDetails struct {
TargetServicePort int
ProxyPort int
Latency int
StatusCode int
StatusCode string
ModifyResponseBody string
HeadersMap string
HeaderMode string
Expand Down