helpers_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright 2017 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 rbac
  14. import (
  15. "testing"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. kapi "k8s.io/kubernetes/pkg/apis/core"
  19. kapihelper "k8s.io/kubernetes/pkg/apis/core/helper"
  20. )
  21. func newPod() *kapi.Pod {
  22. return &kapi.Pod{
  23. ObjectMeta: metav1.ObjectMeta{
  24. Annotations: map[string]string{},
  25. Name: "foo",
  26. OwnerReferences: []metav1.OwnerReference{},
  27. },
  28. }
  29. }
  30. func TestIsOnlyMutatingGCFields(t *testing.T) {
  31. tests := []struct {
  32. name string
  33. obj func() runtime.Object
  34. old func() runtime.Object
  35. expected bool
  36. }{
  37. {
  38. name: "same",
  39. obj: func() runtime.Object {
  40. return newPod()
  41. },
  42. old: func() runtime.Object {
  43. return newPod()
  44. },
  45. expected: true,
  46. },
  47. {
  48. name: "only annotations",
  49. obj: func() runtime.Object {
  50. obj := newPod()
  51. obj.Annotations["foo"] = "bar"
  52. return obj
  53. },
  54. old: func() runtime.Object {
  55. return newPod()
  56. },
  57. expected: false,
  58. },
  59. {
  60. name: "only other",
  61. obj: func() runtime.Object {
  62. obj := newPod()
  63. obj.Spec.RestartPolicy = kapi.RestartPolicyAlways
  64. return obj
  65. },
  66. old: func() runtime.Object {
  67. return newPod()
  68. },
  69. expected: false,
  70. },
  71. {
  72. name: "only ownerRef",
  73. obj: func() runtime.Object {
  74. obj := newPod()
  75. obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"})
  76. return obj
  77. },
  78. old: func() runtime.Object {
  79. return newPod()
  80. },
  81. expected: true,
  82. },
  83. {
  84. name: "ownerRef and finalizer",
  85. obj: func() runtime.Object {
  86. obj := newPod()
  87. obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"})
  88. obj.Finalizers = []string{"final"}
  89. return obj
  90. },
  91. old: func() runtime.Object {
  92. return newPod()
  93. },
  94. expected: true,
  95. },
  96. {
  97. name: "and annotations",
  98. obj: func() runtime.Object {
  99. obj := newPod()
  100. obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"})
  101. obj.Annotations["foo"] = "bar"
  102. return obj
  103. },
  104. old: func() runtime.Object {
  105. return newPod()
  106. },
  107. expected: false,
  108. },
  109. {
  110. name: "and other",
  111. obj: func() runtime.Object {
  112. obj := newPod()
  113. obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"})
  114. obj.Spec.RestartPolicy = kapi.RestartPolicyAlways
  115. return obj
  116. },
  117. old: func() runtime.Object {
  118. return newPod()
  119. },
  120. expected: false,
  121. },
  122. {
  123. name: "and nil",
  124. obj: func() runtime.Object {
  125. obj := newPod()
  126. obj.OwnerReferences = append(obj.OwnerReferences, metav1.OwnerReference{Name: "foo"})
  127. obj.Spec.RestartPolicy = kapi.RestartPolicyAlways
  128. return obj
  129. },
  130. old: func() runtime.Object {
  131. return (*kapi.Pod)(nil)
  132. },
  133. expected: false,
  134. },
  135. }
  136. for _, tc := range tests {
  137. actual := IsOnlyMutatingGCFields(tc.obj(), tc.old(), kapihelper.Semantic)
  138. if tc.expected != actual {
  139. t.Errorf("%s: expected %v, got %v", tc.name, tc.expected, actual)
  140. }
  141. }
  142. }