Empty ip address when endpoint mode dnsrr

This commit is contained in:
Michael 2018-02-20 08:12:02 +01:00 committed by Traefiker Bot
parent f98c537ec2
commit 7ea76929d4
3 changed files with 73 additions and 12 deletions

View file

@ -95,6 +95,25 @@ func taskSlot(slot int) func(*swarm.Task) {
} }
} }
func taskNetworkAttachment(id string, name string, driver string, addresses []string) func(*swarm.Task) {
return func(task *swarm.Task) {
task.NetworksAttachments = append(task.NetworksAttachments, swarm.NetworkAttachment{
Network: swarm.Network{
ID: id,
Spec: swarm.NetworkSpec{
Annotations: swarm.Annotations{
Name: name,
},
DriverConfiguration: &swarm.Driver{
Name: driver,
},
},
},
Addresses: addresses,
})
}
}
func taskStatus(ops ...func(*swarm.TaskStatus)) func(*swarm.Task) { func taskStatus(ops ...func(*swarm.TaskStatus)) func(*swarm.Task) {
return func(task *swarm.Task) { return func(task *swarm.Task) {
status := &swarm.TaskStatus{} status := &swarm.TaskStatus{}
@ -113,6 +132,14 @@ func taskState(state swarm.TaskState) func(*swarm.TaskStatus) {
} }
} }
func taskContainerStatus(id string) func(*swarm.TaskStatus) {
return func(status *swarm.TaskStatus) {
status.ContainerStatus = swarm.ContainerStatus{
ContainerID: id,
}
}
}
func swarmService(ops ...func(*swarm.Service)) swarm.Service { func swarmService(ops ...func(*swarm.Service)) swarm.Service {
service := &swarm.Service{ service := &swarm.Service{
ID: "serviceID", ID: "serviceID",

View file

@ -849,8 +849,10 @@ func listTasks(ctx context.Context, dockerClient client.APIClient, serviceID str
continue continue
} }
dockerData := parseTasks(task, serviceDockerData, networkMap, isGlobalSvc) dockerData := parseTasks(task, serviceDockerData, networkMap, isGlobalSvc)
if len(dockerData.NetworkSettings.Networks) > 0 {
dockerDataList = append(dockerDataList, dockerData) dockerDataList = append(dockerDataList, dockerData)
} }
}
return dockerDataList, err return dockerDataList, err
} }

View file

@ -710,6 +710,7 @@ func TestSwarmTaskParsing(t *testing.T) {
type fakeTasksClient struct { type fakeTasksClient struct {
dockerclient.APIClient dockerclient.APIClient
tasks []swarm.Task tasks []swarm.Task
container dockertypes.ContainerJSON
err error err error
} }
@ -717,6 +718,10 @@ func (c *fakeTasksClient) TaskList(ctx context.Context, options dockertypes.Task
return c.tasks, c.err return c.tasks, c.err
} }
func (c *fakeTasksClient) ContainerInspect(ctx context.Context, container string) (dockertypes.ContainerJSON, error) {
return c.container, c.err
}
func TestListTasks(t *testing.T) { func TestListTasks(t *testing.T) {
testCases := []struct { testCases := []struct {
service swarm.Service service swarm.Service
@ -728,11 +733,30 @@ func TestListTasks(t *testing.T) {
{ {
service: swarmService(serviceName("container")), service: swarmService(serviceName("container")),
tasks: []swarm.Task{ tasks: []swarm.Task{
swarmTask("id1", taskSlot(1), taskStatus(taskState(swarm.TaskStateRunning))), swarmTask("id1",
swarmTask("id2", taskSlot(2), taskStatus(taskState(swarm.TaskStatePending))), taskSlot(1),
swarmTask("id3", taskSlot(3)), taskNetworkAttachment("1", "network1", "overlay", []string{"127.0.0.1"}),
swarmTask("id4", taskSlot(4), taskStatus(taskState(swarm.TaskStateRunning))), taskStatus(taskState(swarm.TaskStateRunning)),
swarmTask("id5", taskSlot(5), taskStatus(taskState(swarm.TaskStateFailed))), ),
swarmTask("id2",
taskSlot(2),
taskNetworkAttachment("1", "network1", "overlay", []string{"127.0.0.2"}),
taskStatus(taskState(swarm.TaskStatePending)),
),
swarmTask("id3",
taskSlot(3),
taskNetworkAttachment("1", "network1", "overlay", []string{"127.0.0.3"}),
),
swarmTask("id4",
taskSlot(4),
taskNetworkAttachment("1", "network1", "overlay", []string{"127.0.0.4"}),
taskStatus(taskState(swarm.TaskStateRunning)),
),
swarmTask("id5",
taskSlot(5),
taskNetworkAttachment("1", "network1", "overlay", []string{"127.0.0.5"}),
taskStatus(taskState(swarm.TaskStateFailed)),
),
}, },
isGlobalSVC: false, isGlobalSVC: false,
expectedTasks: []string{ expectedTasks: []string{
@ -753,7 +777,7 @@ func TestListTasks(t *testing.T) {
t.Parallel() t.Parallel()
dockerData := parseService(test.service, test.networks) dockerData := parseService(test.service, test.networks)
dockerClient := &fakeTasksClient{tasks: test.tasks} dockerClient := &fakeTasksClient{tasks: test.tasks}
taskDockerData, _ := listTasks(context.Background(), dockerClient, test.service.ID, dockerData, map[string]*docker.NetworkResource{}, test.isGlobalSVC) taskDockerData, _ := listTasks(context.Background(), dockerClient, test.service.ID, dockerData, test.networks, test.isGlobalSVC)
if len(test.expectedTasks) != len(taskDockerData) { if len(test.expectedTasks) != len(taskDockerData) {
t.Errorf("expected tasks %v, got %v", spew.Sdump(test.expectedTasks), spew.Sdump(taskDockerData)) t.Errorf("expected tasks %v, got %v", spew.Sdump(test.expectedTasks), spew.Sdump(taskDockerData))
@ -896,8 +920,14 @@ func TestListServices(t *testing.T) {
withEndpointSpec(modeDNSSR)), withEndpointSpec(modeDNSSR)),
}, },
tasks: []swarm.Task{ tasks: []swarm.Task{
swarmTask("id1", taskStatus(taskState(swarm.TaskStateRunning))), swarmTask("id1",
swarmTask("id2", taskStatus(taskState(swarm.TaskStateRunning))), taskNetworkAttachment("yk6l57rfwizjzxxzftn4amaot", "network_name", "overlay", []string{"127.0.0.1"}),
taskStatus(taskState(swarm.TaskStateRunning)),
),
swarmTask("id2",
taskNetworkAttachment("yk6l57rfwizjzxxzftn4amaot", "network_name", "overlay", []string{"127.0.0.1"}),
taskStatus(taskState(swarm.TaskStateRunning)),
),
}, },
dockerVersion: "1.30", dockerVersion: "1.30",
networks: []dockertypes.NetworkResource{ networks: []dockertypes.NetworkResource{
@ -934,7 +964,9 @@ func TestListServices(t *testing.T) {
t.Run(strconv.Itoa(caseID), func(t *testing.T) { t.Run(strconv.Itoa(caseID), func(t *testing.T) {
t.Parallel() t.Parallel()
dockerClient := &fakeServicesClient{services: test.services, tasks: test.tasks, dockerVersion: test.dockerVersion, networks: test.networks} dockerClient := &fakeServicesClient{services: test.services, tasks: test.tasks, dockerVersion: test.dockerVersion, networks: test.networks}
serviceDockerData, _ := listServices(context.Background(), dockerClient)
serviceDockerData, err := listServices(context.Background(), dockerClient)
assert.NoError(t, err)
assert.Equal(t, len(test.expectedServices), len(serviceDockerData)) assert.Equal(t, len(test.expectedServices), len(serviceDockerData))
for i, serviceName := range test.expectedServices { for i, serviceName := range test.expectedServices {