|
1 | 1 | package dockergen
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "os" |
4 | 8 | "testing"
|
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
5 | 11 | )
|
6 | 12 |
|
7 | 13 | func TestSplitDockerImageRepository(t *testing.T) {
|
8 | 14 | registry, repository, tag := splitDockerImage("ubuntu")
|
9 | 15 |
|
10 |
| - if registry != "" { |
11 |
| - t.Fail() |
12 |
| - } |
13 |
| - if repository != "ubuntu" { |
14 |
| - t.Fail() |
15 |
| - } |
16 |
| - if tag != "" { |
17 |
| - t.Fail() |
18 |
| - } |
| 16 | + assert.Equal(t, "", registry) |
| 17 | + assert.Equal(t, "ubuntu", repository) |
| 18 | + assert.Equal(t, "", tag) |
19 | 19 |
|
20 | 20 | dockerImage := DockerImage{
|
21 | 21 | Registry: registry,
|
22 | 22 | Repository: repository,
|
23 | 23 | Tag: tag,
|
24 | 24 | }
|
25 |
| - if "ubuntu" != dockerImage.String() { |
26 |
| - t.Fail() |
27 |
| - } |
| 25 | + assert.Equal(t, "ubuntu", dockerImage.String()) |
28 | 26 | }
|
29 | 27 |
|
30 | 28 | func TestSplitDockerImageWithRegistry(t *testing.T) {
|
31 | 29 | registry, repository, tag := splitDockerImage("custom.registry/ubuntu")
|
32 | 30 |
|
33 |
| - if registry != "custom.registry" { |
34 |
| - t.Fail() |
35 |
| - } |
36 |
| - if repository != "ubuntu" { |
37 |
| - t.Fail() |
38 |
| - } |
39 |
| - if tag != "" { |
40 |
| - t.Fail() |
41 |
| - } |
| 31 | + assert.Equal(t, "custom.registry", registry) |
| 32 | + assert.Equal(t, "ubuntu", repository) |
| 33 | + assert.Equal(t, "", tag) |
| 34 | + |
42 | 35 | dockerImage := DockerImage{
|
43 | 36 | Registry: registry,
|
44 | 37 | Repository: repository,
|
45 | 38 | Tag: tag,
|
46 | 39 | }
|
47 |
| - if "custom.registry/ubuntu" != dockerImage.String() { |
48 |
| - t.Fail() |
49 |
| - } |
50 |
| - |
| 40 | + assert.Equal(t, "custom.registry/ubuntu", dockerImage.String()) |
51 | 41 | }
|
52 | 42 |
|
53 | 43 | func TestSplitDockerImageWithRegistryAndTag(t *testing.T) {
|
54 | 44 | registry, repository, tag := splitDockerImage("custom.registry/ubuntu:12.04")
|
55 | 45 |
|
56 |
| - if registry != "custom.registry" { |
57 |
| - t.Fail() |
58 |
| - } |
59 |
| - if repository != "ubuntu" { |
60 |
| - t.Fail() |
61 |
| - } |
62 |
| - if tag != "12.04" { |
63 |
| - t.Fail() |
64 |
| - } |
| 46 | + assert.Equal(t, "custom.registry", registry) |
| 47 | + assert.Equal(t, "ubuntu", repository) |
| 48 | + assert.Equal(t, "12.04", tag) |
| 49 | + |
65 | 50 | dockerImage := DockerImage{
|
66 | 51 | Registry: registry,
|
67 | 52 | Repository: repository,
|
68 | 53 | Tag: tag,
|
69 | 54 | }
|
70 |
| - if "custom.registry/ubuntu:12.04" != dockerImage.String() { |
71 |
| - t.Fail() |
72 |
| - } |
73 |
| - |
| 55 | + assert.Equal(t, "custom.registry/ubuntu:12.04", dockerImage.String()) |
74 | 56 | }
|
75 | 57 |
|
76 | 58 | func TestSplitDockerImageWithRepositoryAndTag(t *testing.T) {
|
77 | 59 | registry, repository, tag := splitDockerImage("ubuntu:12.04")
|
78 | 60 |
|
79 |
| - if registry != "" { |
80 |
| - t.Fail() |
81 |
| - } |
| 61 | + assert.Equal(t, "", registry) |
| 62 | + assert.Equal(t, "ubuntu", repository) |
| 63 | + assert.Equal(t, "12.04", tag) |
82 | 64 |
|
83 |
| - if repository != "ubuntu" { |
84 |
| - t.Fail() |
85 |
| - } |
86 |
| - |
87 |
| - if tag != "12.04" { |
88 |
| - t.Fail() |
89 |
| - } |
90 | 65 | dockerImage := DockerImage{
|
91 | 66 | Registry: registry,
|
92 | 67 | Repository: repository,
|
93 | 68 | Tag: tag,
|
94 | 69 | }
|
95 |
| - if "ubuntu:12.04" != dockerImage.String() { |
96 |
| - t.Fail() |
97 |
| - } |
| 70 | + assert.Equal(t, "ubuntu:12.04", dockerImage.String()) |
98 | 71 | }
|
99 | 72 |
|
100 | 73 | func TestSplitDockerImageWithPrivateRegistryPath(t *testing.T) {
|
101 | 74 | registry, repository, tag := splitDockerImage("localhost:8888/ubuntu/foo:12.04")
|
102 | 75 |
|
103 |
| - if registry != "localhost:8888" { |
104 |
| - t.Fail() |
105 |
| - } |
106 |
| - |
107 |
| - if repository != "ubuntu/foo" { |
108 |
| - t.Fail() |
109 |
| - } |
| 76 | + assert.Equal(t, "localhost:8888", registry) |
| 77 | + assert.Equal(t, "ubuntu/foo", repository) |
| 78 | + assert.Equal(t, "12.04", tag) |
110 | 79 |
|
111 |
| - if tag != "12.04" { |
112 |
| - t.Fail() |
113 |
| - } |
114 | 80 | dockerImage := DockerImage{
|
115 | 81 | Registry: registry,
|
116 | 82 | Repository: repository,
|
117 | 83 | Tag: tag,
|
118 | 84 | }
|
119 |
| - if "localhost:8888/ubuntu/foo:12.04" != dockerImage.String() { |
120 |
| - t.Fail() |
121 |
| - } |
| 85 | + assert.Equal(t, "localhost:8888/ubuntu/foo:12.04", dockerImage.String()) |
122 | 86 | }
|
123 | 87 | func TestSplitDockerImageWithLocalRepositoryAndTag(t *testing.T) {
|
124 | 88 | registry, repository, tag := splitDockerImage("localhost:8888/ubuntu:12.04")
|
125 | 89 |
|
126 |
| - if registry != "localhost:8888" { |
127 |
| - t.Fatalf("registry does not match: expected %s got %s", "localhost:8888", registry) |
128 |
| - } |
129 |
| - |
130 |
| - if repository != "ubuntu" { |
131 |
| - t.Fatalf("repository does not match: expected %s got %s", "ubuntu", repository) |
132 |
| - } |
| 90 | + assert.Equal(t, "localhost:8888", registry) |
| 91 | + assert.Equal(t, "ubuntu", repository) |
| 92 | + assert.Equal(t, "12.04", tag) |
133 | 93 |
|
134 |
| - if tag != "12.04" { |
135 |
| - t.Fatalf("tag does not match: expected %s got %s", "12.04", tag) |
136 |
| - } |
137 | 94 | dockerImage := DockerImage{
|
138 | 95 | Registry: registry,
|
139 | 96 | Repository: repository,
|
140 | 97 | Tag: tag,
|
141 | 98 | }
|
142 |
| - if "localhost:8888/ubuntu:12.04" != dockerImage.String() { |
143 |
| - t.Fail() |
144 |
| - } |
145 |
| - |
| 99 | + assert.Equal(t, "localhost:8888/ubuntu:12.04", dockerImage.String()) |
146 | 100 | }
|
147 | 101 |
|
148 | 102 | func TestParseHostUnix(t *testing.T) {
|
149 | 103 | proto, addr, err := parseHost("unix:///var/run/docker.sock")
|
150 |
| - if err != nil { |
151 |
| - t.Fatalf("%s", err) |
152 |
| - } |
153 |
| - if proto != "unix" || addr != "/var/run/docker.sock" { |
154 |
| - t.Fatal("failed to parse unix:///var/run/docker.sock") |
155 |
| - } |
| 104 | + assert.NoError(t, err) |
| 105 | + assert.Equal(t, "unix", proto, "failed to parse unix:///var/run/docker.sock") |
| 106 | + assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse unix:///var/run/docker.sock") |
156 | 107 | }
|
157 | 108 |
|
158 | 109 | func TestParseHostUnixDefault(t *testing.T) {
|
159 | 110 | proto, addr, err := parseHost("")
|
160 |
| - if err != nil { |
161 |
| - t.Fatalf("%s", err) |
162 |
| - } |
163 |
| - if proto != "unix" || addr != "/var/run/docker.sock" { |
164 |
| - t.Fatal("failed to parse ''") |
165 |
| - } |
| 111 | + assert.NoError(t, err) |
| 112 | + assert.Equal(t, "unix", proto, "failed to parse ''") |
| 113 | + assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse ''") |
166 | 114 | }
|
167 | 115 |
|
168 | 116 | func TestParseHostUnixDefaultNoPath(t *testing.T) {
|
169 | 117 | proto, addr, err := parseHost("unix://")
|
170 |
| - if err != nil { |
171 |
| - t.Fatalf("%s", err) |
172 |
| - } |
173 |
| - if proto != "unix" || addr != "/var/run/docker.sock" { |
174 |
| - t.Fatal("failed to parse unix://") |
175 |
| - } |
| 118 | + assert.NoError(t, err) |
| 119 | + assert.Equal(t, "unix", proto, "failed to parse unix://") |
| 120 | + assert.Equal(t, "/var/run/docker.sock", addr, "failed to parse unix://") |
176 | 121 | }
|
177 | 122 |
|
178 | 123 | func TestParseHostTCP(t *testing.T) {
|
179 | 124 | proto, addr, err := parseHost("tcp://127.0.0.1:4243")
|
180 |
| - if err != nil { |
181 |
| - t.Fatalf("%s", err) |
182 |
| - } |
183 |
| - if proto != "tcp" || addr != "127.0.0.1:4243" { |
184 |
| - t.Fatal("failed to parse tcp://127.0.0.1:4243") |
185 |
| - } |
| 125 | + assert.NoError(t, err) |
| 126 | + assert.Equal(t, "tcp", proto, "failed to parse tcp://127.0.0.1:4243") |
| 127 | + assert.Equal(t, "127.0.0.1:4243", addr, "failed to parse tcp://127.0.0.1:4243") |
186 | 128 | }
|
187 | 129 |
|
188 | 130 | func TestParseHostTCPDefault(t *testing.T) {
|
189 | 131 | proto, addr, err := parseHost("tcp://:4243")
|
190 |
| - if err != nil { |
191 |
| - t.Fatalf("%s", err) |
| 132 | + assert.NoError(t, err) |
| 133 | + assert.Equal(t, "tcp", proto, "failed to parse tcp://:4243") |
| 134 | + assert.Equal(t, "127.0.0.1:4243", addr, "failed to parse tcp://:4243") |
| 135 | +} |
| 136 | + |
| 137 | +func TestParseHostSystemd(t *testing.T) { |
| 138 | + proto, addr, err := parseHost("fd://") |
| 139 | + assert.NoError(t, err) |
| 140 | + assert.Equal(t, "fd", proto, "failed to parse fd://") |
| 141 | + assert.Equal(t, "fd://", addr, "failed to parse fd://") |
| 142 | +} |
| 143 | + |
| 144 | +func assertParseHostError(t *testing.T, address string) { |
| 145 | + proto, addr, err := parseHost(address) |
| 146 | + message := fmt.Sprintf("should have failed to parse %v", address) |
| 147 | + assert.Error(t, err, message) |
| 148 | + assert.Equal(t, "", proto, message) |
| 149 | + assert.Equal(t, "", addr, message) |
| 150 | +} |
| 151 | + |
| 152 | +func TestParseHostTCPNoAddressError(t *testing.T) { |
| 153 | + assertParseHostError(t, "tcp://") |
| 154 | +} |
| 155 | + |
| 156 | +func TestParseHostTCPIncorrectBindAddressError(t *testing.T) { |
| 157 | + incorrectBindAdresses := []string{ |
| 158 | + "tcp://127.0.0.1:4243:80", |
| 159 | + "tcp://127.0.0.1:", |
| 160 | + "tcp://127.0.0.1", |
| 161 | + } |
| 162 | + |
| 163 | + for _, address := range incorrectBindAdresses { |
| 164 | + assertParseHostError(t, address) |
192 | 165 | }
|
193 |
| - if proto != "tcp" || addr != "127.0.0.1:4243" { |
194 |
| - t.Fatal("failed to parse unix:///var/run/docker.sock") |
| 166 | +} |
| 167 | + |
| 168 | +func TestParseHostWrongProtocolError(t *testing.T) { |
| 169 | + assertParseHostError(t, "foo://") |
| 170 | +} |
| 171 | + |
| 172 | +func TestTlsEnabled(t *testing.T) { |
| 173 | + tls := tlsEnabled("foo", "bar", "baz") |
| 174 | + assert.False(t, tls) |
| 175 | + |
| 176 | + filepaths := map[string]string{ |
| 177 | + "cert": "", |
| 178 | + "caCert": "", |
| 179 | + "key": "", |
| 180 | + } |
| 181 | + // Create temporary files |
| 182 | + for key := range filepaths { |
| 183 | + file, err := ioutil.TempFile("", key) |
| 184 | + if err != nil { |
| 185 | + log.Fatal(err) |
| 186 | + } |
| 187 | + defer os.Remove(file.Name()) |
| 188 | + filepaths[key] = file.Name() |
195 | 189 | }
|
| 190 | + |
| 191 | + tls = tlsEnabled(filepaths["cert"], filepaths["caCert"], filepaths["key"]) |
| 192 | + assert.True(t, tls) |
196 | 193 | }
|
0 commit comments