1
0
Fork 0

Extract index functionality into generic helper function.

Allows to move specific test cases to dedicated tests for new function.
This commit is contained in:
Timo Reimann 2017-04-22 22:07:27 +02:00
parent 16c86022bb
commit a4355569af
2 changed files with 75 additions and 39 deletions

View file

@ -1067,38 +1067,6 @@ func TestMarathonGetPort(t *testing.T) {
},
expected: "",
},
{
desc: "sub-zero port index specified",
applications: []marathon.Application{
{
ID: "app",
Labels: &map[string]string{
"traefik.portIndex": "-1",
},
},
},
task: marathon.Task{
AppID: "app",
Ports: []int{80},
},
expected: "",
},
{
desc: "too high port index specified",
applications: []marathon.Application{
{
ID: "app",
Labels: &map[string]string{
"traefik.portIndex": "42",
},
},
},
task: marathon.Task{
AppID: "app",
Ports: []int{80, 443},
},
expected: "",
},
{
desc: "task port preferred over application port",
applications: []marathon.Application{
@ -1534,3 +1502,61 @@ func TestGetBackendServer(t *testing.T) {
})
}
}
func TestParseIndex(t *testing.T) {
tests := []struct {
idxStr string
length int
shouldSucceed bool
parsed int
}{
{
idxStr: "illegal",
length: 42,
shouldSucceed: false,
},
{
idxStr: "-1",
length: 42,
shouldSucceed: false,
},
{
idxStr: "10",
length: 1,
shouldSucceed: false,
},
{
idxStr: "10",
length: 10,
shouldSucceed: false,
},
{
idxStr: "0",
length: 1,
shouldSucceed: true,
parsed: 0,
},
{
idxStr: "10",
length: 11,
shouldSucceed: true,
parsed: 10,
},
}
for _, test := range tests {
test := test
t.Run(fmt.Sprintf("parseIndex(%s, %d)", test.idxStr, test.length), func(t *testing.T) {
t.Parallel()
parsed, err := parseIndex(test.idxStr, test.length)
if test.shouldSucceed != (err == nil) {
t.Fatalf("got error '%s', want error: %t", err, !test.shouldSucceed)
}
if test.shouldSucceed && parsed != test.parsed {
t.Errorf("got parsed index %d, want %d", parsed, test.parsed)
}
})
}
}