@@ -12,6 +12,7 @@ import (
12
12
"github.com/stretchr/testify/assert"
13
13
"github.com/stretchr/testify/require"
14
14
"go.opentelemetry.io/collector/component/componenttest"
15
+ conventions "go.opentelemetry.io/otel/semconv/v1.6.1"
15
16
"go.uber.org/zap"
16
17
"go.uber.org/zap/zapcore"
17
18
"go.uber.org/zap/zaptest/observer"
@@ -2376,3 +2377,155 @@ func Test_parseServiceVersionFromImage(t *testing.T) {
2376
2377
})
2377
2378
}
2378
2379
}
2380
+
2381
+ func TestGetIdentifiersFromAssoc (t * testing.T ) {
2382
+ tests := map [string ]struct {
2383
+ associations []Association
2384
+ pod * Pod
2385
+ expected []PodIdentifier
2386
+ }{
2387
+ "K8SPodUID" : {
2388
+ associations : []Association {
2389
+ {
2390
+ Sources : []AssociationSource {
2391
+ {
2392
+ From : ResourceSource ,
2393
+ Name : string (conventions .K8SPodUIDKey ),
2394
+ },
2395
+ },
2396
+ },
2397
+ },
2398
+ pod : & Pod {
2399
+ PodUID : "myK8sPodUID" ,
2400
+ },
2401
+ expected : []PodIdentifier {
2402
+ {
2403
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "k8s.pod.uid" }, Value : "myK8sPodUID" },
2404
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2405
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2406
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2407
+ },
2408
+ {
2409
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "k8s.pod.uid" }, Value : "myK8sPodUID" },
2410
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2411
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2412
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2413
+ },
2414
+ },
2415
+ },
2416
+ "ContainerID" : {
2417
+ associations : []Association {
2418
+ {
2419
+ Sources : []AssociationSource {
2420
+ {
2421
+ From : ResourceSource ,
2422
+ Name : string (conventions .ContainerIDKey ),
2423
+ },
2424
+ },
2425
+ },
2426
+ },
2427
+ pod : & Pod {
2428
+ PodUID : "myK8sPodUID" ,
2429
+ Containers : PodContainers {
2430
+ ByID : map [string ]* Container {
2431
+ "id1" : {
2432
+ Name : "id1" ,
2433
+ },
2434
+ "id2" : {
2435
+ Name : "id2" ,
2436
+ },
2437
+ },
2438
+ },
2439
+ },
2440
+ expected : []PodIdentifier {
2441
+ {
2442
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "container.id" }, Value : "id1" },
2443
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2444
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2445
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2446
+ },
2447
+ {
2448
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "container.id" }, Value : "id2" },
2449
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2450
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2451
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2452
+ },
2453
+ {
2454
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "k8s.pod.uid" }, Value : "myK8sPodUID" },
2455
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2456
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2457
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2458
+ },
2459
+ },
2460
+ },
2461
+ "multiple associations" : {
2462
+ associations : []Association {
2463
+ {
2464
+ Sources : []AssociationSource {
2465
+ {
2466
+ From : ResourceSource ,
2467
+ Name : string (conventions .ContainerIDKey ),
2468
+ },
2469
+ {
2470
+ From : ConnectionSource ,
2471
+ },
2472
+ },
2473
+ },
2474
+ },
2475
+ pod : & Pod {
2476
+ PodUID : "myK8sPodUID" ,
2477
+ Address : "localhost" ,
2478
+ Containers : PodContainers {
2479
+ ByID : map [string ]* Container {
2480
+ "id1" : {
2481
+ Name : "id1" ,
2482
+ },
2483
+ "id2" : {
2484
+ Name : "id2" ,
2485
+ },
2486
+ },
2487
+ },
2488
+ },
2489
+ expected : []PodIdentifier {
2490
+ {
2491
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "container.id" }, Value : "id1" },
2492
+ PodIdentifierAttribute {Source : AssociationSource {From : "connection" , Name : "" }, Value : "localhost" },
2493
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2494
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2495
+ },
2496
+ {
2497
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "container.id" }, Value : "id2" },
2498
+ PodIdentifierAttribute {Source : AssociationSource {From : "connection" , Name : "" }, Value : "localhost" },
2499
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2500
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2501
+ },
2502
+ {
2503
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "k8s.pod.uid" }, Value : "myK8sPodUID" },
2504
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2505
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2506
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2507
+ },
2508
+ {
2509
+ PodIdentifierAttribute {Source : AssociationSource {From : "connection" , Name : "" }, Value : "localhost" },
2510
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2511
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2512
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2513
+ },
2514
+ {
2515
+ PodIdentifierAttribute {Source : AssociationSource {From : "resource_attribute" , Name : "k8s.pod.ip" }, Value : "localhost" },
2516
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2517
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2518
+ PodIdentifierAttribute {Source : AssociationSource {From : "" , Name : "" }, Value : "" },
2519
+ },
2520
+ },
2521
+ },
2522
+ }
2523
+ for name , tc := range tests {
2524
+ t .Run (name , func (t * testing.T ) {
2525
+ wc , _ := newTestClient (t )
2526
+ wc .Associations = tc .associations
2527
+ actual := wc .getIdentifiersFromAssoc (tc .pod )
2528
+ assert .ElementsMatch (t , tc .expected , actual )
2529
+ })
2530
+ }
2531
+ }
0 commit comments