2013-12-11 22:38:18 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2013-12-16 18:47:14 +00:00
|
|
|
"bytes"
|
2013-12-19 20:03:57 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2014-01-06 22:18:38 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2014-02-03 23:21:56 +00:00
|
|
|
"os"
|
2013-12-11 22:38:18 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-12-16 18:47:14 +00:00
|
|
|
type MockSink struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
cancel bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) ID() string {
|
|
|
|
return "Mock"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) Cancel() error {
|
|
|
|
m.cancel = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockSink) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
func makeLog(buf []byte) *raft.Log {
|
|
|
|
return &raft.Log{
|
|
|
|
Index: 1,
|
|
|
|
Term: 1,
|
|
|
|
Type: raft.LogCommand,
|
|
|
|
Data: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:38:18 +00:00
|
|
|
func TestFSM_RegisterNode(t *testing.T) {
|
2014-02-03 23:21:56 +00:00
|
|
|
fsm, err := NewFSM(os.Stderr)
|
2013-12-11 22:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
req := structs.RegisterRequest{
|
2013-12-11 23:34:10 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
2013-12-19 20:03:57 +00:00
|
|
|
buf, err := structs.Encode(structs.RegisterRequestType, req)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2013-12-11 23:34:10 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
2014-02-05 02:33:15 +00:00
|
|
|
if idx, found, _ := fsm.state.GetNode("foo"); !found {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("not found!")
|
2014-02-05 02:33:15 +00:00
|
|
|
} else if idx != 1 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify service registered
|
2014-02-05 02:33:15 +00:00
|
|
|
_, services := fsm.state.NodeServices("foo")
|
2014-01-03 01:29:39 +00:00
|
|
|
if len(services.Services) != 0 {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("Services: %v", services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_RegisterNode_Service(t *testing.T) {
|
2014-02-03 23:21:56 +00:00
|
|
|
fsm, err := NewFSM(os.Stderr)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
req := structs.RegisterRequest{
|
2014-01-08 21:39:40 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
|
|
|
Tag: "master",
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db connectivity",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db",
|
|
|
|
},
|
2013-12-11 22:38:18 +00:00
|
|
|
}
|
2013-12-19 20:03:57 +00:00
|
|
|
buf, err := structs.Encode(structs.RegisterRequestType, req)
|
2013-12-11 22:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2013-12-11 22:38:18 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
2014-02-05 02:33:15 +00:00
|
|
|
if _, found, _ := fsm.state.GetNode("foo"); !found {
|
2013-12-11 22:38:18 +00:00
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify service registered
|
2014-02-05 02:33:15 +00:00
|
|
|
_, services := fsm.state.NodeServices("foo")
|
2014-01-03 01:29:39 +00:00
|
|
|
if _, ok := services.Services["db"]; !ok {
|
2013-12-11 22:38:18 +00:00
|
|
|
t.Fatalf("not registered!")
|
|
|
|
}
|
2014-01-08 21:39:40 +00:00
|
|
|
|
|
|
|
// Verify check
|
2014-02-05 02:33:15 +00:00
|
|
|
_, checks := fsm.state.NodeChecks("foo")
|
2014-01-08 21:39:40 +00:00
|
|
|
if checks[0].CheckID != "db" {
|
|
|
|
t.Fatalf("not registered!")
|
|
|
|
}
|
2013-12-11 22:38:18 +00:00
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
|
|
|
|
func TestFSM_DeregisterService(t *testing.T) {
|
2014-02-03 23:21:56 +00:00
|
|
|
fsm, err := NewFSM(os.Stderr)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
req := structs.RegisterRequest{
|
2014-01-08 21:39:40 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
|
|
|
Tag: "master",
|
|
|
|
Port: 8000,
|
|
|
|
},
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
2013-12-19 20:03:57 +00:00
|
|
|
buf, err := structs.Encode(structs.RegisterRequestType, req)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2013-12-11 23:34:10 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
dereg := structs.DeregisterRequest{
|
2014-01-06 22:18:38 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
ServiceID: "db",
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
2013-12-19 20:03:57 +00:00
|
|
|
buf, err = structs.Encode(structs.DeregisterRequestType, dereg)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
resp = fsm.Apply(makeLog(buf))
|
2013-12-11 23:34:10 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
2014-02-05 02:33:15 +00:00
|
|
|
if _, found, _ := fsm.state.GetNode("foo"); !found {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify service not registered
|
2014-02-05 02:33:15 +00:00
|
|
|
_, services := fsm.state.NodeServices("foo")
|
2014-01-03 01:29:39 +00:00
|
|
|
if _, ok := services.Services["db"]; ok {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("db registered!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-08 21:39:40 +00:00
|
|
|
func TestFSM_DeregisterCheck(t *testing.T) {
|
2014-02-03 23:21:56 +00:00
|
|
|
fsm, err := NewFSM(os.Stderr)
|
2014-01-08 21:39:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.RegisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "mem",
|
|
|
|
Name: "memory util",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.RegisterRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
dereg := structs.DeregisterRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "mem",
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.DeregisterRequestType, dereg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
2014-02-05 02:33:15 +00:00
|
|
|
if _, found, _ := fsm.state.GetNode("foo"); !found {
|
2014-01-08 21:39:40 +00:00
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify check not registered
|
2014-02-05 02:33:15 +00:00
|
|
|
_, checks := fsm.state.NodeChecks("foo")
|
2014-01-08 21:39:40 +00:00
|
|
|
if len(checks) != 0 {
|
|
|
|
t.Fatalf("check registered!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 23:34:10 +00:00
|
|
|
func TestFSM_DeregisterNode(t *testing.T) {
|
2014-02-03 23:21:56 +00:00
|
|
|
fsm, err := NewFSM(os.Stderr)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
req := structs.RegisterRequest{
|
2014-01-08 21:39:40 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Service: &structs.NodeService{
|
|
|
|
ID: "db",
|
|
|
|
Service: "db",
|
|
|
|
Tag: "master",
|
|
|
|
Port: 8000,
|
|
|
|
},
|
|
|
|
Check: &structs.HealthCheck{
|
|
|
|
Node: "foo",
|
|
|
|
CheckID: "db",
|
|
|
|
Name: "db connectivity",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "db",
|
|
|
|
},
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
2013-12-19 20:03:57 +00:00
|
|
|
buf, err := structs.Encode(structs.RegisterRequestType, req)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2013-12-11 23:34:10 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2013-12-19 20:03:57 +00:00
|
|
|
dereg := structs.DeregisterRequest{
|
2013-12-11 23:34:10 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Node: "foo",
|
|
|
|
}
|
2013-12-19 20:03:57 +00:00
|
|
|
buf, err = structs.Encode(structs.DeregisterRequestType, dereg)
|
2013-12-11 23:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-06 22:18:38 +00:00
|
|
|
resp = fsm.Apply(makeLog(buf))
|
2013-12-11 23:34:10 +00:00
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are registered
|
2014-02-05 02:33:15 +00:00
|
|
|
if _, found, _ := fsm.state.GetNode("foo"); found {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify service not registered
|
2014-02-05 02:33:15 +00:00
|
|
|
_, services := fsm.state.NodeServices("foo")
|
2014-01-03 01:29:39 +00:00
|
|
|
if len(services.Services) != 0 {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("Services: %v", services)
|
|
|
|
}
|
2014-01-08 21:39:40 +00:00
|
|
|
|
|
|
|
// Verify checks not registered
|
2014-02-05 02:33:15 +00:00
|
|
|
_, checks := fsm.state.NodeChecks("foo")
|
2014-01-08 21:39:40 +00:00
|
|
|
if len(checks) != 0 {
|
|
|
|
t.Fatalf("Services: %v", services)
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
2013-12-16 18:47:14 +00:00
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore(t *testing.T) {
|
2014-02-03 23:21:56 +00:00
|
|
|
fsm, err := NewFSM(os.Stderr)
|
2013-12-16 18:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add some state
|
2014-02-05 02:33:15 +00:00
|
|
|
fsm.state.EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
|
|
|
|
fsm.state.EnsureNode(2, structs.Node{"baz", "127.0.0.2"})
|
|
|
|
fsm.state.EnsureService(3, "foo", &structs.NodeService{"web", "web", "", 80})
|
|
|
|
fsm.state.EnsureService(4, "foo", &structs.NodeService{"db", "db", "primary", 5000})
|
|
|
|
fsm.state.EnsureService(5, "baz", &structs.NodeService{"web", "web", "", 80})
|
|
|
|
fsm.state.EnsureService(6, "baz", &structs.NodeService{"db", "db", "secondary", 5000})
|
|
|
|
fsm.state.EnsureCheck(7, &structs.HealthCheck{
|
2014-01-08 21:39:40 +00:00
|
|
|
Node: "foo",
|
|
|
|
CheckID: "web",
|
|
|
|
Name: "web connectivity",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "web",
|
|
|
|
})
|
2013-12-16 18:47:14 +00:00
|
|
|
|
|
|
|
// Snapshot
|
|
|
|
snap, err := fsm.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
|
|
|
|
// Persist
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
sink := &MockSink{buf, false}
|
|
|
|
if err := snap.Persist(sink); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to restore on a new FSM
|
2014-02-03 23:21:56 +00:00
|
|
|
fsm2, err := NewFSM(os.Stderr)
|
2013-12-16 18:47:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do a restore
|
|
|
|
if err := fsm2.Restore(sink); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the contents
|
2014-02-05 02:33:15 +00:00
|
|
|
_, nodes := fsm2.state.Nodes()
|
2014-01-08 18:31:42 +00:00
|
|
|
if len(nodes) != 2 {
|
2013-12-16 18:47:14 +00:00
|
|
|
t.Fatalf("Bad: %v", nodes)
|
|
|
|
}
|
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
_, fooSrv := fsm2.state.NodeServices("foo")
|
2014-01-03 01:29:39 +00:00
|
|
|
if len(fooSrv.Services) != 2 {
|
2013-12-16 18:47:14 +00:00
|
|
|
t.Fatalf("Bad: %v", fooSrv)
|
|
|
|
}
|
2014-01-03 01:29:39 +00:00
|
|
|
if fooSrv.Services["db"].Tag != "primary" {
|
2013-12-16 18:47:14 +00:00
|
|
|
t.Fatalf("Bad: %v", fooSrv)
|
|
|
|
}
|
2014-01-03 01:29:39 +00:00
|
|
|
if fooSrv.Services["db"].Port != 5000 {
|
2013-12-16 18:47:14 +00:00
|
|
|
t.Fatalf("Bad: %v", fooSrv)
|
|
|
|
}
|
2014-01-08 21:39:40 +00:00
|
|
|
|
2014-02-05 02:33:15 +00:00
|
|
|
_, checks := fsm2.state.NodeChecks("foo")
|
2014-01-08 21:39:40 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
2013-12-16 18:47:14 +00:00
|
|
|
}
|