github.com/xuyutom/docker@v1.6.0/pkg/parsers/parsers_test.go (about) 1 package parsers 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestParseHost(t *testing.T) { 9 var ( 10 defaultHttpHost = "127.0.0.1" 11 defaultUnix = "/var/run/docker.sock" 12 ) 13 if addr, err := ParseHost(defaultHttpHost, defaultUnix, "0.0.0.0"); err == nil { 14 t.Errorf("tcp 0.0.0.0 address expected error return, but err == nil, got %s", addr) 15 } 16 if addr, err := ParseHost(defaultHttpHost, defaultUnix, "tcp://"); err == nil { 17 t.Errorf("default tcp:// address expected error return, but err == nil, got %s", addr) 18 } 19 if addr, err := ParseHost(defaultHttpHost, defaultUnix, "0.0.0.1:5555"); err != nil || addr != "tcp://0.0.0.1:5555" { 20 t.Errorf("0.0.0.1:5555 -> expected tcp://0.0.0.1:5555, got %s", addr) 21 } 22 if addr, err := ParseHost(defaultHttpHost, defaultUnix, ":6666"); err != nil || addr != "tcp://127.0.0.1:6666" { 23 t.Errorf(":6666 -> expected tcp://127.0.0.1:6666, got %s", addr) 24 } 25 if addr, err := ParseHost(defaultHttpHost, defaultUnix, "tcp://:7777"); err != nil || addr != "tcp://127.0.0.1:7777" { 26 t.Errorf("tcp://:7777 -> expected tcp://127.0.0.1:7777, got %s", addr) 27 } 28 if addr, err := ParseHost(defaultHttpHost, defaultUnix, ""); err != nil || addr != "unix:///var/run/docker.sock" { 29 t.Errorf("empty argument -> expected unix:///var/run/docker.sock, got %s", addr) 30 } 31 if addr, err := ParseHost(defaultHttpHost, defaultUnix, "unix:///var/run/docker.sock"); err != nil || addr != "unix:///var/run/docker.sock" { 32 t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr) 33 } 34 if addr, err := ParseHost(defaultHttpHost, defaultUnix, "unix://"); err != nil || addr != "unix:///var/run/docker.sock" { 35 t.Errorf("unix:///var/run/docker.sock -> expected unix:///var/run/docker.sock, got %s", addr) 36 } 37 if addr, err := ParseHost(defaultHttpHost, defaultUnix, "udp://127.0.0.1"); err == nil { 38 t.Errorf("udp protocol address expected error return, but err == nil. Got %s", addr) 39 } 40 if addr, err := ParseHost(defaultHttpHost, defaultUnix, "udp://127.0.0.1:2375"); err == nil { 41 t.Errorf("udp protocol address expected error return, but err == nil. Got %s", addr) 42 } 43 } 44 45 func TestParseRepositoryTag(t *testing.T) { 46 if repo, tag := ParseRepositoryTag("root"); repo != "root" || tag != "" { 47 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "root", "", repo, tag) 48 } 49 if repo, tag := ParseRepositoryTag("root:tag"); repo != "root" || tag != "tag" { 50 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "root", "tag", repo, tag) 51 } 52 if repo, digest := ParseRepositoryTag("root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "root" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { 53 t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "root", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest) 54 } 55 if repo, tag := ParseRepositoryTag("user/repo"); repo != "user/repo" || tag != "" { 56 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "", repo, tag) 57 } 58 if repo, tag := ParseRepositoryTag("user/repo:tag"); repo != "user/repo" || tag != "tag" { 59 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "tag", repo, tag) 60 } 61 if repo, digest := ParseRepositoryTag("user/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "user/repo" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { 62 t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "user/repo", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest) 63 } 64 if repo, tag := ParseRepositoryTag("url:5000/repo"); repo != "url:5000/repo" || tag != "" { 65 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "", repo, tag) 66 } 67 if repo, tag := ParseRepositoryTag("url:5000/repo:tag"); repo != "url:5000/repo" || tag != "tag" { 68 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "tag", repo, tag) 69 } 70 if repo, digest := ParseRepositoryTag("url:5000/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); repo != "url:5000/repo" || digest != "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" { 71 t.Errorf("Expected repo: '%s' and digest: '%s', got '%s' and '%s'", "url:5000/repo", "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", repo, digest) 72 } 73 } 74 75 func TestParsePortMapping(t *testing.T) { 76 data, err := PartParser("ip:public:private", "192.168.1.1:80:8080") 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 if len(data) != 3 { 82 t.FailNow() 83 } 84 if data["ip"] != "192.168.1.1" { 85 t.Fail() 86 } 87 if data["public"] != "80" { 88 t.Fail() 89 } 90 if data["private"] != "8080" { 91 t.Fail() 92 } 93 } 94 95 func TestParsePortRange(t *testing.T) { 96 if start, end, err := ParsePortRange("8000-8080"); err != nil || start != 8000 || end != 8080 { 97 t.Fatalf("Error: %s or Expecting {start,end} values {8000,8080} but found {%d,%d}.", err, start, end) 98 } 99 } 100 101 func TestParsePortRangeIncorrectRange(t *testing.T) { 102 if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "Invalid range specified for the Port") { 103 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 104 } 105 } 106 107 func TestParsePortRangeIncorrectEndRange(t *testing.T) { 108 if _, _, err := ParsePortRange("8000-a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 109 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 110 } 111 112 if _, _, err := ParsePortRange("8000-30a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 113 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 114 } 115 } 116 117 func TestParsePortRangeIncorrectStartRange(t *testing.T) { 118 if _, _, err := ParsePortRange("a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 119 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 120 } 121 122 if _, _, err := ParsePortRange("30a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 123 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 124 } 125 }