strategy_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 controllerrevision
  14. import (
  15. "testing"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  19. "k8s.io/kubernetes/pkg/apis/apps"
  20. api "k8s.io/kubernetes/pkg/apis/core"
  21. )
  22. func TestStrategy_NamespaceScoped(t *testing.T) {
  23. if !Strategy.NamespaceScoped() {
  24. t.Error("ControllerRevision strategy must be namespace scoped")
  25. }
  26. }
  27. func TestStrategy_AllowCreateOnUpdate(t *testing.T) {
  28. if Strategy.AllowCreateOnUpdate() {
  29. t.Error("ControllerRevision should not be created on update")
  30. }
  31. }
  32. func TestStrategy_Validate(t *testing.T) {
  33. ctx := genericapirequest.NewDefaultContext()
  34. var (
  35. valid = newControllerRevision("validname", "validns", newObject(), 0)
  36. badRevision = newControllerRevision("validname", "validns", newObject(), -1)
  37. emptyName = newControllerRevision("", "validns", newObject(), 0)
  38. invalidName = newControllerRevision("NoUppercaseOrSpecialCharsLike=Equals", "validns", newObject(), 0)
  39. emptyNs = newControllerRevision("validname", "", newObject(), 100)
  40. invalidNs = newControllerRevision("validname", "NoUppercaseOrSpecialCharsLike=Equals", newObject(), 100)
  41. nilData = newControllerRevision("validname", "validns", nil, 0)
  42. )
  43. tests := map[string]struct {
  44. history *apps.ControllerRevision
  45. isValid bool
  46. }{
  47. "valid": {valid, true},
  48. "negative revision": {badRevision, false},
  49. "empty name": {emptyName, false},
  50. "invalid name": {invalidName, false},
  51. "empty namespace": {emptyNs, false},
  52. "invalid namespace": {invalidNs, false},
  53. "nil data": {nilData, false},
  54. }
  55. for name, tc := range tests {
  56. errs := Strategy.Validate(ctx, tc.history)
  57. if tc.isValid && len(errs) > 0 {
  58. t.Errorf("%v: unexpected error: %v", name, errs)
  59. }
  60. if !tc.isValid && len(errs) == 0 {
  61. t.Errorf("%v: unexpected non-error", name)
  62. }
  63. }
  64. }
  65. func TestStrategy_ValidateUpdate(t *testing.T) {
  66. ctx := genericapirequest.NewDefaultContext()
  67. var (
  68. valid = newControllerRevision("validname", "validns", newObject(), 0)
  69. changedData = newControllerRevision("validname", "validns",
  70. func() runtime.Object {
  71. modified := newObject()
  72. ss := modified.(*apps.StatefulSet)
  73. ss.Name = "cde"
  74. return modified
  75. }(), 0)
  76. changedRevision = newControllerRevision("validname", "validns", newObject(), 1)
  77. )
  78. cases := []struct {
  79. name string
  80. newHistory *apps.ControllerRevision
  81. oldHistory *apps.ControllerRevision
  82. isValid bool
  83. }{
  84. {
  85. name: "valid",
  86. newHistory: valid,
  87. oldHistory: valid,
  88. isValid: true,
  89. },
  90. {
  91. name: "changed data",
  92. newHistory: changedData,
  93. oldHistory: valid,
  94. isValid: false,
  95. },
  96. {
  97. name: "changed revision",
  98. newHistory: changedRevision,
  99. oldHistory: valid,
  100. isValid: true,
  101. },
  102. }
  103. for _, tc := range cases {
  104. errs := Strategy.ValidateUpdate(ctx, tc.newHistory, tc.oldHistory)
  105. if tc.isValid && len(errs) > 0 {
  106. t.Errorf("%v: unexpected error: %v", tc.name, errs)
  107. }
  108. if !tc.isValid && len(errs) == 0 {
  109. t.Errorf("%v: unexpected non-error", tc.name)
  110. }
  111. }
  112. }
  113. func newControllerRevision(name, namespace string, data runtime.Object, revision int64) *apps.ControllerRevision {
  114. return &apps.ControllerRevision{
  115. ObjectMeta: metav1.ObjectMeta{
  116. Name: name,
  117. Namespace: namespace,
  118. ResourceVersion: "1",
  119. Labels: map[string]string{"foo": "bar"},
  120. },
  121. Data: data,
  122. Revision: revision,
  123. }
  124. }
  125. func newObject() runtime.Object {
  126. return &apps.StatefulSet{
  127. ObjectMeta: metav1.ObjectMeta{Name: "abc", Namespace: metav1.NamespaceDefault},
  128. Spec: apps.StatefulSetSpec{
  129. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
  130. Template: api.PodTemplateSpec{
  131. Spec: api.PodSpec{
  132. RestartPolicy: api.RestartPolicyAlways,
  133. DNSPolicy: api.DNSClusterFirst,
  134. },
  135. ObjectMeta: metav1.ObjectMeta{
  136. Labels: map[string]string{"foo": "bar"},
  137. },
  138. },
  139. },
  140. }
  141. }