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