1
16
17 package options
18
19 import (
20 "reflect"
21 "testing"
22
23 "github.com/spf13/pflag"
24 "github.com/stretchr/testify/assert"
25 )
26
27 func TestValidate(t *testing.T) {
28
29 options := NewAdmissionOptions()
30 options.PluginNames = []string{"ServiceAccount"}
31 options.GenericAdmission.EnablePlugins = []string{"NodeRestriction"}
32 if len(options.Validate()) == 0 {
33 t.Errorf("Expect error, but got none")
34 }
35
36
37 options = NewAdmissionOptions()
38 options.PluginNames = []string{"ServiceAccount"}
39 options.GenericAdmission.DisablePlugins = []string{"NodeRestriction"}
40 if len(options.Validate()) == 0 {
41 t.Errorf("Expect error, but got none")
42 }
43
44
45 options = NewAdmissionOptions()
46 options.PluginNames = []string{"pluginA"}
47 if len(options.Validate()) == 0 {
48 t.Errorf("Expect error, but got none")
49 }
50
51
52 options = NewAdmissionOptions()
53 options.PluginNames = []string{"ServiceAccount"}
54 if errs := options.Validate(); len(errs) > 0 {
55 t.Errorf("Unexpected err: %v", errs)
56 }
57
58
59 options = nil
60 if errs := options.Validate(); errs != nil {
61 t.Errorf("expected no errors, error found %+v", errs)
62 }
63 }
64
65 func TestComputeEnabledAdmission(t *testing.T) {
66 tests := []struct {
67 name string
68 all []string
69 enabled []string
70 expectedDisabled []string
71 }{
72 {
73 name: "matches",
74 all: []string{"one", "two"},
75 enabled: []string{"one", "two"},
76 expectedDisabled: []string{},
77 },
78 {
79 name: "choose one",
80 all: []string{"one", "two"},
81 enabled: []string{"one"},
82 expectedDisabled: []string{"two"},
83 },
84 }
85
86 for _, tc := range tests {
87 t.Run(tc.name, func(t *testing.T) {
88 actualEnabled, actualDisabled := computePluginNames(tc.enabled, tc.all)
89 if e, a := tc.enabled, actualEnabled; !reflect.DeepEqual(e, a) {
90 t.Errorf("expected %v, got %v", e, a)
91 }
92 if e, a := tc.expectedDisabled, actualDisabled; !reflect.DeepEqual(e, a) {
93 t.Errorf("expected %v, got %v", e, a)
94 }
95 })
96 }
97 }
98
99 func TestAdmissionOptionsAddFlags(t *testing.T) {
100 var args = []string{
101 "--enable-admission-plugins=foo,bar,baz",
102 "--admission-control-config-file=admission_control_config.yaml",
103 }
104
105 opts := NewAdmissionOptions()
106 pf := pflag.NewFlagSet("test-admission-opts", pflag.ContinueOnError)
107 opts.AddFlags(pf)
108
109 if err := pf.Parse(args); err != nil {
110 t.Fatal(err)
111 }
112
113
114 assert.Equal(t, opts.GenericAdmission.ConfigFile, "admission_control_config.yaml")
115 assert.Equal(t, opts.GenericAdmission.EnablePlugins, []string{"foo", "bar", "baz"})
116 }
117
View as plain text