forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrumentation_webhook_test.go
309 lines (298 loc) · 8.21 KB
/
instrumentation_webhook_test.go
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package v1alpha1
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"github.com/open-telemetry/opentelemetry-operator/internal/config"
)
var defaultVolumeSize = resource.MustParse("200Mi")
func TestInstrumentationDefaultingWebhook(t *testing.T) {
inst := &Instrumentation{}
err := InstrumentationWebhook{
cfg: config.New(
config.WithAutoInstrumentationJavaImage("java-img:1"),
config.WithAutoInstrumentationNodeJSImage("nodejs-img:1"),
config.WithAutoInstrumentationPythonImage("python-img:1"),
config.WithAutoInstrumentationDotNetImage("dotnet-img:1"),
config.WithAutoInstrumentationApacheHttpdImage("apache-httpd-img:1"),
config.WithAutoInstrumentationNginxImage("nginx-img:1"),
),
}.Default(context.Background(), inst)
assert.NoError(t, err)
assert.Equal(t, "java-img:1", inst.Spec.Java.Image)
assert.Equal(t, "nodejs-img:1", inst.Spec.NodeJS.Image)
assert.Equal(t, "python-img:1", inst.Spec.Python.Image)
assert.Equal(t, "dotnet-img:1", inst.Spec.DotNet.Image)
assert.Equal(t, "apache-httpd-img:1", inst.Spec.ApacheHttpd.Image)
assert.Equal(t, "nginx-img:1", inst.Spec.Nginx.Image)
}
func TestInstrumentationValidatingWebhook(t *testing.T) {
tests := []struct {
name string
err string
warnings admission.Warnings
inst Instrumentation
}{
{
name: "all defaults",
inst: Instrumentation{
Spec: InstrumentationSpec{},
},
warnings: []string{"sampler type not set"},
},
{
name: "sampler configuration not present",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{},
},
},
warnings: []string{"sampler type not set"},
},
{
name: "argument is not a number",
err: "spec.sampler.argument is not a number",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
Argument: "abc",
},
},
},
},
{
name: "argument is a wrong number",
err: "spec.sampler.argument should be in rage [0..1]",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
Argument: "1.99",
},
},
},
},
{
name: "argument is a number",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
Argument: "0.99",
},
},
},
},
{
name: "argument is missing",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
},
},
},
},
{
name: "with volume and volumeSizeLimit",
err: "spec.nodejs.volumeClaimTemplate and spec.nodejs.volumeSizeLimit cannot both be defined",
inst: Instrumentation{
Spec: InstrumentationSpec{
NodeJS: NodeJS{
VolumeClaimTemplate: corev1.PersistentVolumeClaimTemplate{
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
},
},
VolumeSizeLimit: &defaultVolumeSize,
},
},
},
warnings: []string{"sampler type not set"},
},
{
name: "exporter: tls cert set but missing key",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
Argument: "0.99",
},
Exporter: Exporter{
Endpoint: "https://collector:4317",
TLS: &TLS{
Cert: "cert",
},
},
},
},
warnings: []string{"both exporter.tls.key and exporter.tls.cert mut be set"},
},
{
name: "exporter: tls key set but missing cert",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
Argument: "0.99",
},
Exporter: Exporter{
Endpoint: "https://collector:4317",
TLS: &TLS{
Key: "key",
},
},
},
},
warnings: []string{"both exporter.tls.key and exporter.tls.cert mut be set"},
},
{
name: "exporter: tls set but using http://",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
Argument: "0.99",
},
Exporter: Exporter{
Endpoint: "http://collector:4317",
TLS: &TLS{
Key: "key",
Cert: "cert",
},
},
},
},
warnings: []string{"exporter.tls is configured but exporter.endpoint is not enabling TLS with https://"},
},
{
name: "exporter: exporter using http://, but the tls is nil",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
Argument: "0.99",
},
Exporter: Exporter{
Endpoint: "https://collector:4317",
},
},
},
warnings: []string{"exporter is using https:// but exporter.tls is unset"},
},
{
name: "exporter no warning set",
inst: Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: ParentBasedTraceIDRatio,
Argument: "0.99",
},
Exporter: Exporter{
Endpoint: "https://collector:4317",
TLS: &TLS{
Key: "key",
Cert: "cert",
},
},
},
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
if test.err == "" {
warnings, err := InstrumentationWebhook{}.ValidateCreate(ctx, &test.inst)
assert.Equal(t, test.warnings, warnings)
assert.Nil(t, err)
warnings, err = InstrumentationWebhook{}.ValidateUpdate(ctx, nil, &test.inst)
assert.Equal(t, test.warnings, warnings)
assert.Nil(t, err)
} else {
warnings, err := InstrumentationWebhook{}.ValidateCreate(ctx, &test.inst)
assert.Equal(t, test.warnings, warnings)
assert.Contains(t, err.Error(), test.err)
warnings, err = InstrumentationWebhook{}.ValidateUpdate(ctx, nil, &test.inst)
assert.Equal(t, test.warnings, warnings)
assert.Contains(t, err.Error(), test.err)
}
})
}
}
func TestInstrumentationJaegerRemote(t *testing.T) {
tests := []struct {
name string
err string
arg string
}{
{
name: "pollingIntervalMs is not a number",
err: "invalid pollingIntervalMs: abc",
arg: "pollingIntervalMs=abc",
},
{
name: "initialSamplingRate is out of range",
err: "initialSamplingRate should be in rage [0..1]",
arg: "initialSamplingRate=1.99",
},
{
name: "endpoint is missing",
err: "endpoint cannot be empty",
arg: "endpoint=",
},
{
name: "correct jaeger remote sampler configuration",
arg: "endpoint=http://jaeger-collector:14250/,initialSamplingRate=0.99,pollingIntervalMs=1000",
},
}
samplers := []SamplerType{JaegerRemote, ParentBasedJaegerRemote}
for _, sampler := range samplers {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
inst := Instrumentation{
Spec: InstrumentationSpec{
Sampler: Sampler{
Type: sampler,
Argument: test.arg,
},
},
}
ctx := context.Background()
if test.err == "" {
warnings, err := InstrumentationWebhook{}.ValidateCreate(ctx, &inst)
assert.Nil(t, warnings)
assert.Nil(t, err)
warnings, err = InstrumentationWebhook{}.ValidateUpdate(ctx, nil, &inst)
assert.Nil(t, warnings)
assert.Nil(t, err)
} else {
warnings, err := InstrumentationWebhook{}.ValidateCreate(ctx, &inst)
assert.Nil(t, warnings)
assert.Contains(t, err.Error(), test.err)
warnings, err = InstrumentationWebhook{}.ValidateUpdate(ctx, nil, &inst)
assert.Nil(t, warnings)
assert.Contains(t, err.Error(), test.err)
}
})
}
}
}