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