2013-12-11 22:38:18 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2013-12-16 18:47:14 +00:00
|
|
|
"bytes"
|
2016-02-02 00:42:04 +00:00
|
|
|
"fmt"
|
2014-02-03 23:21:56 +00:00
|
|
|
"os"
|
2015-06-30 00:04:47 +00:00
|
|
|
"reflect"
|
2013-12-11 22:38:18 +00:00
|
|
|
"testing"
|
2017-04-19 23:00:11 +00:00
|
|
|
"time"
|
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2016-01-29 19:42:34 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2016-06-07 20:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2016-01-29 19:42:34 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2014-10-09 18:54:47 +00:00
|
|
|
"github.com/hashicorp/raft"
|
2017-08-03 00:05:18 +00:00
|
|
|
"github.com/pascaldekloe/goe/verify"
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 19:42:34 +00:00
|
|
|
func generateUUID() (ret string) {
|
|
|
|
var err error
|
|
|
|
if ret, err = uuid.GenerateUUID(); err != nil {
|
2016-02-02 00:42:04 +00:00
|
|
|
panic(fmt.Sprintf("Unable to generate a UUID, %v", err))
|
2016-01-29 19:42:34 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:38:18 +00:00
|
|
|
func TestFSM_RegisterNode(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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-14 02:18:43 +00:00
|
|
|
_, node, err := fsm.state.GetNode("foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
2017-01-20 07:36:50 +00:00
|
|
|
_, services, err := fsm.state.NodeServices(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-01-08 21:39:40 +00:00
|
|
|
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-14 02:18:43 +00:00
|
|
|
_, node, err := fsm.state.GetNode("foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
2017-01-20 07:36:50 +00:00
|
|
|
_, services, err := fsm.state.NodeServices(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
2017-01-24 07:37:21 +00:00
|
|
|
_, checks, err := fsm.state.NodeChecks(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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-14 02:18:43 +00:00
|
|
|
_, node, err := fsm.state.GetNode("foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
2017-01-20 07:36:50 +00:00
|
|
|
_, services, err := fsm.state.NodeServices(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-01-08 21:39:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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-14 02:18:43 +00:00
|
|
|
_, node, err := fsm.state.GetNode("foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
2017-01-24 07:37:21 +00:00
|
|
|
_, checks, err := fsm.state.NodeChecks(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-01-08 21:39:40 +00:00
|
|
|
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
|
2015-10-14 02:18:43 +00:00
|
|
|
_, node, err := fsm.state.GetNode("foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
2017-01-20 07:36:50 +00:00
|
|
|
_, services, err := fsm.state.NodeServices(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
2017-01-24 07:37:21 +00:00
|
|
|
_, checks, err := fsm.state.NodeChecks(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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-13 05:21:39 +00:00
|
|
|
fsm.state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
2016-02-07 19:36:39 +00:00
|
|
|
fsm.state.EnsureNode(2, &structs.Node{Node: "baz", Address: "127.0.0.2", TaggedAddresses: map[string]string{"hello": "1.2.3.4"}})
|
2015-10-13 05:21:39 +00:00
|
|
|
fsm.state.EnsureService(3, "foo", &structs.NodeService{ID: "web", Service: "web", Tags: nil, Address: "127.0.0.1", Port: 80})
|
|
|
|
fsm.state.EnsureService(4, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000})
|
|
|
|
fsm.state.EnsureService(5, "baz", &structs.NodeService{ID: "web", Service: "web", Tags: nil, Address: "127.0.0.2", Port: 80})
|
|
|
|
fsm.state.EnsureService(6, "baz", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"secondary"}, Address: "127.0.0.2", Port: 5000})
|
|
|
|
fsm.state.EnsureCheck(7, &structs.HealthCheck{
|
2014-01-08 21:39:40 +00:00
|
|
|
Node: "foo",
|
|
|
|
CheckID: "web",
|
|
|
|
Name: "web connectivity",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-01-08 21:39:40 +00:00
|
|
|
ServiceID: "web",
|
|
|
|
})
|
2015-10-13 05:21:39 +00:00
|
|
|
fsm.state.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-13 05:21:39 +00:00
|
|
|
fsm.state.SessionCreate(9, session)
|
2014-10-09 19:23:32 +00:00
|
|
|
acl := &structs.ACL{ID: generateUUID(), Name: "User Token"}
|
2015-10-13 05:21:39 +00:00
|
|
|
fsm.state.ACLSet(10, acl)
|
2017-08-03 00:05:18 +00:00
|
|
|
if _, err := fsm.state.ACLBootstrapInit(10); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2013-12-16 18:47:14 +00:00
|
|
|
|
2015-10-13 05:21:39 +00:00
|
|
|
fsm.state.KVSSet(11, &structs.DirEntry{
|
2014-12-18 23:19:35 +00:00
|
|
|
Key: "/remove",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
})
|
2015-10-13 05:21:39 +00:00
|
|
|
fsm.state.KVSDelete(12, "/remove")
|
2017-01-24 19:20:51 +00:00
|
|
|
idx, _, err := fsm.state.KVSList(nil, "/remove")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := structs.Coordinates{
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "baz",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "foo",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := fsm.state.CoordinateBatchUpdate(13, updates); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2015-06-30 00:04:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 05:22:51 +00:00
|
|
|
query := structs.PreparedQuery{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "web",
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 14,
|
|
|
|
ModifyIndex: 14,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := fsm.state.PreparedQuerySet(14, &query); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
autopilotConf := &structs.AutopilotConfig{
|
|
|
|
CleanupDeadServers: true,
|
|
|
|
LastContactThreshold: 100 * time.Millisecond,
|
|
|
|
MaxTrailingLogs: 222,
|
|
|
|
}
|
|
|
|
if err := fsm.state.AutopilotSetConfig(15, autopilotConf); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
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
|
2017-01-20 07:36:50 +00:00
|
|
|
_, nodes, err := fsm2.state.Nodes(nil)
|
2015-10-09 17:25:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-01-08 18:31:42 +00:00
|
|
|
if len(nodes) != 2 {
|
2016-02-07 19:36:39 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
|
|
|
}
|
|
|
|
if nodes[0].Node != "baz" ||
|
|
|
|
nodes[0].Address != "127.0.0.2" ||
|
|
|
|
len(nodes[0].TaggedAddresses) != 1 ||
|
|
|
|
nodes[0].TaggedAddresses["hello"] != "1.2.3.4" {
|
|
|
|
t.Fatalf("bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
if nodes[1].Node != "foo" ||
|
|
|
|
nodes[1].Address != "127.0.0.1" ||
|
|
|
|
len(nodes[1].TaggedAddresses) != 0 {
|
|
|
|
t.Fatalf("bad: %v", nodes[1])
|
2013-12-16 18:47:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 07:36:50 +00:00
|
|
|
_, fooSrv, err := fsm2.state.NodeServices(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-01-29 19:42:34 +00:00
|
|
|
if !lib.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
|
|
|
|
2017-01-24 07:37:21 +00:00
|
|
|
_, checks, err := fsm2.state.NodeChecks(nil, "foo")
|
2015-10-09 17:25:19 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := fsm2.state.KVSGet(nil, "/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
|
2017-01-24 18:08:14 +00:00
|
|
|
idx, s, err := fsm2.state.SessionGet(nil, 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
|
2017-01-24 08:00:06 +00:00
|
|
|
_, a, err := fsm2.state.ACLGet(nil, 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)
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
gotB, err := fsm2.state.ACLGetBootstrap()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
wantB := &structs.ACLBootstrap{
|
|
|
|
AllowBootstrap: true,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 10,
|
|
|
|
ModifyIndex: 10,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
verify.Values(t, "", gotB, wantB)
|
2014-12-18 23:19:35 +00:00
|
|
|
|
|
|
|
// Verify tombstones are restored
|
2015-10-13 18:48:35 +00:00
|
|
|
func() {
|
|
|
|
snap := fsm2.state.Snapshot()
|
|
|
|
defer snap.Close()
|
2015-10-19 22:51:11 +00:00
|
|
|
stones, err := snap.Tombstones()
|
2015-10-13 18:48:35 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
stone := stones.Next().(*state.Tombstone)
|
2015-10-19 21:56:22 +00:00
|
|
|
if stone == nil {
|
|
|
|
t.Fatalf("missing tombstone")
|
|
|
|
}
|
|
|
|
if stone.Key != "/remove" || stone.Index != 12 {
|
|
|
|
t.Fatalf("bad: %v", stone)
|
|
|
|
}
|
2015-10-19 22:51:11 +00:00
|
|
|
if stones.Next() != nil {
|
2015-10-19 21:56:22 +00:00
|
|
|
t.Fatalf("unexpected extra tombstones")
|
2015-10-13 18:48:35 +00:00
|
|
|
}
|
|
|
|
}()
|
2015-06-30 00:04:47 +00:00
|
|
|
|
|
|
|
// Verify coordinates are restored
|
2017-01-24 16:13:48 +00:00
|
|
|
_, coords, err := fsm2.state.Coordinates(nil)
|
2015-06-30 00:04:47 +00:00
|
|
|
if err != nil {
|
2015-10-23 22:19:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2015-06-30 00:04:47 +00:00
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if !reflect.DeepEqual(coords, updates) {
|
|
|
|
t.Fatalf("bad: %#v", coords)
|
2015-06-30 00:04:47 +00:00
|
|
|
}
|
2015-11-12 05:22:51 +00:00
|
|
|
|
|
|
|
// Verify queries are restored.
|
2017-01-24 17:22:07 +00:00
|
|
|
_, queries, err := fsm2.state.PreparedQueryList(nil)
|
2015-11-12 05:22:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(queries) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", queries)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(queries[0], &query) {
|
|
|
|
t.Fatalf("bad: %#v", queries[0])
|
|
|
|
}
|
2017-01-24 18:38:03 +00:00
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
// Verify autopilot config is restored.
|
|
|
|
_, restoredConf, err := fsm2.state.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(restoredConf, autopilotConf) {
|
|
|
|
t.Fatalf("bad: %#v, %#v", restoredConf, autopilotConf)
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:38:03 +00:00
|
|
|
// Snapshot
|
|
|
|
snap, err = fsm2.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 the old FSM and make sure it abandons the old state
|
|
|
|
// store.
|
|
|
|
abandonCh := fsm.state.AbandonCh()
|
|
|
|
if err := fsm.Restore(sink); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-abandonCh:
|
|
|
|
default:
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
2014-03-31 21:13:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 03:42:34 +00:00
|
|
|
func TestFSM_BadRestore(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2017-01-26 03:42:34 +00:00
|
|
|
// Create an FSM with some state.
|
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)
|
|
|
|
}
|
2017-01-26 03:42:34 +00:00
|
|
|
fsm.state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
abandonCh := fsm.state.AbandonCh()
|
2014-03-31 21:13:03 +00:00
|
|
|
|
2017-01-26 03:42:34 +00:00
|
|
|
// Do a bad restore.
|
|
|
|
buf := bytes.NewBuffer([]byte("bad snapshot"))
|
|
|
|
sink := &MockSink{buf, false}
|
|
|
|
if err := fsm.Restore(sink); err == nil {
|
2014-03-31 21:13:03 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-01-26 03:42:34 +00:00
|
|
|
// Verify the contents didn't get corrupted.
|
|
|
|
_, nodes, err := fsm.state.Nodes(nil)
|
2014-03-31 21:13:03 +00:00
|
|
|
if err != nil {
|
2017-01-26 03:42:34 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("bad: %v", nodes)
|
2014-03-31 21:13:03 +00:00
|
|
|
}
|
2017-01-26 03:42:34 +00:00
|
|
|
if nodes[0].Node != "foo" ||
|
|
|
|
nodes[0].Address != "127.0.0.1" ||
|
|
|
|
len(nodes[0].TaggedAddresses) != 0 {
|
|
|
|
t.Fatalf("bad: %v", nodes[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the old state store didn't get abandoned.
|
|
|
|
select {
|
|
|
|
case <-abandonCh:
|
|
|
|
t.Fatalf("bad")
|
|
|
|
default:
|
2014-03-31 21:13:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_KVSDelete(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-03-31 21:13:03 +00:00
|
|
|
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
|
2017-04-19 23:00:11 +00:00
|
|
|
req.Op = api.KVDelete
|
2014-03-31 21:13:03 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := fsm.state.KVSGet(nil, "/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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-03-31 21:13:03 +00:00
|
|
|
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
|
2017-04-19 23:00:11 +00:00
|
|
|
req.Op = api.KVDeleteTree
|
2014-03-31 21:13:03 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := fsm.state.KVSGet(nil, "/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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2015-01-09 01:26:12 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := fsm.state.KVSGet(nil, "/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
|
2017-04-19 23:00:11 +00:00
|
|
|
req.Op = api.KVDeleteCAS
|
2015-01-09 01:26:12 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err = fsm.state.KVSGet(nil, "/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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVSet,
|
2014-03-31 21:13:03 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := fsm.state.KVSGet(nil, "/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
|
2017-04-19 23:00:11 +00:00
|
|
|
req.Op = api.KVCAS
|
2014-03-31 21:13:03 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err = fsm.state.KVSGet(nil, "/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
|
|
|
|
2015-04-29 02:07:10 +00:00
|
|
|
func TestFSM_CoordinateUpdate(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2015-10-23 22:19:14 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
2015-04-29 02:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
|
|
|
|
// Register some nodes.
|
|
|
|
fsm.state.EnsureNode(1, &structs.Node{Node: "node1", Address: "127.0.0.1"})
|
|
|
|
fsm.state.EnsureNode(2, &structs.Node{Node: "node2", Address: "127.0.0.1"})
|
2015-04-29 02:07:10 +00:00
|
|
|
|
2015-06-06 03:31:33 +00:00
|
|
|
// Write a batch of two coordinates.
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := structs.Coordinates{
|
2015-06-23 02:14:02 +00:00
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node1",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node2",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
2015-04-29 02:07:10 +00:00
|
|
|
}
|
2015-06-23 02:14:02 +00:00
|
|
|
buf, err := structs.Encode(structs.CoordinateBatchUpdateType, updates)
|
2015-04-29 02:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2015-06-06 03:31:33 +00:00
|
|
|
// Read back the two coordinates to make sure they got updated.
|
2017-01-24 16:13:48 +00:00
|
|
|
_, coords, err := fsm.state.Coordinates(nil)
|
2015-06-06 03:31:33 +00:00
|
|
|
if err != nil {
|
2015-10-23 22:19:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
2015-06-06 03:31:33 +00:00
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
if !reflect.DeepEqual(coords, updates) {
|
|
|
|
t.Fatalf("bad: %#v", coords)
|
2015-04-29 02:07:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-16 02:37:58 +00:00
|
|
|
func TestFSM_SessionCreate_Destroy(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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-13 05:21:39 +00:00
|
|
|
fsm.state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
fsm.state.EnsureCheck(2, &structs.HealthCheck{
|
2014-05-16 02:37:58 +00:00
|
|
|
Node: "foo",
|
|
|
|
CheckID: "web",
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthPassing,
|
2014-05-16 02:37:58 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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",
|
2016-06-07 20:24:51 +00:00
|
|
|
Checks: []types.CheckID{"web"},
|
2014-05-16 02:37:58 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
2017-01-24 18:08:14 +00:00
|
|
|
_, session, err := fsm.state.SessionGet(nil, 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)
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:08:14 +00:00
|
|
|
_, session, err = fsm.state.SessionGet(nil, 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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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-13 05:21:39 +00:00
|
|
|
fsm.state.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-13 05:21:39 +00:00
|
|
|
fsm.state.SessionCreate(2, session)
|
2014-05-16 02:49:45 +00:00
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVLock,
|
2014-05-16 02:49:45 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := fsm.state.KVSGet(nil, "/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) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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-13 05:21:39 +00:00
|
|
|
fsm.state.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-13 05:21:39 +00:00
|
|
|
fsm.state.SessionCreate(2, session)
|
2014-05-16 02:49:45 +00:00
|
|
|
|
|
|
|
req := structs.KVSRequest{
|
|
|
|
Datacenter: "dc1",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVLock,
|
2014-05-16 02:49:45 +00:00
|
|
|
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",
|
2017-04-19 23:00:11 +00:00
|
|
|
Op: api.KVUnlock,
|
2014-05-16 02:49:45 +00:00
|
|
|
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
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := fsm.state.KVSGet(nil, "/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
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
func TestFSM_ACL_CRUD(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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)
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
// Create a new ACL.
|
2014-08-05 23:55:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
// Get the ACL.
|
2014-08-05 23:55:58 +00:00
|
|
|
id := resp.(string)
|
2017-01-24 08:00:06 +00:00
|
|
|
_, acl, err := fsm.state.ACLGet(nil, id)
|
2014-08-05 23:55:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if acl == nil {
|
|
|
|
t.Fatalf("missing")
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
// Verify the ACL.
|
2014-08-05 23:55:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:05:18 +00:00
|
|
|
// Try to destroy.
|
2014-08-05 23:55:58 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-01-24 08:00:06 +00:00
|
|
|
_, acl, err = fsm.state.ACLGet(nil, 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")
|
|
|
|
}
|
2017-08-03 00:05:18 +00:00
|
|
|
|
|
|
|
// Initialize bootstrap (should work since we haven't made a management
|
|
|
|
// token).
|
|
|
|
init := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLBootstrapInit,
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.ACLRequestType, init)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if enabled, ok := resp.(bool); !ok || !enabled {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
gotB, err := fsm.state.ACLGetBootstrap()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
wantB := &structs.ACLBootstrap{
|
|
|
|
AllowBootstrap: true,
|
|
|
|
RaftIndex: gotB.RaftIndex,
|
|
|
|
}
|
|
|
|
verify.Values(t, "", gotB, wantB)
|
|
|
|
|
|
|
|
// Do a bootstrap.
|
|
|
|
bootstrap := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLBootstrapNow,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Name: "Bootstrap Token",
|
|
|
|
Type: structs.ACLTypeManagement,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.ACLRequestType, bootstrap)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
respACL, ok := resp.(*structs.ACL)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
bootstrap.ACL.CreateIndex = respACL.CreateIndex
|
|
|
|
bootstrap.ACL.ModifyIndex = respACL.ModifyIndex
|
|
|
|
verify.Values(t, "", respACL, &bootstrap.ACL)
|
2014-08-05 23:55:58 +00:00
|
|
|
}
|
2014-12-18 23:22:10 +00:00
|
|
|
|
2015-11-12 05:22:51 +00:00
|
|
|
func TestFSM_PreparedQuery_CRUD(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2015-11-12 05:22:51 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a service to query on.
|
|
|
|
fsm.state.EnsureNode(1, &structs.Node{Node: "foo", Address: "127.0.0.1"})
|
|
|
|
fsm.state.EnsureService(2, "foo", &structs.NodeService{ID: "web", Service: "web", Tags: nil, Address: "127.0.0.1", Port: 80})
|
|
|
|
|
|
|
|
// Create a new query.
|
|
|
|
query := structs.PreparedQueryRequest{
|
|
|
|
Op: structs.PreparedQueryCreate,
|
|
|
|
Query: &structs.PreparedQuery{
|
|
|
|
ID: generateUUID(),
|
|
|
|
Service: structs.ServiceQuery{
|
|
|
|
Service: "web",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
{
|
|
|
|
buf, err := structs.Encode(structs.PreparedQueryRequestType, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify it's in the state store.
|
|
|
|
{
|
2017-01-24 17:22:07 +00:00
|
|
|
_, actual, err := fsm.state.PreparedQueryGet(nil, query.Query.ID)
|
2015-11-12 05:22:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual.CreateIndex, actual.ModifyIndex = 0, 0
|
|
|
|
if !reflect.DeepEqual(actual, query.Query) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make an update to the query.
|
|
|
|
query.Op = structs.PreparedQueryUpdate
|
|
|
|
query.Query.Name = "my-query"
|
|
|
|
{
|
|
|
|
buf, err := structs.Encode(structs.PreparedQueryRequestType, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the update.
|
|
|
|
{
|
2017-01-24 17:22:07 +00:00
|
|
|
_, actual, err := fsm.state.PreparedQueryGet(nil, query.Query.ID)
|
2015-11-12 05:22:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual.CreateIndex, actual.ModifyIndex = 0, 0
|
|
|
|
if !reflect.DeepEqual(actual, query.Query) {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the query.
|
|
|
|
query.Op = structs.PreparedQueryDelete
|
|
|
|
{
|
|
|
|
buf, err := structs.Encode(structs.PreparedQueryRequestType, query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's gone.
|
|
|
|
{
|
2017-01-24 17:22:07 +00:00
|
|
|
_, actual, err := fsm.state.PreparedQueryGet(nil, query.Query.ID)
|
2015-11-12 05:22:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != nil {
|
|
|
|
t.Fatalf("bad: %v", actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 23:22:10 +00:00
|
|
|
func TestFSM_TombstoneReap(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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
|
2015-10-13 05:21:39 +00:00
|
|
|
fsm.state.KVSSet(11, &structs.DirEntry{
|
2014-12-18 23:22:10 +00:00
|
|
|
Key: "/remove",
|
|
|
|
Value: []byte("foo"),
|
|
|
|
})
|
2015-10-13 05:21:39 +00:00
|
|
|
fsm.state.KVSDelete(12, "/remove")
|
2017-01-24 19:20:51 +00:00
|
|
|
idx, _, err := fsm.state.KVSList(nil, "/remove")
|
2015-10-09 17:25:19 +00:00
|
|
|
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-13 18:48:35 +00:00
|
|
|
snap := fsm.state.Snapshot()
|
|
|
|
defer snap.Close()
|
2015-10-19 22:51:11 +00:00
|
|
|
stones, err := snap.Tombstones()
|
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-19 22:51:11 +00:00
|
|
|
if stones.Next() != nil {
|
2015-10-19 21:56:22 +00:00
|
|
|
t.Fatalf("unexpected extra tombstones")
|
2014-12-18 23:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-06 02:44:21 +00:00
|
|
|
|
2016-05-11 08:35:27 +00:00
|
|
|
func TestFSM_Txn(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2016-05-11 08:35:27 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a key using a transaction.
|
|
|
|
req := structs.TxnRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Ops: structs.TxnOps{
|
|
|
|
&structs.TxnOp{
|
2016-05-11 17:58:27 +00:00
|
|
|
KV: &structs.TxnKVOp{
|
2017-04-19 23:00:11 +00:00
|
|
|
Verb: api.KVSet,
|
2016-05-11 08:35:27 +00:00
|
|
|
DirEnt: structs.DirEntry{
|
|
|
|
Key: "/test/path",
|
|
|
|
Flags: 0,
|
|
|
|
Value: []byte("test"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.TxnRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if _, ok := resp.(structs.TxnResponse); !ok {
|
|
|
|
t.Fatalf("bad response type: %T", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is set directly in the state store.
|
2017-01-24 19:20:51 +00:00
|
|
|
_, d, err := fsm.state.KVSGet(nil, "/test/path")
|
2016-05-11 08:35:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d == nil {
|
|
|
|
t.Fatalf("missing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
func TestFSM_Autopilot(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
2017-03-21 23:36:44 +00:00
|
|
|
fsm, err := NewFSM(nil, os.Stderr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the autopilot config using a request.
|
|
|
|
req := structs.AutopilotSetConfigRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Config: structs.AutopilotConfig{
|
|
|
|
CleanupDeadServers: true,
|
|
|
|
LastContactThreshold: 10 * time.Second,
|
|
|
|
MaxTrailingLogs: 300,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.AutopilotRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if _, ok := resp.(error); ok {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify key is set directly in the state store.
|
|
|
|
_, config, err := fsm.state.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if config.CleanupDeadServers != req.Config.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %v", config.CleanupDeadServers)
|
|
|
|
}
|
|
|
|
if config.LastContactThreshold != req.Config.LastContactThreshold {
|
|
|
|
t.Fatalf("bad: %v", config.LastContactThreshold)
|
|
|
|
}
|
|
|
|
if config.MaxTrailingLogs != req.Config.MaxTrailingLogs {
|
|
|
|
t.Fatalf("bad: %v", config.MaxTrailingLogs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now use CAS and provide an old index
|
|
|
|
req.CAS = true
|
|
|
|
req.Config.CleanupDeadServers = false
|
|
|
|
req.Config.ModifyIndex = config.ModifyIndex - 1
|
|
|
|
buf, err = structs.Encode(structs.AutopilotRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if _, ok := resp.(error); ok {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, config, err = fsm.state.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !config.CleanupDeadServers {
|
|
|
|
t.Fatalf("bad: %v", config.CleanupDeadServers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 02:44:21 +00:00
|
|
|
func TestFSM_IgnoreUnknown(t *testing.T) {
|
2017-05-22 22:14:27 +00:00
|
|
|
t.Parallel()
|
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)
|
|
|
|
}
|
|
|
|
}
|