helpers_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright 2016 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 extensions
  14. import (
  15. "reflect"
  16. "testing"
  17. )
  18. func TestPodAnnotationsFromSysctls(t *testing.T) {
  19. type Test struct {
  20. sysctls []string
  21. expectedValue string
  22. }
  23. for _, test := range []Test{
  24. {sysctls: []string{"a.b"}, expectedValue: "a.b"},
  25. {sysctls: []string{"a.b", "c.d"}, expectedValue: "a.b,c.d"},
  26. {sysctls: []string{"a.b", "a.b"}, expectedValue: "a.b,a.b"},
  27. {sysctls: []string{}, expectedValue: ""},
  28. {sysctls: nil, expectedValue: ""},
  29. } {
  30. a := PodAnnotationsFromSysctls(test.sysctls)
  31. if a != test.expectedValue {
  32. t.Errorf("wrong value for %v: got=%q wanted=%q", test.sysctls, a, test.expectedValue)
  33. }
  34. }
  35. }
  36. func TestSysctlsFromPodSecurityPolicyAnnotation(t *testing.T) {
  37. type Test struct {
  38. expectedValue []string
  39. annotation string
  40. }
  41. for _, test := range []Test{
  42. {annotation: "a.b", expectedValue: []string{"a.b"}},
  43. {annotation: "a.b,c.d", expectedValue: []string{"a.b", "c.d"}},
  44. {annotation: "a.b,a.b", expectedValue: []string{"a.b", "a.b"}},
  45. {annotation: "", expectedValue: []string{}},
  46. } {
  47. sysctls, err := SysctlsFromPodSecurityPolicyAnnotation(test.annotation)
  48. if err != nil {
  49. t.Errorf("error for %q: %v", test.annotation, err)
  50. }
  51. if !reflect.DeepEqual(sysctls, test.expectedValue) {
  52. t.Errorf("wrong value for %q: got=%v wanted=%v", test.annotation, sysctls, test.expectedValue)
  53. }
  54. }
  55. }