Skip to content

Commit

Permalink
test(e2e): check for blocked ports before running e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed May 27, 2024
1 parent e4744fe commit 1d6dca5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 21 deletions.
37 changes: 37 additions & 0 deletions test/e2e/e2e_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,45 @@ const (

var (
traceUnmarshaller = &ptrace.JSONUnmarshaler{}
requiredPorts = []int{1207, 4317, 4318}
)

func CheckIfRequiredPortsAreBlocked() {
portsCurrentlyInUseByKubernetesServices, err := Run(
exec.Command(
"kubectl",
"get",
"svc",
"--all-namespaces",
"-o",
"go-template='{{range .items}}{{range.spec.ports}}{{if .port}}{{.port}}{{\"\\n\"}}{{end}}{{end}}{{end}}'",
))
Expect(err).NotTo(HaveOccurred())
portsCurrentlyInUseArray := GetNonEmptyLines(string(portsCurrentlyInUseByKubernetesServices))
messages := make([]string, 0)
foundBlockedPort := false
for _, usedPortStr := range portsCurrentlyInUseArray {
usedPort, err := strconv.Atoi(usedPortStr)
if err != nil {
continue
}
for _, requiredPort := range requiredPorts {
if usedPort == requiredPort {
messages = append(messages,
fmt.Sprintf(
"Port %d is required by the test suite, but it is already in use by a Kubernetes "+
"service. Please check for conflicting deployed serivces.",
requiredPort,
))
foundBlockedPort = true
}
}
}
if foundBlockedPort {
Fail(strings.Join(messages, "\n"))
}
}

func RenderTemplates() {
By("render yaml templates")
ExpectWithOffset(1, RunAndIgnoreOutput(exec.Command("test-resources/bin/render-templates.sh"))).To(Succeed())
Expand Down
61 changes: 40 additions & 21 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
operatorNamespace = "dash0-operator-system"
operatorImage = "dash0-operator-controller:latest"

kubeContextForTest = "docker-desktop"
applicationUnderTestNamespace = "e2e-application-under-test-namespace"

managerYaml = "config/manager/manager.yaml"
Expand All @@ -28,8 +29,11 @@ var (
applicationNamespaceHasBeenCreated = false
certManagerHasBeenInstalled = false

originalKubeContext string
managerYamlNeedsRevert bool
originalKubeContext string
managerYamlNeedsRevert bool
collectorHasBeenInstalled bool
managerNamespaceHasBeenCreated bool
setupFinishedSuccessfully bool
)

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

CheckIfRequiredPortsAreBlocked()

RenderTemplates()

By("reading current imagePullPolicy")
Expand Down Expand Up @@ -72,25 +78,31 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
originalKubeContext = strings.TrimSpace(string(kubectxOutput))

By("switching to kubectx docker-desktop, previous context " + originalKubeContext + " will be restored later")
ExpectWithOffset(1, RunAndIgnoreOutput(exec.Command("kubectx", "docker-desktop"))).To(Succeed())
if originalKubeContext != kubeContextForTest {
By("switching to kubectx docker-desktop, previous context " + originalKubeContext + " will be restored later")
ExpectWithOffset(1, RunAndIgnoreOutput(exec.Command("kubectx", "docker-desktop"))).To(Succeed())
}

certManagerHasBeenInstalled = EnsureCertManagerIsInstalled()

applicationNamespaceHasBeenCreated = EnsureNamespaceExists(applicationUnderTestNamespace)

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

By("creating manager namespace")
ExpectWithOffset(1, RunAndIgnoreOutput(exec.Command("kubectl", "create", "ns", operatorNamespace))).To(Succeed())
managerNamespaceHasBeenCreated = true

By("building the manager(Operator) image")
ExpectWithOffset(1,
RunAndIgnoreOutput(exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", operatorImage)))).To(Succeed())

By("installing CRDs")
ExpectWithOffset(1, RunAndIgnoreOutput(exec.Command("make", "install"))).To(Succeed())

setupFinishedSuccessfully = true
})

AfterAll(func() {
Expand All @@ -104,38 +116,45 @@ var _ = Describe("Dash0 Kubernetes Operator", Ordered, func() {

UninstallCertManagerIfApplicable(certManagerHasBeenInstalled)

By("uninstalling the collector")
Expect(UninstallCollector(applicationUnderTestNamespace)).To(Succeed())
if collectorHasBeenInstalled {
By("uninstalling the collector")
Expect(UninstallCollector(applicationUnderTestNamespace)).To(Succeed())
}

By("removing manager namespace")
_ = RunAndIgnoreOutput(exec.Command("kubectl", "delete", "ns", operatorNamespace))
if managerNamespaceHasBeenCreated {
By("removing manager namespace")
_ = RunAndIgnoreOutput(exec.Command("kubectl", "delete", "ns", operatorNamespace))
}

if applicationNamespaceHasBeenCreated && applicationUnderTestNamespace != "default" {
By("removing namespace for application under test")
_ = RunAndIgnoreOutput(exec.Command("kubectl", "delete", "ns", applicationUnderTestNamespace))
}

By("switching back to original kubectx " + originalKubeContext)
output, err := Run(exec.Command("kubectx", originalKubeContext))
if err != nil {
fmt.Fprint(GinkgoWriter, err.Error())
if originalKubeContext != "" && originalKubeContext != kubeContextForTest {
By("switching back to original kubectx " + originalKubeContext)
output, err := Run(exec.Command("kubectx", originalKubeContext))
if err != nil {
fmt.Fprint(GinkgoWriter, err.Error())
}
fmt.Fprint(GinkgoWriter, string(output))
}
fmt.Fprint(GinkgoWriter, string(output))
})

BeforeEach(func() {
DeleteTestIdFiles()
})

AfterEach(func() {
// As an alternative to undeploying all applications under test (deployment, daemonset, cronjob, ...) we could
// also delete the whole namespace for the application under test to after each test case get rid of all
// applications (and then recreate the namespace before each test). However, this would mean we also need to
// deploy the OpenTelemetry collector to the target namespace again for each test case, which would slow down
// tests a bit more.
RemoveAllTestApplications(applicationUnderTestNamespace)

DeleteTestIdFiles()
if setupFinishedSuccessfully {
// As an alternative to undeploying all applications under test (deployment, daemonset, cronjob, ...) we
// could also delete the whole namespace for the application under test to after each test case get rid of
// all applications (and then recreate the namespace before each test). However, this would mean we also
// need to deploy the OpenTelemetry collector to the target namespace again for each test case, which would
// slow down tests a bit more.
RemoveAllTestApplications(applicationUnderTestNamespace)
DeleteTestIdFiles()
}
})

Describe("controller", func() {
Expand Down

0 comments on commit 1d6dca5

Please sign in to comment.