From 4172a7c62e95c1e8228d0773574dae65c6aa5e5b Mon Sep 17 00:00:00 2001 From: Christophe Robin Date: Wed, 22 Feb 2017 01:45:13 +0900 Subject: [PATCH] Add task parser unit test for docker provider --- provider/docker_test.go | 87 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/provider/docker_test.go b/provider/docker_test.go index 4c10be819..7ed3fb9d8 100644 --- a/provider/docker_test.go +++ b/provider/docker_test.go @@ -2184,3 +2184,90 @@ func TestSwarmLoadDockerConfig(t *testing.T) { } } } + +func TestSwarmTaskParsing(t *testing.T) { + cases := []struct { + service swarm.Service + tasks []swarm.Task + isGlobalSvc bool + expectedNames map[string]string + networks map[string]*docker.NetworkResource + }{ + { + service: swarm.Service{ + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: "container", + }, + }, + }, + tasks: []swarm.Task{ + { + ID: "id1", + Slot: 1, + }, + { + ID: "id2", + Slot: 2, + }, + { + ID: "id3", + Slot: 3, + }, + }, + isGlobalSvc: false, + expectedNames: map[string]string{ + "id1": "container.1", + "id2": "container.2", + "id3": "container.3", + }, + networks: map[string]*docker.NetworkResource{ + "1": { + Name: "foo", + }, + }, + }, + { + service: swarm.Service{ + Spec: swarm.ServiceSpec{ + Annotations: swarm.Annotations{ + Name: "container", + }, + }, + }, + tasks: []swarm.Task{ + { + ID: "id1", + }, + { + ID: "id2", + }, + { + ID: "id3", + }, + }, + isGlobalSvc: true, + expectedNames: map[string]string{ + "id1": "container.id1", + "id2": "container.id2", + "id3": "container.id3", + }, + networks: map[string]*docker.NetworkResource{ + "1": { + Name: "foo", + }, + }, + }, + } + + for _, e := range cases { + dockerData := parseService(e.service, e.networks) + + for _, task := range e.tasks { + taskDockerData := parseTasks(task, dockerData, map[string]*docker.NetworkResource{}, e.isGlobalSvc) + if !reflect.DeepEqual(taskDockerData.Name, e.expectedNames[task.ID]) { + t.Fatalf("expect %v, got %v", e.expectedNames[task.ID], taskDockerData.Name) + } + } + } +}