2013-12-11 22:57:40 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2013-12-12 00:33:19 +00:00
|
|
|
"fmt"
|
2013-12-19 20:03:57 +00:00
|
|
|
"net/rpc"
|
2013-12-11 22:57:40 +00:00
|
|
|
"os"
|
2014-04-21 19:08:00 +00:00
|
|
|
"strings"
|
2013-12-11 22:57:40 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-10-09 18:54:47 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2016-01-29 19:42:34 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2014-10-09 18:54:47 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2017-01-18 22:26:42 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2015-10-13 23:43:52 +00:00
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2013-12-11 22:57:40 +00:00
|
|
|
)
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_Register(t *testing.T) {
|
2013-12-11 22:57:40 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-11 22:57:40 +00:00
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
arg := structs.RegisterRequest{
|
2014-01-08 21:39:40 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 21:39:40 +00:00
|
|
|
Port: 8000,
|
|
|
|
},
|
2016-12-09 00:01:01 +00:00
|
|
|
Check: &structs.HealthCheck{
|
2017-03-27 08:28:54 +00:00
|
|
|
CheckID: types.CheckID("db-check"),
|
2016-12-09 00:01:01 +00:00
|
|
|
ServiceID: "db",
|
|
|
|
},
|
2013-12-11 22:57:40 +00:00
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
|
2016-07-10 17:24:18 +00:00
|
|
|
if err != nil {
|
2013-12-11 22:57:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-01-18 22:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCatalog_Register_NodeID(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ID: "nope",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
2017-03-27 08:28:54 +00:00
|
|
|
CheckID: types.CheckID("db-check"),
|
2017-01-18 22:26:42 +00:00
|
|
|
ServiceID: "db",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out struct{}
|
2013-12-11 22:57:40 +00:00
|
|
|
|
2017-01-18 22:26:42 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Bad node ID") {
|
2013-12-11 22:57:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2017-01-18 22:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
arg.ID = types.NodeID("adf4238a-882b-9ddc-4a9d-5b6758e4159e")
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-11 22:57:40 +00:00
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_Register_ACLDeny(t *testing.T) {
|
2014-12-01 04:05:15 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
2016-12-08 01:58:23 +00:00
|
|
|
c.ACLEnforceVersion8 = false
|
2014-12-01 04:05:15 +00:00
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-12-01 04:05:15 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-12-01 04:05:15 +00:00
|
|
|
|
2016-12-10 03:15:44 +00:00
|
|
|
// Create the ACL.
|
2014-12-01 04:05:15 +00:00
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
2016-12-10 03:15:44 +00:00
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: `
|
|
|
|
service "foo" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
`,
|
2014-12-01 04:05:15 +00:00
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2016-12-10 03:15:44 +00:00
|
|
|
var id string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &id); err != nil {
|
2014-12-01 04:05:15 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
argR := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
|
|
|
Tags: []string{"master"},
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: id},
|
|
|
|
}
|
|
|
|
var outR struct{}
|
|
|
|
|
2016-12-10 03:15:44 +00:00
|
|
|
// This should fail since we are writing to the "db" service, which isn't
|
|
|
|
// allowed.
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &argR, &outR)
|
2014-12-01 04:05:15 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-12-01 04:10:42 +00:00
|
|
|
|
2016-12-10 03:15:44 +00:00
|
|
|
// The "foo" service should work, though.
|
2014-12-01 04:10:42 +00:00
|
|
|
argR.Service.Service = "foo"
|
2015-10-13 23:43:52 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Register", &argR, &outR)
|
2014-12-01 04:10:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-12-08 01:58:23 +00:00
|
|
|
|
2016-12-10 03:15:44 +00:00
|
|
|
// Try the special case for the "consul" service that allows it no matter
|
|
|
|
// what with pre-version 8 ACL enforcement.
|
2016-12-08 01:58:23 +00:00
|
|
|
argR.Service.Service = "consul"
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Register", &argR, &outR)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the exception goes away when we turn on version 8 ACL
|
|
|
|
// enforcement.
|
|
|
|
s1.config.ACLEnforceVersion8 = true
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Register", &argR, &outR)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-12-09 00:01:01 +00:00
|
|
|
|
|
|
|
// Register a db service using the root token.
|
|
|
|
argR.Service.Service = "db"
|
|
|
|
argR.Service.ID = "my-id"
|
|
|
|
argR.Token = "root"
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Register", &argR, &outR)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prove that we are properly looking up the node services and passing
|
|
|
|
// that to the ACL helper. We can vet the helper independently in its
|
|
|
|
// own unit test after this. This is trying to register over the db
|
|
|
|
// service we created above, which is a check that depends on looking
|
2016-12-10 03:15:44 +00:00
|
|
|
// at the existing registration data with that service ID. This is a new
|
|
|
|
// check for version 8.
|
2016-12-09 00:01:01 +00:00
|
|
|
argR.Service.Service = "foo"
|
|
|
|
argR.Service.ID = "my-id"
|
|
|
|
argR.Token = id
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Register", &argR, &outR)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-12-01 04:05:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_Register_ForwardLeader(t *testing.T) {
|
2013-12-12 00:42:19 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec1 := rpcClient(t, s1)
|
|
|
|
defer codec1.Close()
|
2013-12-12 00:42:19 +00:00
|
|
|
|
|
|
|
dir2, s2 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec2 := rpcClient(t, s2)
|
|
|
|
defer codec2.Close()
|
2013-12-12 00:42:19 +00:00
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
2013-12-30 20:20:17 +00:00
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinLAN([]string{addr}); err != nil {
|
2013-12-12 00:42:19 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
testutil.WaitForLeader(t, s2.RPC, "dc1")
|
2013-12-12 00:42:19 +00:00
|
|
|
|
|
|
|
// Use the follower as the client
|
2015-10-13 23:43:52 +00:00
|
|
|
var codec rpc.ClientCodec
|
2013-12-12 00:42:19 +00:00
|
|
|
if !s1.IsLeader() {
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = codec1
|
2013-12-12 00:42:19 +00:00
|
|
|
} else {
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = codec2
|
2013-12-12 00:42:19 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
arg := structs.RegisterRequest{
|
2014-01-08 21:39:40 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 21:39:40 +00:00
|
|
|
Port: 8000,
|
|
|
|
},
|
2013-12-12 00:42:19 +00:00
|
|
|
}
|
|
|
|
var out struct{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2013-12-12 00:42:19 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_Register_ForwardDC(t *testing.T) {
|
2013-12-12 00:33:19 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-12 00:33:19 +00:00
|
|
|
|
|
|
|
dir2, s2 := testServerDC(t, "dc2")
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
2013-12-30 20:20:17 +00:00
|
|
|
s1.config.SerfWANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinWAN([]string{addr}); err != nil {
|
2013-12-12 00:33:19 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc2")
|
2013-12-12 00:33:19 +00:00
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
arg := structs.RegisterRequest{
|
2015-09-15 12:22:08 +00:00
|
|
|
Datacenter: "dc2", // Should forward through s1
|
2014-01-08 21:39:40 +00:00
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "db",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 21:39:40 +00:00
|
|
|
Port: 8000,
|
|
|
|
},
|
2013-12-12 00:33:19 +00:00
|
|
|
}
|
|
|
|
var out struct{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2013-12-12 00:33:19 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_Deregister(t *testing.T) {
|
2013-12-11 23:34:10 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-11 23:34:10 +00:00
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
arg := structs.DeregisterRequest{
|
2013-12-11 23:34:10 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.Deregister", &arg, &out)
|
2016-07-10 17:24:18 +00:00
|
|
|
if err != nil {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2013-12-11 23:34:10 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Deregister", &arg, &out); err != nil {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 18:35:50 +00:00
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_Deregister_ACLDeny(t *testing.T) {
|
2016-12-10 03:15:44 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
c.ACLEnforceVersion8 = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create the ACL.
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: `
|
|
|
|
node "node" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
|
|
|
|
service "service" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var id string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a node, node check, service, and service check.
|
|
|
|
argR := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "service",
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
Checks: structs.HealthChecks{
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "node-check",
|
|
|
|
},
|
|
|
|
&structs.HealthCheck{
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "service-check",
|
|
|
|
ServiceID: "service",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: id},
|
|
|
|
}
|
|
|
|
var outR struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &argR, &outR); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First pass with version 8 ACL enforcement disabled, we should be able
|
|
|
|
// to deregister everything even without a token.
|
|
|
|
var err error
|
|
|
|
var out struct{}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "service-check"}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "node-check"}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
ServiceID: "service"}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node"}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn on version 8 ACL enforcement and put the catalog entry back.
|
|
|
|
s1.config.ACLEnforceVersion8 = true
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &argR, &outR); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass with version 8 ACL enforcement enabled, these should all
|
|
|
|
// get rejected.
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "service-check"}, &out)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "node-check"}, &out)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
ServiceID: "service"}, &out)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node"}, &out)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Third pass these should all go through with the token set.
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "service-check",
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: id,
|
|
|
|
}}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "node-check",
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: id,
|
|
|
|
}}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
ServiceID: "service",
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: id,
|
|
|
|
}}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: id,
|
|
|
|
}}, &out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try a few error cases.
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
ServiceID: "nope",
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: id,
|
|
|
|
}}, &out)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Unknown service") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Catalog.Deregister",
|
|
|
|
&structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "node",
|
|
|
|
CheckID: "nope",
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Token: id,
|
|
|
|
}}, &out)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Unknown check") {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListDatacenters(t *testing.T) {
|
2013-12-12 18:35:50 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-12 18:35:50 +00:00
|
|
|
|
|
|
|
dir2, s2 := testServerDC(t, "dc2")
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
2013-12-30 20:20:17 +00:00
|
|
|
s1.config.SerfWANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinWAN([]string{addr}); err != nil {
|
2013-12-12 18:35:50 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-08 23:18:35 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2013-12-12 18:35:50 +00:00
|
|
|
|
|
|
|
var out []string
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListDatacenters", struct{}{}, &out); err != nil {
|
2013-12-12 18:35:50 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 22:36:59 +00:00
|
|
|
// The DCs should come out sorted by default.
|
2013-12-12 18:35:50 +00:00
|
|
|
if len(out) != 2 {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out[0] != "dc1" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out[1] != "dc2" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 18:48:36 +00:00
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListDatacenters_DistanceSort(t *testing.T) {
|
2015-07-02 22:36:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-07-02 22:36:59 +00:00
|
|
|
|
|
|
|
dir2, s2 := testServerDC(t, "dc2")
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
|
|
|
|
|
|
|
dir3, s3 := testServerDC(t, "acdc")
|
|
|
|
defer os.RemoveAll(dir3)
|
|
|
|
defer s3.Shutdown()
|
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfWANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinWAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := s3.JoinWAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-15 23:07:16 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2015-07-02 22:36:59 +00:00
|
|
|
|
|
|
|
var out []string
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListDatacenters", struct{}{}, &out); err != nil {
|
2015-07-02 22:36:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's super hard to force the Serfs into a known configuration of
|
|
|
|
// coordinates, so the best we can do is make sure that the sorting
|
|
|
|
// function is getting called (it's tested extensively in rtt_test.go).
|
|
|
|
// Since this is relative to dc1, it will be listed first (proving we
|
2016-03-22 00:30:05 +00:00
|
|
|
// went into the sort fn).
|
2015-07-02 22:36:59 +00:00
|
|
|
if len(out) != 3 {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out[0] != "dc1" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListNodes(t *testing.T) {
|
2013-12-12 18:48:36 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-12 18:48:36 +00:00
|
|
|
|
2014-02-05 19:00:43 +00:00
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.IndexedNodes
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out)
|
2016-07-10 17:24:18 +00:00
|
|
|
if err != nil {
|
2013-12-12 18:48:36 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2013-12-12 18:48:36 +00:00
|
|
|
|
|
|
|
// Just add a node
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-12 18:48:36 +00:00
|
|
|
|
2017-03-23 22:27:16 +00:00
|
|
|
if err := testutil.WaitForResult(func() (bool, error) {
|
2015-10-13 23:43:52 +00:00
|
|
|
msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out)
|
2014-05-09 01:00:56 +00:00
|
|
|
return len(out.Nodes) == 2, nil
|
2017-03-23 22:27:16 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-01-10 01:22:01 +00:00
|
|
|
|
|
|
|
// Server node is auto added from Serf
|
2014-07-23 08:34:03 +00:00
|
|
|
if out.Nodes[1].Node != s1.config.NodeName {
|
2013-12-12 18:48:36 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2014-07-23 08:34:03 +00:00
|
|
|
if out.Nodes[0].Node != "foo" {
|
2013-12-12 18:48:36 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2014-07-23 08:34:03 +00:00
|
|
|
if out.Nodes[0].Address != "127.0.0.1" {
|
2013-12-12 18:48:36 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 19:07:14 +00:00
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestCatalog_ListNodes_NodeMetaFilter(t *testing.T) {
|
2017-01-06 01:21:56 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Add a new node with the right meta k/v pair
|
|
|
|
node := &structs.Node{Node: "foo", Address: "127.0.0.1", Meta: map[string]string{"somekey": "somevalue"}}
|
|
|
|
if err := s1.fsm.State().EnsureNode(1, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-11 19:41:12 +00:00
|
|
|
// Filter by a specific meta k/v pair
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
NodeMetaFilters: map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out structs.IndexedNodes
|
|
|
|
|
2017-03-23 22:27:16 +00:00
|
|
|
if err := testutil.WaitForResult(func() (bool, error) {
|
2017-01-06 01:21:56 +00:00
|
|
|
msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out)
|
|
|
|
return len(out.Nodes) == 1, nil
|
2017-03-23 22:27:16 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-06 01:21:56 +00:00
|
|
|
|
|
|
|
// Verify that only the correct node was returned
|
|
|
|
if out.Nodes[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[0].Address != "127.0.0.1" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if v, ok := out.Nodes[0].Meta["somekey"]; !ok || v != "somevalue" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now filter on a nonexistent meta k/v pair
|
|
|
|
args = structs.DCSpecificRequest{
|
2017-01-11 19:41:12 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
NodeMetaFilters: map[string]string{
|
|
|
|
"somekey": "invalid",
|
|
|
|
},
|
2017-01-06 01:21:56 +00:00
|
|
|
}
|
|
|
|
out = structs.IndexedNodes{}
|
2017-01-11 19:41:12 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out)
|
2017-01-06 01:21:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get an empty list of nodes back
|
2017-03-23 22:27:16 +00:00
|
|
|
if err := testutil.WaitForResult(func() (bool, error) {
|
2017-01-06 01:21:56 +00:00
|
|
|
msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out)
|
|
|
|
return len(out.Nodes) == 0, nil
|
2017-03-23 22:27:16 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-06 01:21:56 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListNodes_StaleRaad(t *testing.T) {
|
2014-04-19 00:26:59 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec1 := rpcClient(t, s1)
|
|
|
|
defer codec1.Close()
|
2014-04-19 00:26:59 +00:00
|
|
|
|
2014-04-19 00:48:50 +00:00
|
|
|
dir2, s2 := testServerDCBootstrap(t, "dc1", false)
|
2014-04-19 00:26:59 +00:00
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec2 := rpcClient(t, s2)
|
|
|
|
defer codec2.Close()
|
2014-04-19 00:26:59 +00:00
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
testutil.WaitForLeader(t, s2.RPC, "dc1")
|
2014-05-07 21:47:16 +00:00
|
|
|
|
2014-04-19 00:26:59 +00:00
|
|
|
// Use the follower as the client
|
2015-10-13 23:43:52 +00:00
|
|
|
var codec rpc.ClientCodec
|
2014-04-19 00:26:59 +00:00
|
|
|
if !s1.IsLeader() {
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = codec1
|
2014-04-19 00:26:59 +00:00
|
|
|
|
|
|
|
// Inject fake data on the follower!
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-19 00:26:59 +00:00
|
|
|
} else {
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = codec2
|
2014-04-19 00:26:59 +00:00
|
|
|
|
|
|
|
// Inject fake data on the follower!
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s2.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-19 00:26:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 21:51:15 +00:00
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{AllowStale: true},
|
|
|
|
}
|
2014-04-19 00:26:59 +00:00
|
|
|
var out structs.IndexedNodes
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out); err != nil {
|
2014-04-19 00:26:59 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-04-19 00:48:50 +00:00
|
|
|
found := false
|
|
|
|
for _, n := range out.Nodes {
|
|
|
|
if n.Node == "foo" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("failed to find foo")
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.QueryMeta.LastContact == 0 {
|
|
|
|
t.Fatalf("should have a last contact time")
|
|
|
|
}
|
|
|
|
if !out.QueryMeta.KnownLeader {
|
|
|
|
t.Fatalf("should have known leader")
|
2014-04-21 19:08:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListNodes_ConsistentRead_Fail(t *testing.T) {
|
2014-04-21 19:08:00 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec1 := rpcClient(t, s1)
|
|
|
|
defer codec1.Close()
|
2014-04-21 19:08:00 +00:00
|
|
|
|
|
|
|
dir2, s2 := testServerDCBootstrap(t, "dc1", false)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec2 := rpcClient(t, s2)
|
|
|
|
defer codec2.Close()
|
2014-04-21 19:08:00 +00:00
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
testutil.WaitForLeader(t, s2.RPC, "dc1")
|
2014-04-21 19:08:00 +00:00
|
|
|
|
|
|
|
// Use the leader as the client, kill the follower
|
2015-10-13 23:43:52 +00:00
|
|
|
var codec rpc.ClientCodec
|
2014-04-21 19:08:00 +00:00
|
|
|
if s1.IsLeader() {
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = codec1
|
2014-04-21 19:08:00 +00:00
|
|
|
s2.Shutdown()
|
|
|
|
} else {
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = codec2
|
2014-04-21 19:08:00 +00:00
|
|
|
s1.Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{RequireConsistent: true},
|
|
|
|
}
|
|
|
|
var out structs.IndexedNodes
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out); !strings.HasPrefix(err.Error(), "leadership lost") {
|
2014-04-21 19:08:00 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.QueryMeta.LastContact != 0 {
|
|
|
|
t.Fatalf("should not have a last contact time")
|
|
|
|
}
|
|
|
|
if out.QueryMeta.KnownLeader {
|
|
|
|
t.Fatalf("should have no known leader")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListNodes_ConsistentRead(t *testing.T) {
|
2014-04-21 19:08:00 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec1 := rpcClient(t, s1)
|
|
|
|
defer codec1.Close()
|
2014-04-21 19:08:00 +00:00
|
|
|
|
|
|
|
dir2, s2 := testServerDCBootstrap(t, "dc1", false)
|
|
|
|
defer os.RemoveAll(dir2)
|
|
|
|
defer s2.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec2 := rpcClient(t, s2)
|
|
|
|
defer codec2.Close()
|
2014-04-21 19:08:00 +00:00
|
|
|
|
|
|
|
// Try to join
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d",
|
|
|
|
s1.config.SerfLANConfig.MemberlistConfig.BindPort)
|
|
|
|
if _, err := s2.JoinLAN([]string{addr}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
testutil.WaitForLeader(t, s2.RPC, "dc1")
|
2014-04-21 19:08:00 +00:00
|
|
|
|
|
|
|
// Use the leader as the client, kill the follower
|
2015-10-13 23:43:52 +00:00
|
|
|
var codec rpc.ClientCodec
|
2014-04-21 19:08:00 +00:00
|
|
|
if s1.IsLeader() {
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = codec1
|
2014-04-21 19:08:00 +00:00
|
|
|
} else {
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = codec2
|
2014-04-21 19:08:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{RequireConsistent: true},
|
|
|
|
}
|
|
|
|
var out structs.IndexedNodes
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out); err != nil {
|
2014-04-21 19:08:00 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if out.QueryMeta.LastContact != 0 {
|
|
|
|
t.Fatalf("should not have a last contact time")
|
|
|
|
}
|
|
|
|
if !out.QueryMeta.KnownLeader {
|
|
|
|
t.Fatalf("should have known leader")
|
2014-04-19 00:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListNodes_DistanceSort(t *testing.T) {
|
2015-06-30 21:25:40 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-06-30 21:25:40 +00:00
|
|
|
|
2015-10-15 23:07:16 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "aaa", Address: "127.0.0.1"}); err != nil {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(2, &structs.Node{Node: "foo", Address: "127.0.0.2"}); err != nil {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(3, &structs.Node{Node: "bar", Address: "127.0.0.3"}); err != nil {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(4, &structs.Node{Node: "baz", Address: "127.0.0.4"}); err != nil {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set all but one of the nodes to known coordinates.
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := structs.Coordinates{
|
2017-03-14 05:56:24 +00:00
|
|
|
{"foo", lib.GenerateCoordinate(2 * time.Millisecond)},
|
|
|
|
{"bar", lib.GenerateCoordinate(5 * time.Millisecond)},
|
|
|
|
{"baz", lib.GenerateCoordinate(1 * time.Millisecond)},
|
2015-06-30 21:25:40 +00:00
|
|
|
}
|
|
|
|
if err := s1.fsm.State().CoordinateBatchUpdate(5, updates); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query with no given source node, should get the natural order from
|
|
|
|
// the index.
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.IndexedNodes
|
2017-03-23 22:27:16 +00:00
|
|
|
if err := testutil.WaitForResult(func() (bool, error) {
|
2015-10-15 23:07:16 +00:00
|
|
|
msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out)
|
2015-06-30 21:25:40 +00:00
|
|
|
return len(out.Nodes) == 5, nil
|
2017-03-23 22:27:16 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-30 21:25:40 +00:00
|
|
|
if out.Nodes[0].Node != "aaa" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[1].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[2].Node != "baz" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[3].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[4].Node != s1.config.NodeName {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query relative to foo, note that there's no known coordinate for the
|
|
|
|
// default-added Serf node nor "aaa" so they will go at the end.
|
|
|
|
args = structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Source: structs.QuerySource{Datacenter: "dc1", Node: "foo"},
|
|
|
|
}
|
2017-03-23 22:27:16 +00:00
|
|
|
if err := testutil.WaitForResult(func() (bool, error) {
|
2015-10-15 23:07:16 +00:00
|
|
|
msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out)
|
2015-06-30 21:25:40 +00:00
|
|
|
return len(out.Nodes) == 5, nil
|
2017-03-23 22:27:16 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-06-30 21:25:40 +00:00
|
|
|
if out.Nodes[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[1].Node != "baz" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[2].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[3].Node != "aaa" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Nodes[4].Node != s1.config.NodeName {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListNodes_ACLFilter(t *testing.T) {
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
c.ACLEnforceVersion8 = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// We scope the reply in each of these since msgpack won't clear out an
|
|
|
|
// existing slice if the incoming one is nil, so it's best to start
|
|
|
|
// clean each time.
|
|
|
|
|
|
|
|
// Prior to version 8, the node policy should be ignored.
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
{
|
|
|
|
reply := structs.IndexedNodes{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(reply.Nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", reply.Nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now turn on version 8 enforcement and try again.
|
|
|
|
s1.config.ACLEnforceVersion8 = true
|
|
|
|
{
|
|
|
|
reply := structs.IndexedNodes{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(reply.Nodes) != 0 {
|
|
|
|
t.Fatalf("bad: %v", reply.Nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an ACL that can read the node.
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: fmt.Sprintf(`
|
|
|
|
node "%s" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`, s1.config.NodeName),
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var id string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try with the token and it will go through.
|
|
|
|
args.Token = id
|
|
|
|
{
|
|
|
|
reply := structs.IndexedNodes{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(reply.Nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", reply.Nodes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_Catalog_ListNodes(t *testing.B) {
|
2014-02-01 20:16:33 +00:00
|
|
|
dir1, s1 := testServer(nil)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(nil, s1)
|
|
|
|
defer codec.Close()
|
2014-02-01 20:16:33 +00:00
|
|
|
|
|
|
|
// Just add a node
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-01 20:16:33 +00:00
|
|
|
|
2014-02-05 19:00:43 +00:00
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2014-02-01 20:16:33 +00:00
|
|
|
for i := 0; i < t.N; i++ {
|
2014-02-05 19:00:43 +00:00
|
|
|
var out structs.IndexedNodes
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListNodes", &args, &out); err != nil {
|
2014-02-01 20:16:33 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListServices(t *testing.T) {
|
2013-12-12 19:07:14 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-12 19:07:14 +00:00
|
|
|
|
2014-02-05 19:00:43 +00:00
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.IndexedServices
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out)
|
2016-07-10 17:24:18 +00:00
|
|
|
if err != nil {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2013-12-12 19:07:14 +00:00
|
|
|
|
|
|
|
// Just add a node
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-12 19:07:14 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out); err != nil {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 19:00:43 +00:00
|
|
|
if len(out.Services) != 2 {
|
2014-01-10 01:57:13 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2014-05-13 07:18:36 +00:00
|
|
|
for _, s := range out.Services {
|
|
|
|
if s == nil {
|
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 01:57:13 +00:00
|
|
|
// Consul service should auto-register
|
2014-04-03 19:03:10 +00:00
|
|
|
if _, ok := out.Services["consul"]; !ok {
|
2014-02-05 19:00:43 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if len(out.Services["db"]) != 1 {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Services["db"][0] != "primary" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestCatalog_ListServices_NodeMetaFilter(t *testing.T) {
|
2017-01-09 19:21:49 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Add a new node with the right meta k/v pair
|
|
|
|
node := &structs.Node{Node: "foo", Address: "127.0.0.1", Meta: map[string]string{"somekey": "somevalue"}}
|
|
|
|
if err := s1.fsm.State().EnsureNode(1, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
// Add a service to the new node
|
|
|
|
if err := s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-11 19:41:12 +00:00
|
|
|
// Filter by a specific meta k/v pair
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
NodeMetaFilters: map[string]string{
|
|
|
|
"somekey": "somevalue",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var out structs.IndexedServices
|
2017-01-09 19:21:49 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.Services) != 1 {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Services["db"] == nil {
|
|
|
|
t.Fatalf("bad: %v", out.Services["db"])
|
|
|
|
}
|
|
|
|
if len(out.Services["db"]) != 1 {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.Services["db"][0] != "primary" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now filter on a nonexistent meta k/v pair
|
|
|
|
args = structs.DCSpecificRequest{
|
2017-01-11 19:41:12 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
NodeMetaFilters: map[string]string{
|
|
|
|
"somekey": "invalid",
|
|
|
|
},
|
2017-01-09 19:21:49 +00:00
|
|
|
}
|
|
|
|
out = structs.IndexedServices{}
|
2017-01-11 19:41:12 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out)
|
2017-01-09 19:21:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should get an empty list of nodes back
|
|
|
|
if len(out.Services) != 0 {
|
|
|
|
t.Fatalf("bad: %v", out.Services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListServices_Blocking(t *testing.T) {
|
2014-02-05 19:00:43 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-02-05 19:00:43 +00:00
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.IndexedServices
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-02-05 19:00:43 +00:00
|
|
|
|
|
|
|
// Run the query
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out); err != nil {
|
2014-02-05 19:00:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a blocking query
|
|
|
|
args.MinQueryIndex = out.Index
|
|
|
|
args.MaxQueryTime = time.Second
|
|
|
|
|
|
|
|
// Async cause a change
|
2015-10-12 07:42:09 +00:00
|
|
|
idx := out.Index
|
2014-02-05 19:00:43 +00:00
|
|
|
start := time.Now()
|
|
|
|
go func() {
|
2014-05-09 00:36:25 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(idx+1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureService(idx+2, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-05 19:00:43 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Re-run the query
|
|
|
|
out = structs.IndexedServices{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out); err != nil {
|
2014-02-05 19:00:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should block at least 100ms
|
2014-05-16 22:49:47 +00:00
|
|
|
if time.Now().Sub(start) < 100*time.Millisecond {
|
2014-02-05 19:00:43 +00:00
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the indexes
|
2015-10-12 07:42:09 +00:00
|
|
|
if out.Index != idx+2 {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2014-02-05 19:00:43 +00:00
|
|
|
|
|
|
|
// Should find the service
|
|
|
|
if len(out.Services) != 2 {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2014-02-05 19:00:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListServices_Timeout(t *testing.T) {
|
2014-02-05 19:00:43 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-02-05 19:00:43 +00:00
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.IndexedServices
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-02-05 19:00:43 +00:00
|
|
|
|
|
|
|
// Run the query
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out); err != nil {
|
2014-02-05 19:00:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a blocking query
|
|
|
|
args.MinQueryIndex = out.Index
|
2014-05-09 00:36:25 +00:00
|
|
|
args.MaxQueryTime = 100 * time.Millisecond
|
2014-02-05 19:00:43 +00:00
|
|
|
|
|
|
|
// Re-run the query
|
|
|
|
start := time.Now()
|
|
|
|
out = structs.IndexedServices{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out); err != nil {
|
2014-02-05 19:00:43 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should block at least 100ms
|
2014-05-16 22:49:47 +00:00
|
|
|
if time.Now().Sub(start) < 100*time.Millisecond {
|
2014-02-05 19:00:43 +00:00
|
|
|
t.Fatalf("too fast")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the indexes, should not change
|
|
|
|
if out.Index != args.MinQueryIndex {
|
2013-12-12 19:07:14 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 19:37:19 +00:00
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListServices_Stale(t *testing.T) {
|
2014-04-21 18:57:39 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-04-21 18:57:39 +00:00
|
|
|
|
|
|
|
args := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
args.AllowStale = true
|
|
|
|
var out structs.IndexedServices
|
|
|
|
|
|
|
|
// Inject a fake service
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-04-21 18:57:39 +00:00
|
|
|
|
|
|
|
// Run the query, do not wait for leader!
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &args, &out); err != nil {
|
2014-04-21 18:57:39 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should find the service
|
|
|
|
if len(out.Services) != 1 {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not have a leader! Stale read
|
|
|
|
if out.KnownLeader {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListServiceNodes(t *testing.T) {
|
2013-12-12 19:37:19 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-12 19:37:19 +00:00
|
|
|
|
2014-01-08 21:52:09 +00:00
|
|
|
args := structs.ServiceSpecificRequest{
|
2013-12-12 19:37:19 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "db",
|
|
|
|
ServiceTag: "slave",
|
|
|
|
TagFilter: false,
|
|
|
|
}
|
2014-02-05 19:10:10 +00:00
|
|
|
var out structs.IndexedServiceNodes
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out)
|
2016-07-10 17:24:18 +00:00
|
|
|
if err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2013-12-12 19:37:19 +00:00
|
|
|
|
|
|
|
// Just add a node
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-12 19:37:19 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 19:10:10 +00:00
|
|
|
if len(out.ServiceNodes) != 1 {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a filter
|
|
|
|
args.TagFilter = true
|
2014-02-05 19:10:10 +00:00
|
|
|
out = structs.IndexedServiceNodes{}
|
2013-12-12 19:37:19 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out); err != nil {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-02-05 19:10:10 +00:00
|
|
|
if len(out.ServiceNodes) != 0 {
|
2013-12-12 19:37:19 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
2013-12-12 19:46:25 +00:00
|
|
|
|
2017-01-14 01:08:43 +00:00
|
|
|
func TestCatalog_ListServiceNodes_NodeMetaFilter(t *testing.T) {
|
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Add 2 nodes with specific meta maps
|
|
|
|
node := &structs.Node{Node: "foo", Address: "127.0.0.1", Meta: map[string]string{"somekey": "somevalue", "common": "1"}}
|
|
|
|
if err := s1.fsm.State().EnsureNode(1, node); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
node2 := &structs.Node{Node: "bar", Address: "127.0.0.2", Meta: map[string]string{"common": "1"}}
|
|
|
|
if err := s1.fsm.State().EnsureNode(2, node2); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := s1.fsm.State().EnsureService(3, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := s1.fsm.State().EnsureService(4, "bar", &structs.NodeService{ID: "db2", Service: "db", Tags: []string{"secondary"}, Address: "127.0.0.2", Port: 5000}); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
filters map[string]string
|
|
|
|
tag string
|
|
|
|
services structs.ServiceNodes
|
|
|
|
}{
|
|
|
|
// Basic meta filter
|
|
|
|
{
|
|
|
|
filters: map[string]string{"somekey": "somevalue"},
|
|
|
|
services: structs.ServiceNodes{&structs.ServiceNode{Node: "foo", ServiceID: "db"}},
|
|
|
|
},
|
|
|
|
// Basic meta filter, tag
|
|
|
|
{
|
|
|
|
filters: map[string]string{"somekey": "somevalue"},
|
|
|
|
tag: "primary",
|
|
|
|
services: structs.ServiceNodes{&structs.ServiceNode{Node: "foo", ServiceID: "db"}},
|
|
|
|
},
|
|
|
|
// Common meta filter
|
|
|
|
{
|
|
|
|
filters: map[string]string{"common": "1"},
|
|
|
|
services: structs.ServiceNodes{
|
|
|
|
&structs.ServiceNode{Node: "bar", ServiceID: "db2"},
|
|
|
|
&structs.ServiceNode{Node: "foo", ServiceID: "db"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Common meta filter, tag
|
|
|
|
{
|
|
|
|
filters: map[string]string{"common": "1"},
|
|
|
|
tag: "secondary",
|
|
|
|
services: structs.ServiceNodes{
|
|
|
|
&structs.ServiceNode{Node: "bar", ServiceID: "db2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Invalid meta filter
|
|
|
|
{
|
|
|
|
filters: map[string]string{"invalid": "nope"},
|
|
|
|
services: structs.ServiceNodes{},
|
|
|
|
},
|
|
|
|
// Multiple filter values
|
|
|
|
{
|
|
|
|
filters: map[string]string{"somekey": "somevalue", "common": "1"},
|
|
|
|
services: structs.ServiceNodes{&structs.ServiceNode{Node: "foo", ServiceID: "db"}},
|
|
|
|
},
|
|
|
|
// Multiple filter values, tag
|
|
|
|
{
|
|
|
|
filters: map[string]string{"somekey": "somevalue", "common": "1"},
|
|
|
|
tag: "primary",
|
|
|
|
services: structs.ServiceNodes{&structs.ServiceNode{Node: "foo", ServiceID: "db"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
args := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
NodeMetaFilters: tc.filters,
|
|
|
|
ServiceName: "db",
|
|
|
|
ServiceTag: tc.tag,
|
|
|
|
TagFilter: tc.tag != "",
|
|
|
|
}
|
|
|
|
var out structs.IndexedServiceNodes
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.ServiceNodes) != len(tc.services) {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, serviceNode := range out.ServiceNodes {
|
|
|
|
if serviceNode.Node != tc.services[i].Node || serviceNode.ServiceID != tc.services[i].ServiceID {
|
|
|
|
t.Fatalf("bad: %v, %v filters: %v", serviceNode, tc.services[i], tc.filters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_ListServiceNodes_DistanceSort(t *testing.T) {
|
2015-06-30 21:25:40 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-15 23:07:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2015-06-30 21:25:40 +00:00
|
|
|
|
|
|
|
args := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "db",
|
|
|
|
}
|
|
|
|
var out structs.IndexedServiceNodes
|
2015-10-15 23:07:16 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out)
|
2016-07-10 17:24:18 +00:00
|
|
|
if err != nil {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:07:16 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2015-06-30 21:25:40 +00:00
|
|
|
|
|
|
|
// Add a few nodes for the associated services.
|
2015-10-23 22:19:14 +00:00
|
|
|
s1.fsm.State().EnsureNode(1, &structs.Node{Node: "aaa", Address: "127.0.0.1"})
|
|
|
|
s1.fsm.State().EnsureService(2, "aaa", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000})
|
|
|
|
s1.fsm.State().EnsureNode(3, &structs.Node{Node: "foo", Address: "127.0.0.2"})
|
|
|
|
s1.fsm.State().EnsureService(4, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.2", Port: 5000})
|
|
|
|
s1.fsm.State().EnsureNode(5, &structs.Node{Node: "bar", Address: "127.0.0.3"})
|
|
|
|
s1.fsm.State().EnsureService(6, "bar", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.3", Port: 5000})
|
|
|
|
s1.fsm.State().EnsureNode(7, &structs.Node{Node: "baz", Address: "127.0.0.4"})
|
|
|
|
s1.fsm.State().EnsureService(8, "baz", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.4", Port: 5000})
|
2015-06-30 21:25:40 +00:00
|
|
|
|
|
|
|
// Set all but one of the nodes to known coordinates.
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := structs.Coordinates{
|
2017-03-14 05:56:24 +00:00
|
|
|
{"foo", lib.GenerateCoordinate(2 * time.Millisecond)},
|
|
|
|
{"bar", lib.GenerateCoordinate(5 * time.Millisecond)},
|
|
|
|
{"baz", lib.GenerateCoordinate(1 * time.Millisecond)},
|
2015-06-30 21:25:40 +00:00
|
|
|
}
|
|
|
|
if err := s1.fsm.State().CoordinateBatchUpdate(9, updates); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query with no given source node, should get the natural order from
|
|
|
|
// the index.
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out); err != nil {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out.ServiceNodes) != 4 {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.ServiceNodes[0].Node != "aaa" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if out.ServiceNodes[1].Node != "bar" {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if out.ServiceNodes[2].Node != "baz" {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if out.ServiceNodes[3].Node != "foo" {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query relative to foo, note that there's no known coordinate for "aaa"
|
|
|
|
// so it will go at the end.
|
|
|
|
args = structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "db",
|
|
|
|
Source: structs.QuerySource{Datacenter: "dc1", Node: "foo"},
|
|
|
|
}
|
2015-10-15 23:07:16 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &args, &out); err != nil {
|
2015-06-30 21:25:40 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(out.ServiceNodes) != 4 {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.ServiceNodes[0].Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.ServiceNodes[1].Node != "baz" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.ServiceNodes[2].Node != "bar" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
if out.ServiceNodes[3].Node != "aaa" {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:04:00 +00:00
|
|
|
func TestCatalog_NodeServices(t *testing.T) {
|
2013-12-12 19:46:25 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2013-12-12 19:46:25 +00:00
|
|
|
|
2014-01-08 21:52:09 +00:00
|
|
|
args := structs.NodeSpecificRequest{
|
2013-12-12 19:46:25 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
}
|
2014-02-05 19:10:10 +00:00
|
|
|
var out structs.IndexedNodeServices
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.NodeServices", &args, &out)
|
2016-07-10 17:24:18 +00:00
|
|
|
if err != nil {
|
2013-12-12 19:46:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2013-12-12 19:46:25 +00:00
|
|
|
|
|
|
|
// Just add a node
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-13 05:21:39 +00:00
|
|
|
if err := s1.fsm.State().EnsureService(3, "foo", &structs.NodeService{ID: "web", Service: "web", Tags: nil, Address: "127.0.0.1", Port: 80}); err != nil {
|
2015-10-12 07:42:09 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-12 19:46:25 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.NodeServices", &args, &out); err != nil {
|
2013-12-12 19:46:25 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-02-05 19:10:10 +00:00
|
|
|
if out.NodeServices.Node.Address != "127.0.0.1" {
|
2014-01-03 01:29:39 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2014-02-05 19:10:10 +00:00
|
|
|
if len(out.NodeServices.Services) != 2 {
|
2013-12-12 19:46:25 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2014-02-05 19:10:10 +00:00
|
|
|
services := out.NodeServices.Services
|
2016-01-29 19:42:34 +00:00
|
|
|
if !lib.StrContains(services["db"].Tags, "primary") || services["db"].Port != 5000 {
|
2013-12-12 19:46:25 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
2014-10-09 18:54:47 +00:00
|
|
|
if len(services["web"].Tags) != 0 || services["web"].Port != 80 {
|
2013-12-12 19:46:25 +00:00
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
}
|
2014-01-01 02:31:17 +00:00
|
|
|
|
|
|
|
// Used to check for a regression against a known bug
|
2016-12-11 00:00:11 +00:00
|
|
|
func TestCatalog_Register_FailedCase1(t *testing.T) {
|
2014-01-01 02:31:17 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
2014-01-01 02:31:17 +00:00
|
|
|
|
|
|
|
arg := structs.RegisterRequest{
|
2014-01-08 21:39:40 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "bar",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
Service: "web",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: nil,
|
2014-01-08 21:39:40 +00:00
|
|
|
Port: 8000,
|
|
|
|
},
|
2014-01-01 02:31:17 +00:00
|
|
|
}
|
|
|
|
var out struct{}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out)
|
2016-07-10 17:24:18 +00:00
|
|
|
if err != nil {
|
2014-01-01 02:31:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
2014-01-01 02:31:17 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", &arg, &out); err != nil {
|
2014-01-01 02:31:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check we can get this back
|
2014-01-08 21:52:09 +00:00
|
|
|
query := &structs.ServiceSpecificRequest{
|
2014-01-01 02:31:17 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "web",
|
|
|
|
}
|
2014-02-05 19:10:10 +00:00
|
|
|
var out2 structs.IndexedServiceNodes
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", query, &out2); err != nil {
|
2014-01-01 02:31:17 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the output
|
2014-02-05 19:10:10 +00:00
|
|
|
if len(out2.ServiceNodes) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", out2)
|
2014-01-01 02:31:17 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-01 04:05:15 +00:00
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
func testACLFilterServer(t *testing.T) (dir, token string, srv *Server, codec rpc.ClientCodec) {
|
2015-06-11 20:23:49 +00:00
|
|
|
dir, srv = testServerWithConfig(t, func(c *Config) {
|
2015-06-11 05:14:58 +00:00
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
2016-12-11 21:22:14 +00:00
|
|
|
c.ACLEnforceVersion8 = false
|
2015-06-11 05:14:58 +00:00
|
|
|
})
|
|
|
|
|
2015-10-13 23:43:52 +00:00
|
|
|
codec = rpcClient(t, srv)
|
|
|
|
testutil.WaitForLeader(t, srv.RPC, "dc1")
|
2015-06-11 05:14:58 +00:00
|
|
|
|
|
|
|
// Create a new token
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
2016-12-10 03:15:44 +00:00
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: `
|
|
|
|
service "foo" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
`,
|
2015-06-11 05:14:58 +00:00
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &token); err != nil {
|
2015-06-11 05:14:58 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a service
|
|
|
|
regArg := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: srv.config.NodeName,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "foo",
|
|
|
|
Service: "foo",
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "service:foo",
|
|
|
|
Name: "service:foo",
|
|
|
|
ServiceID: "foo",
|
2015-07-27 23:57:56 +00:00
|
|
|
Status: structs.HealthPassing,
|
2015-06-11 05:14:58 +00:00
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", ®Arg, nil); err != nil {
|
2015-06-11 05:14:58 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a service which should be denied
|
|
|
|
regArg = structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: srv.config.NodeName,
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "bar",
|
|
|
|
Service: "bar",
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
CheckID: "service:bar",
|
|
|
|
Name: "service:bar",
|
|
|
|
ServiceID: "bar",
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.Register", ®Arg, nil); err != nil {
|
2015-06-11 05:14:58 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
return
|
|
|
|
}
|
2015-06-11 05:14:58 +00:00
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
func TestCatalog_ListServices_FilterACL(t *testing.T) {
|
2015-10-13 23:43:52 +00:00
|
|
|
dir, token, srv, codec := testACLFilterServer(t)
|
2015-06-11 20:23:49 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
defer codec.Close()
|
2015-06-11 20:23:49 +00:00
|
|
|
|
|
|
|
opt := structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
2015-06-11 05:14:58 +00:00
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
reply := structs.IndexedServices{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ListServices", &opt, &reply); err != nil {
|
2015-06-11 20:23:49 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if _, ok := reply.Services["foo"]; !ok {
|
|
|
|
t.Fatalf("bad: %#v", reply.Services)
|
|
|
|
}
|
|
|
|
if _, ok := reply.Services["bar"]; ok {
|
|
|
|
t.Fatalf("bad: %#v", reply.Services)
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 05:14:58 +00:00
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
func TestCatalog_ServiceNodes_FilterACL(t *testing.T) {
|
2015-10-13 23:43:52 +00:00
|
|
|
dir, token, srv, codec := testACLFilterServer(t)
|
2015-06-11 20:23:49 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
defer codec.Close()
|
2015-06-11 20:23:49 +00:00
|
|
|
|
|
|
|
opt := structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "foo",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
reply := structs.IndexedServiceNodes{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &opt, &reply); err != nil {
|
2015-06-11 20:23:49 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, sn := range reply.ServiceNodes {
|
|
|
|
if sn.ServiceID == "foo" {
|
|
|
|
found = true
|
|
|
|
break
|
2015-06-11 05:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
if !found {
|
|
|
|
t.Fatalf("bad: %#v", reply.ServiceNodes)
|
|
|
|
}
|
2015-06-11 05:14:58 +00:00
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
// Filters services we can't access
|
|
|
|
opt = structs.ServiceSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ServiceName: "bar",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
reply = structs.IndexedServiceNodes{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.ServiceNodes", &opt, &reply); err != nil {
|
2015-06-11 20:23:49 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
for _, sn := range reply.ServiceNodes {
|
|
|
|
if sn.ServiceID == "bar" {
|
|
|
|
t.Fatalf("bad: %#v", reply.ServiceNodes)
|
2015-06-11 05:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-11 21:22:14 +00:00
|
|
|
|
|
|
|
// We've already proven that we call the ACL filtering function so we
|
|
|
|
// test node filtering down in acl.go for node cases. This also proves
|
|
|
|
// that we respect the version 8 ACL flag, since the test server sets
|
|
|
|
// that to false (the regression value of *not* changing this is better
|
|
|
|
// for now until we change the sense of the version 8 ACL flag).
|
2015-06-11 20:23:49 +00:00
|
|
|
}
|
2015-06-11 05:14:58 +00:00
|
|
|
|
2016-12-10 05:04:00 +00:00
|
|
|
func TestCatalog_NodeServices_ACLDeny(t *testing.T) {
|
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
c.ACLEnforceVersion8 = false
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testutil.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Prior to version 8, the node policy should be ignored.
|
|
|
|
args := structs.NodeSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: s1.config.NodeName,
|
|
|
|
}
|
|
|
|
reply := structs.IndexedNodeServices{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.NodeServices", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-12-11 00:00:11 +00:00
|
|
|
if reply.NodeServices == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
2016-12-10 05:04:00 +00:00
|
|
|
|
|
|
|
// Now turn on version 8 enforcement and try again.
|
|
|
|
s1.config.ACLEnforceVersion8 = true
|
2016-12-13 00:53:31 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.NodeServices", &args, &reply); err != nil {
|
2016-12-10 05:04:00 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-12-13 00:53:31 +00:00
|
|
|
if reply.NodeServices != nil {
|
|
|
|
t.Fatalf("should not nil")
|
|
|
|
}
|
2016-12-10 05:04:00 +00:00
|
|
|
|
|
|
|
// Create an ACL that can read the node.
|
|
|
|
arg := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: fmt.Sprintf(`
|
|
|
|
node "%s" {
|
2016-12-11 00:00:11 +00:00
|
|
|
policy = "read"
|
2016-12-10 05:04:00 +00:00
|
|
|
}
|
|
|
|
`, s1.config.NodeName),
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
|
|
|
var id string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "ACL.Apply", &arg, &id); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try with the token and it will go through.
|
|
|
|
args.Token = id
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.NodeServices", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-12-11 00:00:11 +00:00
|
|
|
if reply.NodeServices == nil {
|
|
|
|
t.Fatalf("should not be nil")
|
|
|
|
}
|
2016-12-13 00:53:31 +00:00
|
|
|
|
|
|
|
// Make sure an unknown node doesn't cause trouble.
|
|
|
|
args.Node = "nope"
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.NodeServices", &args, &reply); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if reply.NodeServices != nil {
|
|
|
|
t.Fatalf("should not nil")
|
|
|
|
}
|
2016-12-10 05:04:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-11 20:23:49 +00:00
|
|
|
func TestCatalog_NodeServices_FilterACL(t *testing.T) {
|
2015-10-13 23:43:52 +00:00
|
|
|
dir, token, srv, codec := testACLFilterServer(t)
|
2015-06-11 20:23:49 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
defer srv.Shutdown()
|
2015-10-13 23:43:52 +00:00
|
|
|
defer codec.Close()
|
2015-06-11 20:23:49 +00:00
|
|
|
|
|
|
|
opt := structs.NodeSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: srv.config.NodeName,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
reply := structs.IndexedNodeServices{}
|
2015-10-13 23:43:52 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Catalog.NodeServices", &opt, &reply); err != nil {
|
2015-06-11 20:23:49 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, svc := range reply.NodeServices.Services {
|
|
|
|
if svc.ID == "bar" {
|
|
|
|
t.Fatalf("bad: %#v", reply.NodeServices.Services)
|
2015-06-11 05:14:58 +00:00
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
if svc.ID == "foo" {
|
|
|
|
found = true
|
|
|
|
break
|
2015-06-11 05:14:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-11 20:23:49 +00:00
|
|
|
if !found {
|
|
|
|
t.Fatalf("bad: %#v", reply.NodeServices)
|
|
|
|
}
|
2015-06-11 05:14:58 +00:00
|
|
|
}
|