-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtracing_test.go
More file actions
548 lines (477 loc) · 14.3 KB
/
tracing_test.go
File metadata and controls
548 lines (477 loc) · 14.3 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package tracing
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCreateCorrelationContextHeader(t *testing.T) {
t.Run("empty options", func(t *testing.T) {
ctx := context.Background()
options := Options{}
header := CreateCorrelationContextHeader(ctx, options)
// The header should be empty but exist
corrContext := header.Get(CorrelationContextHeader)
assert.Equal(t, "RequestType=StartUp", corrContext)
})
t.Run("with RequestTypeStartUp", func(t *testing.T) {
options := Options{}
header := CreateCorrelationContextHeader(context.Background(), options)
// Should contain RequestTypeStartUp
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, RequestTypeKey+"="+string(RequestTypeStartUp))
})
t.Run("with RequestTypeWatch", func(t *testing.T) {
options := Options{
InitialLoadFinished: true,
}
header := CreateCorrelationContextHeader(context.Background(), options)
// Should contain RequestTypeWatch
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, RequestTypeKey+"="+string(RequestTypeWatch))
})
t.Run("with Host", func(t *testing.T) {
ctx := context.Background()
options := Options{
Host: HostTypeAzureWebApp,
}
header := CreateCorrelationContextHeader(ctx, options)
// Should contain Host
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, HostTypeKey+"="+string(HostTypeAzureWebApp))
})
t.Run("with KeyVault configured", func(t *testing.T) {
ctx := context.Background()
options := Options{
KeyVaultConfigured: true,
}
header := CreateCorrelationContextHeader(ctx, options)
// Should contain KeyVaultConfiguredTag
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, KeyVaultConfiguredTag)
})
t.Run("with KeyVaultRefresh configured", func(t *testing.T) {
ctx := context.Background()
options := Options{
KeyVaultConfigured: true,
KeyVaultRefreshConfigured: true,
}
header := CreateCorrelationContextHeader(ctx, options)
// Should contain KeyVaultRefreshConfiguredTag
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, KeyVaultRefreshConfiguredTag)
})
t.Run("with AI configuration", func(t *testing.T) {
ctx := context.Background()
options := Options{
UseAIConfiguration: true,
}
header := CreateCorrelationContextHeader(ctx, options)
// Should contain AIConfigurationTag
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, FeaturesKey+"="+AIConfigurationTag)
})
t.Run("with AI chat completion configuration", func(t *testing.T) {
ctx := context.Background()
options := Options{
UseAIChatCompletionConfiguration: true,
}
header := CreateCorrelationContextHeader(ctx, options)
// Should contain AIChatCompletionConfigurationTag
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, FeaturesKey+"="+AIChatCompletionConfigurationTag)
})
t.Run("with both AI configurations", func(t *testing.T) {
ctx := context.Background()
options := Options{
UseAIConfiguration: true,
UseAIChatCompletionConfiguration: true,
}
header := CreateCorrelationContextHeader(ctx, options)
// Should contain both AI configuration tags
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, FeaturesKey+"=")
// Extract the Features part
parts := strings.Split(corrContext, DelimiterComma)
var featuresPart string
for _, part := range parts {
if strings.HasPrefix(part, FeaturesKey+"=") {
featuresPart = part
break
}
}
// Check both tags are in the features part
assert.Contains(t, featuresPart, AIConfigurationTag)
assert.Contains(t, featuresPart, AIChatCompletionConfigurationTag)
// Check the delimiter is correct
features := strings.Split(strings.TrimPrefix(featuresPart, FeaturesKey+"="), DelimiterPlus)
assert.Len(t, features, 2)
assert.Contains(t, features, AIConfigurationTag)
assert.Contains(t, features, AIChatCompletionConfigurationTag)
})
t.Run("with snapshot reference", func(t *testing.T) {
ctx := context.Background()
options := Options{
UseSnapshotReference: true,
}
header := CreateCorrelationContextHeader(ctx, options)
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, FeaturesKey+"="+SnapshotReferenceTag)
})
t.Run("with snapshot reference not set", func(t *testing.T) {
ctx := context.Background()
options := Options{
UseSnapshotReference: false,
}
header := CreateCorrelationContextHeader(ctx, options)
corrContext := header.Get(CorrelationContextHeader)
assert.NotContains(t, corrContext, SnapshotReferenceTag)
})
t.Run("with snapshot reference and other features", func(t *testing.T) {
ctx := context.Background()
options := Options{
UseAIConfiguration: true,
UseSnapshotReference: true,
}
header := CreateCorrelationContextHeader(ctx, options)
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, FeaturesKey+"=")
// Extract the Features part
parts := strings.Split(corrContext, DelimiterComma)
var featuresPart string
for _, part := range parts {
if strings.HasPrefix(part, FeaturesKey+"=") {
featuresPart = part
break
}
}
// Check both tags are in the features part
assert.Contains(t, featuresPart, AIConfigurationTag)
assert.Contains(t, featuresPart, SnapshotReferenceTag)
// Check the delimiter is correct
features := strings.Split(strings.TrimPrefix(featuresPart, FeaturesKey+"="), DelimiterPlus)
assert.Len(t, features, 2)
assert.Contains(t, features, AIConfigurationTag)
assert.Contains(t, features, SnapshotReferenceTag)
})
t.Run("with all options", func(t *testing.T) {
options := Options{
Host: HostTypeAzureFunction,
KeyVaultConfigured: true,
UseAIConfiguration: true,
UseAIChatCompletionConfiguration: true,
UseSnapshotReference: true,
}
header := CreateCorrelationContextHeader(context.Background(), options)
// Check the complete header
corrContext := header.Get(CorrelationContextHeader)
assert.Contains(t, corrContext, RequestTypeKey+"="+string(RequestTypeStartUp))
assert.Contains(t, corrContext, HostTypeKey+"="+string(HostTypeAzureFunction))
assert.Contains(t, corrContext, KeyVaultConfiguredTag)
// Extract the Features part
parts := strings.Split(corrContext, DelimiterComma)
var featuresPart string
for _, part := range parts {
if strings.HasPrefix(part, FeaturesKey+"=") {
featuresPart = part
break
}
}
// Check all feature tags are in the features part
assert.Contains(t, featuresPart, AIConfigurationTag)
assert.Contains(t, featuresPart, AIChatCompletionConfigurationTag)
assert.Contains(t, featuresPart, SnapshotReferenceTag)
// Verify the header format
assert.Equal(t, 4, strings.Count(corrContext, DelimiterComma)+1, "Should have 4 parts")
})
t.Run("delimiter handling", func(t *testing.T) {
options := Options{
Host: HostTypeAzureWebApp,
KeyVaultConfigured: true,
}
header := CreateCorrelationContextHeader(context.Background(), options)
// Check the complete header
corrContext := header.Get(CorrelationContextHeader)
// Verify there are exactly 3 parts separated by commas
parts := strings.Split(corrContext, DelimiterComma)
assert.Len(t, parts, 3, "Should have 3 parts separated by commas")
})
}
func TestFeatureFlagTracing_UpdateFeatureFilterTracing(t *testing.T) {
tests := []struct {
name string
filterName string
expectCustom bool
expectTime bool
expectTarget bool
}{
{
name: "Microsoft.TimeWindow filter",
filterName: TimeWindowFilterName,
expectCustom: false,
expectTime: true,
expectTarget: false,
},
{
name: "Microsoft.Targeting filter",
filterName: TargetingFilterName,
expectCustom: false,
expectTime: false,
expectTarget: true,
},
{
name: "Custom filter",
filterName: "Microsoft.CustomFilter",
expectCustom: true,
expectTime: false,
expectTarget: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tracing := FeatureFlagTracing{}
tracing.UpdateFeatureFilterTracing(test.filterName)
assert.Equal(t, test.expectCustom, tracing.UsesCustomFilter)
assert.Equal(t, test.expectTime, tracing.UsesTimeWindowFilter)
assert.Equal(t, test.expectTarget, tracing.UsesTargetingFilter)
})
}
}
func TestFeatureFlagTracing_UpdateMaxVariants(t *testing.T) {
tests := []struct {
name string
initialMax int
newValue int
expectedMax int
}{
{
name: "Update with larger value",
initialMax: 2,
newValue: 5,
expectedMax: 5,
},
{
name: "Update with smaller value",
initialMax: 5,
newValue: 3,
expectedMax: 5,
},
{
name: "Update with equal value",
initialMax: 3,
newValue: 3,
expectedMax: 3,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
tracing := FeatureFlagTracing{MaxVariants: test.initialMax}
tracing.UpdateMaxVariants(test.newValue)
assert.Equal(t, test.expectedMax, tracing.MaxVariants)
})
}
}
func TestFeatureFlagTracing_CreateFeatureFiltersString(t *testing.T) {
tests := []struct {
name string
tracing FeatureFlagTracing
expectedResult string
}{
{
name: "No filters",
tracing: FeatureFlagTracing{
UsesCustomFilter: false,
UsesTimeWindowFilter: false,
UsesTargetingFilter: false,
},
expectedResult: "",
},
{
name: "Only custom filter",
tracing: FeatureFlagTracing{
UsesCustomFilter: true,
UsesTimeWindowFilter: false,
UsesTargetingFilter: false,
},
expectedResult: CustomFilterKey,
},
{
name: "Only time window filter",
tracing: FeatureFlagTracing{
UsesCustomFilter: false,
UsesTimeWindowFilter: true,
UsesTargetingFilter: false,
},
expectedResult: TimeWindowFilterKey,
},
{
name: "Only targeting filter",
tracing: FeatureFlagTracing{
UsesCustomFilter: false,
UsesTimeWindowFilter: false,
UsesTargetingFilter: true,
},
expectedResult: TargetingFilterKey,
},
{
name: "Multiple filters",
tracing: FeatureFlagTracing{
UsesCustomFilter: true,
UsesTimeWindowFilter: true,
UsesTargetingFilter: true,
},
expectedResult: CustomFilterKey + DelimiterPlus + TimeWindowFilterKey + DelimiterPlus + TargetingFilterKey,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.tracing.CreateFeatureFiltersString()
assert.Equal(t, test.expectedResult, result)
})
}
}
func TestFeatureFlagTracing_CreateFeaturesString(t *testing.T) {
tests := []struct {
name string
tracing FeatureFlagTracing
expectedResult string
}{
{
name: "No features",
tracing: FeatureFlagTracing{
UsesSeed: false,
UsesTelemetry: false,
},
expectedResult: "",
},
{
name: "Only seed",
tracing: FeatureFlagTracing{
UsesSeed: true,
UsesTelemetry: false,
},
expectedResult: FFSeedUsedTag,
},
{
name: "Only telemetry",
tracing: FeatureFlagTracing{
UsesSeed: false,
UsesTelemetry: true,
},
expectedResult: FFTelemetryUsedTag,
},
{
name: "Both features",
tracing: FeatureFlagTracing{
UsesSeed: true,
UsesTelemetry: true,
},
expectedResult: FFSeedUsedTag + DelimiterPlus + FFTelemetryUsedTag,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := test.tracing.CreateFeaturesString()
assert.Equal(t, test.expectedResult, result)
})
}
}
func TestCreateCorrelationContextHeader_WithFeatureFlagTracing(t *testing.T) {
tests := []struct {
name string
tracing *FeatureFlagTracing
expected []string
notExpected []string
}{
{
name: "All feature flags features",
tracing: &FeatureFlagTracing{
UsesCustomFilter: true,
UsesTimeWindowFilter: true,
UsesTargetingFilter: true,
UsesTelemetry: true,
UsesSeed: true,
MaxVariants: 3,
},
expected: []string{
FeatureFilterTypeKey + "=" + CustomFilterKey + DelimiterPlus + TimeWindowFilterKey + DelimiterPlus + TargetingFilterKey,
FFFeaturesKey + "=" + FFSeedUsedTag + DelimiterPlus + FFTelemetryUsedTag,
FFMaxVariantsKey + "=3",
},
notExpected: []string{},
},
{
name: "No feature flags features",
tracing: &FeatureFlagTracing{
UsesCustomFilter: false,
UsesTimeWindowFilter: false,
UsesTargetingFilter: false,
UsesTelemetry: false,
UsesSeed: false,
MaxVariants: 0,
},
expected: []string{},
notExpected: []string{
FeatureFilterTypeKey,
FFFeaturesKey,
FFMaxVariantsKey,
},
},
{
name: "Only feature filters",
tracing: &FeatureFlagTracing{
UsesCustomFilter: true,
UsesTimeWindowFilter: false,
UsesTargetingFilter: true,
UsesTelemetry: false,
UsesSeed: false,
MaxVariants: 0,
},
expected: []string{
FeatureFilterTypeKey + "=" + CustomFilterKey + DelimiterPlus + TargetingFilterKey,
},
notExpected: []string{
FFFeaturesKey,
TimeWindowFilterKey,
},
},
{
name: "Only variant count",
tracing: &FeatureFlagTracing{
UsesCustomFilter: false,
UsesTimeWindowFilter: false,
UsesTargetingFilter: false,
UsesTelemetry: false,
UsesSeed: false,
MaxVariants: 5,
},
expected: []string{
FFMaxVariantsKey + "=5",
},
notExpected: []string{
FeatureFilterTypeKey,
FFFeaturesKey,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
options := Options{
FeatureFlagTracing: test.tracing,
}
header := CreateCorrelationContextHeader(ctx, options)
correlationCtx := header.Get(CorrelationContextHeader)
// Check expected strings
for _, exp := range test.expected {
assert.Contains(t, correlationCtx, exp)
}
// Check not expected strings
for _, notExp := range test.notExpected {
assert.NotContains(t, correlationCtx, notExp)
}
})
}
}