github.com/pmorton/docker@v1.5.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, tag := ParseRepositoryTag("user/repo"); repo != "user/repo" || tag != "" { 53 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "", repo, tag) 54 } 55 if repo, tag := ParseRepositoryTag("user/repo:tag"); repo != "user/repo" || tag != "tag" { 56 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "user/repo", "tag", repo, tag) 57 } 58 if repo, tag := ParseRepositoryTag("url:5000/repo"); repo != "url:5000/repo" || tag != "" { 59 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "", repo, tag) 60 } 61 if repo, tag := ParseRepositoryTag("url:5000/repo:tag"); repo != "url:5000/repo" || tag != "tag" { 62 t.Errorf("Expected repo: '%s' and tag: '%s', got '%s' and '%s'", "url:5000/repo", "tag", repo, tag) 63 } 64 } 65 66 func TestParsePortMapping(t *testing.T) { 67 data, err := PartParser("ip:public:private", "192.168.1.1:80:8080") 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 if len(data) != 3 { 73 t.FailNow() 74 } 75 if data["ip"] != "192.168.1.1" { 76 t.Fail() 77 } 78 if data["public"] != "80" { 79 t.Fail() 80 } 81 if data["private"] != "8080" { 82 t.Fail() 83 } 84 } 85 86 func TestParsePortRange(t *testing.T) { 87 if start, end, err := ParsePortRange("8000-8080"); err != nil || start != 8000 || end != 8080 { 88 t.Fatalf("Error: %s or Expecting {start,end} values {8000,8080} but found {%d,%d}.", err, start, end) 89 } 90 } 91 92 func TestParsePortRangeIncorrectRange(t *testing.T) { 93 if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "Invalid range specified for the Port") { 94 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 95 } 96 } 97 98 func TestParsePortRangeIncorrectEndRange(t *testing.T) { 99 if _, _, err := ParsePortRange("8000-a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 100 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 101 } 102 103 if _, _, err := ParsePortRange("8000-30a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 104 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 105 } 106 } 107 108 func TestParsePortRangeIncorrectStartRange(t *testing.T) { 109 if _, _, err := ParsePortRange("a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 110 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 111 } 112 113 if _, _, err := ParsePortRange("30a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") { 114 t.Fatalf("Expecting error 'Invalid range specified for the Port' but received %s.", err) 115 } 116 }