@ -7,6 +7,7 @@ import (
"testing"
"time"
"github.com/hashicorp/consul/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
@ -49,10 +50,7 @@ func TestSubscriptionManager_RegisterDeregister(t *testing.T) {
subCh := mgr . subscribe ( ctx , id , "my-peering" , partition )
var (
gatewayCorrID = subMeshGateway + partition
mysqlCorrID = subExportedService + structs . NewServiceName ( "mysql" , nil ) . String ( )
mysqlCorrID = subExportedService + structs . NewServiceName ( "mysql" , nil ) . String ( )
mysqlProxyCorrID = subExportedService + structs . NewServiceName ( "mysql-sidecar-proxy" , nil ) . String ( )
)
@ -60,11 +58,7 @@ func TestSubscriptionManager_RegisterDeregister(t *testing.T) {
expectEvents ( t , subCh ,
func ( t * testing . T , got cache . UpdateEvent ) {
checkExportedServices ( t , got , [ ] string { } )
} ,
func ( t * testing . T , got cache . UpdateEvent ) {
checkEvent ( t , got , gatewayCorrID , 0 )
} ,
)
} )
// Initially add in L4 failover so that later we can test removing it. We
// cannot do the other way around because it would fail validation to
@ -300,17 +294,6 @@ func TestSubscriptionManager_RegisterDeregister(t *testing.T) {
} ,
} , res . Nodes [ 0 ] )
} ,
func ( t * testing . T , got cache . UpdateEvent ) {
require . Equal ( t , gatewayCorrID , got . CorrelationID )
res := got . Result . ( * pbservice . IndexedCheckServiceNodes )
require . Equal ( t , uint64 ( 0 ) , res . Index )
require . Len ( t , res . Nodes , 1 )
prototest . AssertDeepEqual ( t , & pbservice . CheckServiceNode {
Node : pbNode ( "mgw" , "10.1.1.1" , partition ) ,
Service : pbService ( "mesh-gateway" , "gateway-1" , "gateway" , 8443 , nil ) ,
} , res . Nodes [ 0 ] )
} ,
)
} )
@ -434,13 +417,6 @@ func TestSubscriptionManager_RegisterDeregister(t *testing.T) {
res := got . Result . ( * pbservice . IndexedCheckServiceNodes )
require . Equal ( t , uint64 ( 0 ) , res . Index )
require . Len ( t , res . Nodes , 0 )
} ,
func ( t * testing . T , got cache . UpdateEvent ) {
require . Equal ( t , gatewayCorrID , got . CorrelationID )
res := got . Result . ( * pbservice . IndexedCheckServiceNodes )
require . Equal ( t , uint64 ( 0 ) , res . Index )
require . Len ( t , res . Nodes , 0 )
} ,
)
@ -506,8 +482,6 @@ func TestSubscriptionManager_InitialSnapshot(t *testing.T) {
backend . ensureService ( t , "zip" , mongo . Service )
var (
gatewayCorrID = subMeshGateway + partition
mysqlCorrID = subExportedService + structs . NewServiceName ( "mysql" , nil ) . String ( )
mongoCorrID = subExportedService + structs . NewServiceName ( "mongo" , nil ) . String ( )
chainCorrID = subExportedService + structs . NewServiceName ( "chain" , nil ) . String ( )
@ -521,9 +495,6 @@ func TestSubscriptionManager_InitialSnapshot(t *testing.T) {
expectEvents ( t , subCh ,
func ( t * testing . T , got cache . UpdateEvent ) {
checkExportedServices ( t , got , [ ] string { } )
} ,
func ( t * testing . T , got cache . UpdateEvent ) {
checkEvent ( t , got , gatewayCorrID , 0 )
} )
// At this point in time we'll have a mesh-gateway notification with no
@ -597,9 +568,6 @@ func TestSubscriptionManager_InitialSnapshot(t *testing.T) {
func ( t * testing . T , got cache . UpdateEvent ) {
checkEvent ( t , got , mysqlProxyCorrID , 1 , "mysql-sidecar-proxy" , string ( structs . ServiceKindConnectProxy ) )
} ,
func ( t * testing . T , got cache . UpdateEvent ) {
checkEvent ( t , got , gatewayCorrID , 1 , "gateway" , string ( structs . ServiceKindMeshGateway ) )
} ,
)
} )
}
@ -741,6 +709,102 @@ func TestSubscriptionManager_ServerAddrs(t *testing.T) {
} ,
)
} )
testutil . RunStep ( t , "flipped to peering through mesh gateways" , func ( t * testing . T ) {
require . NoError ( t , backend . store . EnsureConfigEntry ( 1 , & structs . MeshConfigEntry {
Peering : & structs . PeeringMeshConfig {
PeerThroughMeshGateways : true ,
} ,
} ) )
select {
case <- time . After ( 100 * time . Millisecond ) :
case <- subCh :
t . Fatal ( "expected to time out: no mesh gateways are registered" )
}
} )
testutil . RunStep ( t , "registered and received a mesh gateway" , func ( t * testing . T ) {
reg := structs . RegisterRequest {
ID : types . NodeID ( "b5489ca9-f5e9-4dba-a779-61fec4e8e364" ) ,
Node : "gw-node" ,
Address : "1.2.3.4" ,
TaggedAddresses : map [ string ] string {
structs . TaggedAddressWAN : "172.217.22.14" ,
} ,
Service : & structs . NodeService {
ID : "mesh-gateway" ,
Service : "mesh-gateway" ,
Kind : structs . ServiceKindMeshGateway ,
Port : 443 ,
TaggedAddresses : map [ string ] structs . ServiceAddress {
structs . TaggedAddressWAN : { Address : "154.238.12.252" , Port : 8443 } ,
} ,
} ,
}
require . NoError ( t , backend . store . EnsureRegistration ( 2 , & reg ) )
expectEvents ( t , subCh ,
func ( t * testing . T , got cache . UpdateEvent ) {
require . Equal ( t , subServerAddrs , got . CorrelationID )
addrs , ok := got . Result . ( * pbpeering . PeeringServerAddresses )
require . True ( t , ok )
require . Equal ( t , [ ] string { "154.238.12.252:8443" } , addrs . GetAddresses ( ) )
} ,
)
} )
testutil . RunStep ( t , "registered and received a second mesh gateway" , func ( t * testing . T ) {
reg := structs . RegisterRequest {
ID : types . NodeID ( "e4cc0af3-5c09-4ddf-94a9-5840e427bc45" ) ,
Node : "gw-node-2" ,
Address : "1.2.3.5" ,
TaggedAddresses : map [ string ] string {
structs . TaggedAddressWAN : "172.217.22.15" ,
} ,
Service : & structs . NodeService {
ID : "mesh-gateway" ,
Service : "mesh-gateway" ,
Kind : structs . ServiceKindMeshGateway ,
Port : 443 ,
} ,
}
require . NoError ( t , backend . store . EnsureRegistration ( 3 , & reg ) )
expectEvents ( t , subCh ,
func ( t * testing . T , got cache . UpdateEvent ) {
require . Equal ( t , subServerAddrs , got . CorrelationID )
addrs , ok := got . Result . ( * pbpeering . PeeringServerAddresses )
require . True ( t , ok )
require . Equal ( t , [ ] string { "154.238.12.252:8443" , "172.217.22.15:443" } , addrs . GetAddresses ( ) )
} ,
)
} )
testutil . RunStep ( t , "disabled peering through gateways and received server addresses" , func ( t * testing . T ) {
require . NoError ( t , backend . store . EnsureConfigEntry ( 4 , & structs . MeshConfigEntry {
Peering : & structs . PeeringMeshConfig {
PeerThroughMeshGateways : false ,
} ,
} ) )
expectEvents ( t , subCh ,
func ( t * testing . T , got cache . UpdateEvent ) {
require . Equal ( t , subServerAddrs , got . CorrelationID )
addrs , ok := got . Result . ( * pbpeering . PeeringServerAddresses )
require . True ( t , ok )
// New subscriptions receive a snapshot from the event publisher.
// At the start of the test the handler registered a mock that only returns a single address.
require . Equal ( t , [ ] string { "198.18.0.1:8502" } , addrs . GetAddresses ( ) )
} ,
)
} )
}
type testSubscriptionBackend struct {