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
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-05-16 22:49:47 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
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"
|
2013-12-23 22:26:34 +00:00
|
|
|
)
|
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
func TestCatalogRegister(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.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) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.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) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-06-11 19:01:14 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
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
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
})
|
2013-12-23 22:26:34 +00:00
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
|
|
|
|
func TestCatalogNodes(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.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
|
|
|
|
2014-02-05 22:47:42 +00:00
|
|
|
func TestCatalogNodes_Blocking(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
func TestCatalogServices(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCatalogServiceNodes(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.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/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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCatalogNodeServices(t *testing.T) {
|
|
|
|
dir, srv := makeHTTPServer(t)
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
defer srv.agent.Shutdown()
|
|
|
|
|
2014-05-08 23:17:35 +00:00
|
|
|
testutil.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)
|
|
|
|
}
|
|
|
|
}
|