github.com/alexandrev/docker@v1.9.0/opts/opts_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestValidateIPAddress(t *testing.T) {
    12  	if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
    13  		t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
    14  	}
    15  
    16  	if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
    17  		t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
    18  	}
    19  
    20  	if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
    21  		t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
    22  	}
    23  
    24  	if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
    25  		t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
    26  	}
    27  
    28  	if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
    29  		t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
    30  	}
    31  
    32  }
    33  
    34  func TestMapOpts(t *testing.T) {
    35  	tmpMap := make(map[string]string)
    36  	o := NewMapOpts(tmpMap, logOptsValidator)
    37  	o.Set("max-size=1")
    38  	if o.String() != "map[max-size:1]" {
    39  		t.Errorf("%s != [map[max-size:1]", o.String())
    40  	}
    41  
    42  	o.Set("max-file=2")
    43  	if len(tmpMap) != 2 {
    44  		t.Errorf("map length %d != 2", len(tmpMap))
    45  	}
    46  
    47  	if tmpMap["max-file"] != "2" {
    48  		t.Errorf("max-file = %s != 2", tmpMap["max-file"])
    49  	}
    50  
    51  	if tmpMap["max-size"] != "1" {
    52  		t.Errorf("max-size = %s != 1", tmpMap["max-size"])
    53  	}
    54  	if o.Set("dummy-val=3") == nil {
    55  		t.Errorf("validator is not being called")
    56  	}
    57  }
    58  
    59  func TestValidateMACAddress(t *testing.T) {
    60  	if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil {
    61  		t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err)
    62  	}
    63  
    64  	if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil {
    65  		t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC")
    66  	}
    67  
    68  	if _, err := ValidateMACAddress(`random invalid string`); err == nil {
    69  		t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC")
    70  	}
    71  }
    72  
    73  func TestListOptsWithoutValidator(t *testing.T) {
    74  	o := NewListOpts(nil)
    75  	o.Set("foo")
    76  	if o.String() != "[foo]" {
    77  		t.Errorf("%s != [foo]", o.String())
    78  	}
    79  	o.Set("bar")
    80  	if o.Len() != 2 {
    81  		t.Errorf("%d != 2", o.Len())
    82  	}
    83  	o.Set("bar")
    84  	if o.Len() != 3 {
    85  		t.Errorf("%d != 3", o.Len())
    86  	}
    87  	if !o.Get("bar") {
    88  		t.Error("o.Get(\"bar\") == false")
    89  	}
    90  	if o.Get("baz") {
    91  		t.Error("o.Get(\"baz\") == true")
    92  	}
    93  	o.Delete("foo")
    94  	if o.String() != "[bar bar]" {
    95  		t.Errorf("%s != [bar bar]", o.String())
    96  	}
    97  	listOpts := o.GetAll()
    98  	if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
    99  		t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
   100  	}
   101  	mapListOpts := o.GetMap()
   102  	if len(mapListOpts) != 1 {
   103  		t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
   104  	}
   105  
   106  }
   107  
   108  func TestListOptsWithValidator(t *testing.T) {
   109  	// Re-using logOptsvalidator (used by MapOpts)
   110  	o := NewListOpts(logOptsValidator)
   111  	o.Set("foo")
   112  	if o.String() != "[]" {
   113  		t.Errorf("%s != []", o.String())
   114  	}
   115  	o.Set("foo=bar")
   116  	if o.String() != "[]" {
   117  		t.Errorf("%s != []", o.String())
   118  	}
   119  	o.Set("max-file=2")
   120  	if o.Len() != 1 {
   121  		t.Errorf("%d != 1", o.Len())
   122  	}
   123  	if !o.Get("max-file=2") {
   124  		t.Error("o.Get(\"max-file=2\") == false")
   125  	}
   126  	if o.Get("baz") {
   127  		t.Error("o.Get(\"baz\") == true")
   128  	}
   129  	o.Delete("max-file=2")
   130  	if o.String() != "[]" {
   131  		t.Errorf("%s != []", o.String())
   132  	}
   133  }
   134  
   135  func TestValidateDNSSearch(t *testing.T) {
   136  	valid := []string{
   137  		`.`,
   138  		`a`,
   139  		`a.`,
   140  		`1.foo`,
   141  		`17.foo`,
   142  		`foo.bar`,
   143  		`foo.bar.baz`,
   144  		`foo.bar.`,
   145  		`foo.bar.baz`,
   146  		`foo1.bar2`,
   147  		`foo1.bar2.baz`,
   148  		`1foo.2bar.`,
   149  		`1foo.2bar.baz`,
   150  		`foo-1.bar-2`,
   151  		`foo-1.bar-2.baz`,
   152  		`foo-1.bar-2.`,
   153  		`foo-1.bar-2.baz`,
   154  		`1-foo.2-bar`,
   155  		`1-foo.2-bar.baz`,
   156  		`1-foo.2-bar.`,
   157  		`1-foo.2-bar.baz`,
   158  	}
   159  
   160  	invalid := []string{
   161  		``,
   162  		` `,
   163  		`  `,
   164  		`17`,
   165  		`17.`,
   166  		`.17`,
   167  		`17-.`,
   168  		`17-.foo`,
   169  		`.foo`,
   170  		`foo-.bar`,
   171  		`-foo.bar`,
   172  		`foo.bar-`,
   173  		`foo.bar-.baz`,
   174  		`foo.-bar`,
   175  		`foo.-bar.baz`,
   176  		`foo.bar.baz.this.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbe`,
   177  	}
   178  
   179  	for _, domain := range valid {
   180  		if ret, err := ValidateDNSSearch(domain); err != nil || ret == "" {
   181  			t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
   182  		}
   183  	}
   184  
   185  	for _, domain := range invalid {
   186  		if ret, err := ValidateDNSSearch(domain); err == nil || ret != "" {
   187  			t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
   188  		}
   189  	}
   190  }
   191  
   192  func TestValidateExtraHosts(t *testing.T) {
   193  	valid := []string{
   194  		`myhost:192.168.0.1`,
   195  		`thathost:10.0.2.1`,
   196  		`anipv6host:2003:ab34:e::1`,
   197  		`ipv6local:::1`,
   198  	}
   199  
   200  	invalid := map[string]string{
   201  		`myhost:192.notanipaddress.1`:  `invalid IP`,
   202  		`thathost-nosemicolon10.0.0.1`: `bad format`,
   203  		`anipv6host:::::1`:             `invalid IP`,
   204  		`ipv6local:::0::`:              `invalid IP`,
   205  	}
   206  
   207  	for _, extrahost := range valid {
   208  		if _, err := ValidateExtraHost(extrahost); err != nil {
   209  			t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err)
   210  		}
   211  	}
   212  
   213  	for extraHost, expectedError := range invalid {
   214  		if _, err := ValidateExtraHost(extraHost); err == nil {
   215  			t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost)
   216  		} else {
   217  			if !strings.Contains(err.Error(), expectedError) {
   218  				t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError)
   219  			}
   220  		}
   221  	}
   222  }
   223  
   224  func TestValidateAttach(t *testing.T) {
   225  	valid := []string{
   226  		"stdin",
   227  		"stdout",
   228  		"stderr",
   229  		"STDIN",
   230  		"STDOUT",
   231  		"STDERR",
   232  	}
   233  	if _, err := ValidateAttach("invalid"); err == nil {
   234  		t.Fatalf("Expected error with [valid streams are STDIN, STDOUT and STDERR], got nothing")
   235  	}
   236  
   237  	for _, attach := range valid {
   238  		value, err := ValidateAttach(attach)
   239  		if err != nil {
   240  			t.Fatal(err)
   241  		}
   242  		if value != strings.ToLower(attach) {
   243  			t.Fatalf("Expected [%v], got [%v]", attach, value)
   244  		}
   245  	}
   246  }
   247  
   248  func TestValidateLink(t *testing.T) {
   249  	valid := []string{
   250  		"name",
   251  		"dcdfbe62ecd0:alias",
   252  		"7a67485460b7642516a4ad82ecefe7f57d0c4916f530561b71a50a3f9c4e33da",
   253  		"angry_torvalds:linus",
   254  	}
   255  	invalid := map[string]string{
   256  		"":               "empty string specified for links",
   257  		"too:much:of:it": "bad format for links: too:much:of:it",
   258  	}
   259  
   260  	for _, link := range valid {
   261  		if _, err := ValidateLink(link); err != nil {
   262  			t.Fatalf("ValidateLink(`%q`) should succeed: error %q", link, err)
   263  		}
   264  	}
   265  
   266  	for link, expectedError := range invalid {
   267  		if _, err := ValidateLink(link); err == nil {
   268  			t.Fatalf("ValidateLink(`%q`) should have failed validation", link)
   269  		} else {
   270  			if !strings.Contains(err.Error(), expectedError) {
   271  				t.Fatalf("ValidateLink(`%q`) error should contain %q", link, expectedError)
   272  			}
   273  		}
   274  	}
   275  }
   276  
   277  func TestValidatePath(t *testing.T) {
   278  	valid := []string{
   279  		"/home",
   280  		"/home:/home",
   281  		"/home:/something/else",
   282  		"/with space",
   283  		"/home:/with space",
   284  		"relative:/absolute-path",
   285  		"hostPath:/containerPath:ro",
   286  		"/hostPath:/containerPath:rw",
   287  		"/rw:/ro",
   288  		"/path:rw",
   289  		"/path:ro",
   290  		"/rw:rw",
   291  	}
   292  	invalid := map[string]string{
   293  		"":                "bad format for path: ",
   294  		"./":              "./ is not an absolute path",
   295  		"../":             "../ is not an absolute path",
   296  		"/:../":           "../ is not an absolute path",
   297  		"/:path":          "path is not an absolute path",
   298  		":":               "bad format for path: :",
   299  		"/tmp:":           " is not an absolute path",
   300  		":test":           "bad format for path: :test",
   301  		":/test":          "bad format for path: :/test",
   302  		"tmp:":            " is not an absolute path",
   303  		":test:":          "bad format for path: :test:",
   304  		"::":              "bad format for path: ::",
   305  		":::":             "bad format for path: :::",
   306  		"/tmp:::":         "bad format for path: /tmp:::",
   307  		":/tmp::":         "bad format for path: :/tmp::",
   308  		"path:ro":         "path is not an absolute path",
   309  		"/path:/path:sw":  "bad mode specified: sw",
   310  		"/path:/path:rwz": "bad mode specified: rwz",
   311  	}
   312  
   313  	for _, path := range valid {
   314  		if _, err := ValidatePath(path); err != nil {
   315  			t.Fatalf("ValidatePath(`%q`) should succeed: error %q", path, err)
   316  		}
   317  	}
   318  
   319  	for path, expectedError := range invalid {
   320  		if _, err := ValidatePath(path); err == nil {
   321  			t.Fatalf("ValidatePath(`%q`) should have failed validation", path)
   322  		} else {
   323  			if err.Error() != expectedError {
   324  				t.Fatalf("ValidatePath(`%q`) error should contain %q, got %q", path, expectedError, err.Error())
   325  			}
   326  		}
   327  	}
   328  }
   329  func TestValidateDevice(t *testing.T) {
   330  	valid := []string{
   331  		"/home",
   332  		"/home:/home",
   333  		"/home:/something/else",
   334  		"/with space",
   335  		"/home:/with space",
   336  		"relative:/absolute-path",
   337  		"hostPath:/containerPath:r",
   338  		"/hostPath:/containerPath:rw",
   339  		"/hostPath:/containerPath:mrw",
   340  	}
   341  	invalid := map[string]string{
   342  		"":        "bad format for path: ",
   343  		"./":      "./ is not an absolute path",
   344  		"../":     "../ is not an absolute path",
   345  		"/:../":   "../ is not an absolute path",
   346  		"/:path":  "path is not an absolute path",
   347  		":":       "bad format for path: :",
   348  		"/tmp:":   " is not an absolute path",
   349  		":test":   "bad format for path: :test",
   350  		":/test":  "bad format for path: :/test",
   351  		"tmp:":    " is not an absolute path",
   352  		":test:":  "bad format for path: :test:",
   353  		"::":      "bad format for path: ::",
   354  		":::":     "bad format for path: :::",
   355  		"/tmp:::": "bad format for path: /tmp:::",
   356  		":/tmp::": "bad format for path: :/tmp::",
   357  		"path:ro": "ro is not an absolute path",
   358  		"path:rr": "rr is not an absolute path",
   359  		"a:/b:ro": "bad mode specified: ro",
   360  		"a:/b:rr": "bad mode specified: rr",
   361  	}
   362  
   363  	for _, path := range valid {
   364  		if _, err := ValidateDevice(path); err != nil {
   365  			t.Fatalf("ValidateDevice(`%q`) should succeed: error %q", path, err)
   366  		}
   367  	}
   368  
   369  	for path, expectedError := range invalid {
   370  		if _, err := ValidateDevice(path); err == nil {
   371  			t.Fatalf("ValidateDevice(`%q`) should have failed validation", path)
   372  		} else {
   373  			if err.Error() != expectedError {
   374  				t.Fatalf("ValidateDevice(`%q`) error should contain %q, got %q", path, expectedError, err.Error())
   375  			}
   376  		}
   377  	}
   378  }
   379  
   380  func TestValidateEnv(t *testing.T) {
   381  	valids := map[string]string{
   382  		"a":                   "a",
   383  		"something":           "something",
   384  		"_=a":                 "_=a",
   385  		"env1=value1":         "env1=value1",
   386  		"_env1=value1":        "_env1=value1",
   387  		"env2=value2=value3":  "env2=value2=value3",
   388  		"env3=abc!qwe":        "env3=abc!qwe",
   389  		"env_4=value 4":       "env_4=value 4",
   390  		"PATH":                fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
   391  		"PATH=something":      "PATH=something",
   392  		"asd!qwe":             "asd!qwe",
   393  		"1asd":                "1asd",
   394  		"123":                 "123",
   395  		"some space":          "some space",
   396  		"  some space before": "  some space before",
   397  		"some space after  ":  "some space after  ",
   398  	}
   399  	for value, expected := range valids {
   400  		actual, err := ValidateEnv(value)
   401  		if err != nil {
   402  			t.Fatal(err)
   403  		}
   404  		if actual != expected {
   405  			t.Fatalf("Expected [%v], got [%v]", expected, actual)
   406  		}
   407  	}
   408  }
   409  
   410  func TestValidateLabel(t *testing.T) {
   411  	if _, err := ValidateLabel("label"); err == nil || err.Error() != "bad attribute format: label" {
   412  		t.Fatalf("Expected an error [bad attribute format: label], go %v", err)
   413  	}
   414  	if actual, err := ValidateLabel("key1=value1"); err != nil || actual != "key1=value1" {
   415  		t.Fatalf("Expected [key1=value1], got [%v,%v]", actual, err)
   416  	}
   417  	// Validate it's working with more than one =
   418  	if actual, err := ValidateLabel("key1=value1=value2"); err != nil {
   419  		t.Fatalf("Expected [key1=value1=value2], got [%v,%v]", actual, err)
   420  	}
   421  	// Validate it's working with one more
   422  	if actual, err := ValidateLabel("key1=value1=value2=value3"); err != nil {
   423  		t.Fatalf("Expected [key1=value1=value2=value2], got [%v,%v]", actual, err)
   424  	}
   425  }
   426  
   427  func TestParseHost(t *testing.T) {
   428  	invalid := map[string]string{
   429  		"anything":              "Invalid bind address format: anything",
   430  		"something with spaces": "Invalid bind address format: something with spaces",
   431  		"://":                "Invalid bind address format: ://",
   432  		"unknown://":         "Invalid bind address format: unknown://",
   433  		"tcp://:port":        "Invalid bind address format: :port",
   434  		"tcp://invalid":      "Invalid bind address format: invalid",
   435  		"tcp://invalid:port": "Invalid bind address format: invalid:port",
   436  	}
   437  	const defaultHTTPHost = "tcp://127.0.0.1:2375"
   438  	var defaultHOST = "unix:///var/run/docker.sock"
   439  
   440  	if runtime.GOOS == "windows" {
   441  		defaultHOST = defaultHTTPHost
   442  	}
   443  	valid := map[string]string{
   444  		"":                         defaultHOST,
   445  		"fd://":                    "fd://",
   446  		"fd://something":           "fd://something",
   447  		"tcp://host:":              "tcp://host:2375",
   448  		"tcp://":                   "tcp://localhost:2375",
   449  		"tcp://:2375":              "tcp://localhost:2375", // default ip address
   450  		"tcp://:2376":              "tcp://localhost:2376", // default ip address
   451  		"tcp://0.0.0.0:8080":       "tcp://0.0.0.0:8080",
   452  		"tcp://192.168.0.0:12000":  "tcp://192.168.0.0:12000",
   453  		"tcp://192.168:8080":       "tcp://192.168:8080",
   454  		"tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P
   455  		"tcp://docker.com:2375":    "tcp://docker.com:2375",
   456  		"unix://":                  "unix:///var/run/docker.sock", // default unix:// value
   457  		"unix://path/to/socket":    "unix://path/to/socket",
   458  	}
   459  
   460  	for value, errorMessage := range invalid {
   461  		if _, err := ParseHost(defaultHTTPHost, value); err == nil || err.Error() != errorMessage {
   462  			t.Fatalf("Expected an error for %v with [%v], got [%v]", value, errorMessage, err)
   463  		}
   464  	}
   465  	for value, expected := range valid {
   466  		if actual, err := ParseHost(defaultHTTPHost, value); err != nil || actual != expected {
   467  			t.Fatalf("Expected for %v [%v], got [%v, %v]", value, expected, actual, err)
   468  		}
   469  	}
   470  }
   471  
   472  func logOptsValidator(val string) (string, error) {
   473  	allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
   474  	vals := strings.Split(val, "=")
   475  	if allowedKeys[vals[0]] != "" {
   476  		return val, nil
   477  	}
   478  	return "", fmt.Errorf("invalid key %s", vals[0])
   479  }