admission_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 storageobjectinuseprotection
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/davecgh/go-spew/spew"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/runtime/schema"
  21. "k8s.io/apiserver/pkg/admission"
  22. utilfeature "k8s.io/apiserver/pkg/util/feature"
  23. featuregatetesting "k8s.io/component-base/featuregate/testing"
  24. api "k8s.io/kubernetes/pkg/apis/core"
  25. "k8s.io/kubernetes/pkg/features"
  26. volumeutil "k8s.io/kubernetes/pkg/volume/util"
  27. )
  28. func TestAdmit(t *testing.T) {
  29. claim := &api.PersistentVolumeClaim{
  30. TypeMeta: metav1.TypeMeta{
  31. Kind: "PersistentVolumeClaim",
  32. },
  33. ObjectMeta: metav1.ObjectMeta{
  34. Name: "claim",
  35. Namespace: "ns",
  36. },
  37. }
  38. pv := &api.PersistentVolume{
  39. TypeMeta: metav1.TypeMeta{
  40. Kind: "PersistentVolume",
  41. },
  42. ObjectMeta: metav1.ObjectMeta{
  43. Name: "pv",
  44. },
  45. }
  46. claimWithFinalizer := claim.DeepCopy()
  47. claimWithFinalizer.Finalizers = []string{volumeutil.PVCProtectionFinalizer}
  48. pvWithFinalizer := pv.DeepCopy()
  49. pvWithFinalizer.Finalizers = []string{volumeutil.PVProtectionFinalizer}
  50. tests := []struct {
  51. name string
  52. resource schema.GroupVersionResource
  53. object runtime.Object
  54. expectedObject runtime.Object
  55. featureEnabled bool
  56. namespace string
  57. }{
  58. {
  59. "create -> add finalizer",
  60. api.SchemeGroupVersion.WithResource("persistentvolumeclaims"),
  61. claim,
  62. claimWithFinalizer,
  63. true,
  64. claim.Namespace,
  65. },
  66. {
  67. "finalizer already exists -> no new finalizer",
  68. api.SchemeGroupVersion.WithResource("persistentvolumeclaims"),
  69. claimWithFinalizer,
  70. claimWithFinalizer,
  71. true,
  72. claimWithFinalizer.Namespace,
  73. },
  74. {
  75. "disabled feature -> no finalizer",
  76. api.SchemeGroupVersion.WithResource("persistentvolumeclaims"),
  77. claim,
  78. claim,
  79. false,
  80. claim.Namespace,
  81. },
  82. {
  83. "create -> add finalizer",
  84. api.SchemeGroupVersion.WithResource("persistentvolumes"),
  85. pv,
  86. pvWithFinalizer,
  87. true,
  88. pv.Namespace,
  89. },
  90. {
  91. "finalizer already exists -> no new finalizer",
  92. api.SchemeGroupVersion.WithResource("persistentvolumes"),
  93. pvWithFinalizer,
  94. pvWithFinalizer,
  95. true,
  96. pvWithFinalizer.Namespace,
  97. },
  98. {
  99. "disabled feature -> no finalizer",
  100. api.SchemeGroupVersion.WithResource("persistentvolumes"),
  101. pv,
  102. pv,
  103. false,
  104. pv.Namespace,
  105. },
  106. }
  107. ctrl := newPlugin()
  108. for _, test := range tests {
  109. t.Run(test.name, func(t *testing.T) {
  110. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.StorageObjectInUseProtection, test.featureEnabled)()
  111. obj := test.object.DeepCopyObject()
  112. attrs := admission.NewAttributesRecord(
  113. obj, // new object
  114. obj.DeepCopyObject(), // old object, copy to be sure it's not modified
  115. schema.GroupVersionKind{},
  116. test.namespace,
  117. "foo",
  118. test.resource,
  119. "", // subresource
  120. admission.Create,
  121. &metav1.CreateOptions{},
  122. false, // dryRun
  123. nil, // userInfo
  124. )
  125. err := ctrl.Admit(attrs, nil)
  126. if err != nil {
  127. t.Errorf("Test %q: got unexpected error: %v", test.name, err)
  128. }
  129. if !reflect.DeepEqual(test.expectedObject, obj) {
  130. t.Errorf("Test %q: Expected object:\n%s\ngot:\n%s", test.name, spew.Sdump(test.expectedObject), spew.Sdump(obj))
  131. }
  132. })
  133. }
  134. }