github.com/goern/docker@v1.9.0-rc1/pkg/parsers/parsers_test.go (about) 1 package parsers 2 3 import ( 4 "reflect" 5 "runtime" 6 "strings" 7 "testing" 8 ) 9 10 func TestParseDockerDaemonHost(t *testing.T) { 11 var ( 12 defaultHTTPHost = "tcp://127.0.0.1:2376" 13 defaultUnix = "/var/run/docker.sock" 14 defaultHOST = "unix:///var/run/docker.sock" 15 ) 16 if runtime.GOOS == "windows" { 17 defaultHOST = defaultHTTPHost 18 } 19 invalids := map[string]string{ 20 "0.0.0.0": "Invalid bind address format: 0.0.0.0", 21 "tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d", 22 "tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path", 23 "udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1", 24 "udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375", 25 "tcp://unix:///run/docker.sock": "Invalid bind address format: unix", 26 "tcp": "Invalid bind address format: tcp", 27 "unix": "Invalid bind address format: unix", 28 "fd": "Invalid bind address format: fd", 29 } 30 valids := map[string]string{ 31 "0.0.0.1:": "tcp://0.0.0.1:2376", 32 "0.0.0.1:5555": "tcp://0.0.0.1:5555", 33 "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path", 34 ":6666": "tcp://127.0.0.1:6666", 35 ":6666/path": "tcp://127.0.0.1:6666/path", 36 "": defaultHOST, 37 " ": defaultHOST, 38 " ": defaultHOST, 39 "tcp://": defaultHTTPHost, 40 "tcp://:7777": "tcp://127.0.0.1:7777", 41 "tcp://:7777/path": "tcp://127.0.0.1:7777/path", 42 " tcp://:7777/path ": "tcp://127.0.0.1:7777/path", 43 "unix:///run/docker.sock": "unix:///run/docker.sock", 44 "unix://": "unix:///var/run/docker.sock", 45 "fd://": "fd://", 46 "fd://something": "fd://something", 47 } 48 for invalidAddr, expectedError := range invalids { 49 if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, invalidAddr); err == nil || err.Error() != expectedError { 50 t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr) 51 } 52 } 53 for validAddr, expectedAddr := range valids { 54 if addr, err := ParseDockerDaemonHost(defaultHTTPHost, defaultUnix, validAddr); err != nil || addr != expectedAddr { 55 t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr) 56 } 57 } 58 } 59 60 func TestParseTCP(t *testing.T) { 61 var ( 62 defaultHTTPHost = "tcp://127.0.0.1:2376" 63 ) 64 invalids := map[string]string{ 65 "0.0.0.0": "Invalid bind address format: 0.0.0.0", 66 "tcp:a.b.c.d": "Invalid bind address format: tcp:a.b.c.d", 67 "tcp:a.b.c.d/path": "Invalid bind address format: tcp:a.b.c.d/path", 68 "udp://127.0.0.1": "Invalid proto, expected tcp: udp://127.0.0.1", 69 "udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375", 70 } 71 valids := map[string]string{ 72 "": defaultHTTPHost, 73 "tcp://": defaultHTTPHost, 74 "0.0.0.1:": "tcp://0.0.0.1:2376", 75 "0.0.0.1:5555": "tcp://0.0.0.1:5555", 76 "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path", 77 ":6666": "tcp://127.0.0.1:6666", 78 ":6666/path": "tcp://127.0.0.1:6666/path", 79 "tcp://:7777": "tcp://127.0.0.1:7777", 80 "tcp://:7777/path": "tcp://127.0.0.1:7777/path", 81 } 82 for invalidAddr, expectedError := range invalids { 83 if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError { 84 t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr) 85 } 86 } 87 for validAddr, expectedAddr := range valids { 88 if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr { 89 t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr) 90 } 91 } 92 } 93 94 func TestParseInvalidUnixAddrInvalid(t *testing.T) { 95 if _, err := ParseUnixAddr("tcp://127.0.0.1", "unix:///var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" { 96 t.Fatalf("Expected an error, got %v", err) 97 } 98 if _, err := ParseUnixAddr("unix://tcp://127.0.0.1", "/var/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" { 99 t.Fatalf("Expected an error, got %v", err) 100 } 101 if v, err := ParseUnixAddr("", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" { 102 t.Fatalf("Expected an %v, got %v", v, "unix:///var/run/docker.sock") 103 } 104 } 105 106 func TestParseRepositoryTag(t *testing.T) { 107 if repo, tag := ParseRepositoryTag("root"); repo != "root" || tag != "" { 108 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "root", "", repo, tag) 109 } 110 if repo, tag := ParseRepositoryTag("root:tag"); repo != "root" || tag != "tag" { 111 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "root", "tag", repo, tag) 112 } 113 if repo, digest := ParseRepositoryTag("root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "root" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { 114 t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "root", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest) 115 } 116 if repo, tag := ParseRepositoryTag("user/repo"); repo != "user/repo" || tag != "" { 117 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "", repo, tag) 118 } 119 if repo, tag := ParseRepositoryTag("user/repo:tag"); repo != "user/repo" || tag != "tag" { 120 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "tag", repo, tag) 121 } 122 if repo, digest := ParseRepositoryTag("user/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "user/repo" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { 123 t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "user/repo", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest) 124 } 125 if repo, tag := ParseRepositoryTag("url:5000/repo"); repo != "url:5000/repo" || tag != "" { 126 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "", repo, tag) 127 } 128 if repo, tag := ParseRepositoryTag("url:5000/repo:tag"); repo != "url:5000/repo" || tag != "tag" { 129 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "tag", repo, tag) 130 } 131 if repo, digest := ParseRepositoryTag("url:5000/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "url:5000/repo" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { 132 t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "url:5000/repo", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest) 133 } 134 } 135 136 func TestParseKeyValueOpt(t *testing.T) { 137 invalids := map[string]string{ 138 "": "Unable to parse key/value option: ", 139 "key": "Unable to parse key/value option: key", 140 } 141 for invalid, expectedError := range invalids { 142 if _, _, err := ParseKeyValueOpt(invalid); err == nil || err.Error() != expectedError { 143 t.Fatalf("Expected error %v for %v, got %v", expectedError, invalid, err) 144 } 145 } 146 valids := map[string][]string{ 147 "key=value": {"key", "value"}, 148 " key = value ": {"key", "value"}, 149 "key=value1=value2": {"key", "value1=value2"}, 150 " key = value1 = value2 ": {"key", "value1 = value2"}, 151 } 152 for valid, expectedKeyValue := range valids { 153 key, value, err := ParseKeyValueOpt(valid) 154 if err != nil { 155 t.Fatal(err) 156 } 157 if key != expectedKeyValue[0] || value != expectedKeyValue[1] { 158 t.Fatalf("Expected {%v: %v} got {%v: %v}", expectedKeyValue[0], expectedKeyValue[1], key, value) 159 } 160 } 161 } 162 163 func TestParsePortRange(t *testing.T) { 164 if start, end, err := ParsePortRange("8000-8080"); err != nil || start != 8000 || end != 8080 { 165 t.Fatalf("Error: %s or Expecting {start,end} values {8000,8080} but found {%d,%d}.", err, start, end) 166 } 167 } 168 169 func TestParsePortRangeEmpty(t *testing.T) { 170 if _, _, err := ParsePortRange(""); err == nil || err.Error() != "Empty string specified for ports." { 171 t.Fatalf("Expected error 'Empty string specified for ports.', got %v", err) 172 } 173 } 174 175 func TestParsePortRangeWithNoRange(t *testing.T) { 176 start, end, err := ParsePortRange("8080") 177 if err != nil { 178 t.Fatal(err) 179 } 180 if start != 8080 || end != 8080 { 181 t.Fatalf("Expected start and end to be the same and equal to 8080, but were %v and %v", start, end) 182 } 183 } 184 185 func TestParsePortRangeIncorrectRange(t *testing.T) { 186 if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "Invalid range specified for the Port") { 187 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 188 } 189 } 190 191 func TestParsePortRangeIncorrectEndRange(t *testing.T) { 192 if _, _, err := ParsePortRange("8000-a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 193 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 194 } 195 196 if _, _, err := ParsePortRange("8000-30a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 197 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 198 } 199 } 200 201 func TestParsePortRangeIncorrectStartRange(t *testing.T) { 202 if _, _, err := ParsePortRange("a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 203 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 204 } 205 206 if _, _, err := ParsePortRange("30a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 207 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 208 } 209 } 210 211 func TestParseLink(t *testing.T) { 212 name, alias, err := ParseLink("name:alias") 213 if err != nil { 214 t.Fatalf("Expected not to error out on a valid name:alias format but got: %v", err) 215 } 216 if name != "name" { 217 t.Fatalf("Link name should have been name, got %s instead", name) 218 } 219 if alias != "alias" { 220 t.Fatalf("Link alias should have been alias, got %s instead", alias) 221 } 222 // short format definition 223 name, alias, err = ParseLink("name") 224 if err != nil { 225 t.Fatalf("Expected not to error out on a valid name only format but got: %v", err) 226 } 227 if name != "name" { 228 t.Fatalf("Link name should have been name, got %s instead", name) 229 } 230 if alias != "name" { 231 t.Fatalf("Link alias should have been name, got %s instead", alias) 232 } 233 // empty string link definition is not allowed 234 if _, _, err := ParseLink(""); err == nil || !strings.Contains(err.Error(), "empty string specified for links") { 235 t.Fatalf("Expected error 'empty string specified for links' but got: %v", err) 236 } 237 // more than two colons are not allowed 238 if _, _, err := ParseLink("link:alias:wrong"); err == nil || !strings.Contains(err.Error(), "bad format for links: link:alias:wrong") { 239 t.Fatalf("Expected error 'bad format for links: link:alias:wrong' but got: %v", err) 240 } 241 } 242 243 func TestParseUintList(t *testing.T) { 244 valids := map[string]map[int]bool{ 245 "": {}, 246 "7": {7: true}, 247 "1-6": {1: true, 2: true, 3: true, 4: true, 5: true, 6: true}, 248 "0-7": {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true}, 249 "0,3-4,7,8-10": {0: true, 3: true, 4: true, 7: true, 8: true, 9: true, 10: true}, 250 "0-0,0,1-4": {0: true, 1: true, 2: true, 3: true, 4: true}, 251 "03,1-3": {1: true, 2: true, 3: true}, 252 "3,2,1": {1: true, 2: true, 3: true}, 253 "0-2,3,1": {0: true, 1: true, 2: true, 3: true}, 254 } 255 for k, v := range valids { 256 out, err := ParseUintList(k) 257 if err != nil { 258 t.Fatalf("Expected not to fail, got %v", err) 259 } 260 if !reflect.DeepEqual(out, v) { 261 t.Fatalf("Expected %v, got %v", v, out) 262 } 263 } 264 265 invalids := []string{ 266 "this", 267 "1--", 268 "1-10,,10", 269 "10-1", 270 "-1", 271 "-1,0", 272 } 273 for _, v := range invalids { 274 if out, err := ParseUintList(v); err == nil { 275 t.Fatalf("Expected failure with %s but got %v", v, out) 276 } 277 } 278 }