-
Notifications
You must be signed in to change notification settings - Fork 2
/
marcel_test.go
135 lines (113 loc) · 3.39 KB
/
marcel_test.go
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
package main
import (
"os"
"testing"
"github.com/dgageot/marcel/config"
"github.com/dgageot/marcel/machine"
"github.com/stretchr/testify/assert"
)
func ExampleDockerVersion() {
os.Args = []string{"marcel", "--version"}
main()
// Output:
// Docker version 1.10.1, build 9e83765
}
func TestEmptyArgs(t *testing.T) {
executable, args := executable([]string{})
assert.Equal(t, "docker", executable)
assert.Equal(t, []string{}, args)
}
func TestFindCommand(t *testing.T) {
tests := []struct {
args []string
expectedExecutable string
expectedArgs []string
}{
{[]string{}, "docker", []string{}},
{[]string{"--version"}, "docker", []string{"--version"}},
{[]string{"machine", "--version"}, "docker-machine", []string{"--version"}},
{[]string{"compose", "--version"}, "docker-compose", []string{"--version"}},
{[]string{"run", "hello-world"}, "docker", []string{"run", "hello-world"}},
}
for _, test := range tests {
executable, args := executable(test.args)
assert.Equal(t, test.expectedExecutable, executable)
assert.Equal(t, test.expectedArgs, args)
}
}
func TestUseMachine(t *testing.T) {
tests := []struct {
args []string
expectedConfig config.Config
}{
{[]string{"default"}, config.Config{
Type: "machine",
Machine: "default",
}},
{[]string{"other"}, config.Config{
Type: "machine",
Machine: "other",
}},
{[]string{"local"}, config.Config{
Type: "local",
}},
{[]string{"tcp://192.168.99.100:2376", "~/.docker/certs"}, config.Config{
Type: "url",
Url: "tcp://192.168.99.100:2376",
CertPath: "~/.docker/certs",
}},
{[]string{"tcp://192.168.99.150:2376"}, config.Config{
Type: "url",
Url: "tcp://192.168.99.150:2376",
}},
}
for _, test := range tests {
os.Args = append([]string{"marcel", "use"}, test.args...)
main()
config, err := config.Load()
assert.Equal(t, test.expectedConfig, *config)
assert.NoError(t, err)
}
}
type MockEnver struct {
envPerMachine map[string][]string
}
func (m *MockEnver) Env(machine string) ([]string, error) {
return m.envPerMachine[machine], nil
}
func TestDockerEnv(t *testing.T) {
defer func(enver func() machine.Enver) { machine.NewEnver = enver }(machine.NewEnver)
machine.NewEnver = func() machine.Enver {
return &MockEnver{
envPerMachine: map[string][]string{
"default": []string{"DOCKER_TLS_VERIFY=1", "DOCKER_HOST=tcp://192.168.99.100:2376", "DOCKER_CERT_PATH=/Users/dgageot/.docker/machine/machines/default"},
},
}
}
tests := []struct {
config *config.Config
expectedEnv []string
}{
{&config.Config{
Type: "machine",
Machine: "default",
}, []string{"DOCKER_TLS_VERIFY=1", "DOCKER_HOST=tcp://192.168.99.100:2376", "DOCKER_CERT_PATH=/Users/dgageot/.docker/machine/machines/default"}},
{&config.Config{
Type: "local",
}, []string{"DOCKER_TLS_VERIFY=", "DOCKER_HOST=", "DOCKER_CERT_PATH="}},
{&config.Config{
Type: "url",
Url: "tcp://192.168.99.150:2376",
}, []string{"DOCKER_TLS_VERIFY=", "DOCKER_HOST=tcp://192.168.99.150:2376", "DOCKER_CERT_PATH="}},
{&config.Config{
Type: "url",
Url: "tcp://192.168.99.160:2376",
CertPath: "/certs",
}, []string{"DOCKER_TLS_VERIFY=1", "DOCKER_HOST=tcp://192.168.99.160:2376", "DOCKER_CERT_PATH=/certs"}},
}
for _, test := range tests {
env, err := dockerEnv(test.config)
assert.Equal(t, test.expectedEnv, env)
assert.NoError(t, err)
}
}