-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathcontroller_base_test.go
More file actions
172 lines (155 loc) · 4.69 KB
/
controller_base_test.go
File metadata and controls
172 lines (155 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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.
//go:build kubeapiserver
package webhook
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/fx"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
"github.com/DataDog/datadog-agent/cmd/cluster-agent/admission"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/config"
log "github.com/DataDog/datadog-agent/comp/core/log/def"
logmock "github.com/DataDog/datadog-agent/comp/core/log/mock"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
workloadmetafxmock "github.com/DataDog/datadog-agent/comp/core/workloadmeta/fx-mock"
workloadmetamock "github.com/DataDog/datadog-agent/comp/core/workloadmeta/mock"
"github.com/DataDog/datadog-agent/pkg/clusteragent/admission/mutate/autoinstrumentation"
"github.com/DataDog/datadog-agent/pkg/clusteragent/admission/mutate/autoinstrumentation/imageresolver"
configmock "github.com/DataDog/datadog-agent/pkg/config/mock"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)
func TestNewController(t *testing.T) {
client := fake.NewSimpleClientset()
wmeta := fxutil.Test[workloadmeta.Component](t, core.MockBundle(), workloadmetafxmock.MockModule(workloadmeta.NewParams()))
datadogConfig := config.NewMock(t)
factory := informers.NewSharedInformerFactory(client, time.Duration(0))
imageResolver := autoinstrumentation.NewImageResolver(*imageresolver.NewConfig(datadogConfig, nil))
// V1
controller := NewController(
client,
factory.Core().V1().Secrets(),
factory.Admissionregistration(),
factory.Admissionregistration(),
func() bool { return true },
make(<-chan struct{}),
getV1Cfg(t),
wmeta,
nil,
datadogConfig,
nil,
imageResolver,
)
assert.IsType(t, &ControllerV1{}, controller)
// V1beta1
controller = NewController(
client,
factory.Core().V1().Secrets(),
factory.Admissionregistration(),
factory.Admissionregistration(),
func() bool { return true },
make(<-chan struct{}),
getV1beta1Cfg(t),
wmeta,
nil,
datadogConfig,
nil,
imageResolver,
)
assert.IsType(t, &ControllerV1beta1{}, controller)
}
func TestAutoInstrumentation(t *testing.T) {
tests := []struct {
name string
pod *corev1.Pod
config string
expectPatch bool
}{
{
name: "disabled webhooks should not patch",
config: "testdata/all_disabled.yaml",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"admission.datadoghq.com/enabled": "true",
},
Namespace: "foo",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "foo",
},
},
},
},
expectPatch: false,
},
{
name: "disabled webhooks should not patch, even with targets",
config: "testdata/all_disabled_targets.yaml",
pod: &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"admission.datadoghq.com/enabled": "true",
"app": "billing-service",
},
Namespace: "foo",
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "foo",
},
},
},
},
expectPatch: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a mock config.
mockConfig := configmock.NewFromFile(t, tt.config)
// Create a mock meta.
wmeta := fxutil.Test[workloadmetamock.Mock](t, fx.Options(
fx.Supply(config.Params{}),
fx.Provide(func() log.Component { return logmock.New(t) }),
fx.Provide(func() config.Component { return config.NewMock(t) }),
workloadmetafxmock.MockModule(workloadmeta.NewParams()),
))
// Create APM webhook.
imageResolver := autoinstrumentation.NewImageResolver(*imageresolver.NewConfig(mockConfig, nil))
apm, err := generateAutoInstrumentationWebhook(wmeta, mockConfig, imageResolver)
assert.NoError(t, err)
// Create request.
podJSON, err := json.Marshal(tt.pod)
assert.NoError(t, err)
t.Log(string(podJSON))
request := &admission.Request{
Object: podJSON,
Namespace: tt.pod.Namespace,
}
// Send request.
f := apm.WebhookFunc()
response := f(request)
// Check if the patch is expected.
emptyPatch := "null"
if tt.expectPatch {
assert.NotEqual(t, emptyPatch, string(response.Patch))
} else {
assert.Equal(t, emptyPatch, string(response.Patch))
}
})
}
}