github.com/rish1988/moby@v25.0.2+incompatible/plugin/v2/settable_test.go (about) 1 package v2 // import "github.com/docker/docker/plugin/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 func TestIsSettable(t *testing.T) { 45 contexts := []struct { 46 allowedSettableFields []string 47 set settable 48 settable []string 49 result bool 50 err error 51 }{ 52 {allowedSettableFieldsEnv, settable{}, []string{}, false, nil}, 53 {allowedSettableFieldsEnv, settable{field: "value"}, []string{}, false, nil}, 54 {allowedSettableFieldsEnv, settable{}, []string{"value"}, true, nil}, 55 {allowedSettableFieldsEnv, settable{field: "value"}, []string{"value"}, true, nil}, 56 {allowedSettableFieldsEnv, settable{field: "foo"}, []string{"value"}, false, nil}, 57 {allowedSettableFieldsEnv, settable{field: "foo"}, []string{"foo"}, false, nil}, 58 {allowedSettableFieldsEnv, settable{}, []string{"value1", "value2"}, false, errMultipleFields}, 59 } 60 61 for _, c := range contexts { 62 if res, err := c.set.isSettable(c.allowedSettableFields, c.settable); res != c.result { 63 t.Fatalf("expected result to be %t, got %t", c.result, res) 64 } else if err != c.err { 65 t.Fatalf("expected error to be %v, got %v", c.err, err) 66 } 67 } 68 } 69 70 func TestUpdateSettingsEnv(t *testing.T) { 71 contexts := []struct { 72 env []string 73 set settable 74 newEnv []string 75 }{ 76 {[]string{}, settable{name: "DEBUG", value: "1"}, []string{"DEBUG=1"}}, 77 {[]string{"DEBUG=0"}, settable{name: "DEBUG", value: "1"}, []string{"DEBUG=1"}}, 78 {[]string{"FOO=0"}, settable{name: "DEBUG", value: "1"}, []string{"FOO=0", "DEBUG=1"}}, 79 {[]string{"FOO=0", "DEBUG=0"}, settable{name: "DEBUG", value: "1"}, []string{"FOO=0", "DEBUG=1"}}, 80 {[]string{"FOO=0", "DEBUG=0", "BAR=1"}, settable{name: "DEBUG", value: "1"}, []string{"FOO=0", "DEBUG=1", "BAR=1"}}, 81 } 82 83 for _, c := range contexts { 84 updateSettingsEnv(&c.env, &c.set) 85 86 if !reflect.DeepEqual(c.env, c.newEnv) { 87 t.Fatalf("expected env to be %q, got %q", c.newEnv, c.env) 88 } 89 } 90 }