github.com/nalind/docker@v1.5.0/opts/opts_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestValidateIPAddress(t *testing.T) {
     8  	if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
     9  		t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
    10  	}
    11  
    12  	if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
    13  		t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
    14  	}
    15  
    16  	if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
    17  		t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
    18  	}
    19  
    20  	if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
    21  		t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
    22  	}
    23  
    24  	if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
    25  		t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
    26  	}
    27  
    28  }
    29  
    30  func TestListOpts(t *testing.T) {
    31  	o := NewListOpts(nil)
    32  	o.Set("foo")
    33  	if o.String() != "[foo]" {
    34  		t.Errorf("%s != [foo]", o.String())
    35  	}
    36  	o.Set("bar")
    37  	if o.Len() != 2 {
    38  		t.Errorf("%d != 2", o.Len())
    39  	}
    40  	if !o.Get("bar") {
    41  		t.Error("o.Get(\"bar\") == false")
    42  	}
    43  	if o.Get("baz") {
    44  		t.Error("o.Get(\"baz\") == true")
    45  	}
    46  	o.Delete("foo")
    47  	if o.String() != "[bar]" {
    48  		t.Errorf("%s != [bar]", o.String())
    49  	}
    50  }
    51  
    52  func TestValidateDnsSearch(t *testing.T) {
    53  	valid := []string{
    54  		`.`,
    55  		`a`,
    56  		`a.`,
    57  		`1.foo`,
    58  		`17.foo`,
    59  		`foo.bar`,
    60  		`foo.bar.baz`,
    61  		`foo.bar.`,
    62  		`foo.bar.baz`,
    63  		`foo1.bar2`,
    64  		`foo1.bar2.baz`,
    65  		`1foo.2bar.`,
    66  		`1foo.2bar.baz`,
    67  		`foo-1.bar-2`,
    68  		`foo-1.bar-2.baz`,
    69  		`foo-1.bar-2.`,
    70  		`foo-1.bar-2.baz`,
    71  		`1-foo.2-bar`,
    72  		`1-foo.2-bar.baz`,
    73  		`1-foo.2-bar.`,
    74  		`1-foo.2-bar.baz`,
    75  	}
    76  
    77  	invalid := []string{
    78  		``,
    79  		` `,
    80  		`  `,
    81  		`17`,
    82  		`17.`,
    83  		`.17`,
    84  		`17-.`,
    85  		`17-.foo`,
    86  		`.foo`,
    87  		`foo-.bar`,
    88  		`-foo.bar`,
    89  		`foo.bar-`,
    90  		`foo.bar-.baz`,
    91  		`foo.-bar`,
    92  		`foo.-bar.baz`,
    93  	}
    94  
    95  	for _, domain := range valid {
    96  		if ret, err := ValidateDnsSearch(domain); err != nil || ret == "" {
    97  			t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err)
    98  		}
    99  	}
   100  
   101  	for _, domain := range invalid {
   102  		if ret, err := ValidateDnsSearch(domain); err == nil || ret != "" {
   103  			t.Fatalf("ValidateDnsSearch(`"+domain+"`) got %s %s", ret, err)
   104  		}
   105  	}
   106  }