k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/e2e/kubectl/delete.go (about) 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 // OWNER = sig/cli 18 19 package kubectl 20 21 import ( 22 "context" 23 "strings" 24 "time" 25 26 "github.com/onsi/ginkgo/v2" 27 28 apierrors "k8s.io/apimachinery/pkg/api/errors" 29 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 "k8s.io/apimachinery/pkg/util/wait" 31 clientset "k8s.io/client-go/kubernetes" 32 e2edeployment "k8s.io/kubernetes/test/e2e/framework/deployment" 33 admissionapi "k8s.io/pod-security-admission/api" 34 35 commonutils "k8s.io/kubernetes/test/e2e/common" 36 "k8s.io/kubernetes/test/e2e/framework" 37 e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl" 38 ) 39 40 var _ = SIGDescribe("Kubectl delete", func() { 41 defer ginkgo.GinkgoRecover() 42 var deploymentYaml string 43 f := framework.NewDefaultFramework("kubectl-delete") 44 f.NamespacePodSecurityLevel = admissionapi.LevelBaseline 45 deploymentName := "httpd-deployment" 46 47 var ns string 48 var c clientset.Interface 49 ginkgo.BeforeEach(func() { 50 c = f.ClientSet 51 ns = f.Namespace.Name 52 deploymentYaml = commonutils.SubstituteImageName(string(readTestFileOrDie(httpdDeployment1Filename))) 53 }) 54 55 ginkgo.Describe("interactive", func() { 56 ginkgo.It("based on user confirmation input", func(ctx context.Context) { 57 ginkgo.By("apply deployment with replicas 2") 58 e2ekubectl.RunKubectlOrDieInput(ns, deploymentYaml, "apply", "-f", "-") 59 60 ginkgo.By("verifying the deployment is created and running") 61 d, err := c.AppsV1().Deployments(ns).Get(ctx, deploymentName, metav1.GetOptions{}) 62 if err != nil { 63 framework.Failf("Failed getting deployment %v", err) 64 } 65 err = e2edeployment.WaitForDeploymentComplete(c, d) 66 framework.ExpectNoError(err, "waiting for the deployment to complete") 67 68 ginkgo.By("check that resource is not deleted when user types no") 69 output := e2ekubectl.RunKubectlOrDieInput(ns, "n", "delete", "--interactive", "deployment", deploymentName) 70 expectedOutput := "You are about to delete the following 1 resource(s):" 71 if !strings.Contains(output, expectedOutput) || 72 !strings.Contains(output, "deployment.apps/httpd-deployment") || 73 !strings.Contains(output, "deletion is cancelled") { 74 framework.Failf("unexpected output %s", output) 75 } 76 77 ginkgo.By("verify that deployment is not deleted") 78 d, err = c.AppsV1().Deployments(ns).Get(ctx, deploymentName, metav1.GetOptions{}) 79 if err != nil || d.DeletionTimestamp != nil { 80 framework.Failf("Failed getting deployment that shouldn't be deleted %v", err) 81 } 82 83 if d == nil || d.Status.AvailableReplicas != 2 { 84 framework.Failf("unexpected available replicas") 85 } 86 87 ginkgo.By("check that resource is deleted when user types yes") 88 e2ekubectl.RunKubectlOrDieInput(ns, "y", "delete", "--interactive", "deployment", deploymentName) 89 90 ginkgo.By("ensure that the deployment is deleted successfully") 91 err = wait.PollUntilContextTimeout(ctx, 2*time.Second, 30*time.Second, true, func(ctx context.Context) (bool, error) { 92 _, err = c.AppsV1().Deployments(ns).Get(ctx, deploymentName, metav1.GetOptions{}) 93 if err == nil { 94 return false, nil 95 } 96 97 if apierrors.IsNotFound(err) { 98 return true, nil 99 } 100 101 return false, err 102 }) 103 framework.ExpectNoError(err, "waiting for the deployment that is deleted after getting confirmation by user") 104 }) 105 }) 106 })