-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor lb-method key and annotation
- Loading branch information
Showing
5 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package nginx | ||
|
||
func ParseLBMethod(method string) (string, error) { | ||
if method == "round_robin" { | ||
return "", nil | ||
} | ||
return method, nil | ||
} | ||
|
||
func ParseLBMethodForPlus(method string) (string, error) { | ||
if method == "round_robin" { | ||
return "", nil | ||
} | ||
return method, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package nginx | ||
|
||
import "testing" | ||
|
||
func TestParseLBMethod(t *testing.T) { | ||
var testsWithValidInput = []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"", "least_conn"}, | ||
{"least_conn", "least_conn"}, | ||
{"round_robin", ""}, | ||
{"ip_hash", "ip_hash"}, | ||
{"hash $request_id", "hash $request_id"}, | ||
} | ||
|
||
var invalidInput = []string{ | ||
"blabla", | ||
"least_time header", | ||
"hash123", | ||
} | ||
|
||
for _, test := range testsWithValidInput { | ||
result, err := ParseLBMethod(test.input) | ||
if err != nil { | ||
t.Errorf("TestParseLBMethod(%q) returned an error for valid input", test.input) | ||
} | ||
|
||
if result != test.expected { | ||
t.Errorf("TestParseLBMethod(%q) returned %q expected %q", test.input, result, test.expected) | ||
} | ||
} | ||
|
||
for _, input := range invalidInput { | ||
_, err := ParseLBMethod(input) | ||
if err == nil { | ||
t.Errorf("TestParseLBMethod(%q) does not return an error for invalid input", input) | ||
} | ||
} | ||
} | ||
|
||
func TestParseLBMethodForPlus(t *testing.T) { | ||
var testsWithValidInput = []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"", "least_conn"}, | ||
{"least_conn", "least_conn"}, | ||
{"round_robin", ""}, | ||
{"ip_hash", "ip_hash"}, | ||
{"hash $request_id", "hash $request_id"}, | ||
{"least_time header", "least_time header"}, | ||
{"least_time last_byte", "least_time last_byte"}, | ||
{"least_time header inflight", "least_time header inflight"}, | ||
{"least_time last_byte inflight", "least_time last_byte inflight"}, | ||
} | ||
|
||
var invalidInput = []string{ | ||
"blabla", | ||
"hash123", | ||
"least_time header inflight", | ||
} | ||
|
||
for _, test := range testsWithValidInput { | ||
result, err := ParseLBMethod(test.input) | ||
if err != nil { | ||
t.Errorf("TestParseLBMethod(%q) returned an error for valid input", test.input) | ||
} | ||
|
||
if result != test.expected { | ||
t.Errorf("TestParseLBMethod(%q) returned %q expected %q", test.input, result, test.expected) | ||
} | ||
} | ||
|
||
for _, input := range invalidInput { | ||
_, err := ParseLBMethod(input) | ||
if err == nil { | ||
t.Errorf("TestParseLBMethod(%q) does not return an error for invalid input", input) | ||
} | ||
} | ||
} |