admission_test.go 3.6 KB

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