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