From 8ccf59027ea090fdbe460ae14017ade7aaf147ad Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Tue, 19 May 2020 04:33:08 +0000 Subject: [PATCH] Add: test to ensure that a set of pod templates can be removed by delete collection --- test/e2e/common/podtemplates.go | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/test/e2e/common/podtemplates.go b/test/e2e/common/podtemplates.go index 599a33c33f3..d70acdb66f3 100644 --- a/test/e2e/common/podtemplates.go +++ b/test/e2e/common/podtemplates.go @@ -25,6 +25,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/uuid" "k8s.io/kubernetes/test/e2e/framework" + "strconv" "github.com/onsi/ginkgo" ) @@ -99,4 +100,79 @@ var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { framework.ExpectNoError(err, "failed to list PodTemplate") framework.ExpectEqual(len(podTemplateList.Items), 0, "PodTemplate list returned items, failed to delete PodTemplate") }) + + ginkgo.It("should delete a collection of pod templates", func() { + podTemplateNames := []string{"test-podtemplate-1", "test-podtemplate-2", "test-podtemplate-3"} + podTemplateNamesCount := len(podTemplateNames) + + ginkgo.By("Create set of pod templates") + // create a set of pod templates in test namespace + for _, podTemplateName := range podTemplateNames { + _, err := f.ClientSet.CoreV1().PodTemplates(f.Namespace.Name).Create(context.TODO(), &v1.PodTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: podTemplateName, + Labels: map[string]string{"podtemplate-set": "true"}, + }, + Template: v1.PodTemplateSpec{ + Spec: v1.PodSpec{ + Containers: []v1.Container{ + {Name: "nginx", Image: "nginx"}, + }, + }, + }, + }, metav1.CreateOptions{}) + framework.ExpectNoError(err, "failed to create pod template") + framework.Logf("created %v", podTemplateName) + } + + ginkgo.By("get a list of pod templates with a label in the current namespace") + // get a list of pod templates + podTemplateList, err := f.ClientSet.CoreV1().PodTemplates(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ + LabelSelector: "podtemplate-set=true", + }) + framework.ExpectNoError(err, "failed to get a list of pod templates") + + // check that we find all the pod templates created + podTemplateListItemsCount := len(podTemplateList.Items) + errMsg := "found " + strconv.Itoa(podTemplateListItemsCount) + " pod templates when " + strconv.Itoa(podTemplateNamesCount) + " pod templates where expected" + logMsg := "found " + strconv.Itoa(podTemplateListItemsCount) + " pod templates" + + foundCreatedSet := true + if podTemplateListItemsCount != podTemplateNamesCount { + foundCreatedSet = false + } else { + framework.Logf(logMsg) + } + framework.ExpectEqual(foundCreatedSet, true, errMsg) + + ginkgo.By("delete collection of pod templates") + // delete collection + + framework.Logf("requesting DeleteCollection of pod templates") + err = f.ClientSet.CoreV1().PodTemplates(f.Namespace.Name).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{ + LabelSelector: "podtemplate-set=true"}) + framework.ExpectNoError(err, "failed to delete all pod templates") + + ginkgo.By("get a list of pod templates with a label in the current namespace") + // get list of pod templates + podTemplateList, err = f.ClientSet.CoreV1().PodTemplates(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ + LabelSelector: "podtemplate-set=true", + }) + framework.ExpectNoError(err, "failed to get a list of pod templates") + + // check that we don't find any created pod templates + podTemplateListItemsCount = len(podTemplateList.Items) + errMsg = "found " + strconv.Itoa(podTemplateListItemsCount) + " pod templates when zero pod templates where expected" + logMsg = "found " + strconv.Itoa(podTemplateListItemsCount) + " pod templates" + + foundCreatedSet = false + if podTemplateListItemsCount != 0 { + foundCreatedSet = true + } else { + framework.Logf(logMsg) + } + framework.ExpectEqual(foundCreatedSet, false, errMsg) + + }) + })