From 8ccf59027ea090fdbe460ae14017ade7aaf147ad Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Tue, 19 May 2020 04:33:08 +0000 Subject: [PATCH 1/5] 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) + + }) + }) From 7772a42114f271296cd3817b58e5e025aa65905d Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Tue, 19 May 2020 23:44:19 +0000 Subject: [PATCH 2/5] Switch pod template to use Agnhost --- test/e2e/common/podtemplates.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/e2e/common/podtemplates.go b/test/e2e/common/podtemplates.go index d70acdb66f3..660da7120eb 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" + imageutils "k8s.io/kubernetes/test/utils/image" "strconv" "github.com/onsi/ginkgo" @@ -116,7 +117,7 @@ var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { Template: v1.PodTemplateSpec{ Spec: v1.PodSpec{ Containers: []v1.Container{ - {Name: "nginx", Image: "nginx"}, + {Name: "token-test", Image: imageutils.GetE2EImage(imageutils.Agnhost)}, }, }, }, From 758e3c76a4fd8b037948713f032f47545f1b0664 Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Wed, 20 May 2020 01:21:04 +0000 Subject: [PATCH 3/5] Removing extra boilerplate from test --- test/e2e/common/podtemplates.go | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/test/e2e/common/podtemplates.go b/test/e2e/common/podtemplates.go index 660da7120eb..0c0f925594a 100644 --- a/test/e2e/common/podtemplates.go +++ b/test/e2e/common/podtemplates.go @@ -26,7 +26,6 @@ import ( "k8s.io/apimachinery/pkg/util/uuid" "k8s.io/kubernetes/test/e2e/framework" imageutils "k8s.io/kubernetes/test/utils/image" - "strconv" "github.com/onsi/ginkgo" ) @@ -104,7 +103,6 @@ var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { 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 @@ -133,18 +131,7 @@ var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { }) 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) + framework.ExpectEqual(len(podTemplateList.Items), len(podTemplateNames), "looking for expected number of pod templates") ginkgo.By("delete collection of pod templates") // delete collection @@ -161,18 +148,7 @@ var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { }) 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) + framework.ExpectEqual(len(podTemplateList.Items), 0, "pod templates should all be deleted") }) From d373bef4e4aa8d3331f82c6c8bd4404c746e1d06 Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Fri, 22 May 2020 01:33:20 +0000 Subject: [PATCH 4/5] Use polling while deleting the collection of pod templates --- test/e2e/common/podtemplates.go | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/test/e2e/common/podtemplates.go b/test/e2e/common/podtemplates.go index 0c0f925594a..e4eb803b02c 100644 --- a/test/e2e/common/podtemplates.go +++ b/test/e2e/common/podtemplates.go @@ -24,12 +24,19 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/uuid" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/kubernetes/test/e2e/framework" imageutils "k8s.io/kubernetes/test/utils/image" + "time" "github.com/onsi/ginkgo" ) +const ( + podTemplateRetryPeriod = 1 * time.Second + podTemplateRetryTimeout = 1 * time.Minute +) + var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { f := framework.NewDefaultFramework("podtemplate") /* @@ -134,12 +141,10 @@ var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { framework.ExpectEqual(len(podTemplateList.Items), len(podTemplateNames), "looking for expected number of pod templates") ginkgo.By("delete collection of pod templates") - // delete collection + // confirm that delete collection does remove all pod templates - 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") + err = wait.PollImmediate(podTemplateRetryPeriod, podTemplateRetryTimeout, deletePodTemplateCollection(f, "podtemplate-set=true")) + framework.ExpectNoError(err, "failed to delete collection") ginkgo.By("get a list of pod templates with a label in the current namespace") // get list of pod templates @@ -153,3 +158,21 @@ var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { }) }) + +func deletePodTemplateCollection(f *framework.Framework, label string) func() (bool, error) { + return func() (bool, error) { + var err error + + framework.Logf("requesting DeleteCollection of pod templates") + + err = f.ClientSet.CoreV1().PodTemplates(f.Namespace.Name).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{ + LabelSelector: label}) + + if err != nil { + return false, err + } else { + return true, nil + } + + } +} From aaa07342591bb5edcc08c98d07a3309b0101e146 Mon Sep 17 00:00:00 2001 From: Stephen Heywood Date: Tue, 2 Jun 2020 03:20:58 +0000 Subject: [PATCH 5/5] Confirm list quantity after pod template collection deleted --- test/e2e/common/podtemplates.go | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/test/e2e/common/podtemplates.go b/test/e2e/common/podtemplates.go index e4eb803b02c..37f45d597fb 100644 --- a/test/e2e/common/podtemplates.go +++ b/test/e2e/common/podtemplates.go @@ -141,38 +141,37 @@ var _ = ginkgo.Describe("[sig-node] PodTemplates", func() { framework.ExpectEqual(len(podTemplateList.Items), len(podTemplateNames), "looking for expected number of pod templates") ginkgo.By("delete collection of pod templates") - // confirm that delete collection does remove all pod templates + // delete collection - err = wait.PollImmediate(podTemplateRetryPeriod, podTemplateRetryTimeout, deletePodTemplateCollection(f, "podtemplate-set=true")) - framework.ExpectNoError(err, "failed to 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") - - framework.ExpectEqual(len(podTemplateList.Items), 0, "pod templates should all be deleted") + ginkgo.By("check that the list of pod templates matches the requested quantity") + err = wait.PollImmediate(podTemplateRetryPeriod, podTemplateRetryTimeout, checkPodTemplateListQuantity(f, "podtemplate-set=true", 0)) + framework.ExpectNoError(err, "failed to count required pod templates") }) }) -func deletePodTemplateCollection(f *framework.Framework, label string) func() (bool, error) { +func checkPodTemplateListQuantity(f *framework.Framework, label string, quantity int) func() (bool, error) { return func() (bool, error) { var err error - framework.Logf("requesting DeleteCollection of pod templates") + framework.Logf("requesting list of pod templates to confirm quantity") - err = f.ClientSet.CoreV1().PodTemplates(f.Namespace.Name).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{ + list, err := f.ClientSet.CoreV1().PodTemplates(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{ LabelSelector: label}) if err != nil { return false, err - } else { - return true, nil } + if len(list.Items) != quantity { + return false, err + } + return true, nil } }