2013-12-11 22:38:18 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2013-12-16 18:47:14 +00:00
|
|
|
"bytes"
|
2014-02-03 23:21:56 +00:00
|
|
|
"os"
|
2013-12-11 22:38:18 +00:00
|
|
|
"testing"
|
2014-10-09 18:54:47 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/raft"
|
2013-12-11 22:38:18 +00:00
|
|
|
)
|
|
|
|
|
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) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-11 22:38:18 +00:00
|
|
|
|
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
|
2015-10-09 17:25:19 +00:00
|
|
|
node, err := fsm.stateNew.GetNode("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if node == nil {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("not found!")
|
2015-10-09 17:25:19 +00:00
|
|
|
}
|
|
|
|
if node.ModifyIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", node.ModifyIndex)
|
2013-12-11 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify service registered
|
2015-10-09 17:25:19 +00:00
|
|
|
_, services, err := fsm.stateNew.NodeServices("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
|
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",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 21:39:40 +00:00
|
|
|
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
|
2015-10-09 17:25:19 +00:00
|
|
|
node, err := fsm.stateNew.GetNode("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if node == nil {
|
2013-12-11 22:38:18 +00:00
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify service registered
|
2015-10-09 17:25:19 +00:00
|
|
|
_, services, err := fsm.stateNew.NodeServices("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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
|
2015-10-09 17:25:19 +00:00
|
|
|
_, checks, err := fsm.stateNew.NodeChecks("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-11 23:34:10 +00:00
|
|
|
|
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",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 21:39:40 +00:00
|
|
|
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
|
2015-10-09 17:25:19 +00:00
|
|
|
node, err := fsm.stateNew.GetNode("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if node == nil {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify service not registered
|
2015-10-09 17:25:19 +00:00
|
|
|
_, services, err := fsm.stateNew.NodeServices("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, 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
|
2015-10-09 17:25:19 +00:00
|
|
|
node, err := fsm.stateNew.GetNode("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if node == nil {
|
2014-01-08 21:39:40 +00:00
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify check not registered
|
2015-10-09 17:25:19 +00:00
|
|
|
_, checks, err := fsm.stateNew.NodeChecks("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, 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",
|
2014-04-03 19:03:10 +00:00
|
|
|
Tags: []string{"master"},
|
2014-01-08 21:39:40 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:25:19 +00:00
|
|
|
// Verify we are not registered
|
|
|
|
node, err := fsm.stateNew.GetNode("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if node != nil {
|
2013-12-11 23:34:10 +00:00
|
|
|
t.Fatalf("found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify service not registered
|
2015-10-09 17:25:19 +00:00
|
|
|
_, services, err := fsm.stateNew.NodeServices("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-03-05 23:03:12 +00:00
|
|
|
if services != nil {
|
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
|
2015-10-09 17:25:19 +00:00
|
|
|
_, checks, err := fsm.stateNew.NodeChecks("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-16 18:47:14 +00:00
|
|
|
|
|
|
|
// Add some state
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
fsm.stateNew.EnsureNode(2, &structs.Node{Node: "baz", Address: "127.0.0.2"})
|
|
|
|
fsm.stateNew.EnsureService(3, "foo", &structs.NodeService{ID: "web", Service: "web", Tags: nil, Address: "127.0.0.1", Port: 80})
|
|
|
|
fsm.stateNew.EnsureService(4, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000})
|
|
|
|
fsm.stateNew.EnsureService(5, "baz", &structs.NodeService{ID: "web", Service: "web", Tags: nil, Address: "127.0.0.2", Port: 80})
|
|
|
|
fsm.stateNew.EnsureService(6, "baz", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"secondary"}, Address: "127.0.0.2", Port: 5000})
|
|
|
|
fsm.stateNew.EnsureCheck(7, &structs.HealthCheck{
|
2014-01-08 21:39:40 +00:00
|
|
|
Node: "foo",
|
|
|
|
CheckID: "web",
|
|
|
|
Name: "web connectivity",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
ServiceID: "web",
|
|
|
|
})
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.KVSSet(8, &structs.DirEntry{
|
2014-03-31 21:13:03 +00:00
|
|
|
Key: "/test",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
})
|
2014-10-09 18:54:47 +00:00
|
|
|
session := &structs.Session{ID: generateUUID(), Node: "foo"}
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.SessionCreate(9, session)
|
2014-10-09 19:23:32 +00:00
|
|
|
acl := &structs.ACL{ID: generateUUID(), Name: "User Token"}
|
2015-09-20 08:36:39 +00:00
|
|
|
fsm.stateNew.ACLSet(10, acl)
|
2013-12-16 18:47:14 +00:00
|
|
|
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.KVSSet(11, &structs.DirEntry{
|
2014-12-18 23:19:35 +00:00
|
|
|
Key: "/remove",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
})
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.KVSDelete(12, "/remove")
|
|
|
|
idx, _, err := fsm.stateNew.KVSList("/remove")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 12 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2014-12-18 23:19:35 +00:00
|
|
|
|
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
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm2, err := NewFSM(nil, 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
|
2015-10-09 17:25:19 +00:00
|
|
|
_, nodes, err := fsm2.stateNew.Nodes()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:25:19 +00:00
|
|
|
_, fooSrv, err := fsm2.stateNew.NodeServices("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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-04-03 19:03:10 +00:00
|
|
|
if !strContains(fooSrv.Services["db"].Tags, "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
|
|
|
|
2015-10-09 17:25:19 +00:00
|
|
|
_, checks, err := fsm2.stateNew.NodeChecks("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-01-08 21:39:40 +00:00
|
|
|
if len(checks) != 1 {
|
|
|
|
t.Fatalf("Bad: %v", checks)
|
|
|
|
}
|
2014-03-31 21:13:03 +00:00
|
|
|
|
|
|
|
// Verify key is set
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err := fsm2.stateNew.KVSGet("/test")
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if string(d.Value) != "foo" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
2014-05-16 02:57:48 +00:00
|
|
|
|
|
|
|
// Verify session is restored
|
2015-10-12 22:34:32 +00:00
|
|
|
idx, s, err := fsm2.stateNew.SessionGet(session.ID)
|
2014-05-16 02:57:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if s.Node != "foo" {
|
2014-08-05 23:55:58 +00:00
|
|
|
t.Fatalf("bad: %v", s)
|
|
|
|
}
|
2015-10-12 22:34:32 +00:00
|
|
|
if idx <= 1 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
2014-11-13 01:55:45 +00:00
|
|
|
}
|
2014-08-05 23:55:58 +00:00
|
|
|
|
|
|
|
// Verify ACL is restored
|
2015-09-20 08:36:39 +00:00
|
|
|
a, err := fsm2.stateNew.ACLGet(acl.ID)
|
2014-08-05 23:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if a.Name != "User Token" {
|
|
|
|
t.Fatalf("bad: %v", a)
|
2014-05-16 02:57:48 +00:00
|
|
|
}
|
2015-09-20 08:36:39 +00:00
|
|
|
if a.ModifyIndex <= 1 {
|
2014-11-13 01:55:45 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2014-12-18 23:19:35 +00:00
|
|
|
|
|
|
|
// Verify tombstones are restored
|
2015-10-09 17:25:19 +00:00
|
|
|
idx, _, err = fsm2.stateNew.KVSList("/remove")
|
2014-12-18 23:19:35 +00:00
|
|
|
if err != nil {
|
2015-10-09 17:25:19 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2014-12-18 23:19:35 +00:00
|
|
|
}
|
2015-10-09 17:25:19 +00:00
|
|
|
if idx != 12 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
2014-12-18 23:19:35 +00:00
|
|
|
}
|
2014-03-31 21:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_KVSSet(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-03-31 21:13:03 +00:00
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSSet,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Flags: 0,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is set
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err := fsm.stateNew.KVSGet("/test/path")
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatalf("missing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_KVSDelete(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSSet,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Flags: 0,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the delete
|
|
|
|
req.Op = structs.KVSDelete
|
|
|
|
buf, err = structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is not set
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err := fsm.stateNew.KVSGet("/test/path")
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d != nil {
|
|
|
|
t.Fatalf("key present")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_KVSDeleteTree(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-03-31 21:13:03 +00:00
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSSet,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Flags: 0,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the delete tree
|
|
|
|
req.Op = structs.KVSDeleteTree
|
|
|
|
req.DirEnt.Key = "/test"
|
|
|
|
buf, err = structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is not set
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err := fsm.stateNew.KVSGet("/test/path")
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d != nil {
|
|
|
|
t.Fatalf("key present")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 01:26:12 +00:00
|
|
|
func TestFSM_KVSDeleteCheckAndSet(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2015-01-09 01:26:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSSet,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Flags: 0,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is set
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err := fsm.stateNew.KVSGet("/test/path")
|
2015-01-09 01:26:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatalf("key missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the check-and-set
|
|
|
|
req.Op = structs.KVSDeleteCAS
|
|
|
|
req.DirEnt.ModifyIndex = d.ModifyIndex
|
|
|
|
buf, err = structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp.(bool) != true {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is gone
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err = fsm.stateNew.KVSGet("/test/path")
|
2015-01-09 01:26:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d != nil {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 21:13:03 +00:00
|
|
|
func TestFSM_KVSCheckAndSet(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSSet,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Flags: 0,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is set
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err := fsm.stateNew.KVSGet("/test/path")
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatalf("key missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the check-and-set
|
|
|
|
req.Op = structs.KVSCAS
|
|
|
|
req.DirEnt.ModifyIndex = d.ModifyIndex
|
|
|
|
req.DirEnt.Value = []byte("zip")
|
|
|
|
buf, err = structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp.(bool) != true {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is updated
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err = fsm.stateNew.KVSGet("/test/path")
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if string(d.Value) != "zip" {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
2013-12-16 18:47:14 +00:00
|
|
|
}
|
2014-05-16 02:37:58 +00:00
|
|
|
|
|
|
|
func TestFSM_SessionCreate_Destroy(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-16 02:37:58 +00:00
|
|
|
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
fsm.stateNew.EnsureCheck(2, &structs.HealthCheck{
|
2014-05-16 02:37:58 +00:00
|
|
|
Node: "foo",
|
|
|
|
CheckID: "web",
|
|
|
|
Status: structs.HealthPassing,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create a new session
|
|
|
|
req := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
2014-10-09 18:54:47 +00:00
|
|
|
ID: generateUUID(),
|
2014-05-16 02:37:58 +00:00
|
|
|
Node: "foo",
|
|
|
|
Checks: []string{"web"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.SessionRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if err, ok := resp.(error); ok {
|
|
|
|
t.Fatalf("resp: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the session
|
|
|
|
id := resp.(string)
|
2015-10-12 22:34:32 +00:00
|
|
|
_, session, err := fsm.stateNew.SessionGet(id)
|
2014-05-16 02:37:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if session == nil {
|
|
|
|
t.Fatalf("missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the session
|
|
|
|
if session.ID != id {
|
|
|
|
t.Fatalf("bad: %v", *session)
|
|
|
|
}
|
|
|
|
if session.Node != "foo" {
|
|
|
|
t.Fatalf("bad: %v", *session)
|
|
|
|
}
|
|
|
|
if session.Checks[0] != "web" {
|
|
|
|
t.Fatalf("bad: %v", *session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to destroy
|
|
|
|
destroy := structs.SessionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.SessionDestroy,
|
|
|
|
Session: structs.Session{
|
|
|
|
ID: id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.SessionRequestType, destroy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2015-10-12 22:34:32 +00:00
|
|
|
_, session, err = fsm.stateNew.SessionGet(id)
|
2014-05-16 02:37:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if session != nil {
|
|
|
|
t.Fatalf("should be destroyed")
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 02:49:45 +00:00
|
|
|
|
|
|
|
func TestFSM_KVSLock(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-05-16 02:49:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2014-10-09 18:54:47 +00:00
|
|
|
session := &structs.Session{ID: generateUUID(), Node: "foo"}
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.SessionCreate(2, session)
|
2014-05-16 02:49:45 +00:00
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSLock,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Value: []byte("test"),
|
|
|
|
Session: session.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != true {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is locked
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err := fsm.stateNew.KVSGet("/test/path")
|
2014-05-16 02:49:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatalf("missing")
|
|
|
|
}
|
|
|
|
if d.LockIndex != 1 {
|
|
|
|
t.Fatalf("bad: %v", *d)
|
|
|
|
}
|
|
|
|
if d.Session != session.ID {
|
|
|
|
t.Fatalf("bad: %v", *d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_KVSUnlock(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-10-15 22:03:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-05-16 02:49:45 +00:00
|
|
|
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2014-10-09 18:54:47 +00:00
|
|
|
session := &structs.Session{ID: generateUUID(), Node: "foo"}
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.SessionCreate(2, session)
|
2014-05-16 02:49:45 +00:00
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSLock,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Value: []byte("test"),
|
|
|
|
Session: session.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != true {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req = structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.KVSUnlock,
|
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Value: []byte("test"),
|
|
|
|
Session: session.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.KVSRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != true {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is unlocked
|
2015-10-09 17:25:19 +00:00
|
|
|
d, err := fsm.stateNew.KVSGet("/test/path")
|
2014-05-16 02:49:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatalf("missing")
|
|
|
|
}
|
|
|
|
if d.LockIndex != 1 {
|
|
|
|
t.Fatalf("bad: %v", *d)
|
|
|
|
}
|
|
|
|
if d.Session != "" {
|
|
|
|
t.Fatalf("bad: %v", *d)
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 23:55:58 +00:00
|
|
|
|
|
|
|
func TestFSM_ACL_Set_Delete(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-08-05 23:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new ACL
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
2014-10-09 19:23:32 +00:00
|
|
|
ID: generateUUID(),
|
2014-08-05 23:55:58 +00:00
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if err, ok := resp.(error); ok {
|
|
|
|
t.Fatalf("resp: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ACL
|
|
|
|
id := resp.(string)
|
2015-09-20 08:36:39 +00:00
|
|
|
acl, err := fsm.stateNew.ACLGet(id)
|
2014-08-05 23:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if acl == nil {
|
|
|
|
t.Fatalf("missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the ACL
|
|
|
|
if acl.ID != id {
|
|
|
|
t.Fatalf("bad: %v", *acl)
|
|
|
|
}
|
|
|
|
if acl.Name != "User token" {
|
|
|
|
t.Fatalf("bad: %v", *acl)
|
|
|
|
}
|
|
|
|
if acl.Type != structs.ACLTypeClient {
|
|
|
|
t.Fatalf("bad: %v", *acl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to destroy
|
|
|
|
destroy := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLDelete,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
ID: id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.ACLRequestType, destroy)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2015-09-20 08:36:39 +00:00
|
|
|
acl, err = fsm.stateNew.ACLGet(id)
|
2014-08-05 23:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if acl != nil {
|
|
|
|
t.Fatalf("should be destroyed")
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 23:22:10 +00:00
|
|
|
|
|
|
|
func TestFSM_TombstoneReap(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2014-12-18 23:22:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:25:19 +00:00
|
|
|
// Create some tombstones
|
|
|
|
fsm.stateNew.KVSSet(11, &structs.DirEntry{
|
2014-12-18 23:22:10 +00:00
|
|
|
Key: "/remove",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
})
|
2015-10-09 17:25:19 +00:00
|
|
|
fsm.stateNew.KVSDelete(12, "/remove")
|
|
|
|
idx, _, err := fsm.stateNew.KVSList("/remove")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 12 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
2014-12-18 23:22:10 +00:00
|
|
|
|
|
|
|
// Create a new reap request
|
|
|
|
req := structs.TombstoneRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.TombstoneReap,
|
|
|
|
ReapIndex: 12,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.TombstoneRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if err, ok := resp.(error); ok {
|
|
|
|
t.Fatalf("resp: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the tombstones are gone
|
2015-10-09 17:25:19 +00:00
|
|
|
idx, _, err = fsm.stateNew.KVSList("/remove")
|
2014-12-18 23:22:10 +00:00
|
|
|
if err != nil {
|
2015-10-09 17:25:19 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2014-12-18 23:22:10 +00:00
|
|
|
}
|
2015-10-09 17:25:19 +00:00
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
2014-12-18 23:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-06 02:44:21 +00:00
|
|
|
|
|
|
|
func TestFSM_IgnoreUnknown(t *testing.T) {
|
2015-10-13 03:56:31 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2015-05-06 02:44:21 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new reap request
|
|
|
|
type UnknownRequest struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
req := UnknownRequest{Foo: "bar"}
|
|
|
|
msgType := structs.IgnoreUnknownTypeFlag | 64
|
|
|
|
buf, err := structs.Encode(msgType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply should work, even though not supported
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if err, ok := resp.(error); ok {
|
|
|
|
t.Fatalf("resp: %v", err)
|
|
|
|
}
|
|
|
|
}
|