gopkg.in/docker/docker.v1@v1.13.1/plugin/v2/settable_test.go (about)

     1  package v2
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestNewSettable(t *testing.T) {
     9  	contexts := []struct {
    10  		arg   string
    11  		name  string
    12  		field string
    13  		value string
    14  		err   error
    15  	}{
    16  		{"name=value", "name", "", "value", nil},
    17  		{"name", "name", "", "", nil},
    18  		{"name.field=value", "name", "field", "value", nil},
    19  		{"name.field", "name", "field", "", nil},
    20  		{"=value", "", "", "", errInvalidFormat},
    21  		{"=", "", "", "", errInvalidFormat},
    22  	}
    23  
    24  	for _, c := range contexts {
    25  		s, err := newSettable(c.arg)
    26  		if err != c.err {
    27  			t.Fatalf("expected error to be %v, got %v", c.err, err)
    28  		}
    29  
    30  		if s.name != c.name {
    31  			t.Fatalf("expected name to be %q, got %q", c.name, s.name)
    32  		}
    33  
    34  		if s.field != c.field {
    35  			t.Fatalf("expected field to be %q, got %q", c.field, s.field)
    36  		}
    37  
    38  		if s.value != c.value {
    39  			t.Fatalf("expected value to be %q, got %q", c.value, s.value)
    40  		}
    41  
    42  	}
    43  }
    44  
    45  func TestIsSettable(t *testing.T) {
    46  	contexts := []struct {
    47  		allowedSettableFields []string
    48  		set                   settable
    49  		settable              []string
    50  		result                bool
    51  		err                   error
    52  	}{
    53  		{allowedSettableFieldsEnv, settable{}, []string{}, false, nil},
    54  		{allowedSettableFieldsEnv, settable{field: "value"}, []string{}, false, nil},
    55  		{allowedSettableFieldsEnv, settable{}, []string{"value"}, true, nil},
    56  		{allowedSettableFieldsEnv, settable{field: "value"}, []string{"value"}, true, nil},
    57  		{allowedSettableFieldsEnv, settable{field: "foo"}, []string{"value"}, false, nil},
    58  		{allowedSettableFieldsEnv, settable{field: "foo"}, []string{"foo"}, false, nil},
    59  		{allowedSettableFieldsEnv, settable{}, []string{"value1", "value2"}, false, errMultipleFields},
    60  	}
    61  
    62  	for _, c := range contexts {
    63  		if res, err := c.set.isSettable(c.allowedSettableFields, c.settable); res != c.result {
    64  			t.Fatalf("expected result to be %t, got %t", c.result, res)
    65  		} else if err != c.err {
    66  			t.Fatalf("expected error to be %v, got %v", c.err, err)
    67  		}
    68  	}
    69  }
    70  
    71  func TestUpdateSettinsEnv(t *testing.T) {
    72  	contexts := []struct {
    73  		env    []string
    74  		set    settable
    75  		newEnv []string
    76  	}{
    77  		{[]string{}, settable{name: "DEBUG", value: "1"}, []string{"DEBUG=1"}},
    78  		{[]string{"DEBUG=0"}, settable{name: "DEBUG", value: "1"}, []string{"DEBUG=1"}},
    79  		{[]string{"FOO=0"}, settable{name: "DEBUG", value: "1"}, []string{"FOO=0", "DEBUG=1"}},
    80  		{[]string{"FOO=0", "DEBUG=0"}, settable{name: "DEBUG", value: "1"}, []string{"FOO=0", "DEBUG=1"}},
    81  		{[]string{"FOO=0", "DEBUG=0", "BAR=1"}, settable{name: "DEBUG", value: "1"}, []string{"FOO=0", "DEBUG=1", "BAR=1"}},
    82  	}
    83  
    84  	for _, c := range contexts {
    85  		updateSettingsEnv(&c.env, &c.set)
    86  
    87  		if !reflect.DeepEqual(c.env, c.newEnv) {
    88  			t.Fatalf("expected env to be %q, got %q", c.newEnv, c.env)
    89  		}
    90  	}
    91  }