From 8d211cc9ccd817a803a31544ea76fb17f376c841 Mon Sep 17 00:00:00 2001 From: freddygv Date: Wed, 19 Oct 2022 16:26:15 -0600 Subject: [PATCH] Add unit test --- agent/consul/peering_backend.go | 2 +- agent/consul/peering_backend_test.go | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/agent/consul/peering_backend.go b/agent/consul/peering_backend.go index 12ed97cbba..3032641c6f 100644 --- a/agent/consul/peering_backend.go +++ b/agent/consul/peering_backend.go @@ -219,7 +219,7 @@ func parseNodeAddr(node *structs.ServiceNode) string { } func serverAddresses(state *state.Store) ([]string, error) { - _, nodes, err := state.ServiceNodes(nil, "consul", structs.DefaultEnterpriseMetaInDefaultPartition(), structs.DefaultPeerKeyword) + _, nodes, err := state.ServiceNodes(nil, structs.ConsulServiceName, structs.DefaultEnterpriseMetaInDefaultPartition(), structs.DefaultPeerKeyword) if err != nil { return nil, err } diff --git a/agent/consul/peering_backend_test.go b/agent/consul/peering_backend_test.go index a962bdefc0..e8218bc431 100644 --- a/agent/consul/peering_backend_test.go +++ b/agent/consul/peering_backend_test.go @@ -9,6 +9,7 @@ import ( gogrpc "google.golang.org/grpc" + "github.com/hashicorp/consul/acl" "github.com/hashicorp/consul/agent/connect" "github.com/hashicorp/consul/agent/consul/state" "github.com/hashicorp/consul/agent/pool" @@ -99,6 +100,28 @@ func TestPeeringBackend_GetLocalServerAddresses(t *testing.T) { require.Equal(t, []string{expect}, addrs) }) + testutil.RunStep(t, "prefer WAN address for servers", func(t *testing.T) { + req := structs.RegisterRequest{ + Datacenter: cfg.Datacenter, + Node: cfg.NodeName, + ID: cfg.NodeID, + Address: "127.0.0.1", + EnterpriseMeta: *acl.DefaultEnterpriseMeta(), + + // Add a tagged WAN address to the server registration + TaggedAddresses: map[string]string{ + structs.TaggedAddressWAN: "3.4.5.6", + }, + } + require.NoError(t, srv.fsm.State().EnsureRegistration(200, &req)) + + addrs, err := backend.GetLocalServerAddresses() + require.NoError(t, err) + + expect := fmt.Sprintf("3.4.5.6:%d", srv.config.GRPCTLSPort) + require.Equal(t, []string{expect}, addrs) + }) + testutil.RunStep(t, "existence of mesh config entry is not enough to peer through gateways", func(t *testing.T) { mesh := structs.MeshConfigEntry{ // Enable unrelated config. @@ -112,7 +135,7 @@ func TestPeeringBackend_GetLocalServerAddresses(t *testing.T) { require.NoError(t, err) // Still expect server address because PeerThroughMeshGateways was not enabled. - expect := fmt.Sprintf("127.0.0.1:%d", srv.config.GRPCTLSPort) + expect := fmt.Sprintf("3.4.5.6:%d", srv.config.GRPCTLSPort) require.Equal(t, []string{expect}, addrs) })