github.com/squaremo/docker@v1.3.2-0.20150516120342-42cfc9554972/opts/opts_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestValidateIPAddress(t *testing.T) {
    10  	if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
    11  		t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
    12  	}
    13  
    14  	if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
    15  		t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
    16  	}
    17  
    18  	if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
    19  		t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
    20  	}
    21  
    22  	if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
    23  		t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
    24  	}
    25  
    26  	if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
    27  		t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
    28  	}
    29  
    30  }
    31  
    32  func TestMapOpts(t *testing.T) {
    33  	tmpMap := make(map[string]string)
    34  	o := newMapOpt(tmpMap, logOptsValidator)
    35  	o.Set("max-size=1")
    36  	if o.String() != "map[max-size:1]" {
    37  		t.Errorf("%s != [map[max-size:1]", o.String())
    38  	}
    39  
    40  	o.Set("max-file=2")
    41  	if len(tmpMap) != 2 {
    42  		t.Errorf("map length %d != 2", len(tmpMap))
    43  	}
    44  
    45  	if tmpMap["max-file"] != "2" {
    46  		t.Errorf("max-file = %s != 2", tmpMap["max-file"])
    47  	}
    48  
    49  	if tmpMap["max-size"] != "1" {
    50  		t.Errorf("max-size = %s != 1", tmpMap["max-size"])
    51  	}
    52  	if o.Set("dummy-val=3") == nil {
    53  		t.Errorf("validator is not being called")
    54  	}
    55  }
    56  
    57  func TestValidateMACAddress(t *testing.T) {
    58  	if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil {
    59  		t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err)
    60  	}
    61  
    62  	if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil {
    63  		t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC")
    64  	}
    65  
    66  	if _, err := ValidateMACAddress(`random invalid string`); err == nil {
    67  		t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC")
    68  	}
    69  }
    70  
    71  func TestListOpts(t *testing.T) {
    72  	o := NewListOpts(nil)
    73  	o.Set("foo")
    74  	if o.String() != "[foo]" {
    75  		t.Errorf("%s != [foo]", o.String())
    76  	}
    77  	o.Set("bar")
    78  	if o.Len() != 2 {
    79  		t.Errorf("%d != 2", o.Len())
    80  	}
    81  	if !o.Get("bar") {
    82  		t.Error("o.Get(\"bar\") == false")
    83  	}
    84  	if o.Get("baz") {
    85  		t.Error("o.Get(\"baz\") == true")
    86  	}
    87  	o.Delete("foo")
    88  	if o.String() != "[bar]" {
    89  		t.Errorf("%s != [bar]", o.String())
    90  	}
    91  }
    92  
    93  func TestValidateDnsSearch(t *testing.T) {
    94  	valid := []string{
    95  		`.`,
    96  		`a`,
    97  		`a.`,
    98  		`1.foo`,
    99  		`17.foo`,
   100  		`foo.bar`,
   101  		`foo.bar.baz`,
   102  		`foo.bar.`,
   103  		`foo.bar.baz`,
   104  		`foo1.bar2`,
   105  		`foo1.bar2.baz`,
   106  		`1foo.2bar.`,
   107  		`1foo.2bar.baz`,
   108  		`foo-1.bar-2`,
   109  		`foo-1.bar-2.baz`,
   110  		`foo-1.bar-2.`,
   111  		`foo-1.bar-2.baz`,
   112  		`1-foo.2-bar`,
   113  		`1-foo.2-bar.baz`,
   114  		`1-foo.2-bar.`,
   115  		`1-foo.2-bar.baz`,
   116  	}
   117  
   118  	invalid := []string{
   119  		``,
   120  		` `,
   121  		`  `,
   122  		`17`,
   123  		`17.`,
   124  		`.17`,
   125  		`17-.`,
   126  		`17-.foo`,
   127  		`.foo`,
   128  		`foo-.bar`,
   129  		`-foo.bar`,
   130  		`foo.bar-`,
   131  		`foo.bar-.baz`,
   132  		`foo.-bar`,
   133  		`foo.-bar.baz`,
   134  		`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`,
   135  	}
   136  
   137  	for _, domain := range valid {
   138  		if ret, err := ValidateDnsSearch(domain); err != nil || ret == "" {
   139  			t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err)
   140  		}
   141  	}
   142  
   143  	for _, domain := range invalid {
   144  		if ret, err := ValidateDnsSearch(domain); err == nil || ret != "" {
   145  			t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err)
   146  		}
   147  	}
   148  }
   149  
   150  func TestValidateExtraHosts(t *testing.T) {
   151  	valid := []string{
   152  		`myhost:192.168.0.1`,
   153  		`thathost:10.0.2.1`,
   154  		`anipv6host:2003:ab34:e::1`,
   155  		`ipv6local:::1`,
   156  	}
   157  
   158  	invalid := map[string]string{
   159  		`myhost:192.notanipaddress.1`:  `invalid IP`,
   160  		`thathost-nosemicolon10.0.0.1`: `bad format`,
   161  		`anipv6host:::::1`:             `invalid IP`,
   162  		`ipv6local:::0::`:              `invalid IP`,
   163  	}
   164  
   165  	for _, extrahost := range valid {
   166  		if _, err := ValidateExtraHost(extrahost); err != nil {
   167  			t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err)
   168  		}
   169  	}
   170  
   171  	for extraHost, expectedError := range invalid {
   172  		if _, err := ValidateExtraHost(extraHost); err == nil {
   173  			t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost)
   174  		} else {
   175  			if !strings.Contains(err.Error(), expectedError) {
   176  				t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError)
   177  			}
   178  		}
   179  	}
   180  }
   181  
   182  func logOptsValidator(val string) (string, error) {
   183  	allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
   184  	vals := strings.Split(val, "=")
   185  	if allowedKeys[vals[0]] != "" {
   186  		return val, nil
   187  	}
   188  	return "", fmt.Errorf("invalid key %s", vals[0])
   189  }