15
15
package collector
16
16
17
17
import (
18
- "fmt"
19
18
"testing"
20
19
"time"
21
20
22
21
"github.com/stretchr/testify/assert"
23
- "github.com/stretchr/testify/require"
24
22
v1 "k8s.io/api/core/v1"
25
23
"k8s.io/apimachinery/pkg/api/resource"
26
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -89,10 +87,7 @@ func TestTargetAllocator(t *testing.T) {
89
87
},
90
88
want : & v1alpha1.TargetAllocator {
91
89
ObjectMeta : objectMetadata ,
92
- Spec : v1alpha1.TargetAllocatorSpec {
93
- ScrapeConfigs : []v1beta1.AnyConfig {},
94
- GlobalConfig : v1beta1.AnyConfig {},
95
- },
90
+ Spec : v1alpha1.TargetAllocatorSpec {},
96
91
},
97
92
},
98
93
{
@@ -291,7 +286,6 @@ func TestTargetAllocator(t *testing.T) {
291
286
MatchLabels : map [string ]string {"servicemonitorkey" : "servicemonitorkey" },
292
287
},
293
288
},
294
- ScrapeConfigs : []v1beta1.AnyConfig {},
295
289
Observability : v1beta1.ObservabilitySpec {
296
290
Metrics : v1beta1.MetricsConfigSpec {
297
291
EnableMetrics : true ,
@@ -314,172 +308,3 @@ func TestTargetAllocator(t *testing.T) {
314
308
})
315
309
}
316
310
}
317
-
318
- func TestGetScrapeConfigs (t * testing.T ) {
319
- testCases := []struct {
320
- name string
321
- input v1beta1.Config
322
- want []v1beta1.AnyConfig
323
- wantErr error
324
- }{
325
- {
326
- name : "empty scrape configs list" ,
327
- input : v1beta1.Config {
328
- Receivers : v1beta1.AnyConfig {
329
- Object : map [string ]interface {}{
330
- "prometheus" : map [string ]any {
331
- "config" : map [string ]any {
332
- "scrape_configs" : []any {},
333
- },
334
- },
335
- },
336
- },
337
- },
338
- want : []v1beta1.AnyConfig {},
339
- },
340
- {
341
- name : "no scrape configs key" ,
342
- input : v1beta1.Config {
343
- Receivers : v1beta1.AnyConfig {
344
- Object : map [string ]interface {}{
345
- "prometheus" : map [string ]any {
346
- "config" : map [string ]any {},
347
- },
348
- },
349
- },
350
- },
351
- wantErr : fmt .Errorf ("no scrape_configs available as part of the configuration" ),
352
- },
353
- {
354
- name : "one scrape config" ,
355
- input : v1beta1.Config {
356
- Receivers : v1beta1.AnyConfig {
357
- Object : map [string ]interface {}{
358
- "prometheus" : map [string ]any {
359
- "config" : map [string ]any {
360
- "scrape_configs" : []any {
361
- map [string ]any {
362
- "job" : "somejob" ,
363
- },
364
- },
365
- },
366
- },
367
- },
368
- },
369
- },
370
- want : []v1beta1.AnyConfig {
371
- {Object : map [string ]interface {}{"job" : "somejob" }},
372
- },
373
- },
374
- {
375
- name : "regex substitution" ,
376
- input : v1beta1.Config {
377
- Receivers : v1beta1.AnyConfig {
378
- Object : map [string ]interface {}{
379
- "prometheus" : map [string ]any {
380
- "config" : map [string ]any {
381
- "scrape_configs" : []any {
382
- map [string ]any {
383
- "job" : "somejob" ,
384
- "metric_relabel_configs" : []map [string ]any {
385
- {
386
- "action" : "labelmap" ,
387
- "regex" : "label_(.+)" ,
388
- "replacement" : "$$1" ,
389
- },
390
- },
391
- },
392
- },
393
- },
394
- },
395
- },
396
- },
397
- },
398
- want : []v1beta1.AnyConfig {
399
- {Object : map [string ]interface {}{
400
- "job" : "somejob" ,
401
- "metric_relabel_configs" : []any {
402
- map [any ]any {
403
- "action" : "labelmap" ,
404
- "regex" : "label_(.+)" ,
405
- "replacement" : "$1" ,
406
- },
407
- },
408
- }},
409
- },
410
- },
411
- }
412
-
413
- for _ , testCase := range testCases {
414
- testCase := testCase
415
- t .Run (testCase .name , func (t * testing.T ) {
416
- configStr , err := testCase .input .Yaml ()
417
- require .NoError (t , err )
418
- actual , err := getScrapeConfigs (configStr )
419
- assert .Equal (t , testCase .wantErr , err )
420
- assert .Equal (t , testCase .want , actual )
421
- })
422
- }
423
- }
424
-
425
- func Test_getGlobalConfig (t * testing.T ) {
426
- type args struct {
427
- otelConfig v1beta1.Config
428
- }
429
- tests := []struct {
430
- name string
431
- args args
432
- want v1beta1.AnyConfig
433
- wantErr error
434
- }{
435
- {
436
- name : "Valid Global Config" ,
437
- args : args {
438
- otelConfig : v1beta1.Config {
439
- Receivers : v1beta1.AnyConfig {
440
- Object : map [string ]interface {}{
441
- "prometheus" : map [string ]interface {}{
442
- "config" : map [string ]interface {}{
443
- "global" : map [string ]interface {}{
444
- "scrape_interval" : "15s" ,
445
- "scrape_protocols" : []string {"PrometheusProto" , "OpenMetricsText1.0.0" , "OpenMetricsText0.0.1" , "PrometheusText0.0.4" },
446
- },
447
- },
448
- },
449
- },
450
- },
451
- },
452
- },
453
- want : v1beta1.AnyConfig {
454
- Object : map [string ]interface {}{
455
- "scrape_interval" : "15s" ,
456
- "scrape_protocols" : []string {"PrometheusProto" , "OpenMetricsText1.0.0" , "OpenMetricsText0.0.1" , "PrometheusText0.0.4" },
457
- },
458
- },
459
- wantErr : nil ,
460
- },
461
- {
462
- name : "Invalid Global Config - Missing Global" ,
463
- args : args {
464
- otelConfig : v1beta1.Config {
465
- Receivers : v1beta1.AnyConfig {
466
- Object : map [string ]interface {}{
467
- "prometheus" : map [string ]interface {}{
468
- "config" : map [string ]interface {}{},
469
- },
470
- },
471
- },
472
- },
473
- },
474
- want : v1beta1.AnyConfig {},
475
- wantErr : nil ,
476
- },
477
- }
478
- for _ , tt := range tests {
479
- t .Run (tt .name , func (t * testing.T ) {
480
- got , err := getGlobalConfig (tt .args .otelConfig )
481
- assert .Equal (t , tt .wantErr , err )
482
- assert .Equal (t , tt .want , got )
483
- })
484
- }
485
- }
0 commit comments