defaults_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 v1alpha1_test
  14. import (
  15. "reflect"
  16. "testing"
  17. auditregistrationv1alpha1 "k8s.io/api/auditregistration/v1alpha1"
  18. apiequality "k8s.io/apimachinery/pkg/api/equality"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. _ "k8s.io/kubernetes/pkg/apis/auditregistration/install"
  22. . "k8s.io/kubernetes/pkg/apis/auditregistration/v1alpha1"
  23. utilpointer "k8s.io/utils/pointer"
  24. )
  25. func TestSetDefaultAuditSink(t *testing.T) {
  26. defaultURL := "http://test"
  27. tests := []struct {
  28. original *auditregistrationv1alpha1.AuditSink
  29. expected *auditregistrationv1alpha1.AuditSink
  30. }{
  31. { // Missing Throttle
  32. original: &auditregistrationv1alpha1.AuditSink{
  33. Spec: auditregistrationv1alpha1.AuditSinkSpec{
  34. Policy: auditregistrationv1alpha1.Policy{
  35. Level: auditregistrationv1alpha1.LevelMetadata,
  36. },
  37. Webhook: auditregistrationv1alpha1.Webhook{
  38. ClientConfig: auditregistrationv1alpha1.WebhookClientConfig{
  39. URL: &defaultURL,
  40. },
  41. },
  42. },
  43. },
  44. expected: &auditregistrationv1alpha1.AuditSink{
  45. Spec: auditregistrationv1alpha1.AuditSinkSpec{
  46. Policy: auditregistrationv1alpha1.Policy{
  47. Level: auditregistrationv1alpha1.LevelMetadata,
  48. },
  49. Webhook: auditregistrationv1alpha1.Webhook{
  50. Throttle: DefaultThrottle(),
  51. ClientConfig: auditregistrationv1alpha1.WebhookClientConfig{
  52. URL: &defaultURL,
  53. },
  54. },
  55. },
  56. },
  57. },
  58. { // Missing QPS
  59. original: &auditregistrationv1alpha1.AuditSink{
  60. Spec: auditregistrationv1alpha1.AuditSinkSpec{
  61. Policy: auditregistrationv1alpha1.Policy{
  62. Level: auditregistrationv1alpha1.LevelMetadata,
  63. },
  64. Webhook: auditregistrationv1alpha1.Webhook{
  65. Throttle: &auditregistrationv1alpha1.WebhookThrottleConfig{
  66. Burst: utilpointer.Int64Ptr(1),
  67. },
  68. ClientConfig: auditregistrationv1alpha1.WebhookClientConfig{
  69. URL: &defaultURL,
  70. },
  71. },
  72. },
  73. },
  74. expected: &auditregistrationv1alpha1.AuditSink{
  75. Spec: auditregistrationv1alpha1.AuditSinkSpec{
  76. Policy: auditregistrationv1alpha1.Policy{
  77. Level: auditregistrationv1alpha1.LevelMetadata,
  78. },
  79. Webhook: auditregistrationv1alpha1.Webhook{
  80. Throttle: &auditregistrationv1alpha1.WebhookThrottleConfig{
  81. QPS: DefaultThrottle().QPS,
  82. Burst: utilpointer.Int64Ptr(1),
  83. },
  84. ClientConfig: auditregistrationv1alpha1.WebhookClientConfig{
  85. URL: &defaultURL,
  86. },
  87. },
  88. },
  89. },
  90. },
  91. { // Missing Burst
  92. original: &auditregistrationv1alpha1.AuditSink{
  93. Spec: auditregistrationv1alpha1.AuditSinkSpec{
  94. Policy: auditregistrationv1alpha1.Policy{
  95. Level: auditregistrationv1alpha1.LevelMetadata,
  96. },
  97. Webhook: auditregistrationv1alpha1.Webhook{
  98. Throttle: &auditregistrationv1alpha1.WebhookThrottleConfig{
  99. QPS: utilpointer.Int64Ptr(1),
  100. },
  101. ClientConfig: auditregistrationv1alpha1.WebhookClientConfig{
  102. URL: &defaultURL,
  103. },
  104. },
  105. },
  106. },
  107. expected: &auditregistrationv1alpha1.AuditSink{
  108. Spec: auditregistrationv1alpha1.AuditSinkSpec{
  109. Policy: auditregistrationv1alpha1.Policy{
  110. Level: auditregistrationv1alpha1.LevelMetadata,
  111. },
  112. Webhook: auditregistrationv1alpha1.Webhook{
  113. Throttle: &auditregistrationv1alpha1.WebhookThrottleConfig{
  114. QPS: utilpointer.Int64Ptr(1),
  115. Burst: DefaultThrottle().Burst,
  116. },
  117. ClientConfig: auditregistrationv1alpha1.WebhookClientConfig{
  118. URL: &defaultURL,
  119. },
  120. },
  121. },
  122. },
  123. },
  124. }
  125. for i, test := range tests {
  126. original := test.original
  127. expected := test.expected
  128. obj2 := roundTrip(t, runtime.Object(original))
  129. got, ok := obj2.(*auditregistrationv1alpha1.AuditSink)
  130. if !ok {
  131. t.Fatalf("(%d) unexpected object: %v", i, obj2)
  132. }
  133. if !apiequality.Semantic.DeepEqual(got.Spec, expected.Spec) {
  134. t.Errorf("(%d) got different than expected\ngot:\n\t%+v\nexpected:\n\t%+v", i, got.Spec, expected.Spec)
  135. }
  136. }
  137. }
  138. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  139. data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
  140. if err != nil {
  141. t.Errorf("%v\n %#v", err, obj)
  142. return nil
  143. }
  144. obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
  145. if err != nil {
  146. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  147. return nil
  148. }
  149. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  150. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  151. if err != nil {
  152. t.Errorf("%v\nSource: %#v", err, obj2)
  153. return nil
  154. }
  155. return obj3
  156. }