|
| 1 | +package datadog |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + appsv1 "k8s.io/api/apps/v1" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + |
| 10 | + "github.com/DataDog/helm-charts/test/common" |
| 11 | +) |
| 12 | + |
| 13 | +// untaintToleration is the toleration the chart must inject so the node Agent |
| 14 | +// can schedule on nodes carrying the Datadog Operator untaint controller's |
| 15 | +// startup taint agent.datadoghq.com/not-ready=presence:NoSchedule. |
| 16 | +var untaintToleration = corev1.Toleration{ |
| 17 | + Key: "agent.datadoghq.com/not-ready", |
| 18 | + Operator: corev1.TolerationOpEqual, |
| 19 | + Value: "presence", |
| 20 | + Effect: corev1.TaintEffectNoSchedule, |
| 21 | +} |
| 22 | + |
| 23 | +func Test_untaintToleration(t *testing.T) { |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + command common.HelmCommand |
| 27 | + assertions func(t *testing.T, manifest string) |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "disabled by default -- toleration absent", |
| 31 | + command: common.HelmCommand{ |
| 32 | + ReleaseName: "datadog", |
| 33 | + ChartPath: "../../charts/datadog", |
| 34 | + ShowOnly: []string{"templates/daemonset.yaml"}, |
| 35 | + Values: []string{"../../charts/datadog/values.yaml"}, |
| 36 | + Overrides: map[string]string{ |
| 37 | + "datadog.apiKeyExistingSecret": "datadog-secret", |
| 38 | + "datadog.appKeyExistingSecret": "datadog-secret", |
| 39 | + }, |
| 40 | + }, |
| 41 | + assertions: verifyUntaintTolerationAbsent, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "enabled -- toleration injected", |
| 45 | + command: common.HelmCommand{ |
| 46 | + ReleaseName: "datadog", |
| 47 | + ChartPath: "../../charts/datadog", |
| 48 | + ShowOnly: []string{"templates/daemonset.yaml"}, |
| 49 | + Values: []string{"../../charts/datadog/values.yaml"}, |
| 50 | + Overrides: map[string]string{ |
| 51 | + "datadog.apiKeyExistingSecret": "datadog-secret", |
| 52 | + "datadog.appKeyExistingSecret": "datadog-secret", |
| 53 | + "operator.untaintController.enabled": "true", |
| 54 | + }, |
| 55 | + }, |
| 56 | + assertions: verifyUntaintTolerationPresent, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "enabled alongside user tolerations -- both present", |
| 60 | + command: common.HelmCommand{ |
| 61 | + ReleaseName: "datadog", |
| 62 | + ChartPath: "../../charts/datadog", |
| 63 | + ShowOnly: []string{"templates/daemonset.yaml"}, |
| 64 | + Values: []string{"../../charts/datadog/values.yaml"}, |
| 65 | + Overrides: map[string]string{ |
| 66 | + "datadog.apiKeyExistingSecret": "datadog-secret", |
| 67 | + "datadog.appKeyExistingSecret": "datadog-secret", |
| 68 | + "operator.untaintController.enabled": "true", |
| 69 | + "agents.tolerations[0].key": "dedicated", |
| 70 | + "agents.tolerations[0].operator": "Exists", |
| 71 | + "agents.tolerations[0].effect": "NoSchedule", |
| 72 | + }, |
| 73 | + }, |
| 74 | + assertions: verifyUntaintTolerationWithUserToleration, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + manifest, err := common.RenderChart(t, tt.command) |
| 81 | + assert.Nil(t, err, "couldn't render template") |
| 82 | + tt.assertions(t, manifest) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func hasToleration(tolerations []corev1.Toleration, want corev1.Toleration) bool { |
| 88 | + for _, tol := range tolerations { |
| 89 | + if tol == want { |
| 90 | + return true |
| 91 | + } |
| 92 | + } |
| 93 | + return false |
| 94 | +} |
| 95 | + |
| 96 | +func verifyUntaintTolerationAbsent(t *testing.T, manifest string) { |
| 97 | + var ds appsv1.DaemonSet |
| 98 | + common.Unmarshal(t, manifest, &ds) |
| 99 | + assert.False(t, hasToleration(ds.Spec.Template.Spec.Tolerations, untaintToleration), |
| 100 | + "untaint toleration must not be present when operator.untaintController.enabled is false") |
| 101 | +} |
| 102 | + |
| 103 | +func verifyUntaintTolerationPresent(t *testing.T, manifest string) { |
| 104 | + var ds appsv1.DaemonSet |
| 105 | + common.Unmarshal(t, manifest, &ds) |
| 106 | + assert.True(t, hasToleration(ds.Spec.Template.Spec.Tolerations, untaintToleration), |
| 107 | + "untaint toleration must be injected when operator.untaintController.enabled is true") |
| 108 | +} |
| 109 | + |
| 110 | +func verifyUntaintTolerationWithUserToleration(t *testing.T, manifest string) { |
| 111 | + var ds appsv1.DaemonSet |
| 112 | + common.Unmarshal(t, manifest, &ds) |
| 113 | + tolerations := ds.Spec.Template.Spec.Tolerations |
| 114 | + assert.True(t, hasToleration(tolerations, untaintToleration), |
| 115 | + "untaint toleration must be injected") |
| 116 | + assert.True(t, hasToleration(tolerations, corev1.Toleration{ |
| 117 | + Key: "dedicated", |
| 118 | + Operator: corev1.TolerationOpExists, |
| 119 | + Effect: corev1.TaintEffectNoSchedule, |
| 120 | + }), "user-provided agents.tolerations must be preserved alongside the untaint toleration") |
| 121 | +} |
0 commit comments