test
[platform/upstream/docker-engine.git] / registry / config_test.go
1 package registry
2
3 import (
4         "reflect"
5         "sort"
6         "strings"
7         "testing"
8 )
9
10 func TestLoadAllowNondistributableArtifacts(t *testing.T) {
11         testCases := []struct {
12                 registries []string
13                 cidrStrs   []string
14                 hostnames  []string
15                 err        string
16         }{
17                 {
18                         registries: []string{"1.2.3.0/24"},
19                         cidrStrs:   []string{"1.2.3.0/24"},
20                 },
21                 {
22                         registries: []string{"2001:db8::/120"},
23                         cidrStrs:   []string{"2001:db8::/120"},
24                 },
25                 {
26                         registries: []string{"127.0.0.1"},
27                         hostnames:  []string{"127.0.0.1"},
28                 },
29                 {
30                         registries: []string{"127.0.0.1:8080"},
31                         hostnames:  []string{"127.0.0.1:8080"},
32                 },
33                 {
34                         registries: []string{"2001:db8::1"},
35                         hostnames:  []string{"2001:db8::1"},
36                 },
37                 {
38                         registries: []string{"[2001:db8::1]:80"},
39                         hostnames:  []string{"[2001:db8::1]:80"},
40                 },
41                 {
42                         registries: []string{"[2001:db8::1]:80"},
43                         hostnames:  []string{"[2001:db8::1]:80"},
44                 },
45                 {
46                         registries: []string{"1.2.3.0/24", "2001:db8::/120", "127.0.0.1", "127.0.0.1:8080"},
47                         cidrStrs:   []string{"1.2.3.0/24", "2001:db8::/120"},
48                         hostnames:  []string{"127.0.0.1", "127.0.0.1:8080"},
49                 },
50
51                 {
52                         registries: []string{"http://mytest.com"},
53                         err:        "allow-nondistributable-artifacts registry http://mytest.com should not contain '://'",
54                 },
55                 {
56                         registries: []string{"https://mytest.com"},
57                         err:        "allow-nondistributable-artifacts registry https://mytest.com should not contain '://'",
58                 },
59                 {
60                         registries: []string{"HTTP://mytest.com"},
61                         err:        "allow-nondistributable-artifacts registry HTTP://mytest.com should not contain '://'",
62                 },
63                 {
64                         registries: []string{"svn://mytest.com"},
65                         err:        "allow-nondistributable-artifacts registry svn://mytest.com should not contain '://'",
66                 },
67                 {
68                         registries: []string{"-invalid-registry"},
69                         err:        "Cannot begin or end with a hyphen",
70                 },
71                 {
72                         registries: []string{`mytest-.com`},
73                         err:        `allow-nondistributable-artifacts registry mytest-.com is not valid: invalid host "mytest-.com"`,
74                 },
75                 {
76                         registries: []string{`1200:0000:AB00:1234:0000:2552:7777:1313:8080`},
77                         err:        `allow-nondistributable-artifacts registry 1200:0000:AB00:1234:0000:2552:7777:1313:8080 is not valid: invalid host "1200:0000:AB00:1234:0000:2552:7777:1313:8080"`,
78                 },
79                 {
80                         registries: []string{`mytest.com:500000`},
81                         err:        `allow-nondistributable-artifacts registry mytest.com:500000 is not valid: invalid port "500000"`,
82                 },
83                 {
84                         registries: []string{`"mytest.com"`},
85                         err:        `allow-nondistributable-artifacts registry "mytest.com" is not valid: invalid host "\"mytest.com\""`,
86                 },
87                 {
88                         registries: []string{`"mytest.com:5000"`},
89                         err:        `allow-nondistributable-artifacts registry "mytest.com:5000" is not valid: invalid host "\"mytest.com"`,
90                 },
91         }
92         for _, testCase := range testCases {
93                 config := newServiceConfig(ServiceOptions{})
94                 err := config.LoadAllowNondistributableArtifacts(testCase.registries)
95                 if testCase.err == "" {
96                         if err != nil {
97                                 t.Fatalf("expect no error, got '%s'", err)
98                         }
99
100                         cidrStrs := []string{}
101                         for _, c := range config.AllowNondistributableArtifactsCIDRs {
102                                 cidrStrs = append(cidrStrs, c.String())
103                         }
104
105                         sort.Strings(testCase.cidrStrs)
106                         sort.Strings(cidrStrs)
107                         if (len(testCase.cidrStrs) > 0 || len(cidrStrs) > 0) && !reflect.DeepEqual(testCase.cidrStrs, cidrStrs) {
108                                 t.Fatalf("expect AllowNondistributableArtifactsCIDRs to be '%+v', got '%+v'", testCase.cidrStrs, cidrStrs)
109                         }
110
111                         sort.Strings(testCase.hostnames)
112                         sort.Strings(config.AllowNondistributableArtifactsHostnames)
113                         if (len(testCase.hostnames) > 0 || len(config.AllowNondistributableArtifactsHostnames) > 0) && !reflect.DeepEqual(testCase.hostnames, config.AllowNondistributableArtifactsHostnames) {
114                                 t.Fatalf("expect AllowNondistributableArtifactsHostnames to be '%+v', got '%+v'", testCase.hostnames, config.AllowNondistributableArtifactsHostnames)
115                         }
116                 } else {
117                         if err == nil {
118                                 t.Fatalf("expect error '%s', got no error", testCase.err)
119                         }
120                         if !strings.Contains(err.Error(), testCase.err) {
121                                 t.Fatalf("expect error '%s', got '%s'", testCase.err, err)
122                         }
123                 }
124         }
125 }
126
127 func TestValidateMirror(t *testing.T) {
128         valid := []string{
129                 "http://mirror-1.com",
130                 "http://mirror-1.com/",
131                 "https://mirror-1.com",
132                 "https://mirror-1.com/",
133                 "http://localhost",
134                 "https://localhost",
135                 "http://localhost:5000",
136                 "https://localhost:5000",
137                 "http://127.0.0.1",
138                 "https://127.0.0.1",
139                 "http://127.0.0.1:5000",
140                 "https://127.0.0.1:5000",
141         }
142
143         invalid := []string{
144                 "!invalid!://%as%",
145                 "ftp://mirror-1.com",
146                 "http://mirror-1.com/?q=foo",
147                 "http://mirror-1.com/v1/",
148                 "http://mirror-1.com/v1/?q=foo",
149                 "http://mirror-1.com/v1/?q=foo#frag",
150                 "http://mirror-1.com?q=foo",
151                 "https://mirror-1.com#frag",
152                 "https://mirror-1.com/#frag",
153                 "http://foo:bar@mirror-1.com/",
154                 "https://mirror-1.com/v1/",
155                 "https://mirror-1.com/v1/#",
156                 "https://mirror-1.com?q",
157         }
158
159         for _, address := range valid {
160                 if ret, err := ValidateMirror(address); err != nil || ret == "" {
161                         t.Errorf("ValidateMirror(`"+address+"`) got %s %s", ret, err)
162                 }
163         }
164
165         for _, address := range invalid {
166                 if ret, err := ValidateMirror(address); err == nil || ret != "" {
167                         t.Errorf("ValidateMirror(`"+address+"`) got %s %s", ret, err)
168                 }
169         }
170 }
171
172 func TestLoadInsecureRegistries(t *testing.T) {
173         testCases := []struct {
174                 registries []string
175                 index      string
176                 err        string
177         }{
178                 {
179                         registries: []string{"127.0.0.1"},
180                         index:      "127.0.0.1",
181                 },
182                 {
183                         registries: []string{"127.0.0.1:8080"},
184                         index:      "127.0.0.1:8080",
185                 },
186                 {
187                         registries: []string{"2001:db8::1"},
188                         index:      "2001:db8::1",
189                 },
190                 {
191                         registries: []string{"[2001:db8::1]:80"},
192                         index:      "[2001:db8::1]:80",
193                 },
194                 {
195                         registries: []string{"http://mytest.com"},
196                         index:      "mytest.com",
197                 },
198                 {
199                         registries: []string{"https://mytest.com"},
200                         index:      "mytest.com",
201                 },
202                 {
203                         registries: []string{"HTTP://mytest.com"},
204                         index:      "mytest.com",
205                 },
206                 {
207                         registries: []string{"svn://mytest.com"},
208                         err:        "insecure registry svn://mytest.com should not contain '://'",
209                 },
210                 {
211                         registries: []string{"-invalid-registry"},
212                         err:        "Cannot begin or end with a hyphen",
213                 },
214                 {
215                         registries: []string{`mytest-.com`},
216                         err:        `insecure registry mytest-.com is not valid: invalid host "mytest-.com"`,
217                 },
218                 {
219                         registries: []string{`1200:0000:AB00:1234:0000:2552:7777:1313:8080`},
220                         err:        `insecure registry 1200:0000:AB00:1234:0000:2552:7777:1313:8080 is not valid: invalid host "1200:0000:AB00:1234:0000:2552:7777:1313:8080"`,
221                 },
222                 {
223                         registries: []string{`mytest.com:500000`},
224                         err:        `insecure registry mytest.com:500000 is not valid: invalid port "500000"`,
225                 },
226                 {
227                         registries: []string{`"mytest.com"`},
228                         err:        `insecure registry "mytest.com" is not valid: invalid host "\"mytest.com\""`,
229                 },
230                 {
231                         registries: []string{`"mytest.com:5000"`},
232                         err:        `insecure registry "mytest.com:5000" is not valid: invalid host "\"mytest.com"`,
233                 },
234         }
235         for _, testCase := range testCases {
236                 config := newServiceConfig(ServiceOptions{})
237                 err := config.LoadInsecureRegistries(testCase.registries)
238                 if testCase.err == "" {
239                         if err != nil {
240                                 t.Fatalf("expect no error, got '%s'", err)
241                         }
242                         match := false
243                         for index := range config.IndexConfigs {
244                                 if index == testCase.index {
245                                         match = true
246                                 }
247                         }
248                         if !match {
249                                 t.Fatalf("expect index configs to contain '%s', got %+v", testCase.index, config.IndexConfigs)
250                         }
251                 } else {
252                         if err == nil {
253                                 t.Fatalf("expect error '%s', got no error", testCase.err)
254                         }
255                         if !strings.Contains(err.Error(), testCase.err) {
256                                 t.Fatalf("expect error '%s', got '%s'", testCase.err, err)
257                         }
258                 }
259         }
260 }