Skip to content

Commit

Permalink
test: add dedicated test for resource modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed May 17, 2024
1 parent 6a18124 commit 9c8cc17
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
File renamed without changes.
13 changes: 13 additions & 0 deletions internal/k8sresources/k8sresources_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
101 changes: 101 additions & 0 deletions internal/k8sresources/modify_test.go
Original file line number Diff line number Diff line change
@@ -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",
},
},
})
})
})
})
5 changes: 5 additions & 0 deletions internal/webhook/dash0_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 9c8cc17

Please sign in to comment.