|
| 1 | +/* |
| 2 | +Copyright 2025 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 csimock |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/onsi/ginkgo/v2" |
| 24 | + "github.com/onsi/gomega" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/kubernetes/test/e2e/framework" |
| 28 | + e2epod "k8s.io/kubernetes/test/e2e/framework/pod" |
| 29 | + e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" |
| 30 | + "k8s.io/kubernetes/test/e2e/storage/drivers" |
| 31 | + "k8s.io/kubernetes/test/e2e/storage/utils" |
| 32 | + admissionapi "k8s.io/pod-security-admission/api" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = utils.SIGDescribe("CSI Mock when kubelet restart", framework.WithSerial(), framework.WithDisruptive(), func() { |
| 36 | + f := framework.NewDefaultFramework("csi-mock-when-kubelet-restart") |
| 37 | + f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged |
| 38 | + m := newMockDriverSetup(f) |
| 39 | + |
| 40 | + ginkgo.BeforeEach(func() { |
| 41 | + // These tests requires SSH to nodes, so the provider check should be identical to there |
| 42 | + // (the limiting factor is the implementation of util.go's e2essh.GetSigner(...)). |
| 43 | + |
| 44 | + // Cluster must support node reboot |
| 45 | + e2eskipper.SkipUnlessProviderIs(framework.ProvidersWithSSH...) |
| 46 | + e2eskipper.SkipUnlessSSHKeyPresent() |
| 47 | + }) |
| 48 | + |
| 49 | + ginkgo.It("should not umount volume when the pvc is terminating but still used by a running pod", func(ctx context.Context) { |
| 50 | + |
| 51 | + m.init(ctx, testParameters{ |
| 52 | + registerDriver: true, |
| 53 | + }) |
| 54 | + ginkgo.DeferCleanup(m.cleanup) |
| 55 | + |
| 56 | + ginkgo.By("Creating a Pod with a PVC backed by a CSI volume") |
| 57 | + _, pvc, pod := m.createPod(ctx, pvcReference) |
| 58 | + |
| 59 | + ginkgo.By("Waiting for the Pod to be running") |
| 60 | + err := e2epod.WaitForPodRunningInNamespace(ctx, f.ClientSet, pod) |
| 61 | + framework.ExpectNoError(err, "failed to wait for pod %s to be running", pod.Name) |
| 62 | + pod, err = f.ClientSet.CoreV1().Pods(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{}) |
| 63 | + framework.ExpectNoError(err, "failed to get pod %s", pod.Name) |
| 64 | + |
| 65 | + ginkgo.By("Deleting the PVC") |
| 66 | + err = f.ClientSet.CoreV1().PersistentVolumeClaims(pvc.Namespace).Delete(ctx, pvc.Name, metav1.DeleteOptions{}) |
| 67 | + framework.ExpectNoError(err, "failed to delete PVC %s", pvc.Name) |
| 68 | + |
| 69 | + ginkgo.By("Restarting kubelet") |
| 70 | + utils.KubeletCommand(ctx, utils.KRestart, f.ClientSet, pod) |
| 71 | + ginkgo.DeferCleanup(utils.KubeletCommand, utils.KStart, f.ClientSet, pod) |
| 72 | + |
| 73 | + ginkgo.By("Verifying the PVC is terminating during kubelet restart") |
| 74 | + pvc, err = f.ClientSet.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(ctx, pvc.Name, metav1.GetOptions{}) |
| 75 | + framework.ExpectNoError(err, "failed to get PVC %s", pvc.Name) |
| 76 | + gomega.Expect(pvc.DeletionTimestamp).NotTo(gomega.BeNil(), "PVC %s should have deletion timestamp", pvc.Name) |
| 77 | + |
| 78 | + ginkgo.By(fmt.Sprintf("Verifying that the driver didn't receive NodeUnpublishVolume call for PVC %s", pvc.Name)) |
| 79 | + gomega.Consistently(ctx, |
| 80 | + func(ctx context.Context) []drivers.MockCSICall { |
| 81 | + calls, err := m.driver.GetCalls(ctx) |
| 82 | + if err != nil { |
| 83 | + if apierrors.IsUnexpectedServerError(err) { |
| 84 | + // kubelet might not be ready yet when getting the calls |
| 85 | + gomega.TryAgainAfter(framework.Poll).Wrap(err).Now() |
| 86 | + return nil |
| 87 | + } |
| 88 | + return nil |
| 89 | + } |
| 90 | + return calls |
| 91 | + }). |
| 92 | + WithPolling(framework.Poll). |
| 93 | + WithTimeout(framework.ClaimProvisionShortTimeout). |
| 94 | + ShouldNot(gomega.ContainElement(gomega.HaveField("Method", gomega.Equal("NodeUnpublishVolume")))) |
| 95 | + |
| 96 | + ginkgo.By("Verifying the Pod is still running") |
| 97 | + err = e2epod.WaitForPodRunningInNamespace(ctx, f.ClientSet, pod) |
| 98 | + framework.ExpectNoError(err, "failed to wait for pod %s to be running", pod.Name) |
| 99 | + }) |
| 100 | +}) |
0 commit comments