Skip to content

Commit

Permalink
test(e2e): add e2e test for modifying existing deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed May 22, 2024
1 parent 36492a0 commit 967b7c8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 28 deletions.
46 changes: 45 additions & 1 deletion test/e2e/e2e_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package e2e

import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -249,9 +251,31 @@ func DeployOperator(namespace string, image string) {
EventuallyWithOffset(1, verifyControllerUp, 120*time.Second, time.Second).Should(Succeed())
}

func UndeployOperator() {
func UndeployOperator(namespace string) {
By("undeploying the controller-manager")
ExpectWithOffset(1, RunAndIgnoreOutput(exec.Command("make", "undeploy"))).To(Succeed())

// We need to wait until the namespace is really gone, otherwise the next test/suite that tries to create the operator
// will run into issues when trying to recreate the namespace which is still in the process of being deleted.
ExpectWithOffset(1, RunAndIgnoreOutput(exec.Command(
"kubectl",
"wait",
"--for=delete",
fmt.Sprintf("namespace/%s", namespace),
"--timeout=60s",
))).To(Succeed())
}

func DeployDash0Resource(namespace string) {
ExpectWithOffset(1,
RunAndIgnoreOutput(exec.Command(
"kubectl", "apply", "-n", namespace, "-k", "config/samples"))).To(Succeed())
}

func UndeployDash0Resource(namespace string) {
ExpectWithOffset(1,
RunAndIgnoreOutput(exec.Command(
"kubectl", "delete", "-n", namespace, "-k", "config/samples"))).To(Succeed())
}

func InstallNodeJsDeployment(namespace string) error {
Expand Down Expand Up @@ -381,3 +405,23 @@ func GetProjectDir() (string, error) {
wd = strings.Replace(wd, "/test/e2e", "", -1)
return wd, nil
}

func CopyFile(source string, destination string) error {
src, err := os.Open(source)
if err != nil {
return err
}
defer func() {
err = errors.Join(err, src.Close())
}()

dst, err := os.Create(destination)
if err != nil {
return err
}
defer func() {
err = errors.Join(err, dst.Close())
}()
_, err = io.Copy(dst, src)
return err
}
49 changes: 22 additions & 27 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
package e2e

import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -63,7 +61,7 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {
fmt.Fprintf(GinkgoWriter, "original imagePullPolicy: %s\n", originalImagePullPolicy)

if originalImagePullPolicy != "Never" {
err = copyFile(managerYaml, managerYamlBackup)
err = CopyFile(managerYaml, managerYamlBackup)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
managerYamlNeedsRevert = true
By("temporarily changing imagePullPolicy to \"Never\"")
Expand Down Expand Up @@ -92,7 +90,7 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {

applicationNamespaceHasBeenCreated = EnsureNamespaceExists(applicationUnderTestNamespace)

By("installing the collector")
By("(re)installing the collector")
ExpectWithOffset(1, ReinstallCollectorAndClearExportedTelemetry(applicationUnderTestNamespace)).To(Succeed())

By("creating manager namespace")
Expand All @@ -112,7 +110,7 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {

if managerYamlNeedsRevert {
By("reverting changes to " + managerYaml)
err := copyFile(managerYamlBackup, managerYaml)
err := CopyFile(managerYamlBackup, managerYaml)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
err = os.Remove(managerYamlBackup)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -146,38 +144,35 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {

BeforeAll(func() {
DeployOperator(operatorNamespace, operatorImage)
fmt.Fprint(GinkgoWriter, "waiting 10 seconds\n")
fmt.Fprint(GinkgoWriter, "waiting 10 seconds to give the webook some time to get ready\n")
time.Sleep(10 * time.Second)
})

AfterAll(func() {
UndeployOperator()
UndeployOperator(operatorNamespace)
})

It("should modify new deployments", func() {
By("installing the Node.js deployment")
Expect(InstallNodeJsDeployment(applicationUnderTestNamespace)).To(Succeed())
By("verifying that the Node.js deployment has been instrumented by the webhook")
SendRequestsAndVerifySpansHaveBeenProduced()
})
})
})

func copyFile(source string, destination string) error {
src, err := os.Open(source)
if err != nil {
return err
}
defer func() {
err = errors.Join(err, src.Close())
}()

dst, err := os.Create(destination)
if err != nil {
return err
}
defer func() {
err = errors.Join(err, dst.Close())
}()
_, err = io.Copy(dst, src)
return err
}
Context("the Dash0 operator controller", func() {
AfterAll(func() {
UndeployDash0Resource(applicationUnderTestNamespace)
UndeployOperator(operatorNamespace)
})

It("should modify existing deployments", func() {
By("installing the Node.js deployment")
Expect(InstallNodeJsDeployment(applicationUnderTestNamespace)).To(Succeed())
DeployOperator(operatorNamespace, operatorImage)
DeployDash0Resource(applicationUnderTestNamespace)
By("verifying that the Node.js deployment has been instrumented by the controller")
SendRequestsAndVerifySpansHaveBeenProduced()
})
})
})

0 comments on commit 967b7c8

Please sign in to comment.