From 1b74c567f1b40d304ad4603b6a70502e2d44056a Mon Sep 17 00:00:00 2001 From: Attila Szegedi Date: Mon, 12 Aug 2024 12:40:35 +0200 Subject: [PATCH] Add a feature test --- .../feature/profiling/feature_test.go | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 controllers/datadogagent/feature/profiling/feature_test.go diff --git a/controllers/datadogagent/feature/profiling/feature_test.go b/controllers/datadogagent/feature/profiling/feature_test.go new file mode 100644 index 000000000..7544cd09b --- /dev/null +++ b/controllers/datadogagent/feature/profiling/feature_test.go @@ -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(). + 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) +}