Skip to content

Commit

Permalink
test(e2e): only install/uninstall cert-manager if it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed May 22, 2024
1 parent 655f357 commit 6cc1f4e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 75 deletions.
140 changes: 84 additions & 56 deletions test/e2e/e2e_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,58 +31,6 @@ var (
traceUnmarshaller = &ptrace.JSONUnmarshaler{}
)

func RunAndIgnoreOutput(cmd *exec.Cmd, logCommandArgs ...bool) error {
_, err := Run(cmd, logCommandArgs...)
return err
}

// Run executes the provided command within this context
func Run(cmd *exec.Cmd, logCommandArgs ...bool) ([]byte, error) {
var logCommand bool
if len(logCommandArgs) > 0 {
logCommand = logCommandArgs[0]
} else {
logCommand = true
}

dir, _ := GetProjectDir()
cmd.Dir = dir

if err := os.Chdir(cmd.Dir); err != nil {
fmt.Fprintf(GinkgoWriter, "chdir dir: %s\n", err)
}

cmd.Env = append(os.Environ(), "GO111MODULE=on")
command := strings.Join(cmd.Args, " ")
if logCommand {
fmt.Fprintf(GinkgoWriter, "running: %s\n", command)
}
output, err := cmd.CombinedOutput()
if err != nil {
return output, fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
}

return output, nil
}

// RunMultiple executes multiple commands
func RunMultiple(cmds []*exec.Cmd, logCommandArgs ...bool) error {
for _, cmd := range cmds {
if err := RunAndIgnoreOutput(cmd, logCommandArgs...); err != nil {
return err
}
}
return nil
}

func RunMultipleFromStrings(cmdsAsStrings [][]string, logCommandArgs ...bool) error {
cmds := make([]*exec.Cmd, len(cmdsAsStrings))
for i, cmdStrs := range cmdsAsStrings {
cmds[i] = exec.Command(cmdStrs[0], cmdStrs[1:]...)
}
return RunMultiple(cmds, logCommandArgs...)
}

func EnsureNamespaceExists(namespace string) bool {
err := RunAndIgnoreOutput(exec.Command("kubectl", "get", "ns", namespace), false)
if err != nil {
Expand All @@ -94,8 +42,26 @@ func EnsureNamespaceExists(namespace string) bool {
return false
}

// InstallCertManager installs the cert manager bundle.
func InstallCertManager() error {
func EnsureCertManagerIsInstalled() bool {
err := RunAndIgnoreOutput(exec.Command("kubectl", "get", "ns", "cert-manager"), false)
if err != nil {
By("installing the cert-manager")
fmt.Fprint(GinkgoWriter,
"Hint: To get a faster feedback cycle on e2e tests, deploy cert-manager once via "+
"test-resources/cert-manager/deploy.sh. If the e2e tests find an existing cert-manager namespace, they "+
"will not deploy cert-manager and they will also not undeploy it after running the test suite.\n",
)
ExpectWithOffset(1, installCertManager()).To(Succeed())
return true
} else {
fmt.Fprint(GinkgoWriter,
"The cert-manager namespace exists, assuming cert-manager has been deployed already.\n",
)
}
return false
}

func installCertManager() error {
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
if err := RunAndIgnoreOutput(exec.Command("kubectl", "apply", "-f", url)); err != nil {
return err
Expand Down Expand Up @@ -157,8 +123,18 @@ func InstallCertManager() error {
return nil
}

// UninstallCertManager uninstalls the cert manager
func UninstallCertManager() {
func UninstallCertManagerIfApplicable(certManagerHasBeenInstalled bool) {
if certManagerHasBeenInstalled {
By("uninstalling the cert-manager bundle")
uninstallCertManager()
} else {
fmt.Fprint(GinkgoWriter,
"Note: The e2e test suite did not install cert-manager, thus it will also not uninstall it.\n",
)
}
}

func uninstallCertManager() {
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
if err := RunAndIgnoreOutput(exec.Command("kubectl", "delete", "-f", url)); err != nil {
warnError(err)
Expand Down Expand Up @@ -378,6 +354,58 @@ func isHttpServerSpanWithRoute(expectedRoute string) func(span ptrace.Span) bool
}
}

func RunAndIgnoreOutput(cmd *exec.Cmd, logCommandArgs ...bool) error {
_, err := Run(cmd, logCommandArgs...)
return err
}

// Run executes the provided command within this context
func Run(cmd *exec.Cmd, logCommandArgs ...bool) ([]byte, error) {
var logCommand bool
if len(logCommandArgs) > 0 {
logCommand = logCommandArgs[0]
} else {
logCommand = true
}

dir, _ := GetProjectDir()
cmd.Dir = dir

if err := os.Chdir(cmd.Dir); err != nil {
fmt.Fprintf(GinkgoWriter, "chdir dir: %s\n", err)
}

cmd.Env = append(os.Environ(), "GO111MODULE=on")
command := strings.Join(cmd.Args, " ")
if logCommand {
fmt.Fprintf(GinkgoWriter, "running: %s\n", command)
}
output, err := cmd.CombinedOutput()
if err != nil {
return output, fmt.Errorf("%s failed with error: (%v) %s", command, err, string(output))
}

return output, nil
}

// RunMultiple executes multiple commands
func RunMultiple(cmds []*exec.Cmd, logCommandArgs ...bool) error {
for _, cmd := range cmds {
if err := RunAndIgnoreOutput(cmd, logCommandArgs...); err != nil {
return err
}
}
return nil
}

func RunMultipleFromStrings(cmdsAsStrings [][]string, logCommandArgs ...bool) error {
cmds := make([]*exec.Cmd, len(cmdsAsStrings))
for i, cmdStrs := range cmdsAsStrings {
cmds[i] = exec.Command(cmdStrs[0], cmdStrs[1:]...)
}
return RunMultiple(cmds, logCommandArgs...)
}

func warnError(err error) {
fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
}
Expand Down
22 changes: 3 additions & 19 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ const (

var (
applicationNamespaceHasBeenCreated = false
certManagerHasBeenInstalled = false

originalKubeContext string
managerYamlNeedsRevert bool

skipCertManagerInstall = false
skipCertManagerUninstall = false
)

var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {
Expand All @@ -42,14 +40,6 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {
workingDir := strings.TrimSpace(string(pwdOutput))
fmt.Fprintf(GinkgoWriter, "workingDir: %s\n", workingDir)

if v, ok := os.LookupEnv("SKIP_CERT_MANAGER_UNINSTALL"); ok {
skipCertManagerUninstall = strings.ToLower(v) == "true"
}
if v, ok := os.LookupEnv("SKIP_CERT_MANAGER"); ok {
skipCertManagerInstall = strings.ToLower(v) == "true"
skipCertManagerUninstall = skipCertManagerInstall
}

By("Reading current imagePullPolicy")
yqOutput, err := Run(exec.Command(
"yq",
Expand Down Expand Up @@ -83,10 +73,7 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {
By("switching to kubectx docker-desktop, previous context " + originalKubeContext + " will be restored later")
ExpectWithOffset(1, RunAndIgnoreOutput(exec.Command("kubectx", "docker-desktop"))).To(Succeed())

if !skipCertManagerInstall {
By("installing the cert-manager")
ExpectWithOffset(1, InstallCertManager()).To(Succeed())
}
certManagerHasBeenInstalled = EnsureCertManagerIsInstalled()

applicationNamespaceHasBeenCreated = EnsureNamespaceExists(applicationUnderTestNamespace)

Expand Down Expand Up @@ -116,10 +103,7 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}

if !skipCertManagerUninstall {
By("uninstalling the cert-manager bundle")
UninstallCertManager()
}
UninstallCertManagerIfApplicable(certManagerHasBeenInstalled)

By("uninstalling the collector")
Expect(UninstallCollector(applicationUnderTestNamespace)).To(Succeed())
Expand Down

0 comments on commit 6cc1f4e

Please sign in to comment.