|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package storage |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + storagev1 "k8s.io/api/storage/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/labels" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + utilrand "k8s.io/apimachinery/pkg/util/rand" |
| 29 | + "k8s.io/client-go/util/retry" |
| 30 | + "k8s.io/kubernetes/test/e2e/framework" |
| 31 | + "k8s.io/kubernetes/test/e2e/storage/utils" |
| 32 | + |
| 33 | + "github.com/onsi/ginkgo/v2" |
| 34 | + "github.com/onsi/gomega" |
| 35 | +) |
| 36 | + |
| 37 | +var _ = utils.SIGDescribe("CSINodes", func() { |
| 38 | + |
| 39 | + f := framework.NewDefaultFramework("csinodes") |
| 40 | + |
| 41 | + ginkgo.Describe("CSI Conformance", func() { |
| 42 | + |
| 43 | + ginkgo.It("should run through the lifecycle of a csinode", func(ctx context.Context) { |
| 44 | + |
| 45 | + csiNodeClient := f.ClientSet.StorageV1().CSINodes() |
| 46 | + |
| 47 | + initialCSINode := storagev1.CSINode{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "e2e-csinode-" + utilrand.String(5), |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + ginkgo.By(fmt.Sprintf("Creating initial csiNode %q", initialCSINode.Name)) |
| 54 | + csiNode, err := csiNodeClient.Create(ctx, &initialCSINode, metav1.CreateOptions{}) |
| 55 | + framework.ExpectNoError(err, "failed to create csiNode %q", initialCSINode.Name) |
| 56 | + |
| 57 | + ginkgo.By(fmt.Sprintf("Getting initial csiNode %q", initialCSINode.Name)) |
| 58 | + retrievedCSINode, err := csiNodeClient.Get(ctx, initialCSINode.Name, metav1.GetOptions{}) |
| 59 | + framework.ExpectNoError(err, "Failed to retrieve csiNode %q", initialCSINode.Name) |
| 60 | + gomega.Expect(retrievedCSINode.Name).To(gomega.Equal(csiNode.Name), "Checking that the retrieved name has been found") |
| 61 | + |
| 62 | + ginkgo.By(fmt.Sprintf("Patching initial csiNode: %q", initialCSINode.Name)) |
| 63 | + payload := "{\"metadata\":{\"labels\":{\"" + csiNode.Name + "\":\"patched\"}}}" |
| 64 | + patchedCSINode, err := csiNodeClient.Patch(ctx, csiNode.Name, types.StrategicMergePatchType, []byte(payload), metav1.PatchOptions{}) |
| 65 | + framework.ExpectNoError(err, "Failed to patch csiNode %q", csiNode.Name) |
| 66 | + gomega.Expect(patchedCSINode.Labels).To(gomega.HaveKeyWithValue(csiNode.Name, "patched"), "Checking that patched label has been applied") |
| 67 | + |
| 68 | + patchedSelector := labels.Set{csiNode.Name: "patched"}.AsSelector().String() |
| 69 | + ginkgo.By(fmt.Sprintf("Listing csiNodes with LabelSelector %q", patchedSelector)) |
| 70 | + csiNodeList, err := csiNodeClient.List(ctx, metav1.ListOptions{LabelSelector: patchedSelector}) |
| 71 | + framework.ExpectNoError(err, "failed to list csiNodes") |
| 72 | + gomega.Expect(csiNodeList.Items).To(gomega.HaveLen(1)) |
| 73 | + |
| 74 | + ginkgo.By(fmt.Sprintf("Delete initial csiNode: %q", initialCSINode.Name)) |
| 75 | + err = csiNodeClient.Delete(ctx, csiNode.Name, metav1.DeleteOptions{}) |
| 76 | + framework.ExpectNoError(err, "failed to delete csiNode %q", initialCSINode.Name) |
| 77 | + |
| 78 | + ginkgo.By(fmt.Sprintf("Confirm deletion of csiNode %q", initialCSINode.Name)) |
| 79 | + |
| 80 | + type state struct { |
| 81 | + CSINodes []storagev1.CSINode |
| 82 | + } |
| 83 | + |
| 84 | + err = framework.Gomega().Eventually(ctx, framework.HandleRetry(func(ctx context.Context) (*state, error) { |
| 85 | + csiNodeList, err := csiNodeClient.List(ctx, metav1.ListOptions{LabelSelector: patchedSelector}) |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("failed to list CSINode: %w", err) |
| 88 | + } |
| 89 | + return &state{ |
| 90 | + CSINodes: csiNodeList.Items, |
| 91 | + }, nil |
| 92 | + })).WithTimeout(30 * time.Second).Should(framework.MakeMatcher(func(s *state) (func() string, error) { |
| 93 | + if len(s.CSINodes) == 0 { |
| 94 | + return nil, nil |
| 95 | + } |
| 96 | + return func() string { |
| 97 | + return fmt.Sprintf("Expected CSINode to be deleted, found %q", s.CSINodes[0].Name) |
| 98 | + }, nil |
| 99 | + })) |
| 100 | + framework.ExpectNoError(err, "Timeout while waiting to confirm CSINode %q deletion", initialCSINode.Name) |
| 101 | + |
| 102 | + replacementCSINode := storagev1.CSINode{ |
| 103 | + ObjectMeta: metav1.ObjectMeta{ |
| 104 | + Name: "e2e-csinode-" + utilrand.String(5), |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + ginkgo.By(fmt.Sprintf("Creating replacement csiNode %q", replacementCSINode.Name)) |
| 109 | + secondCSINode, err := csiNodeClient.Create(ctx, &replacementCSINode, metav1.CreateOptions{}) |
| 110 | + framework.ExpectNoError(err, "failed to create csiNode %q", replacementCSINode.Name) |
| 111 | + |
| 112 | + ginkgo.By(fmt.Sprintf("Getting replacement csiNode %q", replacementCSINode.Name)) |
| 113 | + retrievedCSINode, err = csiNodeClient.Get(ctx, secondCSINode.Name, metav1.GetOptions{}) |
| 114 | + framework.ExpectNoError(err, "Failed to retrieve CSINode %q", replacementCSINode.Name) |
| 115 | + gomega.Expect(retrievedCSINode.Name).To(gomega.Equal(secondCSINode.Name), "Checking that the retrieved name has been found") |
| 116 | + |
| 117 | + ginkgo.By(fmt.Sprintf("Updating replacement csiNode %q", retrievedCSINode.Name)) |
| 118 | + var updatedCSINode *storagev1.CSINode |
| 119 | + |
| 120 | + err = retry.RetryOnConflict(retry.DefaultRetry, func() error { |
| 121 | + tmpCSINode, err := csiNodeClient.Get(ctx, retrievedCSINode.Name, metav1.GetOptions{}) |
| 122 | + framework.ExpectNoError(err, "Unable to get %q", replacementCSINode.Name) |
| 123 | + tmpCSINode.Labels = map[string]string{replacementCSINode.Name: "updated"} |
| 124 | + updatedCSINode, err = csiNodeClient.Update(ctx, tmpCSINode, metav1.UpdateOptions{}) |
| 125 | + |
| 126 | + return err |
| 127 | + }) |
| 128 | + framework.ExpectNoError(err, "failed to update %q", replacementCSINode.Name) |
| 129 | + gomega.Expect(updatedCSINode.Labels).To(gomega.HaveKeyWithValue(secondCSINode.Name, "updated"), "Checking that updated label has been applied") |
| 130 | + |
| 131 | + updatedSelector := labels.Set{retrievedCSINode.Name: "updated"}.AsSelector().String() |
| 132 | + ginkgo.By(fmt.Sprintf("DeleteCollection of CSINodes with %q label", updatedSelector)) |
| 133 | + err = csiNodeClient.DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: updatedSelector}) |
| 134 | + framework.ExpectNoError(err, "failed to delete csiNode Colllection") |
| 135 | + |
| 136 | + ginkgo.By(fmt.Sprintf("Confirm deletion of replacement csiNode with LabelSelector %q", updatedSelector)) |
| 137 | + |
| 138 | + err = framework.Gomega().Eventually(ctx, framework.HandleRetry(func(ctx context.Context) (*state, error) { |
| 139 | + csiNodeList, err := csiNodeClient.List(ctx, metav1.ListOptions{LabelSelector: updatedSelector}) |
| 140 | + if err != nil { |
| 141 | + return nil, fmt.Errorf("failed to list CSINode: %w", err) |
| 142 | + } |
| 143 | + return &state{ |
| 144 | + CSINodes: csiNodeList.Items, |
| 145 | + }, nil |
| 146 | + })).WithTimeout(30 * time.Second).Should(framework.MakeMatcher(func(s *state) (func() string, error) { |
| 147 | + if len(s.CSINodes) == 0 { |
| 148 | + return nil, nil |
| 149 | + } |
| 150 | + return func() string { |
| 151 | + return fmt.Sprintf("Expected CSINode to be deleted, found %q", s.CSINodes[0].Name) |
| 152 | + }, nil |
| 153 | + })) |
| 154 | + framework.ExpectNoError(err, "Timeout while waiting to confirm CSINode %q deletion", replacementCSINode.Name) |
| 155 | + }) |
| 156 | + }) |
| 157 | +}) |
0 commit comments