forked from adamlaska/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
284 lines (249 loc) · 8.85 KB
/
utils_test.go
File metadata and controls
284 lines (249 loc) · 8.85 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package provision
import (
"fmt"
"regexp"
"strings"
"testing"
"github.com/docker/machine/drivers/fakedriver"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/provision/pkgaction"
"github.com/docker/machine/libmachine/provision/provisiontest"
"github.com/docker/machine/libmachine/provision/serviceaction"
"github.com/docker/machine/libmachine/swarm"
"github.com/stretchr/testify/assert"
)
var (
reDaemonListening = ":2376\\s+.*:.*"
)
func TestMatchNetstatOutMissing(t *testing.T) {
nsOut := `Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:237 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::23760 :::* LISTEN`
if matchNetstatOut(reDaemonListening, nsOut) {
t.Fatal("Expected not to match the netstat output as showing the daemon listening but got a match")
}
}
func TestMatchNetstatOutPresent(t *testing.T) {
nsOut := `Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp6 0 0 :::2376 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN`
if !matchNetstatOut(reDaemonListening, nsOut) {
t.Fatal("Expected to match the netstat output as showing the daemon listening but didn't")
}
}
func TestMatchSsOutMissing(t *testing.T) {
ssOut := `State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 :::23760 :::* `
if matchNetstatOut(reDaemonListening, ssOut) {
t.Fatal("Expected not to match the ss output as showing the daemon listening but got a match")
}
}
func TestMatchSsOutPresent(t *testing.T) {
ssOut := `State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 :::2376 :::* `
if !matchNetstatOut(reDaemonListening, ssOut) {
t.Fatal("Expected to match the ss output as showing the daemon listening but didn't")
}
}
func TestGenerateDockerOptionsBoot2Docker(t *testing.T) {
p := &Boot2DockerProvisioner{
Driver: &fakedriver.Driver{},
}
dockerPort := 1234
p.AuthOptions = auth.Options{
CaCertRemotePath: "/test/ca-cert",
ServerKeyRemotePath: "/test/server-key",
ServerCertRemotePath: "/test/server-cert",
}
engineConfigPath := "/var/lib/boot2docker/profile"
dockerCfg, err := p.GenerateDockerOptions(dockerPort)
if err != nil {
t.Fatal(err)
}
if dockerCfg.EngineOptionsPath != engineConfigPath {
t.Fatalf("expected engine path %s; received %s", engineConfigPath, dockerCfg.EngineOptionsPath)
}
if strings.Index(dockerCfg.EngineOptions, fmt.Sprintf("-H tcp://0.0.0.0:%d", dockerPort)) == -1 {
t.Fatalf("-H docker port invalid; expected %d", dockerPort)
}
if strings.Index(dockerCfg.EngineOptions, fmt.Sprintf("CACERT=%s", p.AuthOptions.CaCertRemotePath)) == -1 {
t.Fatalf("CACERT option invalid; expected %s", p.AuthOptions.CaCertRemotePath)
}
if strings.Index(dockerCfg.EngineOptions, fmt.Sprintf("SERVERKEY=%s", p.AuthOptions.ServerKeyRemotePath)) == -1 {
t.Fatalf("SERVERKEY option invalid; expected %s", p.AuthOptions.ServerKeyRemotePath)
}
if strings.Index(dockerCfg.EngineOptions, fmt.Sprintf("SERVERCERT=%s", p.AuthOptions.ServerCertRemotePath)) == -1 {
t.Fatalf("SERVERCERT option invalid; expected %s", p.AuthOptions.ServerCertRemotePath)
}
}
func TestMachinePortBoot2Docker(t *testing.T) {
p := &Boot2DockerProvisioner{
Driver: &fakedriver.Driver{},
}
dockerPort := engine.DefaultPort
bindURL := fmt.Sprintf("tcp://0.0.0.0:%d", dockerPort)
p.AuthOptions = auth.Options{
CaCertRemotePath: "/test/ca-cert",
ServerKeyRemotePath: "/test/server-key",
ServerCertRemotePath: "/test/server-cert",
}
cfg, err := p.GenerateDockerOptions(dockerPort)
if err != nil {
t.Fatal(err)
}
re := regexp.MustCompile("-H tcp://.*:(.+)")
m := re.FindStringSubmatch(cfg.EngineOptions)
if len(m) == 0 {
t.Errorf("could not find port %d in engine config", dockerPort)
}
b := m[0]
u := strings.Split(b, " ")
url := u[1]
url = strings.Replace(url, "'", "", -1)
url = strings.Replace(url, "\\\"", "", -1)
if url != bindURL {
t.Errorf("expected url %s; received %s", bindURL, url)
}
}
func TestMachineCustomPortBoot2Docker(t *testing.T) {
p := &Boot2DockerProvisioner{
Driver: &fakedriver.Driver{},
}
dockerPort := 3376
bindURL := fmt.Sprintf("tcp://0.0.0.0:%d", dockerPort)
p.AuthOptions = auth.Options{
CaCertRemotePath: "/test/ca-cert",
ServerKeyRemotePath: "/test/server-key",
ServerCertRemotePath: "/test/server-cert",
}
cfg, err := p.GenerateDockerOptions(dockerPort)
if err != nil {
t.Fatal(err)
}
re := regexp.MustCompile("-H tcp://.*:(.+)")
m := re.FindStringSubmatch(cfg.EngineOptions)
if len(m) == 0 {
t.Errorf("could not find port %d in engine config", dockerPort)
}
b := m[0]
u := strings.Split(b, " ")
url := u[1]
url = strings.Replace(url, "'", "", -1)
url = strings.Replace(url, "\\\"", "", -1)
if url != bindURL {
t.Errorf("expected url %s; received %s", bindURL, url)
}
}
func TestUbuntuSystemdDaemonBinary(t *testing.T) {
p := NewUbuntuSystemdProvisioner(&fakedriver.Driver{}).(*UbuntuSystemdProvisioner)
cases := []struct {
output, want string
}{
{"Docker version 1.9.1\n", "docker daemon"},
{"Docker version 1.11.2\n", "docker daemon"},
{"Docker version 1.12.0\n", "dockerd"},
{"Docker version 1.13.0\n", "dockerd"},
}
sshCmder := &provisiontest.FakeSSHCommander{
Responses: make(map[string]string),
}
p.SSHCommander = sshCmder
for _, tc := range cases {
sshCmder.Responses["docker --version"] = tc.output
opts, err := p.GenerateDockerOptions(1234)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(opts.EngineOptions, tc.want) {
t.Fatal("incorrect docker daemon binary in engine options")
}
}
}
type fakeProvisioner struct {
GenericProvisioner
}
func (provisioner *fakeProvisioner) Package(name string, action pkgaction.PackageAction) error {
return nil
}
func (provisioner *fakeProvisioner) Provision(swarmOptions swarm.Options, authOptions auth.Options, engineOptions engine.Options) error {
return nil
}
func (provisioner *fakeProvisioner) Service(name string, action serviceaction.ServiceAction) error {
return nil
}
func (provisioner *fakeProvisioner) String() string {
return "fake"
}
func TestDecideStorageDriver(t *testing.T) {
var tests = []struct {
suppliedDriver string
defaultDriver string
remoteFilesystemType string
expectedDriver string
}{
{"", "aufs", "ext4", "aufs"},
{"", "aufs", "btrfs", "btrfs"},
{"", "overlay", "btrfs", "overlay"},
{"devicemapper", "aufs", "ext4", "devicemapper"},
{"devicemapper", "aufs", "btrfs", "devicemapper"},
}
p := &fakeProvisioner{GenericProvisioner{
Driver: &fakedriver.Driver{},
}}
for _, test := range tests {
p.SSHCommander = provisiontest.NewFakeSSHCommander(
provisiontest.FakeSSHCommanderOptions{
FilesystemType: test.remoteFilesystemType,
},
)
storageDriver, err := decideStorageDriver(p, test.defaultDriver, test.suppliedDriver)
assert.NoError(t, err)
assert.Equal(t, test.expectedDriver, storageDriver)
}
}
func TestGetFilesystemType(t *testing.T) {
p := &fakeProvisioner{GenericProvisioner{
Driver: &fakedriver.Driver{},
}}
p.SSHCommander = &provisiontest.FakeSSHCommander{
Responses: map[string]string{
"stat -f -c %T /var/lib": "btrfs\n",
},
}
fsType, err := getFilesystemType(p, "/var/lib")
assert.NoError(t, err)
assert.Equal(t, "btrfs", fsType)
}
func TestDockerClientVersion(t *testing.T) {
cases := []struct {
output, want string
}{
{"Docker version 1.9.1, build a34a1d5\n", "1.9.1"},
{"Docker version 1.9.1\n", "1.9.1"},
{"Docker version 1.13.0-rc1, build deadbeef\n", "1.13.0-rc1"},
{"Docker version 1.13.0-dev, build deadbeef\n", "1.13.0-dev"},
}
sshCmder := &provisiontest.FakeSSHCommander{
Responses: make(map[string]string),
}
for _, tc := range cases {
sshCmder.Responses["docker --version"] = tc.output
got, err := DockerClientVersion(sshCmder)
if err != nil {
t.Fatal(err)
}
if got != tc.want {
t.Errorf("Unexpected version string from %q; got %q, want %q", tc.output, tc.want, got)
}
}
}