settable_test.go 2.7 KB

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