2013-12-23 22:26:34 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-02-05 22:47:42 +00:00
|
|
|
"fmt"
|
2013-12-24 00:20:51 +00:00
|
|
|
"net/http"
|
2014-04-21 20:11:05 +00:00
|
|
|
"net/http/httptest"
|
2019-04-16 16:00:15 +00:00
|
|
|
"net/url"
|
2013-12-23 22:26:34 +00:00
|
|
|
"testing"
|
2013-12-24 00:20:51 +00:00
|
|
|
"time"
|
2015-07-03 00:21:59 +00:00
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2019-04-16 16:00:15 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2015-07-03 00:21:59 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2018-03-07 01:32:41 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-06 10:34:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2013-12-23 22:26:34 +00:00
|
|
|
)
|
|
|
|
|
2017-05-08 16:34:45 +00:00
|
|
|
func TestCatalogRegister_Service_InvalidAddress(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2017-05-08 16:34:45 +00:00
|
|
|
|
2017-05-09 07:29:13 +00:00
|
|
|
for _, addr := range []string{"0.0.0.0", "::", "[::]"} {
|
|
|
|
t.Run("addr "+addr, func(t *testing.T) {
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "test",
|
|
|
|
Address: addr,
|
|
|
|
Port: 8080,
|
|
|
|
},
|
|
|
|
}
|
2017-09-26 06:11:19 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/catalog/register", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
_, err := a.srv.CatalogRegister(nil, req)
|
2017-05-09 07:29:13 +00:00
|
|
|
if err == nil || err.Error() != "Invalid service address" {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2017-05-08 16:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogDeregister(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-05-07 21:41:14 +00:00
|
|
|
|
|
|
|
// Register node
|
2017-05-09 16:58:12 +00:00
|
|
|
args := &structs.DeregisterRequest{Node: "foo"}
|
2017-09-26 06:11:19 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/v1/catalog/deregister", jsonReader(args))
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogDeregister(nil, req)
|
2013-12-24 19:55:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := obj.(bool)
|
|
|
|
if res != true {
|
|
|
|
t.Fatalf("bad: %v", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-23 22:26:34 +00:00
|
|
|
func TestCatalogDatacenters(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2013-12-23 22:26:34 +00:00
|
|
|
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-09-26 06:11:19 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/datacenters", nil)
|
|
|
|
obj, err := a.srv.CatalogDatacenters(nil, req)
|
2014-06-11 19:01:14 +00:00
|
|
|
if err != nil {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatal(err)
|
2014-06-11 19:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dcs := obj.([]string)
|
2017-04-29 16:34:02 +00:00
|
|
|
if got, want := len(dcs), 1; got != want {
|
|
|
|
r.Fatalf("got %d data centers want %d", got, want)
|
2014-06-11 19:01:14 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2013-12-23 22:26:34 +00:00
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
|
|
|
|
func TestCatalogNodes(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-05-07 21:41:14 +00:00
|
|
|
|
|
|
|
// Register node
|
2013-12-24 00:20:51 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
2014-05-07 21:02:59 +00:00
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2013-12-24 00:20:51 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc1", nil)
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogNodes(resp, req)
|
2013-12-24 00:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
// Verify an index is set
|
|
|
|
assertIndex(t, resp)
|
2014-02-05 22:36:13 +00:00
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
nodes := obj.(structs.Nodes)
|
2014-01-21 19:52:25 +00:00
|
|
|
if len(nodes) != 2 {
|
2018-08-06 23:46:09 +00:00
|
|
|
t.Fatalf("bad: %v ; nodes:=%v", obj, nodes)
|
2013-12-24 00:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-24 19:55:14 +00:00
|
|
|
|
2017-01-09 19:21:49 +00:00
|
|
|
func TestCatalogNodes_MetaFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-01-06 01:21:56 +00:00
|
|
|
|
|
|
|
// Register a node with a meta field
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
NodeMeta: map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2017-01-06 01:21:56 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/nodes?node-meta=somekey:somevalue", nil)
|
2017-01-06 01:21:56 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogNodes(resp, req)
|
2017-01-06 01:21:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify an index is set
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Verify we only get the node with the correct meta field back
|
|
|
|
nodes := obj.(structs.Nodes)
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if v, ok := nodes[0].Meta["somekey"]; !ok || v != "somevalue" {
|
|
|
|
t.Fatalf("bad: %v", nodes[0].Meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
func TestCatalogNodes_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a node with a meta field
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
NodeMeta: map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
require.NoError(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/nodes?filter="+url.QueryEscape("Meta.somekey == somevalue"), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogNodes(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify an index is set
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
// Verify we only get the node with the correct meta field back
|
|
|
|
nodes := obj.(structs.Nodes)
|
|
|
|
require.Len(t, nodes, 1)
|
|
|
|
|
|
|
|
v, ok := nodes[0].Meta["somekey"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, v, "somevalue")
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:02:51 +00:00
|
|
|
func TestCatalogNodes_WanTranslation(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a1 := NewTestAgent(t, t.Name(), `
|
2017-09-25 18:40:42 +00:00
|
|
|
datacenter = "dc1"
|
|
|
|
translate_wan_addrs = true
|
|
|
|
acl_datacenter = ""
|
|
|
|
`)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a1.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a1.RPC, "dc1")
|
2017-05-21 07:11:09 +00:00
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a2 := NewTestAgent(t, t.Name(), `
|
2017-09-25 18:40:42 +00:00
|
|
|
datacenter = "dc2"
|
|
|
|
translate_wan_addrs = true
|
|
|
|
acl_datacenter = ""
|
|
|
|
`)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a2.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a2.RPC, "dc2")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
// Wait for the WAN join.
|
2017-09-25 18:40:42 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a1.Config.SerfPortWAN)
|
2017-05-21 07:11:09 +00:00
|
|
|
if _, err := a2.JoinWAN([]string{addr}); err != nil {
|
2016-08-15 22:34:11 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-08-06 23:46:09 +00:00
|
|
|
testrpc.WaitForLeader(t, a1.RPC, "dc1")
|
|
|
|
testrpc.WaitForLeader(t, a2.RPC, "dc2")
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2017-05-21 07:11:09 +00:00
|
|
|
if got, want := len(a1.WANMembers()), 2; got < want {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("got %d WAN members want at least %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Register a node with DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Node: "wan_translation_test",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": "127.0.0.2",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "http_wan_translation_test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a2.RPC("Catalog.Register", args, &out); err != nil {
|
2016-06-15 18:02:51 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query nodes in DC2 from DC1.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc2", nil)
|
2016-06-15 18:02:51 +00:00
|
|
|
resp1 := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj1, err1 := a1.srv.CatalogNodes(resp1, req)
|
2016-06-15 18:02:51 +00:00
|
|
|
if err1 != nil {
|
|
|
|
t.Fatalf("err: %v", err1)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp1)
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Expect that DC1 gives us a WAN address (since the node is in DC2).
|
2016-06-15 18:02:51 +00:00
|
|
|
nodes1 := obj1.(structs.Nodes)
|
|
|
|
if len(nodes1) != 2 {
|
2018-08-06 23:46:09 +00:00
|
|
|
t.Fatalf("bad: %v, nodes:=%v", obj1, nodes1)
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|
2016-08-15 22:34:11 +00:00
|
|
|
var address string
|
2016-06-15 18:02:51 +00:00
|
|
|
for _, node := range nodes1 {
|
|
|
|
if node.Node == "wan_translation_test" {
|
2016-08-15 22:34:11 +00:00
|
|
|
address = node.Address
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-15 22:34:11 +00:00
|
|
|
if address != "127.0.0.2" {
|
|
|
|
t.Fatalf("bad: %s", address)
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query DC2 from DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
resp2 := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj2, err2 := a2.srv.CatalogNodes(resp2, req)
|
2016-06-15 18:02:51 +00:00
|
|
|
if err2 != nil {
|
|
|
|
t.Fatalf("err: %v", err2)
|
|
|
|
}
|
|
|
|
assertIndex(t, resp2)
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Expect that DC2 gives us a private address (since the node is in DC2).
|
2016-06-15 18:02:51 +00:00
|
|
|
nodes2 := obj2.(structs.Nodes)
|
|
|
|
if len(nodes2) != 2 {
|
|
|
|
t.Fatalf("bad: %v", obj2)
|
|
|
|
}
|
|
|
|
for _, node := range nodes2 {
|
|
|
|
if node.Node == "wan_translation_test" {
|
2016-08-15 22:34:11 +00:00
|
|
|
address = node.Address
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-15 22:34:11 +00:00
|
|
|
if address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %s", address)
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:47:42 +00:00
|
|
|
func TestCatalogNodes_Blocking(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-05-07 21:41:14 +00:00
|
|
|
|
|
|
|
// Register node
|
2014-02-05 22:47:42 +00:00
|
|
|
args := &structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2014-05-07 21:02:59 +00:00
|
|
|
|
2014-02-05 22:47:42 +00:00
|
|
|
var out structs.IndexedNodes
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.ListNodes", *args, &out); err != nil {
|
2014-02-05 22:47:42 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:17:06 +00:00
|
|
|
// t.Fatal must be called from the main go routine
|
|
|
|
// of the test. Because of this we cannot call
|
|
|
|
// t.Fatal from within the go routines and use
|
|
|
|
// an error channel instead.
|
|
|
|
errch := make(chan error, 2)
|
2014-02-05 22:47:42 +00:00
|
|
|
go func() {
|
2018-09-12 16:07:47 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2017-05-23 10:17:06 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
// register a service after the blocking call
|
|
|
|
// in order to unblock it.
|
|
|
|
time.AfterFunc(100*time.Millisecond, func() {
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
errch <- a.RPC("Catalog.Register", args, &out)
|
|
|
|
})
|
|
|
|
|
|
|
|
// now block
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/catalog/nodes?wait=3s&index=%d", out.Index+1), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogNodes(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
2014-02-05 22:47:42 +00:00
|
|
|
}
|
2017-05-23 10:17:06 +00:00
|
|
|
|
|
|
|
// Should block for a while
|
2017-10-17 18:38:24 +00:00
|
|
|
if d := time.Since(start); d < 50*time.Millisecond {
|
2017-05-23 10:17:06 +00:00
|
|
|
errch <- fmt.Errorf("too fast: %v", d)
|
2014-02-05 22:47:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 10:17:06 +00:00
|
|
|
if idx := getIndex(t, resp); idx <= out.Index {
|
|
|
|
errch <- fmt.Errorf("bad: %v", idx)
|
|
|
|
}
|
2014-02-05 22:47:42 +00:00
|
|
|
|
2017-05-23 10:17:06 +00:00
|
|
|
nodes := obj.(structs.Nodes)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
errch <- fmt.Errorf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
errch <- nil
|
|
|
|
}()
|
2014-02-05 22:47:42 +00:00
|
|
|
|
2017-05-23 10:17:06 +00:00
|
|
|
// wait for both go routines to return
|
|
|
|
if err := <-errch; err != nil {
|
|
|
|
t.Fatal(err)
|
2014-02-05 22:47:42 +00:00
|
|
|
}
|
2017-05-23 10:17:06 +00:00
|
|
|
if err := <-errch; err != nil {
|
|
|
|
t.Fatal(err)
|
2014-02-05 22:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 00:21:59 +00:00
|
|
|
func TestCatalogNodes_DistanceSort(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2015-07-03 00:21:59 +00:00
|
|
|
|
|
|
|
// Register nodes.
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-03 00:21:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-03 00:21:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nobody has coordinates set so this will still return them in the
|
|
|
|
// order they are indexed.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc1&near=foo", nil)
|
2015-07-03 00:21:59 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogNodes(resp, req)
|
2015-07-03 00:21:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes := obj.(structs.Nodes)
|
|
|
|
if len(nodes) != 3 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if nodes[2].Node != a.Config.NodeName {
|
2015-07-03 00:21:59 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an update for the node and wait for it to get applied.
|
|
|
|
arg := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: coordinate.NewCoordinate(coordinate.DefaultConfig()),
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Coordinate.Update", &arg, &out); err != nil {
|
2015-07-03 00:21:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-03-22 01:23:11 +00:00
|
|
|
time.Sleep(300 * time.Millisecond)
|
2015-07-03 00:21:59 +00:00
|
|
|
|
|
|
|
// Query again and now foo should have moved to the front of the line.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/catalog/nodes?dc=dc1&near=foo", nil)
|
2015-07-03 00:21:59 +00:00
|
|
|
resp = httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err = a.srv.CatalogNodes(resp, req)
|
2015-07-03 00:21:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes = obj.(structs.Nodes)
|
|
|
|
if len(nodes) != 3 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if nodes[2].Node != a.Config.NodeName {
|
2015-07-03 00:21:59 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogServices(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-10-19 08:53:19 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-05-07 21:41:14 +00:00
|
|
|
|
|
|
|
// Register node
|
2013-12-24 19:55:14 +00:00
|
|
|
args := &structs.RegisterRequest{
|
2014-01-08 23:13:27 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
},
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
2014-05-07 21:02:59 +00:00
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2013-12-24 19:55:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/services?dc=dc1", nil)
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogServices(resp, req)
|
2013-12-24 19:55:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
assertIndex(t, resp)
|
2014-02-05 22:36:13 +00:00
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
services := obj.(structs.Services)
|
2014-01-21 19:52:25 +00:00
|
|
|
if len(services) != 2 {
|
2013-12-24 19:55:14 +00:00
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 19:21:49 +00:00
|
|
|
func TestCatalogServices_NodeMetaFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2017-01-09 19:21:49 +00:00
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
NodeMeta: map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2017-01-09 19:21:49 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/services?node-meta=somekey:somevalue", nil)
|
2017-01-09 19:21:49 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogServices(resp, req)
|
2017-01-09 19:21:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
services := obj.(structs.Services)
|
|
|
|
if len(services) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if _, ok := services[args.Service.Service]; !ok {
|
|
|
|
t.Fatalf("bad: %v", services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 18:33:11 +00:00
|
|
|
func TestCatalogRegister_checkRegistration(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Register node with a service and check
|
|
|
|
check := structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "foo-check",
|
|
|
|
Name: "foo check",
|
|
|
|
ServiceID: "api",
|
|
|
|
Definition: structs.HealthCheckDefinition{
|
|
|
|
TCP: "localhost:8888",
|
|
|
|
Interval: 5 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
},
|
|
|
|
Check: &check,
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/health/checks/api", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.HealthServiceChecks(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
checks := obj.(structs.HealthChecks)
|
|
|
|
if len(checks) != 1 {
|
|
|
|
r.Fatalf("expected 1 check, got: %d", len(checks))
|
|
|
|
}
|
|
|
|
if checks[0].CheckID != check.CheckID {
|
|
|
|
r.Fatalf("expected check id %s, got %s", check.Type, checks[0].Type)
|
|
|
|
}
|
|
|
|
if checks[0].Type != "tcp" {
|
|
|
|
r.Fatalf("expected check type tcp, got %s", checks[0].Type)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogServiceNodes(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2014-05-07 21:41:14 +00:00
|
|
|
|
2018-09-06 10:34:28 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2015-11-15 05:05:37 +00:00
|
|
|
// Make sure an empty list is returned, not a nil
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/api?tag=a", nil)
|
2015-11-15 05:05:37 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
2015-11-15 05:05:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
if nodes == nil || len(nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 21:41:14 +00:00
|
|
|
// Register node
|
2013-12-24 19:55:14 +00:00
|
|
|
args := &structs.RegisterRequest{
|
2014-01-08 23:13:27 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"a"},
|
2014-01-08 23:13:27 +00:00
|
|
|
},
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
2014-05-07 21:02:59 +00:00
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2013-12-24 19:55:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/api?tag=a", nil)
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
2013-12-24 19:55:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
assertIndex(t, resp)
|
2014-02-05 22:36:13 +00:00
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
2018-09-06 10:34:28 +00:00
|
|
|
|
|
|
|
// Test caching
|
|
|
|
{
|
|
|
|
// List instances with cache enabled
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/api?cached", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
|
|
|
require.NoError(err)
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
assert.Len(nodes, 1)
|
|
|
|
|
|
|
|
// Should be a cache miss
|
|
|
|
assert.Equal("MISS", resp.Header().Get("X-Cache"))
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// List instances with cache enabled
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/api?cached", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
|
|
|
require.NoError(err)
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
assert.Len(nodes, 1)
|
|
|
|
|
|
|
|
// Should be a cache HIT now!
|
|
|
|
assert.Equal("HIT", resp.Header().Get("X-Cache"))
|
|
|
|
assert.Equal("0", resp.Header().Get("Age"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure background refresh works
|
|
|
|
{
|
|
|
|
// Register a new instance of the service
|
|
|
|
args2 := args
|
|
|
|
args2.Node = "bar"
|
|
|
|
args2.Address = "127.0.0.2"
|
|
|
|
require.NoError(a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
// List it again
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/api?cached", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
|
|
|
r.Check(err)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
r.Fatalf("Want 2 nodes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be a cache hit! The data should've updated in the cache
|
|
|
|
// in the background so this should've been fetched directly from
|
|
|
|
// the cache.
|
|
|
|
if resp.Header().Get("X-Cache") != "HIT" {
|
|
|
|
r.Fatalf("should be a cache hit")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestCatalogServiceNodes_NodeMetaFilter(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2017-01-14 01:08:43 +00:00
|
|
|
|
|
|
|
// Make sure an empty list is returned, not a nil
|
|
|
|
{
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/api?node-meta=somekey:somevalue", nil)
|
2017-01-14 01:08:43 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
2017-01-14 01:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
if nodes == nil || len(nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
NodeMeta: map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2017-01-14 01:08:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/api?node-meta=somekey:somevalue", nil)
|
2017-01-14 01:08:43 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
2017-01-14 01:08:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
func TestCatalogServiceNodes_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
queryPath := "/v1/catalog/service/api?filter=" + url.QueryEscape("ServiceMeta.somekey == somevalue")
|
|
|
|
|
|
|
|
// Make sure an empty list is returned, not a nil
|
|
|
|
{
|
|
|
|
req, _ := http.NewRequest("GET", queryPath, nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
require.Empty(t, nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register node
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
Meta: map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
require.NoError(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
// Register a second service for the node
|
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "api2",
|
|
|
|
Service: "api",
|
|
|
|
Meta: map[string]string{
|
|
|
|
"somekey": "notvalue",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
SkipNodeUpdate: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", queryPath, nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
require.Len(t, nodes, 1)
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:02:51 +00:00
|
|
|
func TestCatalogServiceNodes_WanTranslation(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a1 := NewTestAgent(t, t.Name(), `
|
2017-09-25 18:40:42 +00:00
|
|
|
datacenter = "dc1"
|
|
|
|
translate_wan_addrs = true
|
|
|
|
acl_datacenter = ""
|
|
|
|
`)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a1.Shutdown()
|
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a2 := NewTestAgent(t, t.Name(), `
|
2017-09-25 18:40:42 +00:00
|
|
|
datacenter = "dc2"
|
|
|
|
translate_wan_addrs = true
|
|
|
|
acl_datacenter = ""
|
|
|
|
`)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a2.Shutdown()
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
// Wait for the WAN join.
|
2017-09-25 18:40:42 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a1.Config.SerfPortWAN)
|
2019-06-17 14:51:50 +00:00
|
|
|
_, err := a2.srv.agent.JoinWAN([]string{addr})
|
|
|
|
require.NoError(t, err)
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2019-06-17 14:51:50 +00:00
|
|
|
require.Len(r, a1.WANMembers(), 2)
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Register a node with DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": "127.0.0.2",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "http_wan_translation_test",
|
2019-06-17 14:51:50 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 8080,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{
|
|
|
|
"wan": structs.ServiceAddress{
|
|
|
|
Address: "1.2.3.4",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
2016-06-15 18:02:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2019-06-17 14:51:50 +00:00
|
|
|
require.NoError(t, a2.RPC("Catalog.Register", args, &out))
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query for the node in DC2 from DC1.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/http_wan_translation_test?dc=dc2", nil)
|
2016-06-15 18:02:51 +00:00
|
|
|
resp1 := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj1, err1 := a1.srv.CatalogServiceNodes(resp1, req)
|
2019-06-17 14:51:50 +00:00
|
|
|
require.NoError(t, err1)
|
|
|
|
require.NoError(t, checkIndex(resp1))
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Expect that DC1 gives us a WAN address (since the node is in DC2).
|
2019-06-17 14:51:50 +00:00
|
|
|
nodes1, ok := obj1.(structs.ServiceNodes)
|
|
|
|
require.True(t, ok, "obj1 is not a structs.ServiceNodes")
|
|
|
|
require.Len(t, nodes1, 1)
|
2016-06-15 18:02:51 +00:00
|
|
|
node1 := nodes1[0]
|
2019-06-17 14:51:50 +00:00
|
|
|
require.Equal(t, node1.Address, "127.0.0.2")
|
|
|
|
require.Equal(t, node1.ServiceAddress, "1.2.3.4")
|
|
|
|
require.Equal(t, node1.ServicePort, 80)
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query DC2 from DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
resp2 := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj2, err2 := a2.srv.CatalogServiceNodes(resp2, req)
|
2019-06-17 14:51:50 +00:00
|
|
|
require.NoError(t, err2)
|
|
|
|
require.NoError(t, checkIndex(resp2))
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Expect that DC2 gives us a local address (since the node is in DC2).
|
2019-06-17 14:51:50 +00:00
|
|
|
nodes2, ok := obj2.(structs.ServiceNodes)
|
|
|
|
require.True(t, ok, "obj2 is not a structs.ServiceNodes")
|
|
|
|
require.Len(t, nodes2, 1)
|
2016-06-15 18:02:51 +00:00
|
|
|
node2 := nodes2[0]
|
2019-06-17 14:51:50 +00:00
|
|
|
require.Equal(t, node2.Address, "127.0.0.1")
|
|
|
|
require.Equal(t, node2.ServiceAddress, "127.0.0.1")
|
|
|
|
require.Equal(t, node2.ServicePort, 8080)
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:21:59 +00:00
|
|
|
func TestCatalogServiceNodes_DistanceSort(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2019-01-07 14:39:23 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
2015-07-03 00:21:59 +00:00
|
|
|
|
|
|
|
// Register nodes.
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
Tags: []string{"a"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-03 00:21:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/service/api?tag=a", nil)
|
2015-07-03 00:21:59 +00:00
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
Tags: []string{"a"},
|
|
|
|
},
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2015-07-03 00:21:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nobody has coordinates set so this will still return them in the
|
|
|
|
// order they are indexed.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ = http.NewRequest("GET", "/v1/catalog/service/api?tag=a&near=foo", nil)
|
2015-07-03 00:21:59 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogServiceNodes(resp, req)
|
2015-07-03 00:21:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an update for the node and wait for it to get applied.
|
|
|
|
arg := structs.CoordinateUpdateRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Coord: coordinate.NewCoordinate(coordinate.DefaultConfig()),
|
|
|
|
}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Coordinate.Update", &arg, &out); err != nil {
|
2015-07-03 00:21:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-07 20:43:20 +00:00
|
|
|
// Eventually foo should move to the front of the line.
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
req, _ = http.NewRequest("GET", "/v1/catalog/service/api?tag=a&near=foo", nil)
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = a.srv.CatalogServiceNodes(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
r.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-07-03 00:21:59 +00:00
|
|
|
|
2019-11-07 20:43:20 +00:00
|
|
|
assertIndex(t, resp)
|
|
|
|
nodes = obj.(structs.ServiceNodes)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
r.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "foo" {
|
|
|
|
r.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node != "bar" {
|
|
|
|
r.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
})
|
2015-07-03 00:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 20:05:06 +00:00
|
|
|
// Test that connect proxies can be queried via /v1/catalog/service/:service
|
|
|
|
// directly and that their results contain the proxy fields.
|
2018-03-07 01:32:41 +00:00
|
|
|
func TestCatalogServiceNodes_ConnectProxy(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2018-03-07 01:32:41 +00:00
|
|
|
defer a.Shutdown()
|
2019-01-07 14:39:23 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
2018-03-07 01:32:41 +00:00
|
|
|
|
|
|
|
// 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)
|
2018-09-12 16:07:47 +00:00
|
|
|
assert.Equal(args.Service.Proxy, nodes[0].ServiceProxy)
|
2018-03-07 01:32:41 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 20:05:06 +00:00
|
|
|
// Test that the Connect-compatible endpoints can be queried for a
|
|
|
|
// service via /v1/catalog/connect/:service.
|
2018-03-09 16:43:17 +00:00
|
|
|
func TestCatalogConnectServiceNodes_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2018-03-09 16:43:17 +00:00
|
|
|
defer a.Shutdown()
|
2019-01-07 14:39:23 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
2018-03-09 16:43:17 +00:00
|
|
|
|
|
|
|
// Register
|
|
|
|
args := structs.TestRegisterRequestProxy(t)
|
2018-03-22 02:54:44 +00:00
|
|
|
args.Service.Address = "127.0.0.55"
|
2018-03-09 16:43:17 +00:00
|
|
|
var out struct{}
|
|
|
|
assert.Nil(a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
2018-09-12 16:07:47 +00:00
|
|
|
"/v1/catalog/connect/%s", args.Service.Proxy.DestinationServiceName), nil)
|
2018-03-09 16:43:17 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogConnectServiceNodes(resp, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
assert.Len(nodes, 1)
|
|
|
|
assert.Equal(structs.ServiceKindConnectProxy, nodes[0].ServiceKind)
|
2018-03-22 02:54:44 +00:00
|
|
|
assert.Equal(args.Service.Address, nodes[0].ServiceAddress)
|
2018-09-12 16:07:47 +00:00
|
|
|
assert.Equal(args.Service.Proxy, nodes[0].ServiceProxy)
|
2018-03-09 16:43:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
func TestCatalogConnectServiceNodes_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register
|
|
|
|
args := structs.TestRegisterRequestProxy(t)
|
|
|
|
args.Service.Address = "127.0.0.55"
|
|
|
|
var out struct{}
|
|
|
|
require.NoError(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
args = structs.TestRegisterRequestProxy(t)
|
|
|
|
args.Service.Address = "127.0.0.55"
|
|
|
|
args.Service.Meta = map[string]string{
|
|
|
|
"version": "2",
|
|
|
|
}
|
|
|
|
args.Service.ID = "web-proxy2"
|
|
|
|
args.SkipNodeUpdate = true
|
|
|
|
require.NoError(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
|
|
|
"/v1/catalog/connect/%s?filter=%s",
|
|
|
|
args.Service.Proxy.DestinationServiceName,
|
|
|
|
url.QueryEscape("ServiceMeta.version == 2")), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogConnectServiceNodes(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
require.Len(t, nodes, 1)
|
|
|
|
require.Equal(t, structs.ServiceKindConnectProxy, nodes[0].ServiceKind)
|
|
|
|
require.Equal(t, args.Service.Address, nodes[0].ServiceAddress)
|
|
|
|
require.Equal(t, args.Service.Proxy, nodes[0].ServiceProxy)
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogNodeServices(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2014-05-07 21:41:14 +00:00
|
|
|
|
2018-09-12 16:07:47 +00:00
|
|
|
// Register node with a regular service and connect proxy
|
2013-12-24 19:55:14 +00:00
|
|
|
args := &structs.RegisterRequest{
|
2014-01-08 23:13:27 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
2014-04-03 19:12:23 +00:00
|
|
|
Tags: []string{"a"},
|
2014-01-08 23:13:27 +00:00
|
|
|
},
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
2014-05-07 21:02:59 +00:00
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
var out struct{}
|
2017-05-21 07:11:09 +00:00
|
|
|
if err := a.RPC("Catalog.Register", args, &out); err != nil {
|
2013-12-24 19:55:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:07:47 +00:00
|
|
|
// Register a connect proxy
|
|
|
|
args.Service = structs.TestNodeServiceProxy(t)
|
|
|
|
require.NoError(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/node/foo?dc=dc1", nil)
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj, err := a.srv.CatalogNodeServices(resp, req)
|
2013-12-24 19:55:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 20:11:05 +00:00
|
|
|
assertIndex(t, resp)
|
2014-02-05 22:36:13 +00:00
|
|
|
|
2014-01-03 01:58:58 +00:00
|
|
|
services := obj.(*structs.NodeServices)
|
2018-09-12 16:07:47 +00:00
|
|
|
if len(services.Services) != 2 {
|
2013-12-24 19:55:14 +00:00
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
2018-09-12 16:07:47 +00:00
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
// Proxy service should have it's config intact
|
|
|
|
require.Equal(t, args.Service.Proxy, services.Services["web-proxy"].Proxy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCatalogNodeServices_Filter(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register node with a regular service and connect proxy
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
Tags: []string{"a"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
require.NoError(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
// Register a connect proxy
|
|
|
|
args.Service = structs.TestNodeServiceProxy(t)
|
|
|
|
require.NoError(t, a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/node/foo?dc=dc1&filter="+url.QueryEscape("Kind == `connect-proxy`"), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogNodeServices(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
services := obj.(*structs.NodeServices)
|
|
|
|
require.Len(t, services.Services, 1)
|
|
|
|
|
2018-09-12 16:07:47 +00:00
|
|
|
// Proxy service should have it's config intact
|
|
|
|
require.Equal(t, args.Service.Proxy, services.Services["web-proxy"].Proxy)
|
2013-12-24 19:55:14 +00:00
|
|
|
}
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2018-03-12 20:05:06 +00:00
|
|
|
// Test that the services on a node contain all the Connect proxies on
|
|
|
|
// the node as well with their fields properly populated.
|
2018-03-07 01:41:39 +00:00
|
|
|
func TestCatalogNodeServices_ConnectProxy(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
assert := assert.New(t)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2018-03-07 01:41:39 +00:00
|
|
|
defer a.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-03-07 01:41:39 +00:00
|
|
|
|
|
|
|
// Register
|
|
|
|
args := structs.TestRegisterRequestProxy(t)
|
|
|
|
var out struct{}
|
|
|
|
assert.Nil(a.RPC("Catalog.Register", args, &out))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf(
|
|
|
|
"/v1/catalog/node/%s", args.Node), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.CatalogNodeServices(resp, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
ns := obj.(*structs.NodeServices)
|
|
|
|
assert.Len(ns.Services, 1)
|
|
|
|
v := ns.Services[args.Service.Service]
|
|
|
|
assert.Equal(structs.ServiceKindConnectProxy, v.Kind)
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:02:51 +00:00
|
|
|
func TestCatalogNodeServices_WanTranslation(t *testing.T) {
|
2017-05-21 07:54:40 +00:00
|
|
|
t.Parallel()
|
2019-02-14 15:59:14 +00:00
|
|
|
a1 := NewTestAgent(t, t.Name(), `
|
2017-09-25 18:40:42 +00:00
|
|
|
datacenter = "dc1"
|
|
|
|
translate_wan_addrs = true
|
|
|
|
acl_datacenter = ""
|
|
|
|
`)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a1.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a1.RPC, "dc1")
|
2017-05-21 07:11:09 +00:00
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a2 := NewTestAgent(t, t.Name(), `
|
2017-09-25 18:40:42 +00:00
|
|
|
datacenter = "dc2"
|
|
|
|
translate_wan_addrs = true
|
|
|
|
acl_datacenter = ""
|
|
|
|
`)
|
2017-05-21 07:11:09 +00:00
|
|
|
defer a2.Shutdown()
|
2018-09-10 15:58:53 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a2.RPC, "dc2")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
// Wait for the WAN join.
|
2017-09-25 18:40:42 +00:00
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", a1.Config.SerfPortWAN)
|
2019-06-17 14:51:50 +00:00
|
|
|
_, err := a2.srv.agent.JoinWAN([]string{addr})
|
|
|
|
require.NoError(t, err)
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2019-06-17 14:51:50 +00:00
|
|
|
require.Len(r, a1.WANMembers(), 2)
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Register a node with DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
{
|
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc2",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
TaggedAddresses: map[string]string{
|
|
|
|
"wan": "127.0.0.2",
|
|
|
|
},
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "http_wan_translation_test",
|
2019-06-17 14:51:50 +00:00
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: 8080,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{
|
|
|
|
"wan": structs.ServiceAddress{
|
|
|
|
Address: "1.2.3.4",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
},
|
2016-06-15 18:02:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
2019-06-17 14:51:50 +00:00
|
|
|
require.NoError(t, a2.RPC("Catalog.Register", args, &out))
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query for the node in DC2 from DC1.
|
2017-05-09 11:38:05 +00:00
|
|
|
req, _ := http.NewRequest("GET", "/v1/catalog/node/foo?dc=dc2", nil)
|
2016-06-15 18:02:51 +00:00
|
|
|
resp1 := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj1, err1 := a1.srv.CatalogNodeServices(resp1, req)
|
2019-06-17 14:51:50 +00:00
|
|
|
require.NoError(t, err1)
|
|
|
|
require.NoError(t, checkIndex(resp1))
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Expect that DC1 gives us a WAN address (since the node is in DC2).
|
2019-06-17 14:51:50 +00:00
|
|
|
service1, ok := obj1.(*structs.NodeServices)
|
|
|
|
require.True(t, ok, "obj1 is not a *structs.NodeServices")
|
|
|
|
require.NotNil(t, service1.Node)
|
|
|
|
require.Equal(t, service1.Node.Address, "127.0.0.2")
|
|
|
|
require.Len(t, service1.Services, 1)
|
|
|
|
ns1, ok := service1.Services["http_wan_translation_test"]
|
|
|
|
require.True(t, ok, "Missing service http_wan_translation_test")
|
|
|
|
require.Equal(t, "1.2.3.4", ns1.Address)
|
|
|
|
require.Equal(t, 80, ns1.Port)
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query DC2 from DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
resp2 := httptest.NewRecorder()
|
2017-05-21 07:11:09 +00:00
|
|
|
obj2, err2 := a2.srv.CatalogNodeServices(resp2, req)
|
2019-06-17 14:51:50 +00:00
|
|
|
require.NoError(t, err2)
|
|
|
|
require.NoError(t, checkIndex(resp2))
|
2016-06-15 18:02:51 +00:00
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Expect that DC2 gives us a private address (since the node is in DC2).
|
2019-06-17 14:51:50 +00:00
|
|
|
service2 := obj2.(*structs.NodeServices)
|
|
|
|
require.True(t, ok, "obj2 is not a *structs.NodeServices")
|
|
|
|
require.NotNil(t, service2.Node)
|
|
|
|
require.Equal(t, service2.Node.Address, "127.0.0.1")
|
|
|
|
require.Len(t, service2.Services, 1)
|
|
|
|
ns2, ok := service2.Services["http_wan_translation_test"]
|
|
|
|
require.True(t, ok, "Missing service http_wan_translation_test")
|
|
|
|
require.Equal(t, ns2.Address, "127.0.0.1")
|
|
|
|
require.Equal(t, ns2.Port, 8080)
|
2016-06-15 18:02:51 +00:00
|
|
|
}
|