Improve and extend TestGetBackendServer.

- Cover error cases.
- Use sub-tests.
This commit is contained in:
Timo Reimann 2017-04-22 22:51:22 +02:00
parent a4355569af
commit 7eb3051a57

View file

@ -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{ desc: "task hostname override",
Ports: &[]marathon.Port{ addIPAddrPerTask: true,
{
Number: 8880,
Name: "p1",
},
},
},
},
},
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{ desc: "task IP address missing",
Ports: &[]marathon.Port{ task: marathon.Task{
{ IPAddresses: []*marathon.IPAddress{},
Number: 8880, },
Name: "p1", addIPAddrPerTask: true,
}, wantServer: "",
}, },
{
desc: "single task IP address",
task: marathon.Task{
IPAddresses: []*marathon.IPAddress{
{
IPAddress: "1.1.1.1",
}, },
}, },
}, },
expected: "sample.com", 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",
}, },
} }
for _, app := range applications { for _, test := range tests {
t.Run(app.application.ID, func(t *testing.T) { test := test
provider := &Provider{} t.Run(test.desc, func(t *testing.T) {
provider.ForceTaskHostname = app.forceTaskHostname t.Parallel()
provider := &Provider{ForceTaskHostname: test.forceTaskHostname}
applications := []marathon.Application{app.application} // Populate application.
if test.application.ID == "" {
task := marathon.Task{ test.application.ID = appID
AppID: app.application.ID,
Host: "sample.com",
IPAddresses: []*marathon.IPAddress{
{
IPAddress: "192.168.0.1",
},
},
} }
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}
actual := provider.getBackendServer(task, applications) // Populate task.
if actual != app.expected { test.task.AppID = appID
t.Errorf("App %s, expected %q, got %q", task.AppID, app.expected, actual) test.task.Host = "host"
gotServer := provider.getBackendServer(test.task, applications)
if gotServer != test.wantServer {
t.Errorf("got server '%s', want '%s'", gotServer, test.wantServer)
} }
}) })
} }