From 38207e460cf3d4fa329451b07b01b4e7ae54567f Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Mon, 1 Nov 2021 11:52:54 +1300 Subject: [PATCH] Create e2e delete service collection test The test validates the following endpoint - deleteCoreV1CollectionNamespacedService --- test/e2e/network/service.go | 75 +++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/test/e2e/network/service.go b/test/e2e/network/service.go index a822fa0f608..59dd0b25101 100644 --- a/test/e2e/network/service.go +++ b/test/e2e/network/service.go @@ -2730,6 +2730,81 @@ var _ = common.SIGDescribe("Services", func() { framework.ExpectNoError(err, "failed to delete Service %v in namespace %v", testService.ObjectMeta.Name, ns) framework.Logf("Service %s deleted", testSvcName) }) + + ginkgo.It("should delete a collection of services", func() { + + ns := f.Namespace.Name + svcClient := f.ClientSet.CoreV1().Services(ns) + svcResource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "services"} + svcDynamicClient := f.DynamicClient.Resource(svcResource).Namespace(ns) + + svcLabel := map[string]string{"e2e-test-service": "delete"} + deleteLabel := labels.SelectorFromSet(svcLabel).String() + + ginkgo.By("creating a collection of services") + + testServices := []struct { + name string + label map[string]string + port int + }{ + { + name: "e2e-svc-a-" + utilrand.String(5), + label: map[string]string{"e2e-test-service": "delete"}, + port: 8001, + }, + { + name: "e2e-svc-b-" + utilrand.String(5), + label: map[string]string{"e2e-test-service": "delete"}, + port: 8002, + }, + { + name: "e2e-svc-c-" + utilrand.String(5), + label: map[string]string{"e2e-test-service": "keep"}, + port: 8003, + }, + } + + for _, testService := range testServices { + func() { + framework.Logf("Creating %s", testService.name) + + svc := v1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: testService.name, + Labels: testService.label, + }, + Spec: v1.ServiceSpec{ + Type: "ClusterIP", + Ports: []v1.ServicePort{{ + Name: "http", + Protocol: v1.ProtocolTCP, + Port: int32(testService.port), + TargetPort: intstr.FromInt(testService.port), + }}, + }, + } + _, err := svcClient.Create(context.TODO(), &svc, metav1.CreateOptions{}) + framework.ExpectNoError(err, "failed to create Service") + + }() + } + + svcList, err := cs.CoreV1().Services(ns).List(context.TODO(), metav1.ListOptions{}) + framework.ExpectNoError(err, "failed to list Services") + framework.ExpectEqual(len(svcList.Items), 3, "Required count of services out of sync") + + ginkgo.By("deleting service collection") + err = svcDynamicClient.DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: deleteLabel}) + framework.ExpectNoError(err, "failed to delete service collection. %v", err) + + svcList, err = cs.CoreV1().Services(ns).List(context.TODO(), metav1.ListOptions{}) + framework.ExpectNoError(err, "failed to list Services") + framework.ExpectEqual(len(svcList.Items), 1, "Required count of services out of sync") + + framework.Logf("Collection of services has been deleted") + }) + }) // execAffinityTestForSessionAffinityTimeout is a helper function that wrap the logic of