Skip to content
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

test(webhook): add cases for multiple containers, volumes etc. #1

Merged
merged 1 commit into from
May 15, 2024
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
101 changes: 84 additions & 17 deletions internal/webhook/dash0_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,107 @@
package webhook

import (
"fmt"

. "github.com/dash0hq/dash0-operator/test/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
)

var _ = Describe("Dash0", func() {
Context("with the Dash0 injection webhook", func() {
It("should inject Dash into a deployment", func() {
deployment := CreateDeployment(DefaultNamespace, DeploymentName)
type deploymentExpectations struct {
volumes int
dash0VolumeIdx int
initContainers int
dash0InitContainerIdx int
containers int
}

var _ = Describe("Dash0 Webhook", func() {
AfterEach(func() {
_ = k8sClient.Delete(ctx, CreateBasicDeployment(DefaultNamespace, DeploymentName))
})

Context("when mutating new deployments", func() {
It("should inject Dash into a new basic deployment", func() {
deployment := CreateBasicDeployment(DefaultNamespace, DeploymentName)
Expect(k8sClient.Create(ctx, deployment)).Should(Succeed())

deployment = GetDeployment(ctx, k8sClient, DefaultNamespace, DeploymentName)
verifyDeployment(deployment, deploymentExpectations{
volumes: 1,
dash0VolumeIdx: 0,
initContainers: 1,
dash0InitContainerIdx: 0,
containers: 1,
})
})

Expect(deployment.Spec.Template.Spec.Volumes).To(HaveLen(1))
volume := deployment.Spec.Template.Spec.Volumes[0]
It("should inject Dash into a new deployment that has multiple containers, and already has volumes and init containers", func() {
deployment := CreateDeploymentWithMoreBellsAndWhistles(DefaultNamespace, DeploymentName)
Expect(k8sClient.Create(ctx, deployment)).Should(Succeed())

deployment = GetDeployment(ctx, k8sClient, DefaultNamespace, DeploymentName)
verifyDeployment(deployment, deploymentExpectations{
volumes: 3,
dash0VolumeIdx: 2,
initContainers: 3,
dash0InitContainerIdx: 2,
containers: 3,
})
})

It("should update existing Dash artifacts in a new deployment", func() {
deployment := CreateDeploymentWithExistingDash0Artifacts(DefaultNamespace, DeploymentName)
Expect(k8sClient.Create(ctx, deployment)).Should(Succeed())

deployment = GetDeployment(ctx, k8sClient, DefaultNamespace, DeploymentName)
verifyDeployment(deployment, deploymentExpectations{
volumes: 3,
dash0VolumeIdx: 1,
initContainers: 3,
dash0InitContainerIdx: 1,
containers: 3,
})
})
})
})

func verifyDeployment(deployment *appsv1.Deployment, expectations deploymentExpectations) {
podSpec := deployment.Spec.Template.Spec

Expect(podSpec.Volumes).To(HaveLen(expectations.volumes))
for i, volume := range podSpec.Volumes {
if i == expectations.dash0VolumeIdx {
Expect(volume.Name).To(Equal("dash0-instrumentation"))
Expect(volume.EmptyDir).NotTo(BeNil())
} else {
Expect(volume.Name).To(Equal(fmt.Sprintf("test-volume-%d", i)))
}
}

Expect(deployment.Spec.Template.Spec.InitContainers).To(HaveLen(1))
initContainer := deployment.Spec.Template.Spec.InitContainers[0]
Expect(podSpec.InitContainers).To(HaveLen(expectations.initContainers))
for i, initContainer := range podSpec.InitContainers {
if i == expectations.dash0InitContainerIdx {
Expect(initContainer.Name).To(Equal("dash0-instrumentation"))
Expect(initContainer.Image).To(MatchRegexp("^dash0-instrumentation:\\d+\\.\\d+\\.\\d+"))
Expect(initContainer.Env).To(HaveLen(1))
Expect(initContainer.Env).To(ContainElement(MatchEnvVar("DASH0_INSTRUMENTATION_FOLDER_DESTINATION", "/opt/dash0")))
Expect(initContainer.SecurityContext).NotTo(BeNil())
Expect(initContainer.VolumeMounts).To(HaveLen(1))
Expect(initContainer.VolumeMounts).To(ContainElement(MatchVolumeMount("dash0-instrumentation", "/opt/dash0")))
} else {
Expect(initContainer.Name).To(Equal(fmt.Sprintf("test-init-container-%d", i)))
Expect(initContainer.Env).To(HaveLen(i + 1))
}
}

Expect(deployment.Spec.Template.Spec.Containers).To(HaveLen(1))
container := deployment.Spec.Template.Spec.Containers[0]
Expect(container.VolumeMounts).To(HaveLen(1))
Expect(container.VolumeMounts).To(ContainElement(MatchVolumeMount("dash0-instrumentation", "/opt/dash0")))
Expect(container.Env).To(HaveLen(1))
Expect(container.Env).To(ContainElement(MatchEnvVar("NODE_OPTIONS", "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js")))
})
})
})
Expect(podSpec.Containers).To(HaveLen(expectations.containers))
for i, container := range podSpec.Containers {
Expect(container.Name).To(Equal(fmt.Sprintf("test-container-%d", i)))
Expect(container.VolumeMounts).To(HaveLen(i + 1))
Expect(container.VolumeMounts).To(ContainElement(MatchVolumeMount("dash0-instrumentation", "/opt/dash0")))
Expect(container.Env).To(HaveLen(i + 1))
Expect(container.Env).To(ContainElement(MatchEnvVar("NODE_OPTIONS", "--require /opt/dash0/instrumentation/node.js/node_modules/@dash0/opentelemetry/src/index.js")))
}
}
5 changes: 2 additions & 3 deletions internal/webhook/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc

func TestAPIs(t *testing.T) {
func TestWebhook(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "Webhook Suite")
}

Expand Down Expand Up @@ -135,7 +134,7 @@ func setupTestResources() {
ns.Name = DefaultNamespace
Expect(k8sClient.Create(ctx, ns)).Should(Succeed())

Expect(k8sClient.Create(ctx, CreateDeployment(DefaultNamespace, DeploymentNameExisting))).Should(Succeed())
Expect(k8sClient.Create(ctx, CreateBasicDeployment(DefaultNamespace, DeploymentNameExisting))).Should(Succeed())
}

var _ = AfterSuite(func() {
Expand Down
185 changes: 183 additions & 2 deletions test/utils/webhook_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,203 @@ const (
DeploymentNameExisting = "existing-deployment"
)

func CreateDeployment(namespace string, name string) *appsv1.Deployment {
func CreateBasicDeployment(namespace string, name string) *appsv1.Deployment {
deployment := &appsv1.Deployment{}
deployment.Namespace = namespace
deployment.Name = name
deployment.Spec = appsv1.DeploymentSpec{}
deployment.Spec.Template = corev1.PodTemplateSpec{}
deployment.Spec.Template.Labels = map[string]string{"app": "test"}
deployment.Spec.Template.Spec.Containers = []corev1.Container{{
Name: "test-container",
Name: "test-container-0",
Image: "ubuntu",
}}
deployment.Spec.Selector = &metav1.LabelSelector{}
deployment.Spec.Selector.MatchLabels = map[string]string{"app": "test"}
return deployment
}

func CreateDeploymentWithMoreBellsAndWhistles(namespace string, name string) *appsv1.Deployment {
deployment := CreateBasicDeployment(namespace, name)
podSpec := &deployment.Spec.Template.Spec
podSpec.Volumes = []corev1.Volume{
{
Name: "test-volume-0",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
{
Name: "test-volume-1",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
}
podSpec.InitContainers = []corev1.Container{
{
Name: "test-init-container-0",
Image: "ubuntu",
Env: []corev1.EnvVar{{
Name: "TEST_INIT_0",
Value: "value",
}},
},
{
Name: "test-init-container-1",
Image: "ubuntu",
Env: []corev1.EnvVar{
{
Name: "TEST_INIT_0",
Value: "value",
},
{
Name: "TEST_INIT_1",
Value: "value",
},
},
},
}
podSpec.Containers = append(
deployment.Spec.Template.Spec.Containers,
corev1.Container{
Name: "test-container-1",
Image: "ubuntu",
VolumeMounts: []corev1.VolumeMount{{
Name: "test-volume-0",
MountPath: "/test-1",
}},
Env: []corev1.EnvVar{{
Name: "TEST0",
Value: "value",
}},
},
corev1.Container{
Name: "test-container-2",
Image: "ubuntu",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume-0",
MountPath: "/test-0",
},
{
Name: "test-volume-1",
MountPath: "/test-1",
},
},
Env: []corev1.EnvVar{
{
Name: "TEST0",
Value: "value",
},
{
Name: "TEST1",
ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.namespace"}},
},
},
})

return deployment
}

func CreateDeploymentWithExistingDash0Artifacts(namespace string, name string) *appsv1.Deployment {
deployment := CreateBasicDeployment(namespace, name)
podSpec := &deployment.Spec.Template.Spec
podSpec.Volumes = []corev1.Volume{
{
Name: "test-volume-0",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
{
Name: "dash0-instrumentation",
// the volume source should be updated/overwritten by the webhook
VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: "/foo/bar"}},
},
{
Name: "test-volume-2",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
},
}
podSpec.InitContainers = []corev1.Container{
{
Name: "test-init-container-0",
Image: "ubuntu",
Env: []corev1.EnvVar{{
Name: "TEST_INIT_0",
Value: "value",
}},
},
{
Name: "dash0-instrumentation",
Image: "ubuntu",
},
{
Name: "test-init-container-2",
Image: "ubuntu",
Env: []corev1.EnvVar{
{
Name: "TEST_INIT_0",
Value: "value",
},
{
Name: "TEST_INIT_1",
Value: "value",
},
{
Name: "TEST_INIT_2",
Value: "value",
},
},
},
}
podSpec.Containers = append(
deployment.Spec.Template.Spec.Containers,
corev1.Container{
Name: "test-container-1",
Image: "ubuntu",
VolumeMounts: []corev1.VolumeMount{
{
Name: "dash0-instrumentation",
MountPath: "/will/be/overwritten",
},
{
Name: "test-volume-0",
MountPath: "/test-1",
},
},
Env: []corev1.EnvVar{{
Name: "TEST0",
Value: "value",
}},
},
corev1.Container{
Name: "test-container-2",
Image: "ubuntu",
VolumeMounts: []corev1.VolumeMount{
{
Name: "test-volume-0",
MountPath: "/test-0",
},
{
Name: "dash0-instrumentation",
MountPath: "/will/be/overwritten",
},
{
Name: "test-volume-2",
MountPath: "/test-2",
},
},
Env: []corev1.EnvVar{
{
Name: "TEST0",
Value: "value",
},
{
Name: "TEST1",
ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{FieldPath: "metadata.namespace"}},
},
},
})

return deployment
}

func GetDeployment(
ctx context.Context,
k8sClient client.Client,
Expand Down