strategy_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 volumeattachment
  14. import (
  15. "testing"
  16. apiequality "k8s.io/apimachinery/pkg/api/equality"
  17. "k8s.io/apimachinery/pkg/api/resource"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/diff"
  20. "k8s.io/apimachinery/pkg/util/validation/field"
  21. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  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/apis/storage"
  26. "k8s.io/kubernetes/pkg/features"
  27. )
  28. func getValidVolumeAttachment(name string) *storage.VolumeAttachment {
  29. return &storage.VolumeAttachment{
  30. ObjectMeta: metav1.ObjectMeta{
  31. Name: name,
  32. },
  33. Spec: storage.VolumeAttachmentSpec{
  34. Attacher: "valid-attacher",
  35. Source: storage.VolumeAttachmentSource{
  36. PersistentVolumeName: &name,
  37. },
  38. NodeName: "valid-node",
  39. },
  40. }
  41. }
  42. func getValidVolumeAttachmentWithInlineSpec(name string) *storage.VolumeAttachment {
  43. volumeAttachment := getValidVolumeAttachment(name)
  44. volumeAttachment.Spec.Source.PersistentVolumeName = nil
  45. volumeAttachment.Spec.Source.InlineVolumeSpec = &api.PersistentVolumeSpec{
  46. Capacity: api.ResourceList{
  47. api.ResourceName(api.ResourceStorage): resource.MustParse("10"),
  48. },
  49. AccessModes: []api.PersistentVolumeAccessMode{api.ReadWriteOnce},
  50. PersistentVolumeSource: api.PersistentVolumeSource{
  51. CSI: &api.CSIPersistentVolumeSource{
  52. Driver: "com.test.foo",
  53. VolumeHandle: name,
  54. },
  55. },
  56. MountOptions: []string{"soft"},
  57. }
  58. return volumeAttachment
  59. }
  60. func TestVolumeAttachmentStrategy(t *testing.T) {
  61. ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{
  62. APIGroup: "storage.k8s.io",
  63. APIVersion: "v1",
  64. Resource: "volumeattachments",
  65. })
  66. if Strategy.NamespaceScoped() {
  67. t.Errorf("VolumeAttachment must not be namespace scoped")
  68. }
  69. if Strategy.AllowCreateOnUpdate() {
  70. t.Errorf("VolumeAttachment should not allow create on update")
  71. }
  72. volumeAttachment := getValidVolumeAttachment("valid-attachment")
  73. Strategy.PrepareForCreate(ctx, volumeAttachment)
  74. errs := Strategy.Validate(ctx, volumeAttachment)
  75. if len(errs) != 0 {
  76. t.Errorf("unexpected error validating %v", errs)
  77. }
  78. // Create with status should drop status
  79. statusVolumeAttachment := volumeAttachment.DeepCopy()
  80. statusVolumeAttachment.Status = storage.VolumeAttachmentStatus{Attached: true}
  81. Strategy.PrepareForCreate(ctx, statusVolumeAttachment)
  82. if !apiequality.Semantic.DeepEqual(statusVolumeAttachment, volumeAttachment) {
  83. t.Errorf("unexpected objects difference after creating with status: %v", diff.ObjectDiff(statusVolumeAttachment, volumeAttachment))
  84. }
  85. // Update of spec is disallowed
  86. newVolumeAttachment := volumeAttachment.DeepCopy()
  87. newVolumeAttachment.Spec.NodeName = "valid-node-2"
  88. Strategy.PrepareForUpdate(ctx, newVolumeAttachment, volumeAttachment)
  89. errs = Strategy.ValidateUpdate(ctx, newVolumeAttachment, volumeAttachment)
  90. if len(errs) == 0 {
  91. t.Errorf("Expected a validation error")
  92. }
  93. // modifying status should be dropped
  94. statusVolumeAttachment = volumeAttachment.DeepCopy()
  95. statusVolumeAttachment.Status = storage.VolumeAttachmentStatus{Attached: true}
  96. Strategy.PrepareForUpdate(ctx, statusVolumeAttachment, volumeAttachment)
  97. if !apiequality.Semantic.DeepEqual(statusVolumeAttachment, volumeAttachment) {
  98. t.Errorf("unexpected objects difference after modfying status: %v", diff.ObjectDiff(statusVolumeAttachment, volumeAttachment))
  99. }
  100. }
  101. func TestVolumeAttachmentStrategySourceInlineSpec(t *testing.T) {
  102. ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{
  103. APIGroup: "storage.k8s.io",
  104. APIVersion: "v1",
  105. Resource: "volumeattachments",
  106. })
  107. volumeAttachment := getValidVolumeAttachmentWithInlineSpec("valid-attachment")
  108. volumeAttachmentSaved := volumeAttachment.DeepCopy()
  109. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigration, true)()
  110. Strategy.PrepareForCreate(ctx, volumeAttachment)
  111. if volumeAttachment.Spec.Source.InlineVolumeSpec == nil {
  112. t.Errorf("InlineVolumeSpec unexpectedly set to nil during PrepareForCreate")
  113. }
  114. if !apiequality.Semantic.DeepEqual(volumeAttachmentSaved, volumeAttachment) {
  115. t.Errorf("unexpected difference in object after creation: %v", diff.ObjectDiff(volumeAttachment, volumeAttachmentSaved))
  116. }
  117. Strategy.PrepareForUpdate(ctx, volumeAttachmentSaved, volumeAttachment)
  118. if volumeAttachmentSaved.Spec.Source.InlineVolumeSpec == nil {
  119. t.Errorf("InlineVolumeSpec unexpectedly set to nil during PrepareForUpdate")
  120. }
  121. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigration, false)()
  122. Strategy.PrepareForUpdate(ctx, volumeAttachmentSaved, volumeAttachment)
  123. if volumeAttachmentSaved.Spec.Source.InlineVolumeSpec == nil {
  124. t.Errorf("InlineVolumeSpec unexpectedly set to nil during PrepareForUpdate")
  125. }
  126. volumeAttachment = getValidVolumeAttachmentWithInlineSpec("valid-attachment")
  127. volumeAttachmentNew := volumeAttachment.DeepCopy()
  128. defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIMigration, false)()
  129. Strategy.PrepareForCreate(ctx, volumeAttachment)
  130. if volumeAttachment.Spec.Source.InlineVolumeSpec != nil {
  131. t.Errorf("InlineVolumeSpec unexpectedly not dropped during PrepareForCreate")
  132. }
  133. Strategy.PrepareForUpdate(ctx, volumeAttachmentNew, volumeAttachment)
  134. if volumeAttachmentNew.Spec.Source.InlineVolumeSpec != nil {
  135. t.Errorf("InlineVolumeSpec unexpectedly not dropped during PrepareForUpdate")
  136. }
  137. }
  138. func TestVolumeAttachmentStatusStrategy(t *testing.T) {
  139. ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{
  140. APIGroup: "storage.k8s.io",
  141. APIVersion: "v1",
  142. Resource: "volumeattachments",
  143. })
  144. volumeAttachment := getValidVolumeAttachment("valid-attachment")
  145. // modifying status should be allowed
  146. statusVolumeAttachment := volumeAttachment.DeepCopy()
  147. statusVolumeAttachment.Status = storage.VolumeAttachmentStatus{Attached: true}
  148. expectedVolumeAttachment := statusVolumeAttachment.DeepCopy()
  149. StatusStrategy.PrepareForUpdate(ctx, statusVolumeAttachment, volumeAttachment)
  150. if !apiequality.Semantic.DeepEqual(statusVolumeAttachment, expectedVolumeAttachment) {
  151. t.Errorf("unexpected objects differerence after modifying status: %v", diff.ObjectDiff(statusVolumeAttachment, expectedVolumeAttachment))
  152. }
  153. // spec and metadata modifications should be dropped
  154. newVolumeAttachment := volumeAttachment.DeepCopy()
  155. newVolumeAttachment.Spec.NodeName = "valid-node-2"
  156. newVolumeAttachment.Labels = map[string]string{"foo": "bar"}
  157. newVolumeAttachment.Annotations = map[string]string{"foo": "baz"}
  158. newVolumeAttachment.OwnerReferences = []metav1.OwnerReference{
  159. {
  160. APIVersion: "v1",
  161. Kind: "Pod",
  162. Name: "Foo",
  163. },
  164. }
  165. StatusStrategy.PrepareForUpdate(ctx, newVolumeAttachment, volumeAttachment)
  166. if !apiequality.Semantic.DeepEqual(newVolumeAttachment, volumeAttachment) {
  167. t.Errorf("unexpected objects differerence after modifying spec: %v", diff.ObjectDiff(newVolumeAttachment, volumeAttachment))
  168. }
  169. }
  170. func TestBetaAndV1StatusUpdate(t *testing.T) {
  171. tests := []struct {
  172. requestInfo genericapirequest.RequestInfo
  173. newStatus bool
  174. expectedStatus bool
  175. }{
  176. {
  177. genericapirequest.RequestInfo{
  178. APIGroup: "storage.k8s.io",
  179. APIVersion: "v1",
  180. Resource: "volumeattachments",
  181. },
  182. true,
  183. false,
  184. },
  185. {
  186. genericapirequest.RequestInfo{
  187. APIGroup: "storage.k8s.io",
  188. APIVersion: "v1beta1",
  189. Resource: "volumeattachments",
  190. },
  191. true,
  192. true,
  193. },
  194. }
  195. for _, test := range tests {
  196. va := getValidVolumeAttachment("valid-attachment")
  197. newAttachment := va.DeepCopy()
  198. newAttachment.Status.Attached = test.newStatus
  199. context := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &test.requestInfo)
  200. Strategy.PrepareForUpdate(context, newAttachment, va)
  201. if newAttachment.Status.Attached != test.expectedStatus {
  202. t.Errorf("expected status to be %v got %v", test.expectedStatus, newAttachment.Status.Attached)
  203. }
  204. }
  205. }
  206. func TestBetaAndV1StatusCreate(t *testing.T) {
  207. tests := []struct {
  208. requestInfo genericapirequest.RequestInfo
  209. newStatus bool
  210. expectedStatus bool
  211. }{
  212. {
  213. genericapirequest.RequestInfo{
  214. APIGroup: "storage.k8s.io",
  215. APIVersion: "v1",
  216. Resource: "volumeattachments",
  217. },
  218. true,
  219. false,
  220. },
  221. {
  222. genericapirequest.RequestInfo{
  223. APIGroup: "storage.k8s.io",
  224. APIVersion: "v1beta1",
  225. Resource: "volumeattachments",
  226. },
  227. true,
  228. true,
  229. },
  230. }
  231. for _, test := range tests {
  232. va := getValidVolumeAttachment("valid-attachment")
  233. va.Status.Attached = test.newStatus
  234. context := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &test.requestInfo)
  235. Strategy.PrepareForCreate(context, va)
  236. if va.Status.Attached != test.expectedStatus {
  237. t.Errorf("expected status to be %v got %v", test.expectedStatus, va.Status.Attached)
  238. }
  239. }
  240. }
  241. func TestVolumeAttachmentValidation(t *testing.T) {
  242. invalidPVName := "invalid-!@#$%^&*()"
  243. validPVName := "valid-volume-name"
  244. tests := []struct {
  245. name string
  246. volumeAttachment *storage.VolumeAttachment
  247. expectBetaError bool
  248. expectV1Error bool
  249. }{
  250. {
  251. "valid attachment",
  252. getValidVolumeAttachment("foo"),
  253. false,
  254. false,
  255. },
  256. {
  257. "invalid PV name",
  258. &storage.VolumeAttachment{
  259. ObjectMeta: metav1.ObjectMeta{
  260. Name: "foo",
  261. },
  262. Spec: storage.VolumeAttachmentSpec{
  263. Attacher: "valid-attacher",
  264. Source: storage.VolumeAttachmentSource{
  265. PersistentVolumeName: &invalidPVName,
  266. },
  267. NodeName: "valid-node",
  268. },
  269. },
  270. false,
  271. true,
  272. },
  273. {
  274. "invalid attacher name",
  275. &storage.VolumeAttachment{
  276. ObjectMeta: metav1.ObjectMeta{
  277. Name: "foo",
  278. },
  279. Spec: storage.VolumeAttachmentSpec{
  280. Attacher: "invalid!@#$%^&*()",
  281. Source: storage.VolumeAttachmentSource{
  282. PersistentVolumeName: &validPVName,
  283. },
  284. NodeName: "valid-node",
  285. },
  286. },
  287. false,
  288. true,
  289. },
  290. {
  291. "invalid volume attachment",
  292. &storage.VolumeAttachment{
  293. ObjectMeta: metav1.ObjectMeta{
  294. Name: "foo",
  295. },
  296. Spec: storage.VolumeAttachmentSpec{
  297. Attacher: "invalid!@#$%^&*()",
  298. Source: storage.VolumeAttachmentSource{
  299. PersistentVolumeName: nil,
  300. },
  301. NodeName: "valid-node",
  302. },
  303. },
  304. true,
  305. true,
  306. },
  307. }
  308. for _, test := range tests {
  309. t.Run(test.name, func(t *testing.T) {
  310. testValidation := func(va *storage.VolumeAttachment, apiVersion string) field.ErrorList {
  311. ctx := genericapirequest.WithRequestInfo(genericapirequest.NewContext(), &genericapirequest.RequestInfo{
  312. APIGroup: "storage.k8s.io",
  313. APIVersion: apiVersion,
  314. Resource: "volumeattachments",
  315. })
  316. return Strategy.Validate(ctx, va)
  317. }
  318. v1Err := testValidation(test.volumeAttachment, "v1")
  319. if len(v1Err) > 0 && !test.expectV1Error {
  320. t.Errorf("Validation of v1 object failed: %+v", v1Err)
  321. }
  322. if len(v1Err) == 0 && test.expectV1Error {
  323. t.Errorf("Validation of v1 object unexpectedly succeeded")
  324. }
  325. betaErr := testValidation(test.volumeAttachment, "v1beta1")
  326. if len(betaErr) > 0 && !test.expectBetaError {
  327. t.Errorf("Validation of v1beta1 object failed: %+v", betaErr)
  328. }
  329. if len(betaErr) == 0 && test.expectBetaError {
  330. t.Errorf("Validation of v1beta1 object unexpectedly succeeded")
  331. }
  332. })
  333. }
  334. }