From 8744945e97c578eba0769643b4dc550a7d3f031d Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Mon, 14 Dec 2020 18:02:48 -0800 Subject: [PATCH] test/: clean up some e2e setup code --- internal/testutils/utils.go | 12 ++++ test/e2e-ansible/e2e_ansible_cluster_test.go | 70 ++++++++------------ test/e2e-go/e2e_go_cluster_test.go | 28 +++----- test/e2e-helm/e2e_helm_cluster_test.go | 36 +++++----- 4 files changed, 67 insertions(+), 79 deletions(-) diff --git a/internal/testutils/utils.go b/internal/testutils/utils.go index 4c63da1236f..81b39c7e49b 100644 --- a/internal/testutils/utils.go +++ b/internal/testutils/utils.go @@ -278,3 +278,15 @@ func (tc TestContext) AllowProjectBeMultiGroup() error { } return nil } + +// WrapWarnOutput is a one-liner to wrap an error from a command that returns (string, error) in a warning. +func WrapWarnOutput(_ string, err error) { + if err != nil { + fmt.Fprintf(GinkgoWriter, "warning: %s", err) + } +} + +// WrapWarn is a one-liner to wrap an error from a command that returns (error) in a warning. +func WrapWarn(err error) { + WrapWarnOutput("", err) +} diff --git a/test/e2e-ansible/e2e_ansible_cluster_test.go b/test/e2e-ansible/e2e_ansible_cluster_test.go index 8a2e639c9a0..60e4d861c63 100644 --- a/test/e2e-ansible/e2e_ansible_cluster_test.go +++ b/test/e2e-ansible/e2e_ansible_cluster_test.go @@ -29,54 +29,42 @@ import ( ) var _ = Describe("Running ansible projects", func() { - var controllerPodName string - var memcachedSampleFile string - var fooSampleFile string - var memfinSampleFile string - var memcachedDeployment string - var metricsClusterRoleBindingName string + + var ( + controllerPodName, memcachedDeploymentName, metricsClusterRoleBindingName string + fooSampleFile, memfinSampleFile, memcachedSampleFile string + ) Context("built with operator-sdk", func() { BeforeEach(func() { metricsClusterRoleBindingName = fmt.Sprintf("%s-metrics-reader", tc.ProjectName) - - By("checking samples") - memcachedSampleFile = filepath.Join(tc.Dir, "config", "samples", + samplesDir := filepath.Join(tc.Dir, "config", "samples") + fooSampleFile = filepath.Join(samplesDir, fmt.Sprintf("%s_%s_foo.yaml", tc.Group, tc.Version)) + memfinSampleFile = filepath.Join(samplesDir, fmt.Sprintf("%s_%s_memfin.yaml", tc.Group, tc.Version)) + memcachedSampleFile = filepath.Join(samplesDir, fmt.Sprintf("%s_%s_%s.yaml", tc.Group, tc.Version, strings.ToLower(tc.Kind))) - fooSampleFile = filepath.Join(tc.Dir, "config", "samples", - fmt.Sprintf("%s_%s_foo.yaml", tc.Group, tc.Version)) - memfinSampleFile = filepath.Join(tc.Dir, "config", "samples", - fmt.Sprintf("%s_%s_memfin.yaml", tc.Group, tc.Version)) By("deploying project on the cluster") - err := tc.Make("deploy", "IMG="+tc.ImageName) - Expect(err).NotTo(HaveOccurred()) + Expect(tc.Make("deploy", "IMG="+tc.ImageName)).To(Succeed()) }) + AfterEach(func() { - By("deleting Curl Pod created") - _, _ = tc.Kubectl.Delete(false, "pod", "curl") + By("deleting curl pod") + testutils.WrapWarnOutput(tc.Kubectl.Delete(false, "pod", "curl")) - By("deleting CR instances created") - _, _ = tc.Kubectl.Delete(false, "-f", memcachedSampleFile) - _, _ = tc.Kubectl.Delete(false, "-f", fooSampleFile) - _, _ = tc.Kubectl.Delete(false, "-f", memfinSampleFile) + By("deleting test CR instances") + for _, sample := range []string{memcachedSampleFile, fooSampleFile, memfinSampleFile} { + testutils.WrapWarnOutput(tc.Kubectl.Delete(false, "-f", sample)) + } By("cleaning up permissions") - _, _ = tc.Kubectl.Command("delete", "clusterrolebinding", - metricsClusterRoleBindingName) + testutils.WrapWarnOutput(tc.Kubectl.Command("delete", "clusterrolebinding", metricsClusterRoleBindingName)) By("undeploy project") - _ = tc.Make("undeploy") + testutils.WrapWarn(tc.Make("undeploy")) By("ensuring that the namespace was deleted") - verifyNamespaceDeleted := func() error { - _, err := tc.Kubectl.Command("get", "namespace", tc.Kubectl.Namespace) - if strings.Contains(err.Error(), "(NotFound): namespaces") { - return err - } - return nil - } - Eventually(verifyNamespaceDeleted, 2*time.Minute, time.Second).ShouldNot(Succeed()) + testutils.WrapWarnOutput(tc.Kubectl.Wait(false, "namespace", "foo", "--for", "delete", "--timeout", "2m")) }) It("should run correctly in a cluster", func() { @@ -165,19 +153,19 @@ var _ = Describe("Running ansible projects", func() { Eventually(verifyControllerProbe, time.Minute, time.Second).ShouldNot(ContainSubstring("Killing")) By("getting memcached deploy by labels") - getMencachedDeploument := func() string { - memcachedDeployment, err = tc.Kubectl.Get( + getMemcachedDeploymentName := func() string { + memcachedDeploymentName, err = tc.Kubectl.Get( false, "deployment", "-l", "app=memcached", "-o", "jsonpath={..metadata.name}") Expect(err).NotTo(HaveOccurred()) - return memcachedDeployment + return memcachedDeploymentName } - Eventually(getMencachedDeploument, 2*time.Minute, time.Second).ShouldNot(BeEmpty()) + Eventually(getMemcachedDeploymentName, 2*time.Minute, time.Second).ShouldNot(BeEmpty()) By("checking the Memcached CR deployment status") verifyCRUp := func() string { output, err := tc.Kubectl.Command( - "rollout", "status", "deployment", memcachedDeployment) + "rollout", "status", "deployment", memcachedDeploymentName) Expect(err).NotTo(HaveOccurred()) return output } @@ -211,14 +199,14 @@ var _ = Describe("Running ansible projects", func() { By("scaling deployment replicas to 2") _, err = tc.Kubectl.Command( - "scale", "deployment", memcachedDeployment, "--replicas", "2") + "scale", "deployment", memcachedDeploymentName, "--replicas", "2") Expect(err).NotTo(HaveOccurred()) By("verifying the deployment automatically scales back down to 1") verifyMemcachedScalesBack := func() error { replicas, err := tc.Kubectl.Get( false, - "deployment", memcachedDeployment, "-o", "jsonpath={..spec.replicas}") + "deployment", memcachedDeploymentName, "-o", "jsonpath={..spec.replicas}") Expect(err).NotTo(HaveOccurred()) if replicas != "1" { return fmt.Errorf("memcached(CR) deployment with %s replicas", replicas) @@ -249,7 +237,7 @@ var _ = Describe("Running ansible projects", func() { verifyMemcachedPatch := func() error { replicas, err := tc.Kubectl.Get( false, - "deployment", memcachedDeployment, "-o", "jsonpath={..spec.replicas}") + "deployment", memcachedDeploymentName, "-o", "jsonpath={..spec.replicas}") Expect(err).NotTo(HaveOccurred()) if replicas != "2" { return fmt.Errorf("memcached(CR) deployment with %s replicas", replicas) @@ -381,7 +369,7 @@ var _ = Describe("Running ansible projects", func() { getMemcachedDeployment := func() error { _, err := tc.Kubectl.Get( false, "deployment", - memcachedDeployment) + memcachedDeploymentName) return err } Eventually(getMemcachedDeployment, time.Minute*2, time.Second).ShouldNot(Succeed()) diff --git a/test/e2e-go/e2e_go_cluster_test.go b/test/e2e-go/e2e_go_cluster_test.go index 39ec38775c2..153a01fae4f 100644 --- a/test/e2e-go/e2e_go_cluster_test.go +++ b/test/e2e-go/e2e_go_cluster_test.go @@ -27,40 +27,34 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" kbtestutils "sigs.k8s.io/kubebuilder/v2/test/e2e/utils" + + "github.com/operator-framework/operator-sdk/internal/testutils" ) var _ = Describe("operator-sdk", func() { - var controllerPodName string - var metricsClusterRoleBindingName string + var controllerPodName, metricsClusterRoleBindingName string Context("built with operator-sdk", func() { + BeforeEach(func() { metricsClusterRoleBindingName = fmt.Sprintf("%s-metrics-reader", tc.ProjectName) - By("deploying project on the cluster") - err := tc.Make("deploy", "IMG="+tc.ImageName) - Expect(err).NotTo(HaveOccurred()) + By("deploying project on the cluster") + Expect(tc.Make("deploy", "IMG="+tc.ImageName)).To(Succeed()) }) + AfterEach(func() { - By("deleting Curl Pod created") - _, _ = tc.Kubectl.Delete(false, "pod", "curl") + By("deleting curl pod") + testutils.WrapWarnOutput(tc.Kubectl.Delete(false, "pod", "curl")) By("cleaning up permissions") - _, _ = tc.Kubectl.Command("delete", "clusterrolebinding", - metricsClusterRoleBindingName) + testutils.WrapWarnOutput(tc.Kubectl.Command("delete", "clusterrolebinding", metricsClusterRoleBindingName)) By("cleaning up created API objects during test process") tc.CleanupManifests(filepath.Join("config", "default")) By("ensuring that the namespace was deleted") - verifyNamespaceDeleted := func() error { - _, err := tc.Kubectl.Command("get", "namespace", tc.Kubectl.Namespace) - if strings.Contains(err.Error(), "(NotFound): namespaces") { - return err - } - return nil - } - Eventually(verifyNamespaceDeleted, 2*time.Minute, time.Second).ShouldNot(Succeed()) + testutils.WrapWarnOutput(tc.Kubectl.Wait(false, "namespace", "foo", "--for", "delete", "--timeout", "2m")) }) It("should run correctly in a cluster", func() { diff --git a/test/e2e-helm/e2e_helm_cluster_test.go b/test/e2e-helm/e2e_helm_cluster_test.go index 1e89f1457ab..05f600875d6 100644 --- a/test/e2e-helm/e2e_helm_cluster_test.go +++ b/test/e2e-helm/e2e_helm_cluster_test.go @@ -29,42 +29,36 @@ import ( ) var _ = Describe("Running Helm projects", func() { - var controllerPodName string - var metricsClusterRoleBindingName string + var ( + controllerPodName, metricsClusterRoleBindingName string + memcachedSampleFile string + ) Context("built with operator-sdk", func() { BeforeEach(func() { metricsClusterRoleBindingName = fmt.Sprintf("%s-metrics-reader", tc.ProjectName) + memcachedSampleFile = filepath.Join(tc.Dir, "config", "samples", + fmt.Sprintf("%s_%s_%s.yaml", tc.Group, tc.Version, strings.ToLower(tc.Kind))) By("deploying project on the cluster") - err := tc.Make("deploy", "IMG="+tc.ImageName) - Expect(err).NotTo(HaveOccurred()) + Expect(tc.Make("deploy", "IMG="+tc.ImageName)).To(Succeed()) }) + AfterEach(func() { - By("deleting Curl Pod created") - _, _ = tc.Kubectl.Delete(true, "pod", "curl") + By("deleting curl pod") + testutils.WrapWarnOutput(tc.Kubectl.Delete(false, "pod", "curl")) - By("deleting CR instances created") - sampleFile := filepath.Join("config", "samples", - fmt.Sprintf("%s_%s_%s.yaml", tc.Group, tc.Version, strings.ToLower(tc.Kind))) - _, _ = tc.Kubectl.Delete(false, "-f", sampleFile) + By("deleting test CR instances") + testutils.WrapWarnOutput(tc.Kubectl.Delete(false, "-f", memcachedSampleFile)) By("cleaning up permissions") - _, _ = tc.Kubectl.Command("delete", "clusterrolebinding", - metricsClusterRoleBindingName) + testutils.WrapWarnOutput(tc.Kubectl.Command("delete", "clusterrolebinding", metricsClusterRoleBindingName)) By("undeploy project") - _ = tc.Make("undeploy") + testutils.WrapWarn(tc.Make("undeploy")) By("ensuring that the namespace was deleted") - verifyNamespaceDeleted := func() error { - _, err := tc.Kubectl.Command("get", "namespace", tc.Kubectl.Namespace) - if strings.Contains(err.Error(), "(NotFound): namespaces") { - return err - } - return nil - } - Eventually(verifyNamespaceDeleted, 2*time.Minute, time.Second).ShouldNot(Succeed()) + testutils.WrapWarnOutput(tc.Kubectl.Wait(false, "namespace", "foo", "--for", "delete", "--timeout", "2m")) }) It("should run correctly in a cluster", func() {