Skip to content

Commit

Permalink
Add a feature test
Browse files Browse the repository at this point in the history
  • Loading branch information
szegedi committed Aug 12, 2024
1 parent d87f907 commit 1b74c56
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions controllers/datadogagent/feature/profiling/feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package profiling

import (
"testing"

apicommon "github.com/DataDog/datadog-operator/apis/datadoghq/common"
apicommonv1 "github.com/DataDog/datadog-operator/apis/datadoghq/common/v1"
"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
v2alpha1test "github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1/test"
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature"
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/fake"
"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/test"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
)

type envVar struct {
name string
value string
present bool
}

func assertEnv(envVars ...envVar) *test.ComponentTest {
return test.NewDefaultComponentTest().WithWantFunc(
func(t testing.TB, mgrInterface feature.PodTemplateManagers) {
mgr := mgrInterface.(*fake.PodTemplateManagers)
agentEnvs := mgr.EnvVarMgr.EnvVarsByC[apicommonv1.ClusterAgentContainerName]

for _, envVar := range envVars {
if !envVar.present {
for _, env := range agentEnvs {
require.NotEqual(t, envVar.name, env.Name)
}
continue
}

expected := &corev1.EnvVar{
Name: envVar.name,
Value: envVar.value,
}
require.Contains(t, agentEnvs, expected)
}
},
)
}

func TestProfilingFeature(t *testing.T) {
test.FeatureTestSuite{
{
Name: "Profiling disabled",
DDAv2: v2alpha1test.NewDatadogAgentBuilder().

Check failure on line 57 in controllers/datadogagent/feature/profiling/feature_test.go

View workflow job for this annotation

GitHub Actions / build

unknown field DDAv2 in struct literal of type struct{Name string; DDA *v2alpha1.DatadogAgent; Options *"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/test".Options; FeatureOptions *feature.Options; StoreOption *dependencies.StoreOptions; StoreInitFunc func(store dependencies.StoreClient); RequiredComponents feature.RequiredComponents; Agent *"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/test".ComponentTest; ClusterAgent *"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/test".ComponentTest; ClusterChecksRunner *"github.com/DataDog/datadog-operator/controllers/datadogagent/feature/test".ComponentTest; WantConfigure bool; WantManageDependenciesErr bool; WantDependenciesFunc func(testing.TB, dependencies.StoreClient)}
WithAdmissionControllerEnabled(true).
WithProfilingEnabled(v2alpha1.ProfilerOff).
Build(),

WantConfigure: true,
ClusterAgent: assertEnv(envVar{name: apicommon.DDAdmissionControllerProfilingEnabled, value: "false", present: true}),
},
{
Name: "Profiling unspecified",
DDAv2: v2alpha1test.NewDatadogAgentBuilder().
WithAdmissionControllerEnabled(true).
WithProfilingEnabled(v2alpha1.ProfilerEnablementUnspecified).
Build(),

WantConfigure: false,
},
{
Name: "Profiling set to auto",
DDAv2: v2alpha1test.NewDatadogAgentBuilder().
WithAdmissionControllerEnabled(true).
WithProfilingEnabled(v2alpha1.ProfilerAuto).
Build(),

WantConfigure: true,
ClusterAgent: assertEnv(envVar{name: apicommon.DDAdmissionControllerProfilingEnabled, value: "auto", present: true}),
},
{
Name: "Profiling enabled",
DDAv2: v2alpha1test.NewDatadogAgentBuilder().
WithAdmissionControllerEnabled(true).
WithProfilingEnabled(v2alpha1.ProfilerOn).
Build(),

WantConfigure: true,
ClusterAgent: assertEnv(envVar{name: apicommon.DDAdmissionControllerProfilingEnabled, value: "true", present: true}),
},
}.Run(t, buildProfilingFeature)
}

0 comments on commit 1b74c56

Please sign in to comment.