github.com/lafolle/docker@v1.6.0/opts/opts_test.go (about)

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