Skip to content

test/: clean up some e2e setup code #4315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT. about push it to upstream?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

70 changes: 29 additions & 41 deletions test/e2e-ansible/e2e_ansible_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

})

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)
Copy link
Contributor

@camilamacedo86 camilamacedo86 Dec 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason. for the break in the lines is for not be required to scroll to check the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these lines I changed should still be < 120 chars wide.

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() {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
28 changes: 11 additions & 17 deletions test/e2e-go/e2e_go_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
36 changes: 15 additions & 21 deletions test/e2e-helm/e2e_helm_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down