diff --git a/agent/catalog_endpoint_test.go b/agent/catalog_endpoint_test.go index 845929117..d3af9bf6d 100644 --- a/agent/catalog_endpoint_test.go +++ b/agent/catalog_endpoint_test.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/testutil/retry" "github.com/hashicorp/serf/coordinate" + "github.com/stretchr/testify/assert" ) func TestCatalogRegister_Service_InvalidAddress(t *testing.T) { @@ -750,6 +751,30 @@ func TestCatalogServiceNodes_DistanceSort(t *testing.T) { } } +func TestCatalogServiceNodes_ConnectProxy(t *testing.T) { + t.Parallel() + + assert := assert.New(t) + a := NewTestAgent(t.Name(), "") + defer a.Shutdown() + + // Register + args := structs.TestRegisterRequestProxy(t) + var out struct{} + assert.Nil(a.RPC("Catalog.Register", args, &out)) + + req, _ := http.NewRequest("GET", fmt.Sprintf( + "/v1/catalog/service/%s", args.Service.Service), nil) + resp := httptest.NewRecorder() + obj, err := a.srv.CatalogServiceNodes(resp, req) + assert.Nil(err) + assertIndex(t, resp) + + nodes := obj.(structs.ServiceNodes) + assert.Len(nodes, 1) + assert.Equal(structs.ServiceKindConnectProxy, nodes[0].ServiceKind) +} + func TestCatalogNodeServices(t *testing.T) { t.Parallel() a := NewTestAgent(t.Name(), "") diff --git a/agent/consul/catalog_endpoint_test.go b/agent/consul/catalog_endpoint_test.go index f6825f990..db49875cb 100644 --- a/agent/consul/catalog_endpoint_test.go +++ b/agent/consul/catalog_endpoint_test.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/consul/testutil/retry" "github.com/hashicorp/consul/types" "github.com/hashicorp/net-rpc-msgpackrpc" + "github.com/stretchr/testify/assert" ) func TestCatalog_Register(t *testing.T) { @@ -1599,6 +1600,37 @@ func TestCatalog_ListServiceNodes_DistanceSort(t *testing.T) { } } +func TestCatalog_ListServiceNodes_ConnectProxy(t *testing.T) { + t.Parallel() + + assert := assert.New(t) + dir1, s1 := testServer(t) + defer os.RemoveAll(dir1) + defer s1.Shutdown() + codec := rpcClient(t, s1) + defer codec.Close() + + testrpc.WaitForLeader(t, s1.RPC, "dc1") + + // Register the service + args := structs.TestRegisterRequestProxy(t) + var out struct{} + assert.Nil(msgpackrpc.CallWithCodec(codec, "Catalog.Register", args, &out)) + + // List + req := structs.ServiceSpecificRequest{ + Datacenter: "dc1", + ServiceName: args.Service.Service, + TagFilter: false, + } + var resp structs.IndexedServiceNodes + assert.Nil(msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &req, &resp)) + assert.Len(resp.ServiceNodes, 1) + v := resp.ServiceNodes[0] + assert.Equal(structs.ServiceKindConnectProxy, v.ServiceKind) + assert.Equal(args.Service.ProxyDestination, v.ServiceProxyDestination) +} + func TestCatalog_NodeServices(t *testing.T) { t.Parallel() dir1, s1 := testServer(t) diff --git a/agent/structs/catalog.go b/agent/structs/catalog.go index b6b443f6f..3f68f43a1 100644 --- a/agent/structs/catalog.go +++ b/agent/structs/catalog.go @@ -13,9 +13,12 @@ const ( SerfCheckFailedOutput = "Agent not live or unreachable" ) -// These are used to manage the "consul" service that's attached to every Consul -// server node in the catalog. const ( + // These are used to manage the "consul" service that's attached to every + // Consul server node in the catalog. ConsulServiceID = "consul" ConsulServiceName = "consul" + + // ConnectProxyServiceName is the name of the proxy services. + ConnectProxyServiceName = "connect-proxy" ) diff --git a/agent/structs/structs.go b/agent/structs/structs.go index 23cd41acc..65ec87024 100644 --- a/agent/structs/structs.go +++ b/agent/structs/structs.go @@ -416,6 +416,7 @@ func (s *ServiceNode) PartialClone() *ServiceNode { Node: s.Node, // Skip Address, see above. // Skip TaggedAddresses, see above. + ServiceKind: s.ServiceKind, ServiceID: s.ServiceID, ServiceName: s.ServiceName, ServiceTags: tags, @@ -423,6 +424,7 @@ func (s *ServiceNode) PartialClone() *ServiceNode { ServicePort: s.ServicePort, ServiceMeta: nsmeta, ServiceEnableTagOverride: s.ServiceEnableTagOverride, + ServiceProxyDestination: s.ServiceProxyDestination, RaftIndex: RaftIndex{ CreateIndex: s.CreateIndex, ModifyIndex: s.ModifyIndex, diff --git a/agent/structs/testing_catalog.go b/agent/structs/testing_catalog.go new file mode 100644 index 000000000..8a002d380 --- /dev/null +++ b/agent/structs/testing_catalog.go @@ -0,0 +1,22 @@ +package structs + +import ( + "github.com/mitchellh/go-testing-interface" +) + +// TestRegisterRequestProxy returns a RegisterRequest for registering a +// Connect proxy. +func TestRegisterRequestProxy(t testing.T) *RegisterRequest { + return &RegisterRequest{ + Datacenter: "dc1", + Node: "foo", + Address: "127.0.0.1", + Service: &NodeService{ + Kind: ServiceKindConnectProxy, + Service: ConnectProxyServiceName, + Address: "127.0.0.2", + Port: 2222, + ProxyDestination: "web", + }, + } +}