99 "context"
1010 "fmt"
1111 "reflect"
12+ "slices"
1213 "sort"
1314 "testing"
1415 "time"
@@ -44,6 +45,7 @@ import (
4445 "github.com/DataDog/datadog-operator/pkg/images"
4546 "github.com/DataDog/datadog-operator/pkg/kubernetes"
4647 "github.com/DataDog/datadog-operator/pkg/testutils"
48+ "github.com/DataDog/datadog-operator/pkg/untaint"
4749 pkgutils "github.com/DataDog/datadog-operator/pkg/utils"
4850)
4951
@@ -61,6 +63,17 @@ type testCase struct {
6163 introspectionEnabled bool // For introspection tests
6264}
6365
66+ // ddaiReconcilerOptionsFromDDA mirrors setup.go wiring so DDAI tests behave like production
67+ // for flags shared with the DatadogAgent reconciler (e.g. UntaintControllerEnabled).
68+ func ddaiReconcilerOptionsFromDDA (opts ReconcilerOptions ) datadogagentinternal.ReconcilerOptions {
69+ return datadogagentinternal.ReconcilerOptions {
70+ ExtendedDaemonsetOptions : opts .ExtendedDaemonsetOptions ,
71+ SupportCilium : opts .SupportCilium ,
72+ OperatorMetricsEnabled : opts .OperatorMetricsEnabled ,
73+ UntaintControllerEnabled : opts .UntaintControllerEnabled ,
74+ }
75+ }
76+
6477// runTestCases runs test cases
6578func runTestCases (t * testing.T , tests []testCase , testFunc func (t * testing.T , tt testCase , opts ReconcilerOptions )) {
6679 for _ , tt := range tests {
@@ -106,7 +119,7 @@ func runDDAReconcilerTest(t *testing.T, tt testCase, opts ReconcilerOptions) {
106119 r .initializeComponentRegistry ()
107120
108121 ri := datadogagentinternal .NewReconciler (
109- datadogagentinternal. ReconcilerOptions {} ,
122+ ddaiReconcilerOptionsFromDDA ( opts ) ,
110123 c ,
111124 kubernetes.PlatformInfo {},
112125 s ,
@@ -179,7 +192,7 @@ func runFullReconcilerTest(t *testing.T, tt testCase, opts ReconcilerOptions) {
179192 r .initializeComponentRegistry ()
180193
181194 ri := datadogagentinternal .NewReconciler (
182- datadogagentinternal. ReconcilerOptions {} ,
195+ ddaiReconcilerOptionsFromDDA ( opts ) ,
183196 c ,
184197 kubernetes.PlatformInfo {},
185198 s ,
@@ -248,6 +261,64 @@ func buildClient(t *testing.T, tt testCase, s *runtime.Scheme) client.Client {
248261 return builder .Build ()
249262}
250263
264+ func TestReconcileDDA_UntaintController_injectsAgentNotReadyToleration (t * testing.T ) {
265+ const resourcesName = "foo"
266+ const resourcesNamespace = "bar"
267+ const dsName = "foo-agent"
268+ defaultRequeueDuration := 15 * time .Second
269+
270+ wantTol := untaint .AgentNotReadyEqualToleration ()
271+ tt := testCase {
272+ name : "untaint controller enabled injects agent-not-ready toleration on node agent DS" ,
273+ loadFunc : func (c client.Client ) * v2alpha1.DatadogAgent {
274+ dda := testutils .NewInitializedDatadogAgentBuilder (resourcesNamespace , resourcesName ).
275+ Build ()
276+ _ = c .Create (context .TODO (), dda )
277+ return dda
278+ },
279+ want : reconcile.Result {RequeueAfter : defaultRequeueDuration },
280+ wantErr : false ,
281+ wantFunc : func (t * testing.T , c client.Client ) {
282+ ds := & appsv1.DaemonSet {}
283+ err := c .Get (context .TODO (), types.NamespacedName {Namespace : resourcesNamespace , Name : dsName }, ds )
284+ assert .NoError (t , err )
285+ assert .True (t , slices .ContainsFunc (ds .Spec .Template .Spec .Tolerations , func (tol corev1.Toleration ) bool {
286+ return reflect .DeepEqual (tol , wantTol )
287+ }), "expected injected toleration %+v in %+v" , wantTol , ds .Spec .Template .Spec .Tolerations )
288+ },
289+ }
290+ runDDAReconcilerTest (t , tt , ReconcilerOptions {UntaintControllerEnabled : true })
291+ }
292+
293+ func TestReconcileDDA_UntaintController_disabledDoesNotInjectAgentNotReadyToleration (t * testing.T ) {
294+ const resourcesName = "foo"
295+ const resourcesNamespace = "bar"
296+ const dsName = "foo-agent"
297+ defaultRequeueDuration := 15 * time .Second
298+
299+ wantTol := untaint .AgentNotReadyEqualToleration ()
300+ tt := testCase {
301+ name : "untaint controller disabled does not inject agent-not-ready toleration on node agent DS" ,
302+ loadFunc : func (c client.Client ) * v2alpha1.DatadogAgent {
303+ dda := testutils .NewInitializedDatadogAgentBuilder (resourcesNamespace , resourcesName ).
304+ Build ()
305+ _ = c .Create (context .TODO (), dda )
306+ return dda
307+ },
308+ want : reconcile.Result {RequeueAfter : defaultRequeueDuration },
309+ wantErr : false ,
310+ wantFunc : func (t * testing.T , c client.Client ) {
311+ ds := & appsv1.DaemonSet {}
312+ err := c .Get (context .TODO (), types.NamespacedName {Namespace : resourcesNamespace , Name : dsName }, ds )
313+ assert .NoError (t , err )
314+ assert .False (t , slices .ContainsFunc (ds .Spec .Template .Spec .Tolerations , func (tol corev1.Toleration ) bool {
315+ return reflect .DeepEqual (tol , wantTol )
316+ }), "did not expect injected toleration %+v in %+v" , wantTol , ds .Spec .Template .Spec .Tolerations )
317+ },
318+ }
319+ runDDAReconcilerTest (t , tt , ReconcilerOptions {UntaintControllerEnabled : false })
320+ }
321+
251322func TestReconcileDatadogAgentV2_Reconcile (t * testing.T ) {
252323 const resourcesName = "foo"
253324 const resourcesNamespace = "bar"
0 commit comments