Skip to content

Commit

Permalink
Code clean up, add negative test
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Borges committed Sep 14, 2018
1 parent 3b7fad6 commit 312d362
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
62 changes: 22 additions & 40 deletions internal/nginx/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,35 @@ func ParseLBMethod(method string) (string, error) {
method, err := validateHashLBMethod(method)
return method, err
}
if strings.HasPrefix(method, "random") {
method, err := validateRandomLBMethod(method)
return method, err
}

if method == "least_conn" || method == "ip_hash" {
if _, exists := nginxLBValidInput[method]; exists {
return method, nil
}
return "", fmt.Errorf("Invalid load balancing method: %q", method)
}

var nginxLBValidInput = map[string]bool{
"least_conn": true,
"ip_hash": true,
"random": true,
"random two": true,
"random two least_conn": true,
}

var nginxPlusLBValidInput = map[string]bool{
"least_time": true,
"last_byte": true,
"least_conn": true,
"ip_hash": true,
"least_time header": true,
"least_time last_byte": true,
"least_time header inflight": true,
"least_time last_byte inflight": true,
"least_time": true,
"last_byte": true,
"least_conn": true,
"ip_hash": true,
"random": true,
"random two": true,
"random two least_conn": true,
"random two least_time=header": true,
"random two least_time=last_byte": true,
"least_time header": true,
"least_time last_byte": true,
"least_time header inflight": true,
"least_time last_byte inflight": true,
}

// ParseLBMethodForPlus parses method and matches it to a corresponding load balancing method in NGINX Plus. An error is returned if method is not valid
Expand All @@ -49,10 +58,6 @@ func ParseLBMethodForPlus(method string) (string, error) {
method, err := validateHashLBMethod(method)
return method, err
}
if strings.HasPrefix(method, "random") {
method, err := validateRandomLBMethodForPlus(method)
return method, err
}

if _, exists := nginxPlusLBValidInput[method]; exists {
return method, nil
Expand All @@ -70,29 +75,6 @@ func validateHashLBMethod(method string) (string, error) {
return "", fmt.Errorf("Invalid load balancing method: %q", method)
}

func validateRandomLBMethod(method string) (string, error) {
keyWords := strings.Split(method, " ")
if keyWords[0] == "random" {
if len(keyWords) == 1 || len(keyWords) == 2 && keyWords[1] == "two" ||
len(keyWords) == 3 && keyWords[1] == "two" && keyWords[2] == "least_conn" {
return method, nil
}
}
return "", fmt.Errorf("Invalid load balancing method: %q", method)
}

func validateRandomLBMethodForPlus(method string) (string, error) {
keyWords := strings.Split(method, " ")
if keyWords[0] == "random" {
if len(keyWords) == 1 || len(keyWords) == 2 && keyWords[1] == "two" ||
len(keyWords) == 3 && keyWords[1] == "two" &&
(keyWords[2] == "least_conn" || keyWords[2] == "least_time=header" || keyWords[2] == "least_time=last_byte") {
return method, nil
}
}
return "", fmt.Errorf("Invalid load balancing method: %q", method)
}

// http://nginx.org/en/docs/syntax.html
var validTimeSuffixes = []string{
"ms",
Expand Down
6 changes: 6 additions & 0 deletions internal/nginx/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func TestParseLBMethod(t *testing.T) {
"least_time header",
"hash123",
"hash $request_id conwrongspelling",
"random one",
"random two least_time=header",
"random two least_time=last_byte",
"random two ip_hash",
}

for _, test := range testsWithValidInput {
Expand Down Expand Up @@ -69,6 +73,8 @@ func TestParseLBMethodForPlus(t *testing.T) {
"blabla",
"hash123",
"least_time inflight header",
"random one",
"random two ip_hash",
}

for _, test := range testsWithValidInput {
Expand Down

0 comments on commit 312d362

Please sign in to comment.