github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/opts/opts_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestValidateIPAddress(t *testing.T) {
    11  	if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
    12  		t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
    13  	}
    14  
    15  	if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
    16  		t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
    17  	}
    18  
    19  	if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
    20  		t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
    21  	}
    22  
    23  	if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
    24  		t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
    25  	}
    26  
    27  	if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
    28  		t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
    29  	}
    30  
    31  }
    32  
    33  func TestMapOpts(t *testing.T) {
    34  	tmpMap := make(map[string]string)
    35  	o := NewMapOpts(tmpMap, logOptsValidator)
    36  	o.Set("max-size=1")
    37  	if o.String() != "map[max-size:1]" {
    38  		t.Errorf("%s != [map[max-size:1]", o.String())
    39  	}
    40  
    41  	o.Set("max-file=2")
    42  	if len(tmpMap) != 2 {
    43  		t.Errorf("map length %d != 2", len(tmpMap))
    44  	}
    45  
    46  	if tmpMap["max-file"] != "2" {
    47  		t.Errorf("max-file = %s != 2", tmpMap["max-file"])
    48  	}
    49  
    50  	if tmpMap["max-size"] != "1" {
    51  		t.Errorf("max-size = %s != 1", tmpMap["max-size"])
    52  	}
    53  	if o.Set("dummy-val=3") == nil {
    54  		t.Errorf("validator is not being called")
    55  	}
    56  }
    57  
    58  func TestValidateMACAddress(t *testing.T) {
    59  	if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil {
    60  		t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err)
    61  	}
    62  
    63  	if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil {
    64  		t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC")
    65  	}
    66  
    67  	if _, err := ValidateMACAddress(`random invalid string`); err == nil {
    68  		t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC")
    69  	}
    70  }
    71  
    72  func TestListOptsWithoutValidator(t *testing.T) {
    73  	o := NewListOpts(nil)
    74  	o.Set("foo")
    75  	if o.String() != "[foo]" {
    76  		t.Errorf("%s != [foo]", o.String())
    77  	}
    78  	o.Set("bar")
    79  	if o.Len() != 2 {
    80  		t.Errorf("%d != 2", o.Len())
    81  	}
    82  	o.Set("bar")
    83  	if o.Len() != 3 {
    84  		t.Errorf("%d != 3", o.Len())
    85  	}
    86  	if !o.Get("bar") {
    87  		t.Error("o.Get(\"bar\") == false")
    88  	}
    89  	if o.Get("baz") {
    90  		t.Error("o.Get(\"baz\") == true")
    91  	}
    92  	o.Delete("foo")
    93  	if o.String() != "[bar bar]" {
    94  		t.Errorf("%s != [bar bar]", o.String())
    95  	}
    96  	listOpts := o.GetAll()
    97  	if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
    98  		t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
    99  	}
   100  	mapListOpts := o.GetMap()
   101  	if len(mapListOpts) != 1 {
   102  		t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
   103  	}
   104  
   105  }
   106  
   107  func TestListOptsWithValidator(t *testing.T) {
   108  	// Re-using logOptsvalidator (used by MapOpts)
   109  	o := NewListOpts(logOptsValidator)
   110  	o.Set("foo")
   111  	if o.String() != "[]" {
   112  		t.Errorf("%s != []", o.String())
   113  	}
   114  	o.Set("foo=bar")
   115  	if o.String() != "[]" {
   116  		t.Errorf("%s != []", o.String())
   117  	}
   118  	o.Set("max-file=2")
   119  	if o.Len() != 1 {
   120  		t.Errorf("%d != 1", o.Len())
   121  	}
   122  	if !o.Get("max-file=2") {
   123  		t.Error("o.Get(\"max-file=2\") == false")
   124  	}
   125  	if o.Get("baz") {
   126  		t.Error("o.Get(\"baz\") == true")
   127  	}
   128  	o.Delete("max-file=2")
   129  	if o.String() != "[]" {
   130  		t.Errorf("%s != []", o.String())
   131  	}
   132  }
   133  
   134  func TestValidateDNSSearch(t *testing.T) {
   135  	valid := []string{
   136  		`.`,
   137  		`a`,
   138  		`a.`,
   139  		`1.foo`,
   140  		`17.foo`,
   141  		`foo.bar`,
   142  		`foo.bar.baz`,
   143  		`foo.bar.`,
   144  		`foo.bar.baz`,
   145  		`foo1.bar2`,
   146  		`foo1.bar2.baz`,
   147  		`1foo.2bar.`,
   148  		`1foo.2bar.baz`,
   149  		`foo-1.bar-2`,
   150  		`foo-1.bar-2.baz`,
   151  		`foo-1.bar-2.`,
   152  		`foo-1.bar-2.baz`,
   153  		`1-foo.2-bar`,
   154  		`1-foo.2-bar.baz`,
   155  		`1-foo.2-bar.`,
   156  		`1-foo.2-bar.baz`,
   157  	}
   158  
   159  	invalid := []string{
   160  		``,
   161  		` `,
   162  		`  `,
   163  		`17`,
   164  		`17.`,
   165  		`.17`,
   166  		`17-.`,
   167  		`17-.foo`,
   168  		`.foo`,
   169  		`foo-.bar`,
   170  		`-foo.bar`,
   171  		`foo.bar-`,
   172  		`foo.bar-.baz`,
   173  		`foo.-bar`,
   174  		`foo.-bar.baz`,
   175  		`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`,
   176  	}
   177  
   178  	for _, domain := range valid {
   179  		if ret, err := ValidateDNSSearch(domain); err != nil || ret == "" {
   180  			t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
   181  		}
   182  	}
   183  
   184  	for _, domain := range invalid {
   185  		if ret, err := ValidateDNSSearch(domain); err == nil || ret != "" {
   186  			t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
   187  		}
   188  	}
   189  }
   190  
   191  func TestValidateExtraHosts(t *testing.T) {
   192  	valid := []string{
   193  		`myhost:192.168.0.1`,
   194  		`thathost:10.0.2.1`,
   195  		`anipv6host:2003:ab34:e::1`,
   196  		`ipv6local:::1`,
   197  	}
   198  
   199  	invalid := map[string]string{
   200  		`myhost:192.notanipaddress.1`:  `invalid IP`,
   201  		`thathost-nosemicolon10.0.0.1`: `bad format`,
   202  		`anipv6host:::::1`:             `invalid IP`,
   203  		`ipv6local:::0::`:              `invalid IP`,
   204  	}
   205  
   206  	for _, extrahost := range valid {
   207  		if _, err := ValidateExtraHost(extrahost); err != nil {
   208  			t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err)
   209  		}
   210  	}
   211  
   212  	for extraHost, expectedError := range invalid {
   213  		if _, err := ValidateExtraHost(extraHost); err == nil {
   214  			t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost)
   215  		} else {
   216  			if !strings.Contains(err.Error(), expectedError) {
   217  				t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError)
   218  			}
   219  		}
   220  	}
   221  }
   222  
   223  func TestValidateAttach(t *testing.T) {
   224  	valid := []string{
   225  		"stdin",
   226  		"stdout",
   227  		"stderr",
   228  		"STDIN",
   229  		"STDOUT",
   230  		"STDERR",
   231  	}
   232  	if _, err := ValidateAttach("invalid"); err == nil {
   233  		t.Fatalf("Expected error with [valid streams are STDIN, STDOUT and STDERR], got nothing")
   234  	}
   235  
   236  	for _, attach := range valid {
   237  		value, err := ValidateAttach(attach)
   238  		if err != nil {
   239  			t.Fatal(err)
   240  		}
   241  		if value != strings.ToLower(attach) {
   242  			t.Fatalf("Expected [%v], got [%v]", attach, value)
   243  		}
   244  	}
   245  }
   246  
   247  func TestValidateEnv(t *testing.T) {
   248  	valids := map[string]string{
   249  		"a":                   "a",
   250  		"something":           "something",
   251  		"_=a":                 "_=a",
   252  		"env1=value1":         "env1=value1",
   253  		"_env1=value1":        "_env1=value1",
   254  		"env2=value2=value3":  "env2=value2=value3",
   255  		"env3=abc!qwe":        "env3=abc!qwe",
   256  		"env_4=value 4":       "env_4=value 4",
   257  		"PATH":                fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
   258  		"PATH=something":      "PATH=something",
   259  		"asd!qwe":             "asd!qwe",
   260  		"1asd":                "1asd",
   261  		"123":                 "123",
   262  		"some space":          "some space",
   263  		"  some space before": "  some space before",
   264  		"some space after  ":  "some space after  ",
   265  	}
   266  	for value, expected := range valids {
   267  		actual, err := ValidateEnv(value)
   268  		if err != nil {
   269  			t.Fatal(err)
   270  		}
   271  		if actual != expected {
   272  			t.Fatalf("Expected [%v], got [%v]", expected, actual)
   273  		}
   274  	}
   275  }
   276  
   277  func TestValidateLabel(t *testing.T) {
   278  	if _, err := ValidateLabel("label"); err == nil || err.Error() != "bad attribute format: label" {
   279  		t.Fatalf("Expected an error [bad attribute format: label], go %v", err)
   280  	}
   281  	if actual, err := ValidateLabel("key1=value1"); err != nil || actual != "key1=value1" {
   282  		t.Fatalf("Expected [key1=value1], got [%v,%v]", actual, err)
   283  	}
   284  	// Validate it's working with more than one =
   285  	if actual, err := ValidateLabel("key1=value1=value2"); err != nil {
   286  		t.Fatalf("Expected [key1=value1=value2], got [%v,%v]", actual, err)
   287  	}
   288  	// Validate it's working with one more
   289  	if actual, err := ValidateLabel("key1=value1=value2=value3"); err != nil {
   290  		t.Fatalf("Expected [key1=value1=value2=value2], got [%v,%v]", actual, err)
   291  	}
   292  }
   293  
   294  func logOptsValidator(val string) (string, error) {
   295  	allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
   296  	vals := strings.Split(val, "=")
   297  	if allowedKeys[vals[0]] != "" {
   298  		return val, nil
   299  	}
   300  	return "", fmt.Errorf("invalid key %s", vals[0])
   301  }