defaults_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 v1_test
  14. import (
  15. "testing"
  16. "github.com/google/go-cmp/cmp"
  17. v1 "k8s.io/api/admissionregistration/v1"
  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. fail := v1.Fail
  27. equivalent := v1.Equivalent
  28. never := v1.NeverReinvocationPolicy
  29. ten := int32(10)
  30. allScopes := v1.AllScopes
  31. tests := []struct {
  32. name string
  33. original runtime.Object
  34. expected runtime.Object
  35. }{
  36. {
  37. name: "ValidatingWebhookConfiguration",
  38. original: &v1.ValidatingWebhookConfiguration{
  39. Webhooks: []v1.ValidatingWebhook{{}},
  40. },
  41. expected: &v1.ValidatingWebhookConfiguration{
  42. Webhooks: []v1.ValidatingWebhook{{
  43. FailurePolicy: &fail,
  44. MatchPolicy: &equivalent,
  45. TimeoutSeconds: &ten,
  46. NamespaceSelector: &metav1.LabelSelector{},
  47. ObjectSelector: &metav1.LabelSelector{},
  48. }},
  49. },
  50. },
  51. {
  52. name: "MutatingWebhookConfiguration",
  53. original: &v1.MutatingWebhookConfiguration{
  54. Webhooks: []v1.MutatingWebhook{{}},
  55. },
  56. expected: &v1.MutatingWebhookConfiguration{
  57. Webhooks: []v1.MutatingWebhook{{
  58. FailurePolicy: &fail,
  59. MatchPolicy: &equivalent,
  60. ReinvocationPolicy: &never,
  61. TimeoutSeconds: &ten,
  62. NamespaceSelector: &metav1.LabelSelector{},
  63. ObjectSelector: &metav1.LabelSelector{},
  64. }},
  65. },
  66. },
  67. {
  68. name: "scope=*",
  69. original: &v1.MutatingWebhookConfiguration{
  70. Webhooks: []v1.MutatingWebhook{{
  71. Rules: []v1.RuleWithOperations{{}},
  72. }},
  73. },
  74. expected: &v1.MutatingWebhookConfiguration{
  75. Webhooks: []v1.MutatingWebhook{{
  76. Rules: []v1.RuleWithOperations{{Rule: v1.Rule{
  77. Scope: &allScopes, // defaulted
  78. }}},
  79. FailurePolicy: &fail,
  80. MatchPolicy: &equivalent,
  81. ReinvocationPolicy: &never,
  82. TimeoutSeconds: &ten,
  83. NamespaceSelector: &metav1.LabelSelector{},
  84. ObjectSelector: &metav1.LabelSelector{},
  85. }},
  86. },
  87. },
  88. {
  89. name: "port=443",
  90. original: &v1.MutatingWebhookConfiguration{
  91. Webhooks: []v1.MutatingWebhook{{
  92. ClientConfig: v1.WebhookClientConfig{
  93. Service: &v1.ServiceReference{},
  94. },
  95. }},
  96. },
  97. expected: &v1.MutatingWebhookConfiguration{
  98. Webhooks: []v1.MutatingWebhook{{
  99. ClientConfig: v1.WebhookClientConfig{
  100. Service: &v1.ServiceReference{
  101. Port: utilpointer.Int32Ptr(443), // defaulted
  102. },
  103. },
  104. FailurePolicy: &fail,
  105. MatchPolicy: &equivalent,
  106. ReinvocationPolicy: &never,
  107. TimeoutSeconds: &ten,
  108. NamespaceSelector: &metav1.LabelSelector{},
  109. ObjectSelector: &metav1.LabelSelector{},
  110. }},
  111. },
  112. },
  113. }
  114. for _, test := range tests {
  115. t.Run(test.name, func(t *testing.T) {
  116. original := test.original
  117. expected := test.expected
  118. legacyscheme.Scheme.Default(original)
  119. if !apiequality.Semantic.DeepEqual(original, expected) {
  120. t.Error(cmp.Diff(expected, original))
  121. }
  122. })
  123. }
  124. }