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"
|
2013-12-23 22:26:34 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2013-12-24 00:20:51 +00:00
|
|
|
"time"
|
2015-07-03 00:21:59 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2015-07-03 00:21:59 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2013-12-23 22:26:34 +00:00
|
|
|
)
|
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
func TestCatalogRegister(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2013-12-24 00:20:51 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:41:14 +00:00
|
|
|
|
|
|
|
// Register node
|
2013-12-24 00:20:51 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/register", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-07 21:51:15 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
req.Body = encodeReq(args)
|
|
|
|
|
|
|
|
obj, err := srv.CatalogRegister(nil, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res := obj.(bool)
|
|
|
|
if res != true {
|
|
|
|
t.Fatalf("bad: %v", res)
|
|
|
|
}
|
2014-10-18 00:33:27 +00:00
|
|
|
|
|
|
|
// Service should be in sync
|
|
|
|
if err := srv.agent.state.syncService("foo"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if _, ok := srv.agent.state.serviceStatus["foo"]; !ok {
|
|
|
|
t.Fatalf("bad: %#v", srv.agent.state.serviceStatus)
|
|
|
|
}
|
|
|
|
if !srv.agent.state.serviceStatus["foo"].inSync {
|
|
|
|
t.Fatalf("should be in sync")
|
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogDeregister(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2013-12-24 19:55:14 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:41:14 +00:00
|
|
|
|
|
|
|
// Register node
|
2013-12-24 19:55:14 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/deregister", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-07 21:51:15 +00:00
|
|
|
args := &structs.DeregisterRequest{
|
|
|
|
Node: "foo",
|
|
|
|
}
|
2013-12-24 19:55:14 +00:00
|
|
|
req.Body = encodeReq(args)
|
|
|
|
|
|
|
|
obj, err := srv.CatalogDeregister(nil, req)
|
|
|
|
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-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2013-12-23 22:26:34 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
if err := testrpc.WaitForResult(func() (bool, error) {
|
2014-06-11 19:01:14 +00:00
|
|
|
obj, err := srv.CatalogDatacenters(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dcs := obj.([]string)
|
|
|
|
if len(dcs) != 1 {
|
|
|
|
return false, fmt.Errorf("missing dc: %v", dcs)
|
|
|
|
}
|
|
|
|
return true, nil
|
2017-03-23 22:27:16 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-12-23 22:26:34 +00:00
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
|
|
|
|
func TestCatalogNodes(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2013-12-24 00:20:51 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := 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 {
|
2013-12-24 00:20:51 +00:00
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 19:55:14 +00:00
|
|
|
|
2017-01-09 19:21:49 +00:00
|
|
|
func TestCatalogNodes_MetaFilter(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2017-01-06 01:21:56 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/nodes?node-meta=somekey:somevalue", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.CatalogNodes(resp, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:02:51 +00:00
|
|
|
func TestCatalogNodes_WanTranslation(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2016-08-15 22:34:11 +00:00
|
|
|
dir1, srv1 := makeHTTPServerWithConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc1"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer srv1.Shutdown()
|
|
|
|
defer srv1.agent.Shutdown()
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv1.agent.RPC, "dc1")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
dir2, srv2 := makeHTTPServerWithConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc2"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer srv2.Shutdown()
|
|
|
|
defer srv2.agent.Shutdown()
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv2.agent.RPC, "dc2")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
// Wait for the WAN join.
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
srv1.agent.config.Ports.SerfWan)
|
|
|
|
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if err := testrpc.WaitForResult(func() (bool, error) {
|
2017-03-23 22:27:16 +00:00
|
|
|
return len(srv1.agent.WANMembers()) > 1, nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("Failed waiting for WAN join: %v", err)
|
|
|
|
}
|
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{}
|
|
|
|
if err := srv2.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query nodes in DC2 from DC1.
|
2016-06-15 18:02:51 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc2", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp1 := httptest.NewRecorder()
|
|
|
|
obj1, err1 := srv1.CatalogNodes(resp1, req)
|
|
|
|
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 {
|
|
|
|
t.Fatalf("bad: %v", obj1)
|
|
|
|
}
|
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()
|
|
|
|
obj2, err2 := srv2.CatalogNodes(resp2, req)
|
|
|
|
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-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2014-02-05 22:47:42 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.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
|
|
|
|
if err := srv.agent.RPC("Catalog.ListNodes", *args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do an update in a little while
|
|
|
|
start := time.Now()
|
|
|
|
go func() {
|
2014-05-07 21:02:59 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2014-02-05 22:47:42 +00:00
|
|
|
args := &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Do a blocking read
|
|
|
|
req, err := http.NewRequest("GET",
|
|
|
|
fmt.Sprintf("/v1/catalog/nodes?wait=60s&index=%d", out.Index), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.CatalogNodes(resp, req)
|
2014-02-05 22:47:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should block for a while
|
2014-05-16 22:49:47 +00:00
|
|
|
if time.Now().Sub(start) < 50*time.Millisecond {
|
2014-02-05 22:47:42 +00:00
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
if idx := getIndex(t, resp); idx <= out.Index {
|
2014-02-05 22:47:42 +00:00
|
|
|
t.Fatalf("bad: %v", idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := obj.(structs.Nodes)
|
|
|
|
if len(nodes) != 2 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 00:21:59 +00:00
|
|
|
func TestCatalogNodes_DistanceSort(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2015-07-03 00:21:59 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nobody has coordinates set so this will still return them in the
|
|
|
|
// order they are indexed.
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/nodes?dc=dc1&near=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.CatalogNodes(resp, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if nodes[2].Node != srv.agent.config.NodeName {
|
|
|
|
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()),
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Coordinate.Update", &arg, &out); err != nil {
|
|
|
|
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.
|
|
|
|
req, err = http.NewRequest("GET", "/v1/catalog/nodes?dc=dc1&near=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.CatalogNodes(resp, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if nodes[2].Node != srv.agent.config.NodeName {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogServices(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2013-12-24 19:55:14 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/services?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := 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-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2017-01-09 19:21:49 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.RPC, "dc1")
|
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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/services?node-meta=somekey:somevalue", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.CatalogServices(resp, req)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogServiceNodes(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2013-12-24 19:55:14 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2014-05-07 21:41:14 +00:00
|
|
|
|
2015-11-15 05:05:37 +00:00
|
|
|
// Make sure an empty list is returned, not a nil
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/service/api?tag=a", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.CatalogServiceNodes(resp, req)
|
|
|
|
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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/service/api?tag=a", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestCatalogServiceNodes_NodeMetaFilter(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2017-01-14 01:08:43 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.RPC, "dc1")
|
2017-01-14 01:08:43 +00:00
|
|
|
|
|
|
|
// Make sure an empty list is returned, not a nil
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/service/api?node-meta=somekey:somevalue", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.CatalogServiceNodes(resp, req)
|
|
|
|
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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/service/api?node-meta=somekey:somevalue", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.CatalogServiceNodes(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
nodes := obj.(structs.ServiceNodes)
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 18:02:51 +00:00
|
|
|
func TestCatalogServiceNodes_WanTranslation(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2016-08-15 22:34:11 +00:00
|
|
|
dir1, srv1 := makeHTTPServerWithConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc1"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer srv1.Shutdown()
|
|
|
|
defer srv1.agent.Shutdown()
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv1.agent.RPC, "dc1")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
dir2, srv2 := makeHTTPServerWithConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc2"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer srv2.Shutdown()
|
|
|
|
defer srv2.agent.Shutdown()
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv2.agent.RPC, "dc2")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
// Wait for the WAN join.
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
srv1.agent.config.Ports.SerfWan)
|
|
|
|
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if err := testrpc.WaitForResult(func() (bool, error) {
|
2017-03-23 22:27:16 +00:00
|
|
|
return len(srv1.agent.WANMembers()) > 1, nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("Failed waiting for WAN join: %v", err)
|
|
|
|
}
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv2.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query for the node in DC2 from DC1.
|
2016-06-15 18:02:51 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/service/http_wan_translation_test?dc=dc2", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp1 := httptest.NewRecorder()
|
|
|
|
obj1, err1 := srv1.CatalogServiceNodes(resp1, req)
|
|
|
|
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.ServiceNodes)
|
|
|
|
if len(nodes1) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj1)
|
|
|
|
}
|
|
|
|
node1 := nodes1[0]
|
|
|
|
if node1.Address != "127.0.0.2" {
|
|
|
|
t.Fatalf("bad: %v", node1)
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query DC2 from DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
resp2 := httptest.NewRecorder()
|
|
|
|
obj2, err2 := srv2.CatalogServiceNodes(resp2, req)
|
|
|
|
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 local address (since the node is in DC2).
|
2016-06-15 18:02:51 +00:00
|
|
|
nodes2 := obj2.(structs.ServiceNodes)
|
|
|
|
if len(nodes2) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj2)
|
|
|
|
}
|
|
|
|
node2 := nodes2[0]
|
|
|
|
if node2.Address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %v", node2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 00:21:59 +00:00
|
|
|
func TestCatalogServiceNodes_DistanceSort(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2015-07-03 00:21:59 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/service/api?tag=a", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = &structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "api",
|
|
|
|
Tags: []string{"a"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nobody has coordinates set so this will still return them in the
|
|
|
|
// order they are indexed.
|
|
|
|
req, err = http.NewRequest("GET", "/v1/catalog/service/api?tag=a&near=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := srv.CatalogServiceNodes(resp, req)
|
|
|
|
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()),
|
|
|
|
}
|
|
|
|
if err := srv.agent.RPC("Coordinate.Update", &arg, &out); err != nil {
|
|
|
|
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.
|
|
|
|
req, err = http.NewRequest("GET", "/v1/catalog/service/api?tag=a&near=foo", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = httptest.NewRecorder()
|
|
|
|
obj, err = srv.CatalogServiceNodes(resp, req)
|
|
|
|
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 != "foo" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[1].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogNodeServices(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2013-12-24 19:55:14 +00:00
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv.agent.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",
|
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{}
|
|
|
|
if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/node/foo?dc=dc1", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-21 20:11:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := 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)
|
|
|
|
if len(services.Services) != 1 {
|
2013-12-24 19:55:14 +00:00
|
|
|
t.Fatalf("bad: %v", obj)
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 18:02:51 +00:00
|
|
|
|
|
|
|
func TestCatalogNodeServices_WanTranslation(t *testing.T) {
|
2017-04-27 00:40:40 +00:00
|
|
|
t.Parallel()
|
2016-08-15 22:34:11 +00:00
|
|
|
dir1, srv1 := makeHTTPServerWithConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc1"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer srv1.Shutdown()
|
|
|
|
defer srv1.agent.Shutdown()
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv1.agent.RPC, "dc1")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
dir2, srv2 := makeHTTPServerWithConfig(t,
|
|
|
|
func(c *Config) {
|
|
|
|
c.Datacenter = "dc2"
|
|
|
|
c.TranslateWanAddrs = true
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer srv2.Shutdown()
|
|
|
|
defer srv2.agent.Shutdown()
|
2017-04-19 23:00:11 +00:00
|
|
|
testrpc.WaitForLeader(t, srv2.agent.RPC, "dc2")
|
2016-08-15 22:34:11 +00:00
|
|
|
|
|
|
|
// Wait for the WAN join.
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
srv1.agent.config.Ports.SerfWan)
|
|
|
|
if _, err := srv2.agent.JoinWAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-04-19 23:00:11 +00:00
|
|
|
if err := testrpc.WaitForResult(func() (bool, error) {
|
2017-03-23 22:27:16 +00:00
|
|
|
return len(srv1.agent.WANMembers()) > 1, nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("Failed waiting for WAN join: %v", err)
|
|
|
|
}
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out struct{}
|
|
|
|
if err := srv2.agent.RPC("Catalog.Register", args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query for the node in DC2 from DC1.
|
2016-06-15 18:02:51 +00:00
|
|
|
req, err := http.NewRequest("GET", "/v1/catalog/node/foo?dc=dc2", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp1 := httptest.NewRecorder()
|
|
|
|
obj1, err1 := srv1.CatalogNodeServices(resp1, req)
|
|
|
|
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
|
|
|
services1 := obj1.(*structs.NodeServices)
|
|
|
|
if len(services1.Services) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj1)
|
|
|
|
}
|
|
|
|
service1 := services1.Node
|
|
|
|
if service1.Address != "127.0.0.2" {
|
|
|
|
t.Fatalf("bad: %v", service1)
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:34:11 +00:00
|
|
|
// Query DC2 from DC2.
|
2016-06-15 18:02:51 +00:00
|
|
|
resp2 := httptest.NewRecorder()
|
|
|
|
obj2, err2 := srv2.CatalogNodeServices(resp2, req)
|
|
|
|
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
|
|
|
services2 := obj2.(*structs.NodeServices)
|
|
|
|
if len(services2.Services) != 1 {
|
|
|
|
t.Fatalf("bad: %v", obj2)
|
|
|
|
}
|
|
|
|
service2 := services2.Node
|
|
|
|
if service2.Address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %v", service2)
|
|
|
|
}
|
|
|
|
}
|