admission_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package options
  14. import (
  15. "reflect"
  16. "testing"
  17. )
  18. func TestValidate(t *testing.T) {
  19. // 1. Both `--admission-control` and `--enable-admission-plugins` are specified
  20. options := NewAdmissionOptions()
  21. options.PluginNames = []string{"ServiceAccount"}
  22. options.GenericAdmission.EnablePlugins = []string{"NodeRestriction"}
  23. if len(options.Validate()) == 0 {
  24. t.Errorf("Expect error, but got none")
  25. }
  26. // 2. Both `--admission-control` and `--disable-admission-plugins` are specified
  27. options = NewAdmissionOptions()
  28. options.PluginNames = []string{"ServiceAccount"}
  29. options.GenericAdmission.DisablePlugins = []string{"NodeRestriction"}
  30. if len(options.Validate()) == 0 {
  31. t.Errorf("Expect error, but got none")
  32. }
  33. // 3. PluginNames is not registered
  34. options = NewAdmissionOptions()
  35. options.PluginNames = []string{"pluginA"}
  36. if len(options.Validate()) == 0 {
  37. t.Errorf("Expect error, but got none")
  38. }
  39. // 4. PluginNames is not valid
  40. options = NewAdmissionOptions()
  41. options.PluginNames = []string{"ServiceAccount"}
  42. if errs := options.Validate(); len(errs) > 0 {
  43. t.Errorf("Unexpected err: %v", errs)
  44. }
  45. }
  46. func TestComputeEnabledAdmission(t *testing.T) {
  47. tests := []struct {
  48. name string
  49. all []string
  50. enabled []string
  51. expectedDisabled []string
  52. }{
  53. {
  54. name: "matches",
  55. all: []string{"one", "two"},
  56. enabled: []string{"one", "two"},
  57. expectedDisabled: []string{},
  58. },
  59. {
  60. name: "choose one",
  61. all: []string{"one", "two"},
  62. enabled: []string{"one"},
  63. expectedDisabled: []string{"two"},
  64. },
  65. }
  66. for _, tc := range tests {
  67. t.Run(tc.name, func(t *testing.T) {
  68. actualEnabled, actualDisabled := computePluginNames(tc.enabled, tc.all)
  69. if e, a := tc.enabled, actualEnabled; !reflect.DeepEqual(e, a) {
  70. t.Errorf("expected %v, got %v", e, a)
  71. }
  72. if e, a := tc.expectedDisabled, actualDisabled; !reflect.DeepEqual(e, a) {
  73. t.Errorf("expected %v, got %v", e, a)
  74. }
  75. })
  76. }
  77. }