Improve and extend TestGetBackendServer.
- Cover error cases. - Use sub-tests.
This commit is contained in:
parent
a4355569af
commit
7eb3051a57
1 changed files with 118 additions and 52 deletions
|
@ -1431,73 +1431,139 @@ func TestMarathonGetSubDomain(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetBackendServer(t *testing.T) {
|
func TestGetBackendServer(t *testing.T) {
|
||||||
|
appID := "appId"
|
||||||
applications := []struct {
|
host := "host"
|
||||||
forceTaskHostname bool
|
tests := []struct {
|
||||||
|
desc string
|
||||||
application marathon.Application
|
application marathon.Application
|
||||||
expected string
|
addIPAddrPerTask bool
|
||||||
|
task marathon.Task
|
||||||
|
forceTaskHostname bool
|
||||||
|
wantServer string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
application: marathon.Application{
|
desc: "application missing",
|
||||||
ID: "app-without-IP-per-task",
|
application: marathon.Application{ID: "other"},
|
||||||
},
|
wantServer: "",
|
||||||
expected: "sample.com",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
application: marathon.Application{
|
desc: "application without IP-per-task",
|
||||||
|
wantServer: host,
|
||||||
ID: "app-with-IP-per-task",
|
},
|
||||||
IPAddressPerTask: &marathon.IPAddressPerTask{
|
|
||||||
Discovery: &marathon.Discovery{
|
|
||||||
Ports: &[]marathon.Port{
|
|
||||||
{
|
{
|
||||||
Number: 8880,
|
desc: "task hostname override",
|
||||||
Name: "p1",
|
addIPAddrPerTask: true,
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expected: "192.168.0.1",
|
|
||||||
}, {
|
|
||||||
forceTaskHostname: true,
|
forceTaskHostname: true,
|
||||||
application: marathon.Application{
|
wantServer: host,
|
||||||
ID: "app-with-hostname-enforced",
|
},
|
||||||
IPAddressPerTask: &marathon.IPAddressPerTask{
|
|
||||||
Discovery: &marathon.Discovery{
|
|
||||||
Ports: &[]marathon.Port{
|
|
||||||
{
|
{
|
||||||
Number: 8880,
|
desc: "task IP address missing",
|
||||||
Name: "p1",
|
task: marathon.Task{
|
||||||
|
IPAddresses: []*marathon.IPAddress{},
|
||||||
},
|
},
|
||||||
|
addIPAddrPerTask: true,
|
||||||
|
wantServer: "",
|
||||||
},
|
},
|
||||||
},
|
{
|
||||||
},
|
desc: "single task IP address",
|
||||||
},
|
task: marathon.Task{
|
||||||
expected: "sample.com",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, app := range applications {
|
|
||||||
t.Run(app.application.ID, func(t *testing.T) {
|
|
||||||
provider := &Provider{}
|
|
||||||
provider.ForceTaskHostname = app.forceTaskHostname
|
|
||||||
|
|
||||||
applications := []marathon.Application{app.application}
|
|
||||||
|
|
||||||
task := marathon.Task{
|
|
||||||
AppID: app.application.ID,
|
|
||||||
Host: "sample.com",
|
|
||||||
IPAddresses: []*marathon.IPAddress{
|
IPAddresses: []*marathon.IPAddress{
|
||||||
{
|
{
|
||||||
IPAddress: "192.168.0.1",
|
IPAddress: "1.1.1.1",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
addIPAddrPerTask: true,
|
||||||
|
wantServer: "1.1.1.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "multiple task IP addresses without index label",
|
||||||
|
task: marathon.Task{
|
||||||
|
IPAddresses: []*marathon.IPAddress{
|
||||||
|
{
|
||||||
|
IPAddress: "1.1.1.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
IPAddress: "2.2.2.2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
addIPAddrPerTask: true,
|
||||||
|
wantServer: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "multiple task IP addresses with invalid index label",
|
||||||
|
application: marathon.Application{
|
||||||
|
Labels: &map[string]string{"traefik.ipAddressIdx": "invalid"},
|
||||||
|
},
|
||||||
|
task: marathon.Task{
|
||||||
|
IPAddresses: []*marathon.IPAddress{
|
||||||
|
{
|
||||||
|
IPAddress: "1.1.1.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
IPAddress: "2.2.2.2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
addIPAddrPerTask: true,
|
||||||
|
wantServer: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "multiple task IP addresses with valid index label",
|
||||||
|
application: marathon.Application{
|
||||||
|
Labels: &map[string]string{"traefik.ipAddressIdx": "1"},
|
||||||
|
},
|
||||||
|
task: marathon.Task{
|
||||||
|
IPAddresses: []*marathon.IPAddress{
|
||||||
|
{
|
||||||
|
IPAddress: "1.1.1.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
IPAddress: "2.2.2.2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
addIPAddrPerTask: true,
|
||||||
|
wantServer: "2.2.2.2",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
actual := provider.getBackendServer(task, applications)
|
for _, test := range tests {
|
||||||
if actual != app.expected {
|
test := test
|
||||||
t.Errorf("App %s, expected %q, got %q", task.AppID, app.expected, actual)
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
provider := &Provider{ForceTaskHostname: test.forceTaskHostname}
|
||||||
|
|
||||||
|
// Populate application.
|
||||||
|
if test.application.ID == "" {
|
||||||
|
test.application.ID = appID
|
||||||
|
}
|
||||||
|
if test.application.Labels == nil {
|
||||||
|
test.application.Labels = &map[string]string{}
|
||||||
|
}
|
||||||
|
if test.addIPAddrPerTask {
|
||||||
|
test.application.IPAddressPerTask = &marathon.IPAddressPerTask{
|
||||||
|
Discovery: &marathon.Discovery{
|
||||||
|
Ports: &[]marathon.Port{
|
||||||
|
{
|
||||||
|
Number: 8000,
|
||||||
|
Name: "port",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
applications := []marathon.Application{test.application}
|
||||||
|
|
||||||
|
// Populate task.
|
||||||
|
test.task.AppID = appID
|
||||||
|
test.task.Host = "host"
|
||||||
|
|
||||||
|
gotServer := provider.getBackendServer(test.task, applications)
|
||||||
|
|
||||||
|
if gotServer != test.wantServer {
|
||||||
|
t.Errorf("got server '%s', want '%s'", gotServer, test.wantServer)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue