1package container // import "github.com/docker/docker/daemon/cluster/executor/container"
2
3import (
4	"testing"
5
6	"github.com/docker/docker/api/types/container"
7	swarmapi "github.com/docker/swarmkit/api"
8	"gotest.tools/assert"
9)
10
11func TestIsolationConversion(t *testing.T) {
12	cases := []struct {
13		name string
14		from swarmapi.ContainerSpec_Isolation
15		to   container.Isolation
16	}{
17		{name: "default", from: swarmapi.ContainerIsolationDefault, to: container.IsolationDefault},
18		{name: "process", from: swarmapi.ContainerIsolationProcess, to: container.IsolationProcess},
19		{name: "hyperv", from: swarmapi.ContainerIsolationHyperV, to: container.IsolationHyperV},
20	}
21	for _, c := range cases {
22		t.Run(c.name, func(t *testing.T) {
23			task := swarmapi.Task{
24				Spec: swarmapi.TaskSpec{
25					Runtime: &swarmapi.TaskSpec_Container{
26						Container: &swarmapi.ContainerSpec{
27							Image:     "alpine:latest",
28							Isolation: c.from,
29						},
30					},
31				},
32			}
33			config := containerConfig{task: &task}
34			assert.Equal(t, c.to, config.hostConfig().Isolation)
35		})
36	}
37}
38
39func TestContainerLabels(t *testing.T) {
40	c := &containerConfig{
41		task: &swarmapi.Task{
42			ID: "real-task.id",
43			Spec: swarmapi.TaskSpec{
44				Runtime: &swarmapi.TaskSpec_Container{
45					Container: &swarmapi.ContainerSpec{
46						Labels: map[string]string{
47							"com.docker.swarm.task":         "user-specified-task",
48							"com.docker.swarm.task.id":      "user-specified-task.id",
49							"com.docker.swarm.task.name":    "user-specified-task.name",
50							"com.docker.swarm.node.id":      "user-specified-node.id",
51							"com.docker.swarm.service.id":   "user-specified-service.id",
52							"com.docker.swarm.service.name": "user-specified-service.name",
53							"this-is-a-user-label":          "this is a user label's value",
54						},
55					},
56				},
57			},
58			ServiceID: "real-service.id",
59			Slot:      123,
60			NodeID:    "real-node.id",
61			Annotations: swarmapi.Annotations{
62				Name: "real-service.name.123.real-task.id",
63			},
64			ServiceAnnotations: swarmapi.Annotations{
65				Name: "real-service.name",
66			},
67		},
68	}
69
70	expected := map[string]string{
71		"com.docker.swarm.task":         "",
72		"com.docker.swarm.task.id":      "real-task.id",
73		"com.docker.swarm.task.name":    "real-service.name.123.real-task.id",
74		"com.docker.swarm.node.id":      "real-node.id",
75		"com.docker.swarm.service.id":   "real-service.id",
76		"com.docker.swarm.service.name": "real-service.name",
77		"this-is-a-user-label":          "this is a user label's value",
78	}
79
80	labels := c.labels()
81	assert.DeepEqual(t, expected, labels)
82}
83
84func TestCredentialSpecConversion(t *testing.T) {
85	cases := []struct {
86		name string
87		from swarmapi.Privileges_CredentialSpec
88		to   []string
89	}{
90		{
91			name: "none",
92			from: swarmapi.Privileges_CredentialSpec{},
93			to:   nil,
94		},
95		{
96			name: "config",
97			from: swarmapi.Privileges_CredentialSpec{
98				Source: &swarmapi.Privileges_CredentialSpec_Config{Config: "0bt9dmxjvjiqermk6xrop3ekq"},
99			},
100			to: []string{"credentialspec=config://0bt9dmxjvjiqermk6xrop3ekq"},
101		},
102		{
103			name: "file",
104			from: swarmapi.Privileges_CredentialSpec{
105				Source: &swarmapi.Privileges_CredentialSpec_File{File: "foo.json"},
106			},
107			to: []string{"credentialspec=file://foo.json"},
108		},
109		{
110			name: "registry",
111			from: swarmapi.Privileges_CredentialSpec{
112				Source: &swarmapi.Privileges_CredentialSpec_Registry{Registry: "testing"},
113			},
114			to: []string{"credentialspec=registry://testing"},
115		},
116	}
117	for _, c := range cases {
118		c := c
119		t.Run(c.name, func(t *testing.T) {
120			task := swarmapi.Task{
121				Spec: swarmapi.TaskSpec{
122					Runtime: &swarmapi.TaskSpec_Container{
123						Container: &swarmapi.ContainerSpec{
124							Privileges: &swarmapi.Privileges{
125								CredentialSpec: &c.from,
126							},
127						},
128					},
129				},
130			}
131			config := containerConfig{task: &task}
132			assert.DeepEqual(t, c.to, config.hostConfig().SecurityOpt)
133		})
134	}
135}
136