strategy_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Copyright 2015 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 daemonset
  14. import (
  15. "reflect"
  16. "testing"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  20. "k8s.io/apiserver/pkg/registry/rest"
  21. "k8s.io/kubernetes/pkg/apis/apps"
  22. api "k8s.io/kubernetes/pkg/apis/core"
  23. )
  24. const (
  25. fakeImageName = "fake-name"
  26. fakeImage = "fakeimage"
  27. daemonsetName = "test-daemonset"
  28. namespace = "test-namespace"
  29. )
  30. func TestDaemonsetDefaultGarbageCollectionPolicy(t *testing.T) {
  31. // Make sure we correctly implement the interface.
  32. // Otherwise a typo could silently change the default.
  33. var gcds rest.GarbageCollectionDeleteStrategy = Strategy
  34. tests := []struct {
  35. requestInfo genericapirequest.RequestInfo
  36. expectedGCPolicy rest.GarbageCollectionPolicy
  37. isNilRequestInfo bool
  38. }{
  39. {
  40. genericapirequest.RequestInfo{
  41. APIGroup: "extensions",
  42. APIVersion: "v1beta1",
  43. Resource: "daemonsets",
  44. },
  45. rest.OrphanDependents,
  46. false,
  47. },
  48. {
  49. genericapirequest.RequestInfo{
  50. APIGroup: "apps",
  51. APIVersion: "v1beta2",
  52. Resource: "daemonsets",
  53. },
  54. rest.OrphanDependents,
  55. false,
  56. },
  57. {
  58. genericapirequest.RequestInfo{
  59. APIGroup: "apps",
  60. APIVersion: "v1",
  61. Resource: "daemonsets",
  62. },
  63. rest.DeleteDependents,
  64. false,
  65. },
  66. {
  67. expectedGCPolicy: rest.DeleteDependents,
  68. isNilRequestInfo: true,
  69. },
  70. }
  71. for _, test := range tests {
  72. context := genericapirequest.NewContext()
  73. if !test.isNilRequestInfo {
  74. context = genericapirequest.WithRequestInfo(context, &test.requestInfo)
  75. }
  76. if got, want := gcds.DefaultGarbageCollectionPolicy(context), test.expectedGCPolicy; got != want {
  77. t.Errorf("%s/%s: DefaultGarbageCollectionPolicy() = %#v, want %#v", test.requestInfo.APIGroup,
  78. test.requestInfo.APIVersion, got, want)
  79. }
  80. }
  81. }
  82. func TestSelectorImmutability(t *testing.T) {
  83. tests := []struct {
  84. requestInfo genericapirequest.RequestInfo
  85. oldSelectorLabels map[string]string
  86. newSelectorLabels map[string]string
  87. expectedErrorList field.ErrorList
  88. }{
  89. {
  90. genericapirequest.RequestInfo{
  91. APIGroup: "apps",
  92. APIVersion: "v1",
  93. Resource: "daemonsets",
  94. },
  95. map[string]string{"a": "b"},
  96. map[string]string{"c": "d"},
  97. field.ErrorList{
  98. &field.Error{
  99. Type: field.ErrorTypeInvalid,
  100. Field: field.NewPath("spec").Child("selector").String(),
  101. BadValue: &metav1.LabelSelector{
  102. MatchLabels: map[string]string{"c": "d"},
  103. MatchExpressions: []metav1.LabelSelectorRequirement{},
  104. },
  105. Detail: "field is immutable",
  106. },
  107. },
  108. },
  109. {
  110. genericapirequest.RequestInfo{
  111. APIGroup: "apps",
  112. APIVersion: "v1beta2",
  113. Resource: "daemonsets",
  114. },
  115. map[string]string{"a": "b"},
  116. map[string]string{"c": "d"},
  117. field.ErrorList{
  118. &field.Error{
  119. Type: field.ErrorTypeInvalid,
  120. Field: field.NewPath("spec").Child("selector").String(),
  121. BadValue: &metav1.LabelSelector{
  122. MatchLabels: map[string]string{"c": "d"},
  123. MatchExpressions: []metav1.LabelSelectorRequirement{},
  124. },
  125. Detail: "field is immutable",
  126. },
  127. },
  128. },
  129. {
  130. genericapirequest.RequestInfo{
  131. APIGroup: "extensions",
  132. APIVersion: "v1beta1",
  133. Resource: "daemonsets",
  134. },
  135. map[string]string{"a": "b"},
  136. map[string]string{"c": "d"},
  137. field.ErrorList{},
  138. },
  139. }
  140. for _, test := range tests {
  141. oldDaemonSet := newDaemonSetWithSelectorLabels(test.oldSelectorLabels, 1)
  142. newDaemonSet := newDaemonSetWithSelectorLabels(test.newSelectorLabels, 2)
  143. context := genericapirequest.NewContext()
  144. context = genericapirequest.WithRequestInfo(context, &test.requestInfo)
  145. errorList := daemonSetStrategy{}.ValidateUpdate(context, newDaemonSet, oldDaemonSet)
  146. if !reflect.DeepEqual(test.expectedErrorList, errorList) {
  147. t.Errorf("Unexpected error list, expected: %v, actual: %v", test.expectedErrorList, errorList)
  148. }
  149. }
  150. }
  151. func newDaemonSetWithSelectorLabels(selectorLabels map[string]string, templateGeneration int64) *apps.DaemonSet {
  152. return &apps.DaemonSet{
  153. ObjectMeta: metav1.ObjectMeta{
  154. Name: daemonsetName,
  155. Namespace: namespace,
  156. ResourceVersion: "1",
  157. },
  158. Spec: apps.DaemonSetSpec{
  159. Selector: &metav1.LabelSelector{
  160. MatchLabels: selectorLabels,
  161. MatchExpressions: []metav1.LabelSelectorRequirement{},
  162. },
  163. UpdateStrategy: apps.DaemonSetUpdateStrategy{
  164. Type: apps.OnDeleteDaemonSetStrategyType,
  165. },
  166. TemplateGeneration: templateGeneration,
  167. Template: api.PodTemplateSpec{
  168. ObjectMeta: metav1.ObjectMeta{
  169. Labels: selectorLabels,
  170. },
  171. Spec: api.PodSpec{
  172. RestartPolicy: api.RestartPolicyAlways,
  173. DNSPolicy: api.DNSClusterFirst,
  174. Containers: []api.Container{{Name: fakeImageName, Image: fakeImage, ImagePullPolicy: "IfNotPresent", TerminationMessagePolicy: api.TerminationMessageReadFile}},
  175. },
  176. },
  177. },
  178. }
  179. }