From 9c8cc176afe7e795e263c8c2613ea52004aebe5e Mon Sep 17 00:00:00 2001 From: Bastian Krol Date: Fri, 17 May 2024 13:06:27 +0200 Subject: [PATCH] test: add dedicated test for resource modifications --- ...suite_test.go => controller_suite_test.go} | 0 .../k8sresources/k8sresources_suite_test.go | 13 +++ internal/k8sresources/modify_test.go | 101 ++++++++++++++++++ internal/webhook/dash0_webhook_test.go | 5 + 4 files changed, 119 insertions(+) rename internal/controller/{suite_test.go => controller_suite_test.go} (100%) create mode 100644 internal/k8sresources/k8sresources_suite_test.go create mode 100644 internal/k8sresources/modify_test.go diff --git a/internal/controller/suite_test.go b/internal/controller/controller_suite_test.go similarity index 100% rename from internal/controller/suite_test.go rename to internal/controller/controller_suite_test.go diff --git a/internal/k8sresources/k8sresources_suite_test.go b/internal/k8sresources/k8sresources_suite_test.go new file mode 100644 index 00000000..55f047d6 --- /dev/null +++ b/internal/k8sresources/k8sresources_suite_test.go @@ -0,0 +1,13 @@ +package k8sresources_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestK8sresources(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "K8s Resources Suite") +} diff --git a/internal/k8sresources/modify_test.go b/internal/k8sresources/modify_test.go new file mode 100644 index 00000000..24a7ed51 --- /dev/null +++ b/internal/k8sresources/modify_test.go @@ -0,0 +1,101 @@ +// SPDX-FileCopyrightText: Copyright 2024 Dash0 Inc. +// SPDX-License-Identifier: Apache-2.0 + +package k8sresources + +import ( + "context" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "sigs.k8s.io/controller-runtime/pkg/log" + + . "github.com/dash0hq/dash0-operator/test/util" +) + +// Maintenance note: There is some overlap of test cases between this file and dash0_webhook_test.go. This is +// intentional. However, this test should be used for more fine-grained test cases, while dash0_webhook_test.go should +// be used to verify external effects (recording events etc.) that cannot be covered in this test. + +var _ = Describe("Dash0 Webhook", func() { + + ctx := context.Background() + + Context("when mutating new deployments", func() { + It("should inject Dash into a new basic deployment", func() { + deployment := BasicDeployment(TestNamespaceName, DeploymentName) + result := ModifyPodSpec(&deployment.Spec.Template.Spec, log.FromContext(ctx)) + + Expect(result).To(BeTrue()) + VerifyModifiedDeployment(deployment, DeploymentExpectations{ + Volumes: 1, + Dash0VolumeIdx: 0, + InitContainers: 1, + Dash0InitContainerIdx: 0, + Containers: []ContainerExpectations{{ + VolumeMounts: 1, + Dash0VolumeMountIdx: 0, + EnvVars: 1, + NodeOptionsEnvVarIdx: 0, + }}, + }) + }) + + It("should inject Dash into a new deployment that has multiple Containers, and already has Volumes and init Containers", func() { + deployment := DeploymentWithMoreBellsAndWhistles(TestNamespaceName, DeploymentName) + result := ModifyPodSpec(&deployment.Spec.Template.Spec, log.FromContext(ctx)) + + Expect(result).To(BeTrue()) + VerifyModifiedDeployment(deployment, DeploymentExpectations{ + Volumes: 3, + Dash0VolumeIdx: 2, + InitContainers: 3, + Dash0InitContainerIdx: 2, + Containers: []ContainerExpectations{ + { + VolumeMounts: 2, + Dash0VolumeMountIdx: 1, + EnvVars: 2, + NodeOptionsEnvVarIdx: 1, + }, + { + VolumeMounts: 3, + Dash0VolumeMountIdx: 2, + EnvVars: 3, + NodeOptionsEnvVarIdx: 2, + }, + }, + }) + }) + + It("should update existing Dash artifacts in a new deployment", func() { + deployment := DeploymentWithExistingDash0Artifacts(TestNamespaceName, DeploymentName) + result := ModifyPodSpec(&deployment.Spec.Template.Spec, log.FromContext(ctx)) + + Expect(result).To(BeTrue()) + VerifyModifiedDeployment(deployment, DeploymentExpectations{ + Volumes: 3, + Dash0VolumeIdx: 1, + InitContainers: 3, + Dash0InitContainerIdx: 1, + Containers: []ContainerExpectations{ + { + VolumeMounts: 2, + Dash0VolumeMountIdx: 1, + EnvVars: 2, + NodeOptionsEnvVarIdx: 1, + NodeOptionsUsesValueFrom: true, + }, + { + VolumeMounts: 3, + Dash0VolumeMountIdx: 1, + EnvVars: 3, + NodeOptionsEnvVarIdx: 1, + NodeOptionsValue: "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js --require something-else --experimental-modules", + }, + }, + }) + }) + }) +}) diff --git a/internal/webhook/dash0_webhook_test.go b/internal/webhook/dash0_webhook_test.go index 72a7d534..bfe62ba3 100644 --- a/internal/webhook/dash0_webhook_test.go +++ b/internal/webhook/dash0_webhook_test.go @@ -9,6 +9,11 @@ import ( . "github.com/onsi/gomega" ) +// Maintenance note: There is some overlap of test cases between this file and k8sresources/modify_test.go. This is +// intentional. However, this test should be used to verify external effects (recording events etc.) that cannot be +// covered modify_test.go, while more fine-grained test cases and variations should rather be added to +// k8sresources/modify_test.go. + var _ = Describe("Dash0 Webhook", func() { AfterEach(func() { _ = k8sClient.Delete(ctx, BasicDeployment(TestNamespaceName, DeploymentName))