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