@@ -2479,17 +2479,26 @@ func TestAgent_RegisterService(t *testing.T) {
2479
2479
2480
2480
func TestAgent_RegisterService_TranslateKeys (t * testing.T ) {
2481
2481
t .Parallel ()
2482
- a := NewTestAgent (t , t .Name (), `
2482
+ tests := []struct {
2483
+ ip string
2484
+ expectedTCPCheckStart string
2485
+ }{
2486
+ {"127.0.0.1" , "127.0.0.1:" }, // private network address
2487
+ {"::1" , "[::1]:" }, // shared address space
2488
+ }
2489
+ for _ , tt := range tests {
2490
+ t .Run (tt .ip , func (t * testing.T ) {
2491
+ a := NewTestAgent (t , t .Name (), `
2483
2492
connect {
2484
2493
proxy {
2485
2494
allow_managed_api_registration = true
2486
2495
}
2487
2496
}
2488
2497
` )
2489
- defer a .Shutdown ()
2490
- testrpc .WaitForTestAgent (t , a .RPC , "dc1" )
2498
+ defer a .Shutdown ()
2499
+ testrpc .WaitForTestAgent (t , a .RPC , "dc1" )
2491
2500
2492
- json := `
2501
+ json := `
2493
2502
{
2494
2503
"name":"test",
2495
2504
"port":8000,
@@ -2499,14 +2508,14 @@ func TestAgent_RegisterService_TranslateKeys(t *testing.T) {
2499
2508
"enable_tag_override": "meta is 'opaque' so should not get translated"
2500
2509
},
2501
2510
"kind": "connect-proxy",` +
2502
- // Note the uppercase P is important here - it ensures translation works
2503
- // correctly in case-insensitive way. Without it this test can pass even
2504
- // when translation is broken for other valid inputs.
2505
- `"Proxy": {
2511
+ // Note the uppercase P is important here - it ensures translation works
2512
+ // correctly in case-insensitive way. Without it this test can pass even
2513
+ // when translation is broken for other valid inputs.
2514
+ `"Proxy": {
2506
2515
"destination_service_name": "web",
2507
2516
"destination_service_id": "web",
2508
2517
"local_service_port": 1234,
2509
- "local_service_address": "127.0.0.1 ",
2518
+ "local_service_address": "` + tt . ip + ` ",
2510
2519
"config": {
2511
2520
"destination_type": "proxy.config is 'opaque' so should not get translated"
2512
2521
},
@@ -2515,7 +2524,7 @@ func TestAgent_RegisterService_TranslateKeys(t *testing.T) {
2515
2524
"destination_type": "service",
2516
2525
"destination_namespace": "default",
2517
2526
"destination_name": "db",
2518
- "local_bind_address": "127.0.0.1 ",
2527
+ "local_bind_address": "` + tt . ip + ` ",
2519
2528
"local_bind_port": 1234,
2520
2529
"config": {
2521
2530
"destination_type": "proxy.upstreams.config is 'opaque' so should not get translated"
@@ -2534,7 +2543,7 @@ func TestAgent_RegisterService_TranslateKeys(t *testing.T) {
2534
2543
"destination_type": "service",
2535
2544
"destination_namespace": "default",
2536
2545
"destination_name": "db",
2537
- "local_bind_address": "127.0.0.1 ",
2546
+ "local_bind_address": "` + tt . ip + ` ",
2538
2547
"local_bind_port": 1234,
2539
2548
"config": {
2540
2549
"destination_type": "connect.proxy.upstreams.config is 'opaque' so should not get translated"
@@ -2555,13 +2564,13 @@ func TestAgent_RegisterService_TranslateKeys(t *testing.T) {
2555
2564
"destination_service_name": "test",
2556
2565
"destination_service_id": "test",
2557
2566
"local_service_port": 4321,
2558
- "local_service_address": "127.0.0.1 ",
2567
+ "local_service_address": "` + tt . ip + ` ",
2559
2568
"upstreams": [
2560
2569
{
2561
2570
"destination_type": "service",
2562
2571
"destination_namespace": "default",
2563
2572
"destination_name": "db",
2564
- "local_bind_address": "127.0.0.1 ",
2573
+ "local_bind_address": "` + tt . ip + ` ",
2565
2574
"local_bind_port": 1234,
2566
2575
"config": {
2567
2576
"destination_type": "sidecar_service.proxy.upstreams.config is 'opaque' so should not get translated"
@@ -2575,109 +2584,121 @@ func TestAgent_RegisterService_TranslateKeys(t *testing.T) {
2575
2584
"passing": 16
2576
2585
}
2577
2586
}`
2578
- req , _ := http .NewRequest ("PUT" , "/v1/agent/service/register" , strings .NewReader (json ))
2587
+ req , _ := http .NewRequest ("PUT" , "/v1/agent/service/register" , strings .NewReader (json ))
2579
2588
2580
- rr := httptest .NewRecorder ()
2581
- obj , err := a .srv .AgentRegisterService (rr , req )
2582
- require .NoError (t , err )
2583
- require .Nil (t , obj )
2584
- require .Equal (t , 200 , rr .Code , "body: %s" , rr .Body )
2589
+ rr := httptest .NewRecorder ()
2590
+ obj , err := a .srv .AgentRegisterService (rr , req )
2591
+ require .NoError (t , err )
2592
+ require .Nil (t , obj )
2593
+ require .Equal (t , 200 , rr .Code , "body: %s" , rr .Body )
2585
2594
2586
- svc := & structs.NodeService {
2587
- ID : "test" ,
2588
- Service : "test" ,
2589
- Meta : map [string ]string {
2590
- "some" : "meta" ,
2591
- "enable_tag_override" : "meta is 'opaque' so should not get translated" ,
2592
- },
2593
- Port : 8000 ,
2594
- EnableTagOverride : true ,
2595
- Weights : & structs.Weights {Passing : 16 , Warning : 0 },
2596
- Kind : structs .ServiceKindConnectProxy ,
2597
- Proxy : structs.ConnectProxyConfig {
2598
- DestinationServiceName : "web" ,
2599
- DestinationServiceID : "web" ,
2600
- LocalServiceAddress : "127.0.0.1" ,
2601
- LocalServicePort : 1234 ,
2602
- Config : map [string ]interface {}{
2603
- "destination_type" : "proxy.config is 'opaque' so should not get translated" ,
2604
- },
2605
- Upstreams : structs.Upstreams {
2606
- {
2607
- DestinationType : structs .UpstreamDestTypeService ,
2608
- DestinationName : "db" ,
2609
- DestinationNamespace : "default" ,
2610
- LocalBindAddress : "127.0.0.1" ,
2611
- LocalBindPort : 1234 ,
2595
+ svc := & structs.NodeService {
2596
+ ID : "test" ,
2597
+ Service : "test" ,
2598
+ Meta : map [string ]string {
2599
+ "some" : "meta" ,
2600
+ "enable_tag_override" : "meta is 'opaque' so should not get translated" ,
2601
+ },
2602
+ Port : 8000 ,
2603
+ EnableTagOverride : true ,
2604
+ Weights : & structs.Weights {Passing : 16 , Warning : 0 },
2605
+ Kind : structs .ServiceKindConnectProxy ,
2606
+ Proxy : structs.ConnectProxyConfig {
2607
+ DestinationServiceName : "web" ,
2608
+ DestinationServiceID : "web" ,
2609
+ LocalServiceAddress : tt .ip ,
2610
+ LocalServicePort : 1234 ,
2612
2611
Config : map [string ]interface {}{
2613
- "destination_type" : "proxy.upstreams.config is 'opaque' so should not get translated" ,
2612
+ "destination_type" : "proxy.config is 'opaque' so should not get translated" ,
2613
+ },
2614
+ Upstreams : structs.Upstreams {
2615
+ {
2616
+ DestinationType : structs .UpstreamDestTypeService ,
2617
+ DestinationName : "db" ,
2618
+ DestinationNamespace : "default" ,
2619
+ LocalBindAddress : tt .ip ,
2620
+ LocalBindPort : 1234 ,
2621
+ Config : map [string ]interface {}{
2622
+ "destination_type" : "proxy.upstreams.config is 'opaque' so should not get translated" ,
2623
+ },
2624
+ },
2614
2625
},
2615
2626
},
2616
- },
2617
- },
2618
- Connect : structs.ServiceConnect {
2619
- Proxy : & structs.ServiceDefinitionConnectProxy {
2620
- ExecMode : "script" ,
2621
- Config : map [string ]interface {}{
2622
- "destination_type" : "connect.proxy.config is 'opaque' so should not get translated" ,
2623
- },
2624
- Upstreams : structs.Upstreams {
2625
- {
2626
- DestinationType : structs .UpstreamDestTypeService ,
2627
- DestinationName : "db" ,
2628
- DestinationNamespace : "default" ,
2629
- LocalBindAddress : "127.0.0.1" ,
2630
- LocalBindPort : 1234 ,
2627
+ Connect : structs.ServiceConnect {
2628
+ Proxy : & structs.ServiceDefinitionConnectProxy {
2629
+ ExecMode : "script" ,
2631
2630
Config : map [string ]interface {}{
2632
- "destination_type" : "connect.proxy.upstreams.config is 'opaque' so should not get translated" ,
2631
+ "destination_type" : "connect.proxy.config is 'opaque' so should not get translated" ,
2632
+ },
2633
+ Upstreams : structs.Upstreams {
2634
+ {
2635
+ DestinationType : structs .UpstreamDestTypeService ,
2636
+ DestinationName : "db" ,
2637
+ DestinationNamespace : "default" ,
2638
+ LocalBindAddress : tt .ip ,
2639
+ LocalBindPort : 1234 ,
2640
+ Config : map [string ]interface {}{
2641
+ "destination_type" : "connect.proxy.upstreams.config is 'opaque' so should not get translated" ,
2642
+ },
2643
+ },
2633
2644
},
2634
2645
},
2646
+ // The sidecar service is nilled since it is only config sugar and
2647
+ // shouldn't be represented in state. We assert that the translations
2648
+ // there worked by inspecting the registered sidecar below.
2649
+ SidecarService : nil ,
2635
2650
},
2636
- },
2637
- // The sidecar service is nilled since it is only config sugar and
2638
- // shouldn't be represented in state. We assert that the translations
2639
- // there worked by inspecting the registered sidecar below.
2640
- SidecarService : nil ,
2641
- },
2642
- }
2651
+ }
2643
2652
2644
- got := a .State .Service ("test" )
2645
- require .Equal (t , svc , got )
2653
+ got := a .State .Service ("test" )
2654
+ require .Equal (t , svc , got )
2646
2655
2647
- sidecarSvc := & structs.NodeService {
2648
- Kind : structs .ServiceKindConnectProxy ,
2649
- ID : "test-sidecar-proxy" ,
2650
- Service : "test-proxy" ,
2651
- Meta : map [string ]string {
2652
- "some" : "meta" ,
2653
- "enable_tag_override" : "sidecar_service.meta is 'opaque' so should not get translated" ,
2654
- },
2655
- Port : 8001 ,
2656
- EnableTagOverride : true ,
2657
- Weights : & structs.Weights {Passing : 1 , Warning : 1 },
2658
- LocallyRegisteredAsSidecar : true ,
2659
- Proxy : structs.ConnectProxyConfig {
2660
- DestinationServiceName : "test" ,
2661
- DestinationServiceID : "test" ,
2662
- LocalServiceAddress : "127.0.0.1" ,
2663
- LocalServicePort : 4321 ,
2664
- Upstreams : structs.Upstreams {
2665
- {
2666
- DestinationType : structs .UpstreamDestTypeService ,
2667
- DestinationName : "db" ,
2668
- DestinationNamespace : "default" ,
2669
- LocalBindAddress : "127.0.0.1" ,
2670
- LocalBindPort : 1234 ,
2671
- Config : map [string ]interface {}{
2672
- "destination_type" : "sidecar_service.proxy.upstreams.config is 'opaque' so should not get translated" ,
2656
+ sidecarSvc := & structs.NodeService {
2657
+ Kind : structs .ServiceKindConnectProxy ,
2658
+ ID : "test-sidecar-proxy" ,
2659
+ Service : "test-proxy" ,
2660
+ Meta : map [string ]string {
2661
+ "some" : "meta" ,
2662
+ "enable_tag_override" : "sidecar_service.meta is 'opaque' so should not get translated" ,
2663
+ },
2664
+ Port : 8001 ,
2665
+ EnableTagOverride : true ,
2666
+ Weights : & structs.Weights {Passing : 1 , Warning : 1 },
2667
+ LocallyRegisteredAsSidecar : true ,
2668
+ Proxy : structs.ConnectProxyConfig {
2669
+ DestinationServiceName : "test" ,
2670
+ DestinationServiceID : "test" ,
2671
+ LocalServiceAddress : tt .ip ,
2672
+ LocalServicePort : 4321 ,
2673
+ Upstreams : structs.Upstreams {
2674
+ {
2675
+ DestinationType : structs .UpstreamDestTypeService ,
2676
+ DestinationName : "db" ,
2677
+ DestinationNamespace : "default" ,
2678
+ LocalBindAddress : tt .ip ,
2679
+ LocalBindPort : 1234 ,
2680
+ Config : map [string ]interface {}{
2681
+ "destination_type" : "sidecar_service.proxy.upstreams.config is 'opaque' so should not get translated" ,
2682
+ },
2683
+ },
2673
2684
},
2674
2685
},
2675
- },
2676
- },
2686
+ }
2687
+ gotSidecar := a .State .Service ("test-sidecar-proxy" )
2688
+ hasNoCorrectTCPCheck := true
2689
+ for _ , v := range a .checkTCPs {
2690
+ if strings .HasPrefix (v .TCP , tt .expectedTCPCheckStart ) {
2691
+ hasNoCorrectTCPCheck = false
2692
+ break
2693
+ }
2694
+ fmt .Println ("TCP Check:= " , v )
2695
+ }
2696
+ if hasNoCorrectTCPCheck {
2697
+ t .Fatalf ("Did not find the expected TCP Healtcheck '%s' in %#v " , tt .expectedTCPCheckStart , a .checkTCPs )
2698
+ }
2699
+ require .Equal (t , sidecarSvc , gotSidecar )
2700
+ })
2677
2701
}
2678
-
2679
- gotSidecar := a .State .Service ("test-sidecar-proxy" )
2680
- require .Equal (t , sidecarSvc , gotSidecar )
2681
2702
}
2682
2703
2683
2704
func TestAgent_RegisterService_ACLDeny (t * testing.T ) {
0 commit comments