mustmatchpatterns_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 sysctl
  14. import (
  15. "testing"
  16. api "k8s.io/kubernetes/pkg/apis/core"
  17. )
  18. func TestValidate(t *testing.T) {
  19. tests := map[string]struct {
  20. whitelist []string
  21. forbiddenSafe []string
  22. allowedUnsafe []string
  23. allowed []string
  24. disallowed []string
  25. }{
  26. // no container requests
  27. "with allow all": {
  28. whitelist: []string{"foo"},
  29. allowed: []string{"foo"},
  30. },
  31. "empty": {
  32. whitelist: []string{"foo"},
  33. forbiddenSafe: []string{"*"},
  34. disallowed: []string{"foo"},
  35. },
  36. "without wildcard": {
  37. whitelist: []string{"a", "a.b"},
  38. allowed: []string{"a", "a.b"},
  39. disallowed: []string{"b"},
  40. },
  41. "with catch-all wildcard and non-wildcard": {
  42. allowedUnsafe: []string{"a.b.c", "*"},
  43. allowed: []string{"a", "a.b", "a.b.c", "b"},
  44. },
  45. "without catch-all wildcard": {
  46. allowedUnsafe: []string{"a.*", "b.*", "c.d.e", "d.e.f.*"},
  47. allowed: []string{"a.b", "b.c", "c.d.e", "d.e.f.g.h"},
  48. disallowed: []string{"a", "b", "c", "c.d", "d.e", "d.e.f"},
  49. },
  50. }
  51. for k, v := range tests {
  52. strategy := NewMustMatchPatterns(v.whitelist, v.allowedUnsafe, v.forbiddenSafe)
  53. pod := &api.Pod{}
  54. errs := strategy.Validate(pod)
  55. if len(errs) != 0 {
  56. t.Errorf("%s: unexpected validaton errors for empty sysctls: %v", k, errs)
  57. }
  58. testAllowed := func() {
  59. sysctls := []api.Sysctl{}
  60. for _, s := range v.allowed {
  61. sysctls = append(sysctls, api.Sysctl{
  62. Name: s,
  63. Value: "dummy",
  64. })
  65. }
  66. pod.Spec.SecurityContext = &api.PodSecurityContext{
  67. Sysctls: sysctls,
  68. }
  69. errs = strategy.Validate(pod)
  70. if len(errs) != 0 {
  71. t.Errorf("%s: unexpected validaton errors for sysctls: %v", k, errs)
  72. }
  73. }
  74. testDisallowed := func() {
  75. for _, s := range v.disallowed {
  76. pod.Spec.SecurityContext = &api.PodSecurityContext{
  77. Sysctls: []api.Sysctl{
  78. {
  79. Name: s,
  80. Value: "dummy",
  81. },
  82. },
  83. }
  84. errs = strategy.Validate(pod)
  85. if len(errs) == 0 {
  86. t.Errorf("%s: expected error for sysctl %q", k, s)
  87. }
  88. }
  89. }
  90. testAllowed()
  91. testDisallowed()
  92. }
  93. }