defaults_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright 2019 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 v1beta1_test
  14. import (
  15. "testing"
  16. "github.com/google/go-cmp/cmp"
  17. v1beta1 "k8s.io/api/admissionregistration/v1beta1"
  18. apiequality "k8s.io/apimachinery/pkg/api/equality"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/kubernetes/pkg/api/legacyscheme"
  22. _ "k8s.io/kubernetes/pkg/apis/admissionregistration/install"
  23. utilpointer "k8s.io/utils/pointer"
  24. )
  25. func TestDefaultAdmissionWebhook(t *testing.T) {
  26. ignore := v1beta1.Ignore
  27. exact := v1beta1.Exact
  28. never := v1beta1.NeverReinvocationPolicy
  29. thirty := int32(30)
  30. allScopes := v1beta1.AllScopes
  31. unknown := v1beta1.SideEffectClassUnknown
  32. tests := []struct {
  33. name string
  34. original runtime.Object
  35. expected runtime.Object
  36. }{
  37. {
  38. name: "ValidatingWebhookConfiguration",
  39. original: &v1beta1.ValidatingWebhookConfiguration{
  40. Webhooks: []v1beta1.ValidatingWebhook{{}},
  41. },
  42. expected: &v1beta1.ValidatingWebhookConfiguration{
  43. Webhooks: []v1beta1.ValidatingWebhook{{
  44. FailurePolicy: &ignore,
  45. MatchPolicy: &exact,
  46. TimeoutSeconds: &thirty,
  47. NamespaceSelector: &metav1.LabelSelector{},
  48. ObjectSelector: &metav1.LabelSelector{},
  49. SideEffects: &unknown,
  50. AdmissionReviewVersions: []string{"v1beta1"},
  51. }},
  52. },
  53. },
  54. {
  55. name: "MutatingWebhookConfiguration",
  56. original: &v1beta1.MutatingWebhookConfiguration{
  57. Webhooks: []v1beta1.MutatingWebhook{{}},
  58. },
  59. expected: &v1beta1.MutatingWebhookConfiguration{
  60. Webhooks: []v1beta1.MutatingWebhook{{
  61. FailurePolicy: &ignore,
  62. MatchPolicy: &exact,
  63. ReinvocationPolicy: &never,
  64. TimeoutSeconds: &thirty,
  65. NamespaceSelector: &metav1.LabelSelector{},
  66. ObjectSelector: &metav1.LabelSelector{},
  67. SideEffects: &unknown,
  68. AdmissionReviewVersions: []string{"v1beta1"},
  69. }},
  70. },
  71. },
  72. {
  73. name: "scope=*",
  74. original: &v1beta1.MutatingWebhookConfiguration{
  75. Webhooks: []v1beta1.MutatingWebhook{{
  76. Rules: []v1beta1.RuleWithOperations{{}},
  77. }},
  78. },
  79. expected: &v1beta1.MutatingWebhookConfiguration{
  80. Webhooks: []v1beta1.MutatingWebhook{{
  81. Rules: []v1beta1.RuleWithOperations{{Rule: v1beta1.Rule{
  82. Scope: &allScopes, // defaulted
  83. }}},
  84. FailurePolicy: &ignore,
  85. MatchPolicy: &exact,
  86. ReinvocationPolicy: &never,
  87. TimeoutSeconds: &thirty,
  88. NamespaceSelector: &metav1.LabelSelector{},
  89. ObjectSelector: &metav1.LabelSelector{},
  90. SideEffects: &unknown,
  91. AdmissionReviewVersions: []string{"v1beta1"},
  92. }},
  93. },
  94. },
  95. {
  96. name: "port=443",
  97. original: &v1beta1.MutatingWebhookConfiguration{
  98. Webhooks: []v1beta1.MutatingWebhook{{
  99. ClientConfig: v1beta1.WebhookClientConfig{
  100. Service: &v1beta1.ServiceReference{},
  101. },
  102. }},
  103. },
  104. expected: &v1beta1.MutatingWebhookConfiguration{
  105. Webhooks: []v1beta1.MutatingWebhook{{
  106. ClientConfig: v1beta1.WebhookClientConfig{
  107. Service: &v1beta1.ServiceReference{
  108. Port: utilpointer.Int32Ptr(443), // defaulted
  109. },
  110. },
  111. FailurePolicy: &ignore,
  112. MatchPolicy: &exact,
  113. ReinvocationPolicy: &never,
  114. TimeoutSeconds: &thirty,
  115. NamespaceSelector: &metav1.LabelSelector{},
  116. ObjectSelector: &metav1.LabelSelector{},
  117. SideEffects: &unknown,
  118. AdmissionReviewVersions: []string{"v1beta1"},
  119. }},
  120. },
  121. },
  122. }
  123. for _, test := range tests {
  124. t.Run(test.name, func(t *testing.T) {
  125. original := test.original
  126. expected := test.expected
  127. legacyscheme.Scheme.Default(original)
  128. if !apiequality.Semantic.DeepEqual(original, expected) {
  129. t.Error(cmp.Diff(expected, original))
  130. }
  131. })
  132. }
  133. }