2015-07-04 01:41:36 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2015-07-06 20:01:10 +00:00
|
|
|
"bytes"
|
2021-01-22 14:18:17 +00:00
|
|
|
"context"
|
2016-01-30 02:18:29 +00:00
|
|
|
"fmt"
|
2015-07-06 20:01:10 +00:00
|
|
|
"reflect"
|
2017-09-19 14:47:10 +00:00
|
|
|
"strings"
|
2015-07-04 01:41:36 +00:00
|
|
|
"testing"
|
2015-08-16 00:38:13 +00:00
|
|
|
"time"
|
2015-07-04 01:41:36 +00:00
|
|
|
|
2017-09-01 23:40:27 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2017-02-08 05:22:48 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2022-11-17 17:08:04 +00:00
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
"github.com/kr/pretty"
|
2022-12-06 14:15:29 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2022-11-17 17:08:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2018-09-15 23:23:13 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-02-23 01:38:44 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2015-08-11 21:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2020-12-11 15:40:50 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/stream"
|
2015-07-04 01:41:36 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2016-01-30 02:18:29 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-07-04 01:41:36 +00:00
|
|
|
)
|
|
|
|
|
2015-07-06 20:01:10 +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
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
func testStateStore(t *testing.T) *state.StateStore {
|
2017-10-13 21:36:02 +00:00
|
|
|
return state.TestStateStore(t)
|
2015-08-11 21:27:14 +00:00
|
|
|
}
|
|
|
|
|
2015-07-04 01:41:36 +00:00
|
|
|
func testFSM(t *testing.T) *nomadFSM {
|
2016-01-29 23:31:32 +00:00
|
|
|
broker := testBroker(t, 0)
|
2018-06-13 22:33:25 +00:00
|
|
|
dispatcher, _ := testPeriodicDispatcher(t)
|
2018-11-07 18:08:23 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2017-10-13 21:36:02 +00:00
|
|
|
fsmConfig := &FSMConfig{
|
2020-10-08 18:27:52 +00:00
|
|
|
EvalBroker: broker,
|
|
|
|
Periodic: dispatcher,
|
|
|
|
Blocked: NewBlockedEvals(broker, logger),
|
|
|
|
Logger: logger,
|
|
|
|
Region: "global",
|
|
|
|
EnableEventBroker: true,
|
|
|
|
EventBufferSize: 100,
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
|
|
|
fsm, err := NewFSM(fsmConfig)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if fsm == nil {
|
|
|
|
t.Fatalf("missing fsm")
|
|
|
|
}
|
|
|
|
return fsm
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeLog(buf []byte) *raft.Log {
|
|
|
|
return &raft.Log{
|
|
|
|
Index: 1,
|
|
|
|
Term: 1,
|
|
|
|
Type: raft.LogCommand,
|
|
|
|
Data: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 00:52:12 +00:00
|
|
|
func TestFSM_UpsertNodeEvents(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-01 22:17:33 +00:00
|
|
|
require := require.New(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
node := mock.Node()
|
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
err := state.UpsertNode(structs.MsgTypeTestSetup, 1000, node)
|
2018-03-01 22:17:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeEvent := &structs.NodeEvent{
|
2018-03-08 10:46:35 +00:00
|
|
|
Message: "Heartbeating failed",
|
|
|
|
Subsystem: "Heartbeat",
|
2018-03-27 17:50:09 +00:00
|
|
|
Timestamp: time.Now(),
|
2018-03-01 22:17:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 00:30:49 +00:00
|
|
|
nodeEvents := []*structs.NodeEvent{nodeEvent}
|
|
|
|
allEvents := map[string][]*structs.NodeEvent{node.ID: nodeEvents}
|
|
|
|
|
2018-03-12 01:00:13 +00:00
|
|
|
req := structs.EmitNodeEventsRequest{
|
2018-03-09 00:30:49 +00:00
|
|
|
NodeEvents: allEvents,
|
2018-03-08 19:23:34 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
2018-03-01 22:17:33 +00:00
|
|
|
}
|
2018-03-14 00:52:12 +00:00
|
|
|
buf, err := structs.Encode(structs.UpsertNodeEventsType, req)
|
2018-03-01 22:17:33 +00:00
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
// the response in this case will be an error
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
2018-03-09 00:30:49 +00:00
|
|
|
out, err := state.NodeByID(ws, node.ID)
|
2018-03-01 22:17:33 +00:00
|
|
|
require.Nil(err)
|
|
|
|
|
2018-03-14 00:59:37 +00:00
|
|
|
require.Equal(2, len(out.Events))
|
2018-03-01 22:17:33 +00:00
|
|
|
|
2018-03-14 00:59:37 +00:00
|
|
|
first := out.Events[1]
|
2018-03-01 22:17:33 +00:00
|
|
|
require.Equal(uint64(1), first.CreateIndex)
|
2018-03-08 14:34:08 +00:00
|
|
|
require.Equal("Heartbeating failed", first.Message)
|
2018-03-01 22:17:33 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 03:47:42 +00:00
|
|
|
func TestFSM_UpsertNode(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-04 01:41:36 +00:00
|
|
|
fsm := testFSM(t)
|
2016-04-01 01:11:27 +00:00
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
node := mock.Node()
|
|
|
|
|
|
|
|
// Mark an eval as blocked.
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.ClassEligibility = map[string]bool{node.ComputedClass: true}
|
|
|
|
fsm.blockedEvals.Block(eval)
|
2015-07-04 01:41:36 +00:00
|
|
|
|
2015-07-07 16:51:42 +00:00
|
|
|
req := structs.NodeRegisterRequest{
|
2016-04-01 01:11:27 +00:00
|
|
|
Node: node,
|
2015-07-04 01:41:36 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
2015-07-04 01:41:36 +00:00
|
|
|
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
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
n, err := fsm.State().NodeByID(ws, req.Node.ID)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-04-01 01:11:27 +00:00
|
|
|
if n == nil {
|
2015-07-04 01:41:36 +00:00
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
2016-04-01 01:11:27 +00:00
|
|
|
if n.CreateIndex != 1 {
|
2015-07-04 01:41:36 +00:00
|
|
|
t.Fatalf("bad index: %d", node.CreateIndex)
|
|
|
|
}
|
2015-08-16 00:38:13 +00:00
|
|
|
|
|
|
|
tt := fsm.TimeTable()
|
|
|
|
index := tt.NearestIndex(time.Now().UTC())
|
|
|
|
if index != 1 {
|
|
|
|
t.Fatalf("bad: %d", index)
|
|
|
|
}
|
2016-04-01 01:11:27 +00:00
|
|
|
|
|
|
|
// Verify the eval was unblocked.
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
bStats := fsm.blockedEvals.Stats()
|
|
|
|
if bStats.TotalBlocked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", bStats)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
|
|
|
|
2015-07-04 01:41:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 17:10:39 +00:00
|
|
|
func TestFSM_UpsertNode_Canonicalize(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2021-03-26 17:10:39 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
// Setup a node without eligibility, ensure that upsert/canonicalize put it back
|
|
|
|
node := mock.Node()
|
|
|
|
node.SchedulingEligibility = ""
|
|
|
|
|
|
|
|
req := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
require.Nil(fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Verify we are registered
|
|
|
|
n, err := fsm.State().NodeByID(nil, req.Node.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(n)
|
|
|
|
require.EqualValues(1, n.CreateIndex)
|
|
|
|
require.Equal(structs.NodeSchedulingEligible, n.SchedulingEligibility)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_UpsertNode_Canonicalize_Ineligible(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2021-03-26 17:10:39 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
// Setup a node without eligibility, ensure that upsert/canonicalize put it back
|
|
|
|
node := mock.DrainNode()
|
|
|
|
node.SchedulingEligibility = ""
|
|
|
|
|
|
|
|
req := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
require.Nil(fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Verify we are registered
|
|
|
|
n, err := fsm.State().NodeByID(nil, req.Node.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(n)
|
|
|
|
require.EqualValues(1, n.CreateIndex)
|
|
|
|
require.Equal(structs.NodeSchedulingIneligible, n.SchedulingEligibility)
|
|
|
|
}
|
|
|
|
|
2015-07-04 01:41:36 +00:00
|
|
|
func TestFSM_DeregisterNode(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-04 01:41:36 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
node := mock.Node()
|
2015-07-07 16:51:42 +00:00
|
|
|
req := structs.NodeRegisterRequest{
|
2015-07-04 01:41:36 +00:00
|
|
|
Node: node,
|
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:57:58 +00:00
|
|
|
req2 := structs.NodeBatchDeregisterRequest{
|
2019-06-05 14:47:29 +00:00
|
|
|
NodeIDs: []string{node.ID},
|
2015-07-04 01:41:36 +00:00
|
|
|
}
|
2019-06-26 14:57:58 +00:00
|
|
|
buf, err = structs.Encode(structs.NodeBatchDeregisterRequestType, req2)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
node, err = fsm.State().NodeByID(ws, req.Node.ID)
|
2015-07-04 01:41:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if node != nil {
|
|
|
|
t.Fatalf("node found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_UpdateNodeStatus(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-05-11 21:53:41 +00:00
|
|
|
require := require.New(t)
|
2015-07-04 01:41:36 +00:00
|
|
|
fsm := testFSM(t)
|
2016-01-29 23:31:32 +00:00
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
2015-07-04 01:41:36 +00:00
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
node := mock.Node()
|
2015-07-07 16:51:42 +00:00
|
|
|
req := structs.NodeRegisterRequest{
|
2015-07-04 01:41:36 +00:00
|
|
|
Node: node,
|
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
2018-05-11 21:53:41 +00:00
|
|
|
require.NoError(err)
|
2015-07-04 01:41:36 +00:00
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2018-05-11 21:53:41 +00:00
|
|
|
require.Nil(resp)
|
2015-07-04 01:41:36 +00:00
|
|
|
|
2016-01-29 23:31:32 +00:00
|
|
|
// Mark an eval as blocked.
|
|
|
|
eval := mock.Eval()
|
2016-01-30 01:46:44 +00:00
|
|
|
eval.ClassEligibility = map[string]bool{node.ComputedClass: true}
|
2016-01-29 23:31:32 +00:00
|
|
|
fsm.blockedEvals.Block(eval)
|
|
|
|
|
2018-05-11 21:53:41 +00:00
|
|
|
event := &structs.NodeEvent{
|
|
|
|
Message: "Node ready foo",
|
|
|
|
Subsystem: structs.NodeEventSubsystemCluster,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
}
|
2015-07-07 16:51:42 +00:00
|
|
|
req2 := structs.NodeUpdateStatusRequest{
|
2018-05-11 21:53:41 +00:00
|
|
|
NodeID: node.ID,
|
|
|
|
Status: structs.NodeStatusReady,
|
|
|
|
NodeEvent: event,
|
2015-07-04 01:41:36 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err = structs.Encode(structs.NodeUpdateStatusRequestType, req2)
|
2018-05-11 21:53:41 +00:00
|
|
|
require.NoError(err)
|
2015-07-04 01:41:36 +00:00
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
2018-05-11 21:53:41 +00:00
|
|
|
require.Nil(resp)
|
2015-07-04 01:41:36 +00:00
|
|
|
|
2016-01-29 23:31:32 +00:00
|
|
|
// Verify the status is ready.
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
node, err = fsm.State().NodeByID(ws, req.Node.ID)
|
2018-05-11 21:53:41 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(structs.NodeStatusReady, node.Status)
|
|
|
|
require.Len(node.Events, 2)
|
|
|
|
require.Equal(event.Message, node.Events[1].Message)
|
2016-01-29 23:31:32 +00:00
|
|
|
|
|
|
|
// Verify the eval was unblocked.
|
2016-01-30 02:18:29 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
bStats := fsm.blockedEvals.Stats()
|
|
|
|
if bStats.TotalBlocked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", bStats)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
2015-07-04 01:41:36 +00:00
|
|
|
}
|
2015-07-06 20:01:10 +00:00
|
|
|
|
2018-03-09 22:15:21 +00:00
|
|
|
func TestFSM_BatchUpdateNodeDrain(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-09 22:15:21 +00:00
|
|
|
require := require.New(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
node := mock.Node()
|
|
|
|
req := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
strategy := &structs.DrainStrategy{
|
|
|
|
DrainSpec: structs.DrainSpec{
|
|
|
|
Deadline: 10 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
2018-05-10 23:30:54 +00:00
|
|
|
event := &structs.NodeEvent{
|
|
|
|
Message: "Drain strategy enabled",
|
|
|
|
Subsystem: structs.NodeEventSubsystemDrain,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
}
|
2018-03-09 22:15:21 +00:00
|
|
|
req2 := structs.BatchNodeUpdateDrainRequest{
|
|
|
|
Updates: map[string]*structs.DrainUpdate{
|
2018-03-20 18:31:55 +00:00
|
|
|
node.ID: {
|
2018-03-09 22:15:21 +00:00
|
|
|
DrainStrategy: strategy,
|
|
|
|
},
|
|
|
|
},
|
2018-05-10 23:30:54 +00:00
|
|
|
NodeEvents: map[string]*structs.NodeEvent{
|
|
|
|
node.ID: event,
|
|
|
|
},
|
2018-03-09 22:15:21 +00:00
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.BatchNodeUpdateDrainRequestType, req2)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
2018-03-14 23:38:19 +00:00
|
|
|
// Verify drain is set
|
2018-03-09 22:15:21 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
node, err = fsm.State().NodeByID(ws, req.Node.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(node.DrainStrategy, strategy)
|
2018-05-10 23:30:54 +00:00
|
|
|
require.Len(node.Events, 2)
|
2018-03-09 22:15:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 02:55:38 +00:00
|
|
|
func TestFSM_UpdateNodeDrain(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-23 18:42:43 +00:00
|
|
|
require := require.New(t)
|
2015-09-07 02:55:38 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
node := mock.Node()
|
|
|
|
req := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
2018-02-23 18:42:43 +00:00
|
|
|
require.Nil(err)
|
2015-09-07 02:55:38 +00:00
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2018-02-23 18:42:43 +00:00
|
|
|
require.Nil(resp)
|
2015-09-07 02:55:38 +00:00
|
|
|
|
2018-02-23 18:42:43 +00:00
|
|
|
strategy := &structs.DrainStrategy{
|
2018-02-26 22:34:32 +00:00
|
|
|
DrainSpec: structs.DrainSpec{
|
|
|
|
Deadline: 10 * time.Second,
|
|
|
|
},
|
2018-02-23 18:42:43 +00:00
|
|
|
}
|
2015-09-07 02:55:38 +00:00
|
|
|
req2 := structs.NodeUpdateDrainRequest{
|
2018-02-23 18:42:43 +00:00
|
|
|
NodeID: node.ID,
|
|
|
|
DrainStrategy: strategy,
|
2018-05-10 23:30:54 +00:00
|
|
|
NodeEvent: &structs.NodeEvent{
|
|
|
|
Message: "Drain strategy enabled",
|
|
|
|
Subsystem: structs.NodeEventSubsystemDrain,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
},
|
2015-09-07 02:55:38 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err = structs.Encode(structs.NodeUpdateDrainRequestType, req2)
|
2018-02-23 18:42:43 +00:00
|
|
|
require.Nil(err)
|
2015-09-07 02:55:38 +00:00
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
2018-02-23 18:42:43 +00:00
|
|
|
require.Nil(resp)
|
2015-09-07 02:55:38 +00:00
|
|
|
|
|
|
|
// Verify we are NOT registered
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
node, err = fsm.State().NodeByID(ws, req.Node.ID)
|
2018-02-23 18:42:43 +00:00
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(node.DrainStrategy, strategy)
|
2018-05-10 23:30:54 +00:00
|
|
|
require.Len(node.Events, 2)
|
2015-09-07 02:55:38 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 00:34:42 +00:00
|
|
|
func TestFSM_UpdateNodeEligibility(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-27 00:34:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
node := mock.Node()
|
|
|
|
req := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
2018-05-11 21:32:34 +00:00
|
|
|
event := &structs.NodeEvent{
|
|
|
|
Message: "Node marked as ineligible",
|
|
|
|
Subsystem: structs.NodeEventSubsystemCluster,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
}
|
|
|
|
|
2018-02-27 00:34:42 +00:00
|
|
|
// Set the eligibility
|
|
|
|
req2 := structs.NodeUpdateEligibilityRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Eligibility: structs.NodeSchedulingIneligible,
|
2018-05-11 21:32:34 +00:00
|
|
|
NodeEvent: event,
|
2018-02-27 00:34:42 +00:00
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.NodeUpdateEligibilityRequestType, req2)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
// Lookup the node and check
|
|
|
|
node, err = fsm.State().NodeByID(nil, req.Node.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(node.SchedulingEligibility, structs.NodeSchedulingIneligible)
|
2018-05-11 21:32:34 +00:00
|
|
|
require.Len(node.Events, 2)
|
|
|
|
require.Equal(event.Message, node.Events[1].Message)
|
2018-02-27 00:34:42 +00:00
|
|
|
|
|
|
|
// Update the drain
|
|
|
|
strategy := &structs.DrainStrategy{
|
|
|
|
DrainSpec: structs.DrainSpec{
|
|
|
|
Deadline: 10 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req3 := structs.NodeUpdateDrainRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
DrainStrategy: strategy,
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.NodeUpdateDrainRequestType, req3)
|
|
|
|
require.Nil(err)
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
// Try forcing eligibility
|
|
|
|
req4 := structs.NodeUpdateEligibilityRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Eligibility: structs.NodeSchedulingEligible,
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.NodeUpdateEligibilityRequestType, req4)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
require.NotNil(resp)
|
|
|
|
err, ok := resp.(error)
|
|
|
|
require.True(ok)
|
|
|
|
require.Contains(err.Error(), "draining")
|
|
|
|
}
|
|
|
|
|
2018-02-27 22:08:29 +00:00
|
|
|
func TestFSM_UpdateNodeEligibility_Unblock(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-27 22:08:29 +00:00
|
|
|
require := require.New(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
node := mock.Node()
|
|
|
|
req := structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.NodeRegisterRequestType, req)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
// Set the eligibility
|
|
|
|
req2 := structs.NodeUpdateEligibilityRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Eligibility: structs.NodeSchedulingIneligible,
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.NodeUpdateEligibilityRequestType, req2)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
// Mark an eval as blocked.
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.ClassEligibility = map[string]bool{node.ComputedClass: true}
|
|
|
|
fsm.blockedEvals.Block(eval)
|
|
|
|
|
|
|
|
// Set eligible
|
|
|
|
req4 := structs.NodeUpdateEligibilityRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Eligibility: structs.NodeSchedulingEligible,
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.NodeUpdateEligibilityRequestType, req4)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
// Verify the eval was unblocked.
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
bStats := fsm.blockedEvals.Stats()
|
|
|
|
if bStats.TotalBlocked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", bStats)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-07 16:55:47 +00:00
|
|
|
func TestFSM_RegisterJob(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-07 16:55:47 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-12-19 01:51:30 +00:00
|
|
|
job := mock.PeriodicJob()
|
2015-07-07 16:55:47 +00:00
|
|
|
req := structs.JobRegisterRequest{
|
2015-12-01 22:54:57 +00:00
|
|
|
Job: job,
|
2017-09-07 23:56:15 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2015-07-07 16:55:47 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
2015-07-07 16:55:47 +00:00
|
|
|
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
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2017-09-07 23:56:15 +00:00
|
|
|
jobOut, err := fsm.State().JobByID(ws, req.Namespace, req.Job.ID)
|
2015-07-07 16:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-12-01 22:54:57 +00:00
|
|
|
if jobOut == nil {
|
2015-07-07 16:55:47 +00:00
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
2015-12-01 22:54:57 +00:00
|
|
|
if jobOut.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", jobOut.CreateIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify it was added to the periodic runner.
|
2017-09-07 23:56:15 +00:00
|
|
|
tuple := structs.NamespacedID{
|
|
|
|
ID: job.ID,
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
}
|
|
|
|
if _, ok := fsm.periodicDispatcher.tracked[tuple]; !ok {
|
2015-12-01 22:54:57 +00:00
|
|
|
t.Fatal("job not added to periodic runner")
|
2015-07-07 16:55:47 +00:00
|
|
|
}
|
2015-12-19 01:51:30 +00:00
|
|
|
|
|
|
|
// Verify the launch time was tracked.
|
2017-09-07 23:56:15 +00:00
|
|
|
launchOut, err := fsm.State().PeriodicLaunchByID(ws, req.Namespace, req.Job.ID)
|
2015-12-19 01:51:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if launchOut == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if launchOut.Launch.IsZero() {
|
|
|
|
t.Fatalf("bad launch time: %v", launchOut.Launch)
|
|
|
|
}
|
2015-07-07 16:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 21:55:17 +00:00
|
|
|
func TestFSM_RegisterPeriodicJob_NonLeader(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-12-11 21:55:17 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Disable the dispatcher
|
|
|
|
fsm.periodicDispatcher.SetEnabled(false)
|
|
|
|
|
|
|
|
job := mock.PeriodicJob()
|
|
|
|
req := structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
|
|
|
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
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
jobOut, err := fsm.State().JobByID(ws, req.Namespace, req.Job.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if jobOut == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if jobOut.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", jobOut.CreateIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify it wasn't added to the periodic runner.
|
|
|
|
tuple := structs.NamespacedID{
|
|
|
|
ID: job.ID,
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
}
|
|
|
|
if _, ok := fsm.periodicDispatcher.tracked[tuple]; ok {
|
|
|
|
t.Fatal("job added to periodic runner")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the launch time was tracked.
|
|
|
|
launchOut, err := fsm.State().PeriodicLaunchByID(ws, req.Namespace, req.Job.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if launchOut == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if launchOut.Launch.IsZero() {
|
|
|
|
t.Fatalf("bad launch time: %v", launchOut.Launch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
func TestFSM_RegisterJob_BadNamespace(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-09-19 14:47:10 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
job.Namespace = "foo"
|
|
|
|
req := structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatalf("no resp: %v", resp)
|
|
|
|
}
|
|
|
|
err, ok := resp.(error)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("resp not of error type: %T %v", resp, resp)
|
|
|
|
}
|
2018-03-12 18:26:37 +00:00
|
|
|
if !strings.Contains(err.Error(), "nonexistent namespace") {
|
2017-09-19 14:47:10 +00:00
|
|
|
t.Fatalf("bad error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are not registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
jobOut, err := fsm.State().JobByID(ws, req.Namespace, req.Job.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if jobOut != nil {
|
|
|
|
t.Fatalf("job found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 23:25:29 +00:00
|
|
|
func TestFSM_DeregisterJob_Error(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-08-25 23:25:29 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
|
|
|
|
deregReq := structs.JobDeregisterRequest{
|
|
|
|
JobID: job.ID,
|
|
|
|
Purge: true,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobDeregisterRequestType, deregReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
require.NotNil(t, resp)
|
|
|
|
respErr, ok := resp.(error)
|
|
|
|
require.Truef(t, ok, "expected response to be an error but found: %T", resp)
|
|
|
|
require.Error(t, respErr)
|
|
|
|
}
|
|
|
|
|
2017-04-15 03:54:30 +00:00
|
|
|
func TestFSM_DeregisterJob_Purge(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-07 16:55:47 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-12-19 01:51:30 +00:00
|
|
|
job := mock.PeriodicJob()
|
2015-07-07 16:55:47 +00:00
|
|
|
req := structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
2017-09-07 23:56:15 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2015-07-07 16:55:47 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
2015-07-07 16:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req2 := structs.JobDeregisterRequest{
|
2015-07-23 22:15:48 +00:00
|
|
|
JobID: job.ID,
|
2017-04-15 03:54:30 +00:00
|
|
|
Purge: true,
|
2017-09-07 23:56:15 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2015-07-07 16:55:47 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err = structs.Encode(structs.JobDeregisterRequestType, req2)
|
2015-07-07 16:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2017-09-07 23:56:15 +00:00
|
|
|
jobOut, err := fsm.State().JobByID(ws, req.Namespace, req.Job.ID)
|
2015-07-07 16:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-12-01 22:54:57 +00:00
|
|
|
if jobOut != nil {
|
2015-07-07 16:55:47 +00:00
|
|
|
t.Fatalf("job found!")
|
|
|
|
}
|
2015-12-01 22:54:57 +00:00
|
|
|
|
|
|
|
// Verify it was removed from the periodic runner.
|
2017-09-07 23:56:15 +00:00
|
|
|
tuple := structs.NamespacedID{
|
|
|
|
ID: job.ID,
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
}
|
|
|
|
if _, ok := fsm.periodicDispatcher.tracked[tuple]; ok {
|
2015-12-01 22:54:57 +00:00
|
|
|
t.Fatal("job not removed from periodic runner")
|
|
|
|
}
|
2015-12-19 01:51:30 +00:00
|
|
|
|
|
|
|
// Verify it was removed from the periodic launch table.
|
2017-09-07 23:56:15 +00:00
|
|
|
launchOut, err := fsm.State().PeriodicLaunchByID(ws, req.Namespace, req.Job.ID)
|
2015-12-19 01:51:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if launchOut != nil {
|
|
|
|
t.Fatalf("launch found!")
|
|
|
|
}
|
2015-07-07 16:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 03:54:30 +00:00
|
|
|
func TestFSM_DeregisterJob_NoPurge(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-04-15 03:54:30 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
job := mock.PeriodicJob()
|
|
|
|
req := structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
2017-09-07 23:56:15 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-04-15 03:54:30 +00:00
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
req2 := structs.JobDeregisterRequest{
|
|
|
|
JobID: job.ID,
|
|
|
|
Purge: false,
|
2017-09-07 23:56:15 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-04-15 03:54:30 +00:00
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.JobDeregisterRequestType, req2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
ws := memdb.NewWatchSet()
|
2017-09-07 23:56:15 +00:00
|
|
|
jobOut, err := fsm.State().JobByID(ws, req.Namespace, req.Job.ID)
|
2017-04-15 03:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if jobOut == nil {
|
|
|
|
t.Fatalf("job not found!")
|
|
|
|
}
|
|
|
|
if !jobOut.Stop {
|
|
|
|
t.Fatalf("job not stopped found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify it was removed from the periodic runner.
|
2017-09-07 23:56:15 +00:00
|
|
|
tuple := structs.NamespacedID{
|
|
|
|
ID: job.ID,
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
}
|
|
|
|
if _, ok := fsm.periodicDispatcher.tracked[tuple]; ok {
|
2017-04-15 03:54:30 +00:00
|
|
|
t.Fatal("job not removed from periodic runner")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify it was removed from the periodic launch table.
|
2017-09-07 23:56:15 +00:00
|
|
|
launchOut, err := fsm.State().PeriodicLaunchByID(ws, req.Namespace, req.Job.ID)
|
2017-04-15 03:54:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if launchOut == nil {
|
|
|
|
t.Fatalf("launch not found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 22:32:18 +00:00
|
|
|
func TestFSM_BatchDeregisterJob(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-14 22:32:18 +00:00
|
|
|
require := require.New(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
job := mock.PeriodicJob()
|
|
|
|
req := structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
|
|
|
require.Nil(err)
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
job2 := mock.Job()
|
|
|
|
req2 := structs.JobRegisterRequest{
|
|
|
|
Job: job2,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job2.Namespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err = structs.Encode(structs.JobRegisterRequestType, req2)
|
|
|
|
require.Nil(err)
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
req3 := structs.JobBatchDeregisterRequest{
|
|
|
|
Jobs: map[structs.NamespacedID]*structs.JobDeregisterOptions{
|
2018-03-16 23:31:16 +00:00
|
|
|
{
|
2018-03-14 22:32:18 +00:00
|
|
|
ID: job.ID,
|
|
|
|
Namespace: job.Namespace,
|
2018-03-16 23:31:16 +00:00
|
|
|
}: {},
|
|
|
|
{
|
2018-03-14 22:32:18 +00:00
|
|
|
ID: job2.ID,
|
|
|
|
Namespace: job2.Namespace,
|
2018-03-16 23:31:16 +00:00
|
|
|
}: {
|
2018-03-14 22:32:18 +00:00
|
|
|
Purge: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.JobBatchDeregisterRequestType, req3)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
jobOut, err := fsm.State().JobByID(ws, req.Namespace, req.Job.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(jobOut)
|
|
|
|
require.True(jobOut.Stop)
|
|
|
|
|
|
|
|
// Verify it was removed from the periodic runner.
|
|
|
|
tuple := structs.NamespacedID{
|
|
|
|
ID: job.ID,
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
}
|
|
|
|
require.NotContains(fsm.periodicDispatcher.tracked, tuple)
|
|
|
|
|
|
|
|
// Verify it was not removed from the periodic launch table.
|
|
|
|
launchOut, err := fsm.State().PeriodicLaunchByID(ws, job.Namespace, job.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(launchOut)
|
|
|
|
|
|
|
|
// Verify the other jbo was purged
|
|
|
|
jobOut2, err := fsm.State().JobByID(ws, job2.Namespace, job2.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Nil(jobOut2)
|
|
|
|
}
|
|
|
|
|
2015-07-23 22:52:38 +00:00
|
|
|
func TestFSM_UpdateEval(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-23 22:52:38 +00:00
|
|
|
fsm := testFSM(t)
|
2015-08-06 18:32:42 +00:00
|
|
|
fsm.evalBroker.SetEnabled(true)
|
2015-07-23 22:52:38 +00:00
|
|
|
|
|
|
|
req := structs.EvalUpdateRequest{
|
2015-08-11 21:27:14 +00:00
|
|
|
Evals: []*structs.Evaluation{mock.Eval()},
|
2015-07-23 22:52:38 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.EvalUpdateRequestType, req)
|
2015-07-23 22:52:38 +00:00
|
|
|
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
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
eval, err := fsm.State().EvalByID(ws, req.Evals[0].ID)
|
2015-07-23 22:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if eval.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", eval.CreateIndex)
|
|
|
|
}
|
2015-08-06 18:32:42 +00:00
|
|
|
|
|
|
|
// Verify enqueued
|
|
|
|
stats := fsm.evalBroker.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v %#v", stats, eval)
|
|
|
|
}
|
2015-07-23 22:52:38 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 23:31:32 +00:00
|
|
|
func TestFSM_UpdateEval_Blocked(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-01-29 23:31:32 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.evalBroker.SetEnabled(true)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
// Create a blocked eval.
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.Status = structs.EvalStatusBlocked
|
|
|
|
|
|
|
|
req := structs.EvalUpdateRequest{
|
|
|
|
Evals: []*structs.Evaluation{eval},
|
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.EvalUpdateRequestType, req)
|
2016-01-29 23:31:32 +00:00
|
|
|
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
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().EvalByID(ws, eval.ID)
|
2016-01-29 23:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if out.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", out.CreateIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the eval wasn't enqueued
|
|
|
|
stats := fsm.evalBroker.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v %#v", stats, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the eval was added to the blocked tracker.
|
|
|
|
bStats := fsm.blockedEvals.Stats()
|
2016-01-30 00:45:09 +00:00
|
|
|
if bStats.TotalBlocked != 1 {
|
2016-01-29 23:31:32 +00:00
|
|
|
t.Fatalf("bad: %#v %#v", bStats, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 23:25:03 +00:00
|
|
|
func TestFSM_UpdateEval_Untrack(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-01-04 23:25:03 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.evalBroker.SetEnabled(true)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
// Mark an eval as blocked.
|
|
|
|
bEval := mock.Eval()
|
|
|
|
bEval.ClassEligibility = map[string]bool{"v1:123": true}
|
|
|
|
fsm.blockedEvals.Block(bEval)
|
|
|
|
|
|
|
|
// Create a successful eval for the same job
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.JobID = bEval.JobID
|
|
|
|
eval.Status = structs.EvalStatusComplete
|
|
|
|
|
|
|
|
req := structs.EvalUpdateRequest{
|
|
|
|
Evals: []*structs.Evaluation{eval},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.EvalUpdateRequestType, req)
|
|
|
|
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
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().EvalByID(ws, eval.ID)
|
2017-01-04 23:25:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if out.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", out.CreateIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the eval wasn't enqueued
|
|
|
|
stats := fsm.evalBroker.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v %#v", stats, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the eval was untracked in the blocked tracker.
|
|
|
|
bStats := fsm.blockedEvals.Stats()
|
|
|
|
if bStats.TotalBlocked != 0 {
|
|
|
|
t.Fatalf("bad: %#v %#v", bStats, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_UpdateEval_NoUntrack(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-01-04 23:25:03 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.evalBroker.SetEnabled(true)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
// Mark an eval as blocked.
|
|
|
|
bEval := mock.Eval()
|
|
|
|
bEval.ClassEligibility = map[string]bool{"v1:123": true}
|
|
|
|
fsm.blockedEvals.Block(bEval)
|
|
|
|
|
|
|
|
// Create a successful eval for the same job but with placement failures
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.JobID = bEval.JobID
|
|
|
|
eval.Status = structs.EvalStatusComplete
|
|
|
|
eval.FailedTGAllocs = make(map[string]*structs.AllocMetric)
|
|
|
|
eval.FailedTGAllocs["test"] = new(structs.AllocMetric)
|
|
|
|
|
|
|
|
req := structs.EvalUpdateRequest{
|
|
|
|
Evals: []*structs.Evaluation{eval},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.EvalUpdateRequestType, req)
|
|
|
|
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
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().EvalByID(ws, eval.ID)
|
2017-01-04 23:25:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if out.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", out.CreateIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the eval wasn't enqueued
|
|
|
|
stats := fsm.evalBroker.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v %#v", stats, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the eval was not untracked in the blocked tracker.
|
|
|
|
bStats := fsm.blockedEvals.Stats()
|
|
|
|
if bStats.TotalBlocked != 1 {
|
|
|
|
t.Fatalf("bad: %#v %#v", bStats, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 22:52:38 +00:00
|
|
|
func TestFSM_DeleteEval(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-23 22:52:38 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2015-07-23 22:52:38 +00:00
|
|
|
req := structs.EvalUpdateRequest{
|
2015-08-06 21:51:15 +00:00
|
|
|
Evals: []*structs.Evaluation{eval},
|
2015-07-23 22:52:38 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.EvalUpdateRequestType, req)
|
2015-07-23 22:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:30:11 +00:00
|
|
|
req2 := structs.EvalReapRequest{
|
2015-08-15 22:39:29 +00:00
|
|
|
Evals: []string{eval.ID},
|
2015-07-23 22:52:38 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err = structs.Encode(structs.EvalDeleteRequestType, req2)
|
2015-07-23 22:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
eval, err = fsm.State().EvalByID(ws, req.Evals[0].ID)
|
2015-07-23 22:52:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if eval != nil {
|
|
|
|
t.Fatalf("eval found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 02:00:33 +00:00
|
|
|
func TestFSM_UpdateAllocFromClient_Unblock(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-01-29 23:31:32 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
node := mock.Node()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertNode(structs.MsgTypeTestSetup, 1, node)
|
2016-01-29 23:31:32 +00:00
|
|
|
|
|
|
|
// Mark an eval as blocked.
|
|
|
|
eval := mock.Eval()
|
2016-01-30 01:46:44 +00:00
|
|
|
eval.ClassEligibility = map[string]bool{node.ComputedClass: true}
|
2016-01-29 23:31:32 +00:00
|
|
|
fsm.blockedEvals.Block(eval)
|
|
|
|
|
|
|
|
bStats := fsm.blockedEvals.Stats()
|
2016-01-30 00:45:09 +00:00
|
|
|
if bStats.TotalBlocked != 1 {
|
2016-01-29 23:31:32 +00:00
|
|
|
t.Fatalf("bad: %#v", bStats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a completed eval
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
2016-02-22 01:58:56 +00:00
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.NodeID = node.ID
|
2016-07-21 21:43:21 +00:00
|
|
|
state.UpsertJobSummary(8, mock.JobSummary(alloc.JobID))
|
|
|
|
state.UpsertJobSummary(9, mock.JobSummary(alloc2.JobID))
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertAllocs(structs.MsgTypeTestSetup, 10, []*structs.Allocation{alloc, alloc2})
|
2016-01-29 23:31:32 +00:00
|
|
|
|
|
|
|
clientAlloc := new(structs.Allocation)
|
|
|
|
*clientAlloc = *alloc
|
2016-03-24 01:08:19 +00:00
|
|
|
clientAlloc.ClientStatus = structs.AllocClientStatusComplete
|
2016-02-22 01:58:56 +00:00
|
|
|
update2 := &structs.Allocation{
|
|
|
|
ID: alloc2.ID,
|
2023-01-25 20:53:59 +00:00
|
|
|
NodeID: alloc2.NodeID,
|
2016-02-22 01:58:56 +00:00
|
|
|
ClientStatus: structs.AllocClientStatusRunning,
|
|
|
|
}
|
2016-01-29 23:31:32 +00:00
|
|
|
|
|
|
|
req := structs.AllocUpdateRequest{
|
2016-02-22 01:58:56 +00:00
|
|
|
Alloc: []*structs.Allocation{clientAlloc, update2},
|
2016-01-29 23:31:32 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.AllocClientUpdateRequestType, req)
|
2016-01-29 23:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2016-02-22 01:58:56 +00:00
|
|
|
// Verify we are updated
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().AllocByID(ws, alloc.ID)
|
2016-01-29 23:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
clientAlloc.CreateIndex = out.CreateIndex
|
|
|
|
clientAlloc.ModifyIndex = out.ModifyIndex
|
|
|
|
if !reflect.DeepEqual(clientAlloc, out) {
|
|
|
|
t.Fatalf("bad: %#v %#v", clientAlloc, out)
|
|
|
|
}
|
|
|
|
|
2017-02-08 05:22:48 +00:00
|
|
|
out, err = fsm.State().AllocByID(ws, alloc2.ID)
|
2016-02-22 01:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
alloc2.CreateIndex = out.CreateIndex
|
|
|
|
alloc2.ModifyIndex = out.ModifyIndex
|
|
|
|
alloc2.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
alloc2.TaskStates = nil
|
|
|
|
if !reflect.DeepEqual(alloc2, out) {
|
|
|
|
t.Fatalf("bad: %#v %#v", alloc2, out)
|
|
|
|
}
|
|
|
|
|
2016-01-29 23:31:32 +00:00
|
|
|
// Verify the eval was unblocked.
|
2016-01-30 02:18:29 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
bStats = fsm.blockedEvals.Stats()
|
|
|
|
if bStats.TotalBlocked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v %#v", bStats, out)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
2016-01-29 23:31:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:00:33 +00:00
|
|
|
func TestFSM_UpdateAllocFromClient(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-08-26 01:00:14 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2018-01-20 02:48:37 +00:00
|
|
|
require := require.New(t)
|
2015-08-26 01:00:14 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2016-07-21 21:43:21 +00:00
|
|
|
state.UpsertJobSummary(9, mock.JobSummary(alloc.JobID))
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertAllocs(structs.MsgTypeTestSetup, 10, []*structs.Allocation{alloc})
|
2015-08-26 01:00:14 +00:00
|
|
|
|
|
|
|
clientAlloc := new(structs.Allocation)
|
|
|
|
*clientAlloc = *alloc
|
|
|
|
clientAlloc.ClientStatus = structs.AllocClientStatusFailed
|
|
|
|
|
2018-01-20 02:48:37 +00:00
|
|
|
eval := mock.Eval()
|
|
|
|
eval.JobID = alloc.JobID
|
|
|
|
eval.TriggeredBy = structs.EvalTriggerRetryFailedAlloc
|
|
|
|
eval.Type = alloc.Job.Type
|
|
|
|
|
2015-08-26 01:00:14 +00:00
|
|
|
req := structs.AllocUpdateRequest{
|
|
|
|
Alloc: []*structs.Allocation{clientAlloc},
|
2018-01-20 02:48:37 +00:00
|
|
|
Evals: []*structs.Evaluation{eval},
|
2015-08-26 01:00:14 +00:00
|
|
|
}
|
2016-02-21 01:36:39 +00:00
|
|
|
buf, err := structs.Encode(structs.AllocClientUpdateRequestType, req)
|
2018-01-20 02:48:37 +00:00
|
|
|
require.Nil(err)
|
2015-08-26 01:00:14 +00:00
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2018-01-20 02:48:37 +00:00
|
|
|
require.Nil(resp)
|
2015-08-26 01:00:14 +00:00
|
|
|
|
|
|
|
// Verify we are registered
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().AllocByID(ws, alloc.ID)
|
2018-01-20 02:48:37 +00:00
|
|
|
require.Nil(err)
|
2015-08-26 01:00:14 +00:00
|
|
|
clientAlloc.CreateIndex = out.CreateIndex
|
|
|
|
clientAlloc.ModifyIndex = out.ModifyIndex
|
2018-01-20 02:48:37 +00:00
|
|
|
require.Equal(clientAlloc, out)
|
|
|
|
|
|
|
|
// Verify eval was inserted
|
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
evals, err := fsm.State().EvalsByJob(ws, eval.Namespace, eval.JobID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(1, len(evals))
|
|
|
|
res := evals[0]
|
|
|
|
eval.CreateIndex = res.CreateIndex
|
|
|
|
eval.ModifyIndex = res.ModifyIndex
|
|
|
|
require.Equal(eval, res)
|
2015-08-26 01:00:14 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
func TestFSM_UpdateAllocDesiredTransition(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-21 18:58:04 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.Job = alloc.Job
|
|
|
|
alloc2.JobID = alloc.JobID
|
|
|
|
state.UpsertJobSummary(9, mock.JobSummary(alloc.JobID))
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertAllocs(structs.MsgTypeTestSetup, 10, []*structs.Allocation{alloc, alloc2})
|
2018-02-21 18:58:04 +00:00
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
t1 := &structs.DesiredTransition{
|
2022-08-17 16:26:34 +00:00
|
|
|
Migrate: pointer.Of(true),
|
2018-02-21 18:58:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
eval := &structs.Evaluation{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Namespace: alloc.Namespace,
|
|
|
|
Priority: alloc.Job.Priority,
|
|
|
|
Type: alloc.Job.Type,
|
|
|
|
TriggeredBy: structs.EvalTriggerNodeDrain,
|
|
|
|
JobID: alloc.Job.ID,
|
|
|
|
JobModifyIndex: alloc.Job.ModifyIndex,
|
|
|
|
Status: structs.EvalStatusPending,
|
|
|
|
}
|
|
|
|
req := structs.AllocUpdateDesiredTransitionRequest{
|
|
|
|
Allocs: map[string]*structs.DesiredTransition{
|
2018-02-21 18:58:04 +00:00
|
|
|
alloc.ID: t1,
|
|
|
|
alloc2.ID: t1,
|
|
|
|
},
|
2018-02-23 01:38:44 +00:00
|
|
|
Evals: []*structs.Evaluation{eval},
|
2018-02-21 18:58:04 +00:00
|
|
|
}
|
2018-02-23 01:38:44 +00:00
|
|
|
buf, err := structs.Encode(structs.AllocUpdateDesiredTransitionRequestType, req)
|
2018-02-21 18:58:04 +00:00
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(resp)
|
|
|
|
|
|
|
|
// Verify we are registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, err := fsm.State().AllocByID(ws, alloc.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
out2, err := fsm.State().AllocByID(ws, alloc2.ID)
|
|
|
|
require.Nil(err)
|
2018-02-26 23:01:27 +00:00
|
|
|
evalOut, err := fsm.State().EvalByID(ws, eval.ID)
|
2018-02-23 01:38:44 +00:00
|
|
|
require.Nil(err)
|
2018-02-26 23:01:27 +00:00
|
|
|
require.NotNil(evalOut)
|
|
|
|
require.Equal(eval.ID, evalOut.ID)
|
2018-02-21 18:58:04 +00:00
|
|
|
|
2018-02-23 01:38:44 +00:00
|
|
|
require.NotNil(out1.DesiredTransition.Migrate)
|
|
|
|
require.NotNil(out2.DesiredTransition.Migrate)
|
|
|
|
require.True(*out1.DesiredTransition.Migrate)
|
|
|
|
require.True(*out2.DesiredTransition.Migrate)
|
2018-02-21 18:58:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 20:13:51 +00:00
|
|
|
func TestFSM_UpsertVaultAccessor(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-08-19 20:13:51 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
va := mock.VaultAccessor()
|
|
|
|
va2 := mock.VaultAccessor()
|
2016-08-22 20:57:27 +00:00
|
|
|
req := structs.VaultAccessorsRequest{
|
2016-08-19 20:13:51 +00:00
|
|
|
Accessors: []*structs.VaultAccessor{va, va2},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.VaultAccessorRegisterRequestType, req)
|
|
|
|
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
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, err := fsm.State().VaultAccessor(ws, va.Accessor)
|
2016-08-19 20:13:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out1 == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if out1.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", out1.CreateIndex)
|
|
|
|
}
|
2017-02-08 05:22:48 +00:00
|
|
|
out2, err := fsm.State().VaultAccessor(ws, va2.Accessor)
|
2016-08-19 20:13:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out2 == nil {
|
|
|
|
t.Fatalf("not found!")
|
|
|
|
}
|
|
|
|
if out1.CreateIndex != 1 {
|
|
|
|
t.Fatalf("bad index: %d", out2.CreateIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := fsm.TimeTable()
|
|
|
|
index := tt.NearestIndex(time.Now().UTC())
|
|
|
|
if index != 1 {
|
|
|
|
t.Fatalf("bad: %d", index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 20:57:27 +00:00
|
|
|
func TestFSM_DeregisterVaultAccessor(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-08-22 20:57:27 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
va := mock.VaultAccessor()
|
|
|
|
va2 := mock.VaultAccessor()
|
|
|
|
accessors := []*structs.VaultAccessor{va, va2}
|
|
|
|
|
|
|
|
// Insert the accessors
|
|
|
|
if err := fsm.State().UpsertVaultAccessor(1000, accessors); err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.VaultAccessorsRequest{
|
|
|
|
Accessors: accessors,
|
|
|
|
}
|
2018-03-11 17:53:22 +00:00
|
|
|
buf, err := structs.Encode(structs.VaultAccessorDeregisterRequestType, req)
|
2016-08-22 20:57:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, err := fsm.State().VaultAccessor(ws, va.Accessor)
|
2016-08-22 20:57:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out1 != nil {
|
|
|
|
t.Fatalf("not deleted!")
|
|
|
|
}
|
|
|
|
|
|
|
|
tt := fsm.TimeTable()
|
|
|
|
index := tt.NearestIndex(time.Now().UTC())
|
|
|
|
if index != 1 {
|
|
|
|
t.Fatalf("bad: %d", index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
func TestFSM_UpsertSITokenAccessor(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-06 20:46:46 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
a1 := mock.SITokenAccessor()
|
|
|
|
a2 := mock.SITokenAccessor()
|
|
|
|
request := structs.SITokenAccessorsRequest{
|
|
|
|
Accessors: []*structs.SITokenAccessor{a1, a2},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ServiceIdentityAccessorRegisterRequestType, request)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
response := fsm.Apply(makeLog(buf))
|
|
|
|
r.Nil(response)
|
|
|
|
|
|
|
|
// Verify the accessors got registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
result1, err := fsm.State().SITokenAccessor(ws, a1.AccessorID)
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotNil(result1)
|
|
|
|
r.Equal(uint64(1), result1.CreateIndex)
|
|
|
|
|
|
|
|
result2, err := fsm.State().SITokenAccessor(ws, a2.AccessorID)
|
|
|
|
r.NoError(err)
|
|
|
|
r.NotNil(result2)
|
|
|
|
r.Equal(uint64(1), result2.CreateIndex)
|
|
|
|
|
|
|
|
tt := fsm.TimeTable()
|
|
|
|
latestIndex := tt.NearestIndex(time.Now())
|
|
|
|
r.Equal(uint64(1), latestIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeregisterSITokenAccessor(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-06 20:46:46 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.blockedEvals.SetEnabled(true)
|
|
|
|
|
|
|
|
a1 := mock.SITokenAccessor()
|
|
|
|
a2 := mock.SITokenAccessor()
|
|
|
|
accessors := []*structs.SITokenAccessor{a1, a2}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Insert the accessors
|
|
|
|
err = fsm.State().UpsertSITokenAccessors(1000, accessors)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
request := structs.SITokenAccessorsRequest{Accessors: accessors}
|
|
|
|
buf, err := structs.Encode(structs.ServiceIdentityAccessorDeregisterRequestType, request)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
response := fsm.Apply(makeLog(buf))
|
|
|
|
r.Nil(response)
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
|
|
|
|
result1, err := fsm.State().SITokenAccessor(ws, a1.AccessorID)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Nil(result1) // should have been deleted
|
|
|
|
|
|
|
|
result2, err := fsm.State().SITokenAccessor(ws, a2.AccessorID)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Nil(result2) // should have been deleted
|
|
|
|
|
|
|
|
tt := fsm.TimeTable()
|
|
|
|
latestIndex := tt.NearestIndex(time.Now())
|
|
|
|
r.Equal(uint64(1), latestIndex)
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:31:32 +00:00
|
|
|
func TestFSM_ApplyPlanResults(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-06-27 17:31:32 +00:00
|
|
|
fsm := testFSM(t)
|
2018-10-18 03:29:29 +00:00
|
|
|
fsm.evalBroker.SetEnabled(true)
|
2017-06-27 17:31:32 +00:00
|
|
|
// Create the request and create a deployment
|
|
|
|
alloc := mock.Alloc()
|
2018-10-03 16:47:18 +00:00
|
|
|
alloc.Resources = &structs.Resources{} // COMPAT(0.11): Remove in 0.11, used to bypass resource creation in state store
|
2017-06-27 17:31:32 +00:00
|
|
|
job := alloc.Job
|
|
|
|
alloc.Job = nil
|
|
|
|
|
|
|
|
d := mock.Deployment()
|
|
|
|
d.JobID = job.ID
|
|
|
|
d.JobModifyIndex = job.ModifyIndex
|
|
|
|
d.JobVersion = job.Version
|
|
|
|
|
|
|
|
alloc.DeploymentID = d.ID
|
|
|
|
|
2017-12-18 16:03:55 +00:00
|
|
|
eval := mock.Eval()
|
|
|
|
eval.JobID = job.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
fsm.State().UpsertEvals(structs.MsgTypeTestSetup, 1, []*structs.Evaluation{eval})
|
2017-12-18 16:03:55 +00:00
|
|
|
|
2017-06-27 17:31:32 +00:00
|
|
|
fsm.State().UpsertJobSummary(1, mock.JobSummary(alloc.JobID))
|
2018-10-18 03:29:29 +00:00
|
|
|
|
|
|
|
// set up preempted jobs and allocs
|
|
|
|
job1 := mock.Job()
|
|
|
|
job2 := mock.Job()
|
|
|
|
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.Job = job1
|
|
|
|
alloc1.JobID = job1.ID
|
|
|
|
alloc1.PreemptedByAllocation = alloc.ID
|
|
|
|
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.Job = job2
|
|
|
|
alloc2.JobID = job2.ID
|
|
|
|
alloc2.PreemptedByAllocation = alloc.ID
|
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
fsm.State().UpsertAllocs(structs.MsgTypeTestSetup, 1, []*structs.Allocation{alloc1, alloc2})
|
2018-10-18 03:29:29 +00:00
|
|
|
|
|
|
|
// evals for preempted jobs
|
|
|
|
eval1 := mock.Eval()
|
|
|
|
eval1.JobID = job1.ID
|
|
|
|
|
|
|
|
eval2 := mock.Eval()
|
|
|
|
eval2.JobID = job2.ID
|
|
|
|
|
2017-06-27 17:31:32 +00:00
|
|
|
req := structs.ApplyPlanResultsRequest{
|
|
|
|
AllocUpdateRequest: structs.AllocUpdateRequest{
|
|
|
|
Job: job,
|
|
|
|
Alloc: []*structs.Allocation{alloc},
|
|
|
|
},
|
2018-10-18 03:29:29 +00:00
|
|
|
Deployment: d,
|
|
|
|
EvalID: eval.ID,
|
|
|
|
NodePreemptions: []*structs.Allocation{alloc1, alloc2},
|
|
|
|
PreemptionEvals: []*structs.Evaluation{eval1, eval2},
|
2017-06-27 17:31:32 +00:00
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ApplyPlanResultsRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the allocation is registered
|
|
|
|
ws := memdb.NewWatchSet()
|
2017-12-18 16:03:55 +00:00
|
|
|
assert := assert.New(t)
|
2017-06-27 17:31:32 +00:00
|
|
|
out, err := fsm.State().AllocByID(ws, alloc.ID)
|
2017-12-18 16:03:55 +00:00
|
|
|
assert.Nil(err)
|
2017-06-27 17:31:32 +00:00
|
|
|
alloc.CreateIndex = out.CreateIndex
|
|
|
|
alloc.ModifyIndex = out.ModifyIndex
|
|
|
|
alloc.AllocModifyIndex = out.AllocModifyIndex
|
|
|
|
|
|
|
|
// Job should be re-attached
|
|
|
|
alloc.Job = job
|
2017-12-18 16:03:55 +00:00
|
|
|
assert.Equal(alloc, out)
|
2017-06-27 17:31:32 +00:00
|
|
|
|
2018-10-18 03:29:29 +00:00
|
|
|
// Verify that evals for preempted jobs have been created
|
|
|
|
e1, err := fsm.State().EvalByID(ws, eval1.ID)
|
|
|
|
require := require.New(t)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(e1)
|
|
|
|
|
|
|
|
e2, err := fsm.State().EvalByID(ws, eval2.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(e2)
|
|
|
|
|
|
|
|
// Verify that eval broker has both evals
|
|
|
|
_, ok := fsm.evalBroker.evals[e1.ID]
|
|
|
|
require.True(ok)
|
|
|
|
|
|
|
|
_, ok = fsm.evalBroker.evals[e1.ID]
|
|
|
|
require.True(ok)
|
|
|
|
|
2017-06-27 17:31:32 +00:00
|
|
|
dout, err := fsm.State().DeploymentByID(ws, d.ID)
|
2017-12-18 16:03:55 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
tg, ok := dout.TaskGroups[alloc.TaskGroup]
|
|
|
|
assert.True(ok)
|
|
|
|
assert.NotNil(tg)
|
|
|
|
assert.Equal(1, tg.PlacedAllocs)
|
2017-06-27 17:31:32 +00:00
|
|
|
|
|
|
|
// Ensure that the original job is used
|
|
|
|
evictAlloc := alloc.Copy()
|
|
|
|
job = mock.Job()
|
|
|
|
job.Priority = 123
|
2017-12-18 16:03:55 +00:00
|
|
|
eval = mock.Eval()
|
|
|
|
eval.JobID = job.ID
|
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
fsm.State().UpsertEvals(structs.MsgTypeTestSetup, 2, []*structs.Evaluation{eval})
|
2017-06-27 17:31:32 +00:00
|
|
|
|
|
|
|
evictAlloc.Job = nil
|
|
|
|
evictAlloc.DesiredStatus = structs.AllocDesiredStatusEvict
|
|
|
|
req2 := structs.ApplyPlanResultsRequest{
|
|
|
|
AllocUpdateRequest: structs.AllocUpdateRequest{
|
|
|
|
Job: job,
|
|
|
|
Alloc: []*structs.Allocation{evictAlloc},
|
|
|
|
},
|
2017-12-18 16:03:55 +00:00
|
|
|
EvalID: eval.ID,
|
2017-06-27 17:31:32 +00:00
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.ApplyPlanResultsRequestType, req2)
|
2017-12-18 16:03:55 +00:00
|
|
|
assert.Nil(err)
|
2017-06-27 17:31:32 +00:00
|
|
|
|
2017-12-18 16:03:55 +00:00
|
|
|
log := makeLog(buf)
|
|
|
|
//set the index to something other than 1
|
|
|
|
log.Index = 25
|
|
|
|
resp = fsm.Apply(log)
|
|
|
|
assert.Nil(resp)
|
2017-06-27 17:31:32 +00:00
|
|
|
|
|
|
|
// Verify we are evicted
|
|
|
|
out, err = fsm.State().AllocByID(ws, alloc.ID)
|
2017-12-18 16:03:55 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(structs.AllocDesiredStatusEvict, out.DesiredStatus)
|
|
|
|
assert.NotNil(out.Job)
|
|
|
|
assert.NotEqual(123, out.Job.Priority)
|
|
|
|
|
|
|
|
evalOut, err := fsm.State().EvalByID(ws, eval.ID)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Equal(log.Index, evalOut.ModifyIndex)
|
|
|
|
|
2017-06-27 17:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeploymentStatusUpdate(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-06-27 17:31:32 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.evalBroker.SetEnabled(true)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
// Upsert a deployment
|
|
|
|
d := mock.Deployment()
|
2017-07-04 20:31:01 +00:00
|
|
|
if err := state.UpsertDeployment(1, d); err != nil {
|
2017-06-27 17:31:32 +00:00
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a request to update the deployment, create an eval and job
|
|
|
|
e := mock.Eval()
|
|
|
|
j := mock.Job()
|
|
|
|
status, desc := structs.DeploymentStatusFailed, "foo"
|
|
|
|
req := &structs.DeploymentStatusUpdateRequest{
|
|
|
|
DeploymentUpdate: &structs.DeploymentStatusUpdate{
|
|
|
|
DeploymentID: d.ID,
|
|
|
|
Status: status,
|
|
|
|
StatusDescription: desc,
|
|
|
|
},
|
|
|
|
Job: j,
|
|
|
|
Eval: e,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.DeploymentStatusUpdateRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the status was updated properly
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
dout, err := state.DeploymentByID(ws, d.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if dout.Status != status || dout.StatusDescription != desc {
|
|
|
|
t.Fatalf("bad: %#v", dout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the evaluation was created
|
|
|
|
eout, _ := state.EvalByID(ws, e.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if eout == nil {
|
|
|
|
t.Fatalf("bad: %#v", eout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the job was created
|
2017-09-07 23:56:15 +00:00
|
|
|
jout, _ := state.JobByID(ws, j.Namespace, j.ID)
|
2017-06-27 17:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if jout == nil {
|
|
|
|
t.Fatalf("bad: %#v", jout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the eval was enqueued
|
|
|
|
stats := fsm.evalBroker.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v %#v", stats, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 19:49:13 +00:00
|
|
|
func TestFSM_JobStabilityUpdate(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-06 19:49:13 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.evalBroker.SetEnabled(true)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
// Upsert a deployment
|
|
|
|
job := mock.Job()
|
2020-10-19 13:30:15 +00:00
|
|
|
if err := state.UpsertJob(structs.MsgTypeTestSetup, 1, job); err != nil {
|
2017-07-06 19:49:13 +00:00
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a request to update the job to stable
|
|
|
|
req := &structs.JobStabilityRequest{
|
|
|
|
JobID: job.ID,
|
|
|
|
JobVersion: job.Version,
|
|
|
|
Stable: true,
|
2017-09-07 23:56:15 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Namespace: job.Namespace,
|
|
|
|
},
|
2017-07-06 19:49:13 +00:00
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobStabilityRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the stability was updated properly
|
|
|
|
ws := memdb.NewWatchSet()
|
2017-09-07 23:56:15 +00:00
|
|
|
jout, _ := state.JobByIDAndVersion(ws, job.Namespace, job.ID, job.Version)
|
2017-07-06 19:49:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if jout == nil || !jout.Stable {
|
|
|
|
t.Fatalf("bad: %#v", jout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:31:32 +00:00
|
|
|
func TestFSM_DeploymentPromotion(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-06-27 17:31:32 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.evalBroker.SetEnabled(true)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
// Create a job with two task groups
|
|
|
|
j := mock.Job()
|
|
|
|
tg1 := j.TaskGroups[0]
|
|
|
|
tg2 := tg1.Copy()
|
|
|
|
tg2.Name = "foo"
|
|
|
|
j.TaskGroups = append(j.TaskGroups, tg2)
|
2020-10-19 13:30:15 +00:00
|
|
|
if err := state.UpsertJob(structs.MsgTypeTestSetup, 1, j); err != nil {
|
2017-06-27 17:31:32 +00:00
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a deployment
|
|
|
|
d := mock.Deployment()
|
|
|
|
d.JobID = j.ID
|
|
|
|
d.TaskGroups = map[string]*structs.DeploymentState{
|
2017-09-26 22:26:33 +00:00
|
|
|
"web": {
|
2017-06-27 17:31:32 +00:00
|
|
|
DesiredTotal: 10,
|
|
|
|
DesiredCanaries: 1,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"foo": {
|
2017-06-27 17:31:32 +00:00
|
|
|
DesiredTotal: 10,
|
|
|
|
DesiredCanaries: 1,
|
|
|
|
},
|
|
|
|
}
|
2017-07-04 20:31:01 +00:00
|
|
|
if err := state.UpsertDeployment(2, d); err != nil {
|
2017-06-27 17:31:32 +00:00
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a set of allocations
|
|
|
|
c1 := mock.Alloc()
|
|
|
|
c1.JobID = j.ID
|
|
|
|
c1.DeploymentID = d.ID
|
2017-07-06 16:55:39 +00:00
|
|
|
d.TaskGroups[c1.TaskGroup].PlacedCanaries = append(d.TaskGroups[c1.TaskGroup].PlacedCanaries, c1.ID)
|
2017-06-27 17:31:32 +00:00
|
|
|
c1.DeploymentStatus = &structs.AllocDeploymentStatus{
|
2022-08-17 16:26:34 +00:00
|
|
|
Healthy: pointer.Of(true),
|
2017-06-27 17:31:32 +00:00
|
|
|
}
|
|
|
|
c2 := mock.Alloc()
|
|
|
|
c2.JobID = j.ID
|
|
|
|
c2.DeploymentID = d.ID
|
2017-07-06 16:55:39 +00:00
|
|
|
d.TaskGroups[c2.TaskGroup].PlacedCanaries = append(d.TaskGroups[c2.TaskGroup].PlacedCanaries, c2.ID)
|
2017-06-27 17:31:32 +00:00
|
|
|
c2.TaskGroup = tg2.Name
|
|
|
|
c2.DeploymentStatus = &structs.AllocDeploymentStatus{
|
2022-08-17 16:26:34 +00:00
|
|
|
Healthy: pointer.Of(true),
|
2017-06-27 17:31:32 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
if err := state.UpsertAllocs(structs.MsgTypeTestSetup, 3, []*structs.Allocation{c1, c2}); err != nil {
|
2017-06-27 17:31:32 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an eval
|
|
|
|
e := mock.Eval()
|
|
|
|
|
|
|
|
// Promote the canaries
|
|
|
|
req := &structs.ApplyDeploymentPromoteRequest{
|
|
|
|
DeploymentPromoteRequest: structs.DeploymentPromoteRequest{
|
|
|
|
DeploymentID: d.ID,
|
|
|
|
All: true,
|
|
|
|
},
|
|
|
|
Eval: e,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.DeploymentPromoteRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the status per task group was updated properly
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
dout, err := state.DeploymentByID(ws, d.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if len(dout.TaskGroups) != 2 {
|
|
|
|
t.Fatalf("bad: %#v", dout.TaskGroups)
|
|
|
|
}
|
|
|
|
for tg, state := range dout.TaskGroups {
|
|
|
|
if !state.Promoted {
|
|
|
|
t.Fatalf("bad: group %q not promoted %#v", tg, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the evaluation was created
|
|
|
|
eout, _ := state.EvalByID(ws, e.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if eout == nil {
|
|
|
|
t.Fatalf("bad: %#v", eout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the eval was enqueued
|
|
|
|
stats := fsm.evalBroker.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v %#v", stats, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeploymentAllocHealth(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-06-27 17:31:32 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
fsm.evalBroker.SetEnabled(true)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
// Insert a deployment
|
|
|
|
d := mock.Deployment()
|
2017-07-04 20:31:01 +00:00
|
|
|
if err := state.UpsertDeployment(1, d); err != nil {
|
2017-06-27 17:31:32 +00:00
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert two allocations
|
|
|
|
a1 := mock.Alloc()
|
|
|
|
a1.DeploymentID = d.ID
|
|
|
|
a2 := mock.Alloc()
|
|
|
|
a2.DeploymentID = d.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
if err := state.UpsertAllocs(structs.MsgTypeTestSetup, 2, []*structs.Allocation{a1, a2}); err != nil {
|
2017-06-27 17:31:32 +00:00
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a job to roll back to
|
|
|
|
j := mock.Job()
|
|
|
|
|
|
|
|
// Create an eval that should be upserted
|
|
|
|
e := mock.Eval()
|
|
|
|
|
|
|
|
// Create a status update for the deployment
|
|
|
|
status, desc := structs.DeploymentStatusFailed, "foo"
|
|
|
|
u := &structs.DeploymentStatusUpdate{
|
|
|
|
DeploymentID: d.ID,
|
|
|
|
Status: status,
|
|
|
|
StatusDescription: desc,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set health against the deployment
|
|
|
|
req := &structs.ApplyDeploymentAllocHealthRequest{
|
|
|
|
DeploymentAllocHealthRequest: structs.DeploymentAllocHealthRequest{
|
|
|
|
DeploymentID: d.ID,
|
|
|
|
HealthyAllocationIDs: []string{a1.ID},
|
|
|
|
UnhealthyAllocationIDs: []string{a2.ID},
|
|
|
|
},
|
|
|
|
Job: j,
|
|
|
|
Eval: e,
|
|
|
|
DeploymentUpdate: u,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.DeploymentAllocHealthRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the status was updated properly
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
dout, err := state.DeploymentByID(ws, d.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if dout.Status != status || dout.StatusDescription != desc {
|
|
|
|
t.Fatalf("bad: %#v", dout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the evaluation was created
|
|
|
|
eout, _ := state.EvalByID(ws, e.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if eout == nil {
|
|
|
|
t.Fatalf("bad: %#v", eout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the job was created
|
2017-09-07 23:56:15 +00:00
|
|
|
jout, _ := state.JobByID(ws, j.Namespace, j.ID)
|
2017-06-27 17:31:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
if jout == nil {
|
|
|
|
t.Fatalf("bad: %#v", jout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the status of the allocs
|
|
|
|
out1, err := state.AllocByID(ws, a1.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
out2, err := state.AllocByID(ws, a2.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !out1.DeploymentStatus.IsHealthy() {
|
|
|
|
t.Fatalf("bad: alloc %q not healthy", out1.ID)
|
|
|
|
}
|
|
|
|
if !out2.DeploymentStatus.IsUnhealthy() {
|
|
|
|
t.Fatalf("bad: alloc %q not unhealthy", out2.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert the eval was enqueued
|
|
|
|
stats := fsm.evalBroker.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v %#v", stats, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 19:32:37 +00:00
|
|
|
func TestFSM_DeleteDeployment(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-06-29 19:32:37 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
// Upsert a deployments
|
|
|
|
d := mock.Deployment()
|
2017-07-04 20:31:01 +00:00
|
|
|
if err := state.UpsertDeployment(1, d); err != nil {
|
2017-06-29 19:32:37 +00:00
|
|
|
t.Fatalf("bad: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.DeploymentDeleteRequest{
|
|
|
|
Deployments: []string{d.ID},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.DeploymentDeleteRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
deployment, err := state.DeploymentByID(ws, d.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if deployment != nil {
|
|
|
|
t.Fatalf("deployment found!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 04:01:14 +00:00
|
|
|
func TestFSM_UpsertACLPolicies(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-08 04:01:14 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
policy := mock.ACLPolicy()
|
|
|
|
req := structs.ACLPolicyUpsertRequest{
|
|
|
|
Policies: []*structs.ACLPolicy{policy},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLPolicyUpsertRequestType, req)
|
|
|
|
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
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().ACLPolicyByName(ws, policy.Name)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out)
|
|
|
|
}
|
|
|
|
|
2017-08-08 03:53:07 +00:00
|
|
|
func TestFSM_DeleteACLPolicies(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-08 03:53:07 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
policy := mock.ACLPolicy()
|
2020-12-01 16:11:34 +00:00
|
|
|
err := fsm.State().UpsertACLPolicies(structs.MsgTypeTestSetup, 1000, []*structs.ACLPolicy{policy})
|
2017-08-08 03:53:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
req := structs.ACLPolicyDeleteRequest{
|
|
|
|
Names: []string{policy.Name},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLPolicyDeleteRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().ACLPolicyByName(ws, policy.Name)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, out)
|
|
|
|
}
|
|
|
|
|
2017-08-21 01:19:26 +00:00
|
|
|
func TestFSM_BootstrapACLTokens(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-21 01:19:26 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
token := mock.ACLToken()
|
|
|
|
req := structs.ACLTokenBootstrapRequest{
|
|
|
|
Token: token,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLTokenBootstrapRequestType, req)
|
|
|
|
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
|
|
|
|
out, err := fsm.State().ACLTokenByAccessorID(nil, token.AccessorID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out)
|
2017-09-10 23:03:30 +00:00
|
|
|
|
2017-09-11 17:30:59 +00:00
|
|
|
// Test with reset
|
2017-09-10 23:03:30 +00:00
|
|
|
token2 := mock.ACLToken()
|
|
|
|
req = structs.ACLTokenBootstrapRequest{
|
|
|
|
Token: token2,
|
|
|
|
ResetIndex: out.CreateIndex,
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.ACLTokenBootstrapRequestType, req)
|
|
|
|
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
|
|
|
|
out2, err := fsm.State().ACLTokenByAccessorID(nil, token2.AccessorID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out2)
|
2017-08-21 01:19:26 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 22:44:05 +00:00
|
|
|
func TestFSM_UpsertACLTokens(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-12 22:44:05 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
token := mock.ACLToken()
|
|
|
|
req := structs.ACLTokenUpsertRequest{
|
|
|
|
Tokens: []*structs.ACLToken{token},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLTokenUpsertRequestType, req)
|
|
|
|
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
|
|
|
|
ws := memdb.NewWatchSet()
|
2017-08-12 22:46:19 +00:00
|
|
|
out, err := fsm.State().ACLTokenByAccessorID(ws, token.AccessorID)
|
2017-08-12 22:44:05 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeleteACLTokens(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-12 22:44:05 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
token := mock.ACLToken()
|
2020-12-01 16:11:34 +00:00
|
|
|
err := fsm.State().UpsertACLTokens(structs.MsgTypeTestSetup, 1000, []*structs.ACLToken{token})
|
2017-08-12 22:44:05 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
req := structs.ACLTokenDeleteRequest{
|
|
|
|
AccessorIDs: []string{token.AccessorID},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLTokenDeleteRequestType, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
ws := memdb.NewWatchSet()
|
2017-08-12 22:46:19 +00:00
|
|
|
out, err := fsm.State().ACLTokenByAccessorID(ws, token.AccessorID)
|
2017-08-12 22:44:05 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, out)
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:01:10 +00:00
|
|
|
func testSnapshotRestore(t *testing.T, fsm *nomadFSM) *nomadFSM {
|
|
|
|
// 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
|
|
|
|
fsm2 := testFSM(t)
|
2017-02-08 19:18:03 +00:00
|
|
|
snap, err = fsm2.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
|
|
|
|
abandonCh := fsm2.State().AbandonCh()
|
2015-07-06 20:01:10 +00:00
|
|
|
|
|
|
|
// Do a restore
|
|
|
|
if err := fsm2.Restore(sink); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-02-08 19:18:03 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-abandonCh:
|
|
|
|
default:
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:01:10 +00:00
|
|
|
return fsm2
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_Nodes(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-06 20:01:10 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2021-02-11 15:40:59 +00:00
|
|
|
node := mock.Node()
|
|
|
|
state.UpsertNode(structs.MsgTypeTestSetup, 1000, node)
|
2015-07-06 20:01:10 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2021-02-11 15:40:59 +00:00
|
|
|
out, _ := state2.NodeByID(nil, node.ID)
|
|
|
|
if !reflect.DeepEqual(node, out) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out, node)
|
2015-07-06 20:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-06 21:51:01 +00:00
|
|
|
|
2015-07-07 16:55:47 +00:00
|
|
|
func TestFSM_SnapshotRestore_Jobs(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-07 16:55:47 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
job1 := mock.Job()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 1000, job1)
|
2015-08-11 21:27:14 +00:00
|
|
|
job2 := mock.Job()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 1001, job2)
|
2015-07-07 16:55:47 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2015-07-07 16:55:47 +00:00
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2017-09-07 23:56:15 +00:00
|
|
|
out1, _ := state2.JobByID(ws, job1.Namespace, job1.ID)
|
|
|
|
out2, _ := state2.JobByID(ws, job2.Namespace, job2.ID)
|
2015-07-07 16:55:47 +00:00
|
|
|
if !reflect.DeepEqual(job1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, job1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(job2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, job2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-23 22:52:38 +00:00
|
|
|
func TestFSM_SnapshotRestore_Evals(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-23 22:52:38 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
eval1 := mock.Eval()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertEvals(structs.MsgTypeTestSetup, 1000, []*structs.Evaluation{eval1})
|
2015-08-11 21:27:14 +00:00
|
|
|
eval2 := mock.Eval()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertEvals(structs.MsgTypeTestSetup, 1001, []*structs.Evaluation{eval2})
|
2015-07-23 22:52:38 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, _ := state2.EvalByID(ws, eval1.ID)
|
|
|
|
out2, _ := state2.EvalByID(ws, eval2.ID)
|
2015-07-23 22:52:38 +00:00
|
|
|
if !reflect.DeepEqual(eval1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, eval1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(eval2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, eval2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:04:26 +00:00
|
|
|
func TestFSM_SnapshotRestore_Allocs(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-08-04 21:04:26 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc2 := mock.Alloc()
|
2016-07-21 21:43:21 +00:00
|
|
|
state.UpsertJobSummary(998, mock.JobSummary(alloc1.JobID))
|
|
|
|
state.UpsertJobSummary(999, mock.JobSummary(alloc2.JobID))
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc1})
|
|
|
|
state.UpsertAllocs(structs.MsgTypeTestSetup, 1001, []*structs.Allocation{alloc2})
|
2015-08-04 21:04:26 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, _ := state2.AllocByID(ws, alloc1.ID)
|
|
|
|
out2, _ := state2.AllocByID(ws, alloc2.ID)
|
2015-08-04 21:04:26 +00:00
|
|
|
if !reflect.DeepEqual(alloc1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, alloc1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(alloc2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, alloc2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 22:23:56 +00:00
|
|
|
func TestFSM_SnapshotRestore_Allocs_Canonicalize(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-01-08 22:23:56 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
|
|
|
|
// remove old versions to force migration path
|
|
|
|
alloc.AllocatedResources = nil
|
|
|
|
|
|
|
|
state.UpsertJobSummary(998, mock.JobSummary(alloc.JobID))
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc})
|
2020-01-08 22:23:56 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := state2.AllocByID(ws, alloc.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotNil(t, out.AllocatedResources)
|
|
|
|
require.Contains(t, out.AllocatedResources.Tasks, "web")
|
|
|
|
|
|
|
|
alloc.Canonicalize()
|
|
|
|
require.Equal(t, alloc, out)
|
|
|
|
}
|
|
|
|
|
2015-07-06 21:51:01 +00:00
|
|
|
func TestFSM_SnapshotRestore_Indexes(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-06 21:51:01 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
2015-08-11 21:27:14 +00:00
|
|
|
node1 := mock.Node()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertNode(structs.MsgTypeTestSetup, 1000, node1)
|
2015-07-06 21:51:01 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
|
2015-09-07 03:56:38 +00:00
|
|
|
index, err := state2.Index("nodes")
|
2015-07-06 21:51:01 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if index != 1000 {
|
|
|
|
t.Fatalf("bad: %d", index)
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 00:38:13 +00:00
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_TimeTable(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-08-16 00:38:13 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
tt := fsm.TimeTable()
|
|
|
|
start := time.Now().UTC()
|
|
|
|
tt.Witness(1000, start)
|
|
|
|
tt.Witness(2000, start.Add(10*time.Minute))
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
|
|
|
|
tt2 := fsm2.TimeTable()
|
|
|
|
if tt2.NearestTime(1500) != start {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if tt2.NearestIndex(start.Add(15*time.Minute)) != 2000 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
2015-12-07 23:58:17 +00:00
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_PeriodicLaunches(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-12-07 23:58:17 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
job1 := mock.Job()
|
2017-09-07 23:56:15 +00:00
|
|
|
launch1 := &structs.PeriodicLaunch{
|
|
|
|
ID: job1.ID,
|
|
|
|
Namespace: job1.Namespace,
|
|
|
|
Launch: time.Now(),
|
|
|
|
}
|
2015-12-07 23:58:17 +00:00
|
|
|
state.UpsertPeriodicLaunch(1000, launch1)
|
|
|
|
job2 := mock.Job()
|
2017-09-07 23:56:15 +00:00
|
|
|
launch2 := &structs.PeriodicLaunch{
|
|
|
|
ID: job2.ID,
|
|
|
|
Namespace: job2.Namespace,
|
|
|
|
Launch: time.Now(),
|
|
|
|
}
|
2015-12-07 23:58:17 +00:00
|
|
|
state.UpsertPeriodicLaunch(1001, launch2)
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2017-09-07 23:56:15 +00:00
|
|
|
out1, _ := state2.PeriodicLaunchByID(ws, launch1.Namespace, launch1.ID)
|
|
|
|
out2, _ := state2.PeriodicLaunchByID(ws, launch2.Namespace, launch2.ID)
|
2017-09-01 23:40:27 +00:00
|
|
|
|
|
|
|
if !cmp.Equal(launch1, out1) {
|
|
|
|
t.Fatalf("bad: %v", cmp.Diff(launch1, out1))
|
2015-12-07 23:58:17 +00:00
|
|
|
}
|
2017-09-01 23:40:27 +00:00
|
|
|
if !cmp.Equal(launch2, out2) {
|
|
|
|
t.Fatalf("bad: %v", cmp.Diff(launch2, out2))
|
2015-12-07 23:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-05 20:55:11 +00:00
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_JobSummary(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-07-05 20:55:11 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
job1 := mock.Job()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 1000, job1)
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2017-09-07 23:56:15 +00:00
|
|
|
js1, _ := state.JobSummaryByID(ws, job1.Namespace, job1.ID)
|
2016-07-05 20:55:11 +00:00
|
|
|
|
|
|
|
job2 := mock.Job()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 1001, job2)
|
2017-09-07 23:56:15 +00:00
|
|
|
js2, _ := state.JobSummaryByID(ws, job2.Namespace, job2.ID)
|
2016-07-05 20:55:11 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2017-09-07 23:56:15 +00:00
|
|
|
out1, _ := state2.JobSummaryByID(ws, job1.Namespace, job1.ID)
|
|
|
|
out2, _ := state2.JobSummaryByID(ws, job2.Namespace, job2.ID)
|
2016-07-05 20:55:11 +00:00
|
|
|
if !reflect.DeepEqual(js1, out1) {
|
2016-07-25 21:11:32 +00:00
|
|
|
t.Fatalf("bad: \n%#v\n%#v", js1, out1)
|
2016-07-05 20:55:11 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(js2, out2) {
|
2016-07-25 21:11:32 +00:00
|
|
|
t.Fatalf("bad: \n%#v\n%#v", js2, out2)
|
2016-07-05 20:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-26 00:26:10 +00:00
|
|
|
|
2016-08-19 01:14:58 +00:00
|
|
|
func TestFSM_SnapshotRestore_VaultAccessors(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-08-19 01:14:58 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
a1 := mock.VaultAccessor()
|
|
|
|
a2 := mock.VaultAccessor()
|
|
|
|
state.UpsertVaultAccessor(1000, []*structs.VaultAccessor{a1, a2})
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, _ := state2.VaultAccessor(ws, a1.Accessor)
|
|
|
|
out2, _ := state2.VaultAccessor(ws, a2.Accessor)
|
2016-08-19 01:14:58 +00:00
|
|
|
if !reflect.DeepEqual(a1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, a1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(a2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, a2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:49:23 +00:00
|
|
|
func TestFSM_SnapshotRestore_JobVersions(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-04-24 21:49:23 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
job1 := mock.Job()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 1000, job1)
|
2017-04-24 21:49:23 +00:00
|
|
|
job2 := mock.Job()
|
|
|
|
job2.ID = job1.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 1001, job2)
|
2017-04-24 21:49:23 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2017-09-07 23:56:15 +00:00
|
|
|
out1, _ := state2.JobByIDAndVersion(ws, job1.Namespace, job1.ID, job1.Version)
|
|
|
|
out2, _ := state2.JobByIDAndVersion(ws, job2.Namespace, job2.ID, job2.Version)
|
2017-04-24 21:49:23 +00:00
|
|
|
if !reflect.DeepEqual(job1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, job1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(job2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, job2)
|
|
|
|
}
|
|
|
|
if job2.Version != 1 {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", 1, job2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_Deployments(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-04-24 21:49:23 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
d1 := mock.Deployment()
|
|
|
|
d2 := mock.Deployment()
|
2018-05-31 19:39:39 +00:00
|
|
|
|
|
|
|
j := mock.Job()
|
|
|
|
d1.JobID = j.ID
|
|
|
|
d2.JobID = j.ID
|
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 999, j)
|
2017-07-04 20:31:01 +00:00
|
|
|
state.UpsertDeployment(1000, d1)
|
|
|
|
state.UpsertDeployment(1001, d2)
|
2017-04-24 21:49:23 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, _ := state2.DeploymentByID(ws, d1.ID)
|
|
|
|
out2, _ := state2.DeploymentByID(ws, d2.ID)
|
|
|
|
if !reflect.DeepEqual(d1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, d1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(d2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, d2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 21:38:18 +00:00
|
|
|
func TestFSM_SnapshotRestore_ACLPolicy(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-07 21:38:18 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
p1 := mock.ACLPolicy()
|
|
|
|
p2 := mock.ACLPolicy()
|
2020-12-01 16:11:34 +00:00
|
|
|
state.UpsertACLPolicies(structs.MsgTypeTestSetup, 1000, []*structs.ACLPolicy{p1, p2})
|
2017-08-07 21:38:18 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, _ := state2.ACLPolicyByName(ws, p1.Name)
|
|
|
|
out2, _ := state2.ACLPolicyByName(ws, p2.Name)
|
|
|
|
assert.Equal(t, p1, out1)
|
|
|
|
assert.Equal(t, p2, out2)
|
|
|
|
}
|
|
|
|
|
2017-08-12 22:22:38 +00:00
|
|
|
func TestFSM_SnapshotRestore_ACLTokens(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-12 22:22:38 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
tk1 := mock.ACLToken()
|
|
|
|
tk2 := mock.ACLToken()
|
2020-12-01 16:11:34 +00:00
|
|
|
state.UpsertACLTokens(structs.MsgTypeTestSetup, 1000, []*structs.ACLToken{tk1, tk2})
|
2017-08-12 22:22:38 +00:00
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
ws := memdb.NewWatchSet()
|
2017-08-12 22:46:19 +00:00
|
|
|
out1, _ := state2.ACLTokenByAccessorID(ws, tk1.AccessorID)
|
|
|
|
out2, _ := state2.ACLTokenByAccessorID(ws, tk2.AccessorID)
|
2017-08-12 22:22:38 +00:00
|
|
|
assert.Equal(t, tk1, out1)
|
|
|
|
assert.Equal(t, tk2, out2)
|
|
|
|
}
|
|
|
|
|
2018-10-18 02:40:14 +00:00
|
|
|
func TestFSM_SnapshotRestore_SchedulerConfiguration(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-10-18 02:40:14 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
schedConfig := &structs.SchedulerConfiguration{
|
2020-05-09 16:14:02 +00:00
|
|
|
SchedulerAlgorithm: "spread",
|
2018-10-18 02:40:14 +00:00
|
|
|
PreemptionConfig: structs.PreemptionConfig{
|
|
|
|
SystemSchedulerEnabled: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state.SchedulerSetConfig(1000, schedConfig)
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
require := require.New(t)
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
index, out, err := state2.SchedulerConfig()
|
|
|
|
require.Nil(err)
|
|
|
|
require.EqualValues(1000, index)
|
|
|
|
require.Equal(schedConfig, out)
|
2019-11-14 13:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_ClusterMetadata(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-11-14 13:18:29 +00:00
|
|
|
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
clusterID := "12345678-1234-1234-1234-1234567890"
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
meta := &structs.ClusterMetadata{ClusterID: clusterID, CreateTime: now}
|
|
|
|
state.ClusterSetMetadata(1000, meta)
|
2018-10-18 02:40:14 +00:00
|
|
|
|
2019-11-14 13:18:29 +00:00
|
|
|
// Verify the contents
|
|
|
|
require := require.New(t)
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
2020-06-26 17:23:32 +00:00
|
|
|
out, err := state2.ClusterMetadata(memdb.NewWatchSet())
|
2019-11-14 13:18:29 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(clusterID, out.ClusterID)
|
2018-10-18 02:40:14 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 13:53:27 +00:00
|
|
|
func TestFSM_SnapshotRestore_ServiceRegistrations(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
// Create our initial FSM which will be snapshotted.
|
|
|
|
fsm := testFSM(t)
|
|
|
|
testState := fsm.State()
|
|
|
|
|
|
|
|
// Generate and upsert some service registrations.
|
|
|
|
serviceRegs := mock.ServiceRegistrations()
|
|
|
|
require.NoError(t, testState.UpsertServiceRegistrations(structs.MsgTypeTestSetup, 10, serviceRegs))
|
|
|
|
|
|
|
|
// Perform a snapshot restore.
|
|
|
|
restoredFSM := testSnapshotRestore(t, fsm)
|
|
|
|
restoredState := restoredFSM.State()
|
|
|
|
|
|
|
|
// List the service registrations from restored state and ensure everything
|
|
|
|
// is as expected.
|
|
|
|
iter, err := restoredState.GetServiceRegistrations(memdb.NewWatchSet())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var restoredRegs []*structs.ServiceRegistration
|
|
|
|
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
restoredRegs = append(restoredRegs, raw.(*structs.ServiceRegistration))
|
|
|
|
}
|
|
|
|
require.ElementsMatch(t, restoredRegs, serviceRegs)
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:33:41 +00:00
|
|
|
func TestFSM_SnapshotRestore_ACLRoles(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
// Create our initial FSM which will be snapshotted.
|
|
|
|
fsm := testFSM(t)
|
|
|
|
testState := fsm.State()
|
|
|
|
|
|
|
|
// Create the policies our ACL roles wants to link to.
|
|
|
|
policy1 := mock.ACLPolicy()
|
|
|
|
policy1.Name = "mocked-test-policy-1"
|
|
|
|
policy2 := mock.ACLPolicy()
|
|
|
|
policy2.Name = "mocked-test-policy-2"
|
|
|
|
|
|
|
|
require.NoError(t, testState.UpsertACLPolicies(
|
|
|
|
structs.MsgTypeTestSetup, 10, []*structs.ACLPolicy{policy1, policy2}))
|
|
|
|
|
|
|
|
// Generate and upsert some ACL roles.
|
|
|
|
aclRoles := []*structs.ACLRole{mock.ACLRole(), mock.ACLRole()}
|
2022-08-22 06:54:07 +00:00
|
|
|
require.NoError(t, testState.UpsertACLRoles(structs.MsgTypeTestSetup, 10, aclRoles, false))
|
2022-08-09 07:33:41 +00:00
|
|
|
|
|
|
|
// Perform a snapshot restore.
|
|
|
|
restoredFSM := testSnapshotRestore(t, fsm)
|
|
|
|
restoredState := restoredFSM.State()
|
|
|
|
|
|
|
|
// List the ACL roles from restored state and ensure everything is as
|
|
|
|
// expected.
|
|
|
|
iter, err := restoredState.GetACLRoles(memdb.NewWatchSet())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var restoredACLRoles []*structs.ACLRole
|
|
|
|
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
restoredACLRoles = append(restoredACLRoles, raw.(*structs.ACLRole))
|
|
|
|
}
|
|
|
|
require.ElementsMatch(t, restoredACLRoles, aclRoles)
|
|
|
|
}
|
|
|
|
|
2022-12-06 14:15:29 +00:00
|
|
|
func TestFSM_SnapshotRestore_ACLAuthMethods(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
// Create our initial FSM which will be snapshotted.
|
|
|
|
fsm := testFSM(t)
|
|
|
|
testState := fsm.State()
|
|
|
|
|
|
|
|
// Generate and upsert some ACL auth methods.
|
|
|
|
authMethods := []*structs.ACLAuthMethod{mock.ACLAuthMethod(), mock.ACLAuthMethod()}
|
|
|
|
must.NoError(t, testState.UpsertACLAuthMethods(10, authMethods))
|
|
|
|
|
|
|
|
// Perform a snapshot restore.
|
|
|
|
restoredFSM := testSnapshotRestore(t, fsm)
|
|
|
|
restoredState := restoredFSM.State()
|
|
|
|
|
|
|
|
// List the ACL auth methods from restored state and ensure everything is as
|
|
|
|
// expected.
|
|
|
|
iter, err := restoredState.GetACLAuthMethods(memdb.NewWatchSet())
|
|
|
|
must.NoError(t, err)
|
|
|
|
|
|
|
|
var restoredACLAuthMethods []*structs.ACLAuthMethod
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
restoredACLAuthMethods = append(restoredACLAuthMethods, raw.(*structs.ACLAuthMethod))
|
|
|
|
}
|
|
|
|
must.SliceContainsAll(t, restoredACLAuthMethods, authMethods)
|
|
|
|
}
|
|
|
|
|
2022-12-14 07:48:18 +00:00
|
|
|
func TestFSM_SnapshotRestore_ACLBindingRules(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
// Create our initial FSM which will be snapshotted.
|
|
|
|
fsm := testFSM(t)
|
|
|
|
testState := fsm.State()
|
|
|
|
|
|
|
|
// Generate a some mocked ACL binding rules for testing and upsert these
|
|
|
|
// straight into state.
|
|
|
|
mockedACLBindingRoles := []*structs.ACLBindingRule{mock.ACLBindingRule(), mock.ACLBindingRule()}
|
|
|
|
must.NoError(t, testState.UpsertACLBindingRules(10, mockedACLBindingRoles, true))
|
|
|
|
|
|
|
|
// Perform a snapshot restore.
|
|
|
|
restoredFSM := testSnapshotRestore(t, fsm)
|
|
|
|
restoredState := restoredFSM.State()
|
|
|
|
|
|
|
|
// List the ACL binding rules from restored state and ensure everything is
|
|
|
|
// as expected.
|
|
|
|
iter, err := restoredState.GetACLBindingRules(memdb.NewWatchSet())
|
|
|
|
must.NoError(t, err)
|
|
|
|
|
|
|
|
var restoredACLBindingRules []*structs.ACLBindingRule
|
|
|
|
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
restoredACLBindingRules = append(restoredACLBindingRules, raw.(*structs.ACLBindingRule))
|
|
|
|
}
|
|
|
|
must.SliceContainsAll(t, restoredACLBindingRules, mockedACLBindingRoles)
|
|
|
|
}
|
|
|
|
|
2016-08-05 21:40:35 +00:00
|
|
|
func TestFSM_ReconcileSummaries(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-08-05 21:40:35 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
|
2016-08-03 18:58:36 +00:00
|
|
|
// Add a node
|
|
|
|
node := mock.Node()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.NoError(t, state.UpsertNode(structs.MsgTypeTestSetup, 800, node))
|
2016-08-03 18:58:36 +00:00
|
|
|
|
|
|
|
// Make a job so that none of the tasks can be placed
|
2016-07-26 00:26:10 +00:00
|
|
|
job1 := mock.Job()
|
2016-08-03 18:58:36 +00:00
|
|
|
job1.TaskGroups[0].Tasks[0].Resources.CPU = 5000
|
2020-10-19 13:30:15 +00:00
|
|
|
require.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1000, job1))
|
2016-08-03 18:58:36 +00:00
|
|
|
|
2016-08-03 23:08:30 +00:00
|
|
|
// make a job which can make partial progress
|
2016-08-04 01:08:37 +00:00
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.NoError(t, state.UpsertJob(structs.MsgTypeTestSetup, 1010, alloc.Job))
|
|
|
|
require.NoError(t, state.UpsertAllocs(structs.MsgTypeTestSetup, 1011, []*structs.Allocation{alloc}))
|
2016-08-03 18:58:36 +00:00
|
|
|
|
|
|
|
// Delete the summaries
|
2020-08-27 14:18:53 +00:00
|
|
|
require.NoError(t, state.DeleteJobSummary(1030, job1.Namespace, job1.ID))
|
|
|
|
require.NoError(t, state.DeleteJobSummary(1040, alloc.Namespace, alloc.Job.ID))
|
2016-08-03 18:58:36 +00:00
|
|
|
|
2016-08-05 21:40:35 +00:00
|
|
|
req := structs.GenericRequest{}
|
|
|
|
buf, err := structs.Encode(structs.ReconcileJobSummariesRequestType, req)
|
|
|
|
if err != nil {
|
2016-08-03 18:58:36 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2016-07-26 00:26:10 +00:00
|
|
|
|
2016-08-05 21:40:35 +00:00
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatalf("resp: %v", resp)
|
|
|
|
}
|
2016-08-03 18:58:36 +00:00
|
|
|
|
2017-02-08 05:22:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2020-08-27 14:18:53 +00:00
|
|
|
out1, err := state.JobSummaryByID(ws, job1.Namespace, job1.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-07-26 00:26:10 +00:00
|
|
|
expected := structs.JobSummary{
|
2017-09-07 23:56:15 +00:00
|
|
|
JobID: job1.ID,
|
|
|
|
Namespace: job1.Namespace,
|
2016-07-26 00:26:10 +00:00
|
|
|
Summary: map[string]structs.TaskGroupSummary{
|
2017-09-26 22:26:33 +00:00
|
|
|
"web": {
|
2016-07-26 00:26:10 +00:00
|
|
|
Queued: 10,
|
|
|
|
},
|
|
|
|
},
|
2016-08-04 22:14:01 +00:00
|
|
|
CreateIndex: 1000,
|
2016-08-05 21:40:35 +00:00
|
|
|
ModifyIndex: out1.ModifyIndex,
|
2016-07-26 00:26:10 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&expected, out1) {
|
2016-07-26 05:22:55 +00:00
|
|
|
t.Fatalf("expected: %#v, actual: %#v", &expected, out1)
|
2016-07-26 00:26:10 +00:00
|
|
|
}
|
2016-08-03 18:58:36 +00:00
|
|
|
|
2016-08-04 01:08:37 +00:00
|
|
|
// This exercises the code path which adds the allocations made by the
|
|
|
|
// planner and the number of unplaced allocations in the reconcile summaries
|
|
|
|
// codepath
|
2020-08-27 14:18:53 +00:00
|
|
|
out2, err := state.JobSummaryByID(ws, alloc.Namespace, alloc.Job.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-08-03 18:58:36 +00:00
|
|
|
expected = structs.JobSummary{
|
2017-09-07 23:56:15 +00:00
|
|
|
JobID: alloc.Job.ID,
|
|
|
|
Namespace: alloc.Job.Namespace,
|
2016-08-03 18:58:36 +00:00
|
|
|
Summary: map[string]structs.TaskGroupSummary{
|
2017-09-26 22:26:33 +00:00
|
|
|
"web": {
|
2017-06-29 18:01:41 +00:00
|
|
|
Queued: 9,
|
2016-08-04 01:08:37 +00:00
|
|
|
Starting: 1,
|
2016-08-03 18:58:36 +00:00
|
|
|
},
|
|
|
|
},
|
2016-08-04 22:14:01 +00:00
|
|
|
CreateIndex: 1010,
|
2016-08-05 21:40:35 +00:00
|
|
|
ModifyIndex: out2.ModifyIndex,
|
2016-08-03 18:58:36 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&expected, out2) {
|
2017-06-29 18:01:41 +00:00
|
|
|
t.Fatalf("Diff % #v", pretty.Diff(&expected, out2))
|
2016-08-03 18:58:36 +00:00
|
|
|
}
|
2016-07-26 00:26:10 +00:00
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
|
2019-01-17 17:30:43 +00:00
|
|
|
// COMPAT: Remove in 0.11
|
2019-01-17 20:29:48 +00:00
|
|
|
func TestFSM_ReconcileParentJobSummary(t *testing.T) {
|
2019-01-17 17:30:43 +00:00
|
|
|
// This test exercises code to handle https://github.com/hashicorp/nomad/issues/3886
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-01-17 17:30:43 +00:00
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
|
|
|
|
// Add a node
|
|
|
|
node := mock.Node()
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertNode(structs.MsgTypeTestSetup, 800, node)
|
2019-01-17 17:30:43 +00:00
|
|
|
|
|
|
|
// Make a parameterized job
|
|
|
|
job1 := mock.BatchJob()
|
|
|
|
job1.ID = "test"
|
|
|
|
job1.ParameterizedJob = &structs.ParameterizedJobConfig{
|
|
|
|
Payload: "random",
|
|
|
|
}
|
|
|
|
job1.TaskGroups[0].Count = 1
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 1000, job1)
|
2019-01-17 17:30:43 +00:00
|
|
|
|
2019-01-17 20:29:48 +00:00
|
|
|
// Make a child job
|
2019-01-17 17:30:43 +00:00
|
|
|
childJob := job1.Copy()
|
|
|
|
childJob.ID = job1.ID + "dispatch-23423423"
|
|
|
|
childJob.ParentID = job1.ID
|
2019-01-17 20:29:48 +00:00
|
|
|
childJob.Dispatched = true
|
2019-01-17 17:30:43 +00:00
|
|
|
childJob.Status = structs.JobStatusRunning
|
|
|
|
|
2019-01-17 20:29:48 +00:00
|
|
|
// Create an alloc for child job
|
2019-01-17 17:30:43 +00:00
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
|
|
|
alloc.Job = childJob
|
|
|
|
alloc.JobID = childJob.ID
|
2019-01-17 20:29:48 +00:00
|
|
|
alloc.ClientStatus = structs.AllocClientStatusRunning
|
2019-01-17 17:30:43 +00:00
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
state.UpsertJob(structs.MsgTypeTestSetup, 1010, childJob)
|
|
|
|
state.UpsertAllocs(structs.MsgTypeTestSetup, 1011, []*structs.Allocation{alloc})
|
2019-01-17 17:30:43 +00:00
|
|
|
|
|
|
|
// Make the summary incorrect in the state store
|
|
|
|
summary, err := state.JobSummaryByID(nil, job1.Namespace, job1.ID)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
summary.Children = nil
|
|
|
|
summary.Summary = make(map[string]structs.TaskGroupSummary)
|
|
|
|
summary.Summary["web"] = structs.TaskGroupSummary{
|
|
|
|
Queued: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
req := structs.GenericRequest{}
|
|
|
|
buf, err := structs.Encode(structs.ReconcileJobSummariesRequestType, req)
|
2019-01-17 20:29:48 +00:00
|
|
|
require.Nil(err)
|
2019-01-17 17:30:43 +00:00
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
2019-01-17 20:29:48 +00:00
|
|
|
require.Nil(resp)
|
2019-01-17 17:30:43 +00:00
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, _ := state.JobSummaryByID(ws, job1.Namespace, job1.ID)
|
|
|
|
expected := structs.JobSummary{
|
|
|
|
JobID: job1.ID,
|
|
|
|
Namespace: job1.Namespace,
|
|
|
|
Summary: make(map[string]structs.TaskGroupSummary),
|
|
|
|
CreateIndex: 1000,
|
|
|
|
ModifyIndex: out1.ModifyIndex,
|
|
|
|
Children: &structs.JobChildrenSummary{
|
|
|
|
Running: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(&expected, out1)
|
|
|
|
}
|
|
|
|
|
2018-05-23 23:44:21 +00:00
|
|
|
func TestFSM_LeakedDeployments(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-05-23 23:44:21 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
d := mock.Deployment()
|
|
|
|
require.NoError(state.UpsertDeployment(1000, d))
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
out, _ := state2.DeploymentByID(nil, d.ID)
|
|
|
|
require.NotNil(out)
|
|
|
|
require.Equal(structs.DeploymentStatusCancelled, out.Status)
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
func TestFSM_Autopilot(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-12-18 21:16:23 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Set the autopilot config using a request.
|
|
|
|
req := structs.AutopilotSetConfigRequest{
|
|
|
|
Datacenter: "dc1",
|
2018-01-30 03:53:34 +00:00
|
|
|
Config: structs.AutopilotConfig{
|
2017-12-18 21:16:23 +00:00
|
|
|
CleanupDeadServers: true,
|
|
|
|
LastContactThreshold: 10 * time.Second,
|
|
|
|
MaxTrailingLogs: 300,
|
2020-02-16 21:23:20 +00:00
|
|
|
MinQuorum: 3,
|
2017-12-18 21:16:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2020-02-16 21:23:20 +00:00
|
|
|
if config.MinQuorum != req.Config.MinQuorum {
|
|
|
|
t.Fatalf("bad: %v", config.MinQuorum)
|
|
|
|
}
|
2017-12-18 21:16:23 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
func TestFSM_SchedulerConfig(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-28 04:27:38 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
2019-04-29 23:48:07 +00:00
|
|
|
// Set the scheduler config using a request.
|
2018-09-28 04:27:38 +00:00
|
|
|
req := structs.SchedulerSetConfigRequest{
|
|
|
|
Config: structs.SchedulerConfiguration{
|
2018-10-01 14:26:52 +00:00
|
|
|
PreemptionConfig: structs.PreemptionConfig{
|
|
|
|
SystemSchedulerEnabled: true,
|
2019-05-03 19:06:12 +00:00
|
|
|
BatchSchedulerEnabled: true,
|
2018-10-01 14:26:52 +00:00
|
|
|
},
|
2018-09-28 04:27:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.SchedulerConfigRequestType, req)
|
|
|
|
require.Nil(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.SchedulerConfig()
|
|
|
|
require.Nil(err)
|
|
|
|
|
2018-10-01 14:26:52 +00:00
|
|
|
require.Equal(config.PreemptionConfig.SystemSchedulerEnabled, req.Config.PreemptionConfig.SystemSchedulerEnabled)
|
2019-05-03 19:06:12 +00:00
|
|
|
require.Equal(config.PreemptionConfig.BatchSchedulerEnabled, req.Config.PreemptionConfig.BatchSchedulerEnabled)
|
2018-09-28 04:27:38 +00:00
|
|
|
|
|
|
|
// Now use CAS and provide an old index
|
|
|
|
req.CAS = true
|
2019-05-03 19:06:12 +00:00
|
|
|
req.Config.PreemptionConfig = structs.PreemptionConfig{SystemSchedulerEnabled: false, BatchSchedulerEnabled: false}
|
2018-09-28 04:27:38 +00:00
|
|
|
req.Config.ModifyIndex = config.ModifyIndex - 1
|
|
|
|
buf, err = structs.Encode(structs.SchedulerConfigRequestType, req)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
resp = fsm.Apply(makeLog(buf))
|
|
|
|
if _, ok := resp.(error); ok {
|
|
|
|
t.Fatalf("bad: %v", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, config, err = fsm.state.SchedulerConfig()
|
|
|
|
require.Nil(err)
|
|
|
|
// Verify that preemption is still enabled
|
2018-10-01 14:26:52 +00:00
|
|
|
require.True(config.PreemptionConfig.SystemSchedulerEnabled)
|
2019-05-03 19:06:12 +00:00
|
|
|
require.True(config.PreemptionConfig.BatchSchedulerEnabled)
|
2018-09-28 04:27:38 +00:00
|
|
|
}
|
2019-11-14 13:18:29 +00:00
|
|
|
|
|
|
|
func TestFSM_ClusterMetadata(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-11-14 13:18:29 +00:00
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
fsm := testFSM(t)
|
|
|
|
clusterID := "12345678-1234-1234-1234-1234567890"
|
|
|
|
now := time.Now().UnixNano()
|
|
|
|
meta := structs.ClusterMetadata{
|
|
|
|
ClusterID: clusterID,
|
|
|
|
CreateTime: now,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ClusterMetadataRequestType, meta)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
result := fsm.Apply(makeLog(buf))
|
|
|
|
r.Nil(result)
|
|
|
|
|
|
|
|
// Verify the clusterID is set directly in the state store
|
2020-06-26 17:23:32 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
storedMetadata, err := fsm.state.ClusterMetadata(ws)
|
2019-11-14 13:18:29 +00:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(clusterID, storedMetadata.ClusterID)
|
|
|
|
|
2021-03-16 22:05:08 +00:00
|
|
|
// Assert cluster ID cannot be overwritten and is not regenerated
|
2019-11-14 13:18:29 +00:00
|
|
|
erroneous := structs.ClusterMetadata{
|
|
|
|
ClusterID: "99999999-9999-9999-9999-9999999999",
|
|
|
|
}
|
|
|
|
buf, err = structs.Encode(structs.ClusterMetadataRequestType, erroneous)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
result = fsm.Apply(makeLog(buf))
|
|
|
|
r.Error(result.(error))
|
|
|
|
|
2020-06-26 17:23:32 +00:00
|
|
|
storedMetadata, err = fsm.state.ClusterMetadata(ws)
|
2019-11-14 13:18:29 +00:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(clusterID, storedMetadata.ClusterID)
|
|
|
|
r.Equal(now, storedMetadata.CreateTime)
|
|
|
|
}
|
2020-10-21 04:16:25 +00:00
|
|
|
|
|
|
|
func TestFSM_UpsertNamespaces(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-21 04:16:25 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
ns1 := mock.Namespace()
|
|
|
|
ns2 := mock.Namespace()
|
|
|
|
req := structs.NamespaceUpsertRequest{
|
|
|
|
Namespaces: []*structs.Namespace{ns1, ns2},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.NamespaceUpsertRequestType, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Nil(fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Verify we are registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().NamespaceByName(ws, ns1.Name)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.NotNil(out)
|
|
|
|
|
|
|
|
out, err = fsm.State().NamespaceByName(ws, ns2.Name)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.NotNil(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeleteNamespaces(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-21 04:16:25 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
ns1 := mock.Namespace()
|
|
|
|
ns2 := mock.Namespace()
|
|
|
|
assert.Nil(fsm.State().UpsertNamespaces(1000, []*structs.Namespace{ns1, ns2}))
|
|
|
|
|
|
|
|
req := structs.NamespaceDeleteRequest{
|
|
|
|
Namespaces: []string{ns1.Name, ns2.Name},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.NamespaceDeleteRequestType, req)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Nil(fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().NamespaceByName(ws, ns1.Name)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Nil(out)
|
|
|
|
|
|
|
|
out, err = fsm.State().NamespaceByName(ws, ns2.Name)
|
|
|
|
assert.Nil(err)
|
|
|
|
assert.Nil(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_SnapshotRestore_Namespaces(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-21 04:16:25 +00:00
|
|
|
// Add some state
|
|
|
|
fsm := testFSM(t)
|
|
|
|
state := fsm.State()
|
|
|
|
ns1 := mock.Namespace()
|
|
|
|
ns2 := mock.Namespace()
|
|
|
|
state.UpsertNamespaces(1000, []*structs.Namespace{ns1, ns2})
|
|
|
|
|
|
|
|
// Verify the contents
|
|
|
|
fsm2 := testSnapshotRestore(t, fsm)
|
|
|
|
state2 := fsm2.State()
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out1, _ := state2.NamespaceByName(ws, ns1.Name)
|
|
|
|
out2, _ := state2.NamespaceByName(ws, ns2.Name)
|
|
|
|
if !reflect.DeepEqual(ns1, out1) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out1, ns1)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ns2, out2) {
|
|
|
|
t.Fatalf("bad: \n%#v\n%#v", out2, ns2)
|
|
|
|
}
|
|
|
|
}
|
2020-12-11 15:40:50 +00:00
|
|
|
|
2022-03-03 10:24:29 +00:00
|
|
|
func TestFSM_UpsertServiceRegistrations(t *testing.T) {
|
2022-03-24 07:45:13 +00:00
|
|
|
ci.Parallel(t)
|
2022-03-03 10:24:29 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Generate our test service registrations.
|
|
|
|
services := mock.ServiceRegistrations()
|
|
|
|
|
|
|
|
// Build and apply our message.
|
|
|
|
req := structs.ServiceRegistrationUpsertRequest{Services: services}
|
|
|
|
buf, err := structs.Encode(structs.ServiceRegistrationUpsertRequestType, req)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Check that both services are found within state.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().GetServiceRegistrationByID(ws, services[0].Namespace, services[0].ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out)
|
|
|
|
|
|
|
|
out, err = fsm.State().GetServiceRegistrationByID(ws, services[1].Namespace, services[1].ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeleteServiceRegistrationsByID(t *testing.T) {
|
2022-03-24 07:45:13 +00:00
|
|
|
ci.Parallel(t)
|
2022-03-03 10:24:29 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Generate our test service registrations.
|
|
|
|
services := mock.ServiceRegistrations()
|
|
|
|
|
|
|
|
// Upsert the services.
|
|
|
|
assert.NoError(t, fsm.State().UpsertServiceRegistrations(structs.MsgTypeTestSetup, uint64(10), services))
|
|
|
|
|
|
|
|
// Build and apply our message.
|
|
|
|
req := structs.ServiceRegistrationDeleteByIDRequest{ID: services[0].ID}
|
|
|
|
buf, err := structs.Encode(structs.ServiceRegistrationDeleteByIDRequestType, req)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Check that the service has been deleted, whilst the other is still
|
|
|
|
// available.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().GetServiceRegistrationByID(ws, services[0].Namespace, services[0].ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, out)
|
|
|
|
|
|
|
|
out, err = fsm.State().GetServiceRegistrationByID(ws, services[1].Namespace, services[1].ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotNil(t, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeleteServiceRegistrationsByNodeID(t *testing.T) {
|
2022-03-24 07:45:13 +00:00
|
|
|
ci.Parallel(t)
|
2022-03-03 10:24:29 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Generate our test service registrations. Set them both to have the same
|
|
|
|
// node ID.
|
|
|
|
services := mock.ServiceRegistrations()
|
|
|
|
services[1].NodeID = services[0].NodeID
|
|
|
|
|
|
|
|
// Upsert the services.
|
|
|
|
assert.NoError(t, fsm.State().UpsertServiceRegistrations(structs.MsgTypeTestSetup, uint64(10), services))
|
|
|
|
|
|
|
|
// Build and apply our message.
|
|
|
|
req := structs.ServiceRegistrationDeleteByNodeIDRequest{NodeID: services[0].NodeID}
|
|
|
|
buf, err := structs.Encode(structs.ServiceRegistrationDeleteByNodeIDRequestType, req)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Check both services have been removed.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().GetServiceRegistrationByID(ws, services[0].Namespace, services[0].ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, out)
|
|
|
|
|
|
|
|
out, err = fsm.State().GetServiceRegistrationByID(ws, services[1].Namespace, services[1].ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, out)
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestFSM_SnapshotRestore_Variables(t *testing.T) {
|
2022-05-27 13:33:41 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
// Create our initial FSM which will be snapshotted.
|
|
|
|
fsm := testFSM(t)
|
|
|
|
testState := fsm.State()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Generate and upsert some variables.
|
|
|
|
msvs := mock.VariablesEncrypted(3, 3)
|
2022-05-27 13:33:41 +00:00
|
|
|
svs := msvs.List()
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
for _, sv := range svs {
|
2022-08-26 18:03:56 +00:00
|
|
|
setResp := testState.VarSet(10, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
})
|
|
|
|
require.NoError(t, setResp.Error)
|
|
|
|
}
|
2022-05-27 13:33:41 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// Update the mock variables data with the actual create information
|
|
|
|
iter, err := testState.Variables(memdb.NewWatchSet())
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := raw.(*structs.VariableEncrypted)
|
2022-05-27 13:33:41 +00:00
|
|
|
msvs[sv.Path].CreateIndex = sv.CreateIndex
|
|
|
|
msvs[sv.Path].CreateTime = sv.CreateTime
|
|
|
|
msvs[sv.Path].ModifyIndex = sv.ModifyIndex
|
|
|
|
msvs[sv.Path].ModifyTime = sv.ModifyTime
|
|
|
|
}
|
|
|
|
svs = msvs.List()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// List the variables from restored state and ensure everything
|
2022-05-27 13:33:41 +00:00
|
|
|
// is as expected.
|
|
|
|
|
|
|
|
// Perform a snapshot restore.
|
|
|
|
restoredFSM := testSnapshotRestore(t, fsm)
|
|
|
|
restoredState := restoredFSM.State()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// List the variables from restored state and ensure everything
|
2022-05-27 13:33:41 +00:00
|
|
|
// is as expected.
|
2022-08-26 18:03:56 +00:00
|
|
|
iter, err = restoredState.Variables(memdb.NewWatchSet())
|
2022-05-27 13:33:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
var restoredSVs []*structs.VariableEncrypted
|
2022-05-27 13:33:41 +00:00
|
|
|
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2022-08-26 18:03:56 +00:00
|
|
|
restoredSVs = append(restoredSVs, raw.(*structs.VariableEncrypted))
|
2022-05-27 13:33:41 +00:00
|
|
|
}
|
|
|
|
require.ElementsMatch(t, restoredSVs, svs)
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:33:41 +00:00
|
|
|
func TestFSM_ApplyACLRolesUpsert(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Create the policies our ACL roles wants to link to.
|
|
|
|
policy1 := mock.ACLPolicy()
|
|
|
|
policy1.Name = "mocked-test-policy-1"
|
|
|
|
policy2 := mock.ACLPolicy()
|
|
|
|
policy2.Name = "mocked-test-policy-2"
|
|
|
|
|
|
|
|
require.NoError(t, fsm.State().UpsertACLPolicies(
|
|
|
|
structs.MsgTypeTestSetup, 10, []*structs.ACLPolicy{policy1, policy2}))
|
|
|
|
|
|
|
|
// Generate the upsert request and apply the change.
|
|
|
|
req := structs.ACLRolesUpsertRequest{
|
|
|
|
ACLRoles: []*structs.ACLRole{mock.ACLRole(), mock.ACLRole()},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLRolesUpsertRequestType, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Read out both ACL roles and perform an equality check using the hash.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().GetACLRoleByName(ws, req.ACLRoles[0].Name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, req.ACLRoles[0].Hash, out.Hash)
|
|
|
|
|
|
|
|
out, err = fsm.State().GetACLRoleByName(ws, req.ACLRoles[1].Name)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, req.ACLRoles[1].Hash, out.Hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_ApplyACLRolesDeleteByID(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Create the policies our ACL roles wants to link to.
|
|
|
|
policy1 := mock.ACLPolicy()
|
|
|
|
policy1.Name = "mocked-test-policy-1"
|
|
|
|
policy2 := mock.ACLPolicy()
|
|
|
|
policy2.Name = "mocked-test-policy-2"
|
|
|
|
|
|
|
|
require.NoError(t, fsm.State().UpsertACLPolicies(
|
|
|
|
structs.MsgTypeTestSetup, 10, []*structs.ACLPolicy{policy1, policy2}))
|
|
|
|
|
|
|
|
// Generate and upsert two ACL roles.
|
|
|
|
aclRoles := []*structs.ACLRole{mock.ACLRole(), mock.ACLRole()}
|
2022-08-22 06:54:07 +00:00
|
|
|
require.NoError(t, fsm.State().UpsertACLRoles(structs.MsgTypeTestSetup, 10, aclRoles, false))
|
2022-08-09 07:33:41 +00:00
|
|
|
|
|
|
|
// Build and apply our message.
|
|
|
|
req := structs.ACLRolesDeleteByIDRequest{ACLRoleIDs: []string{aclRoles[0].ID, aclRoles[1].ID}}
|
|
|
|
buf, err := structs.Encode(structs.ACLRolesDeleteByIDRequestType, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// List all ACL roles within state to ensure both have been removed.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
iter, err := fsm.State().GetACLRoles(ws)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var count int
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
require.Equal(t, 0, count)
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:40:50 +00:00
|
|
|
func TestFSM_ACLEvents(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-12-11 15:40:50 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
setupfn func(t *testing.T, fsm *nomadFSM)
|
|
|
|
raftReq func(t *testing.T) []byte
|
|
|
|
reqTopic structs.Topic
|
|
|
|
eventfn func(t *testing.T, e []structs.Event)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "ACLToken upserted",
|
|
|
|
raftReq: func(t *testing.T) []byte {
|
|
|
|
req := structs.ACLTokenUpsertRequest{
|
|
|
|
Tokens: []*structs.ACLToken{mock.ACLToken()},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLTokenUpsertRequestType, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return buf
|
|
|
|
},
|
|
|
|
reqTopic: structs.TopicACLToken,
|
|
|
|
eventfn: func(t *testing.T, e []structs.Event) {
|
|
|
|
require.Len(t, e, 1)
|
|
|
|
require.Equal(t, e[0].Topic, structs.TopicACLToken)
|
|
|
|
require.Empty(t, e[0].Payload.(*structs.ACLTokenEvent).ACLToken.SecretID)
|
|
|
|
require.Equal(t, e[0].Type, structs.TypeACLTokenUpserted)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ACLToken deleted",
|
|
|
|
setupfn: func(t *testing.T, fsm *nomadFSM) {
|
|
|
|
token := mock.ACLToken()
|
|
|
|
token.SecretID = "26be01d3-df3a-45e9-9f49-4487a3dc3496"
|
|
|
|
token.AccessorID = "b971acba-bbe5-4274-bdfa-8bb1f542a8c1"
|
|
|
|
|
|
|
|
require.NoError(t,
|
|
|
|
fsm.State().UpsertACLTokens(
|
|
|
|
structs.MsgTypeTestSetup, 10, []*structs.ACLToken{token}))
|
|
|
|
},
|
|
|
|
raftReq: func(t *testing.T) []byte {
|
|
|
|
req := structs.ACLTokenDeleteRequest{
|
|
|
|
AccessorIDs: []string{"b971acba-bbe5-4274-bdfa-8bb1f542a8c1"},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLTokenDeleteRequestType, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return buf
|
|
|
|
},
|
|
|
|
reqTopic: structs.TopicACLToken,
|
|
|
|
eventfn: func(t *testing.T, e []structs.Event) {
|
|
|
|
require.Len(t, e, 1)
|
|
|
|
require.Equal(t, e[0].Topic, structs.TopicACLToken)
|
|
|
|
require.Empty(t, e[0].Payload.(*structs.ACLTokenEvent).ACLToken.SecretID)
|
|
|
|
require.Equal(t, e[0].Type, structs.TypeACLTokenDeleted)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ACLPolicy upserted",
|
|
|
|
raftReq: func(t *testing.T) []byte {
|
|
|
|
req := structs.ACLPolicyUpsertRequest{
|
|
|
|
Policies: []*structs.ACLPolicy{mock.ACLPolicy()},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLPolicyUpsertRequestType, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return buf
|
|
|
|
},
|
|
|
|
reqTopic: structs.TopicACLPolicy,
|
|
|
|
eventfn: func(t *testing.T, e []structs.Event) {
|
|
|
|
require.Len(t, e, 1)
|
|
|
|
require.Equal(t, e[0].Topic, structs.TopicACLPolicy)
|
|
|
|
require.Equal(t, e[0].Type, structs.TypeACLPolicyUpserted)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ACLPolicy deleted",
|
|
|
|
setupfn: func(t *testing.T, fsm *nomadFSM) {
|
|
|
|
policy := mock.ACLPolicy()
|
|
|
|
policy.Name = "some-policy"
|
|
|
|
|
|
|
|
require.NoError(t,
|
|
|
|
fsm.State().UpsertACLPolicies(
|
|
|
|
structs.MsgTypeTestSetup, 10, []*structs.ACLPolicy{policy}))
|
|
|
|
},
|
|
|
|
raftReq: func(t *testing.T) []byte {
|
|
|
|
req := structs.ACLPolicyDeleteRequest{
|
|
|
|
Names: []string{"some-policy"},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLPolicyDeleteRequestType, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return buf
|
|
|
|
},
|
|
|
|
reqTopic: structs.TopicACLPolicy,
|
|
|
|
eventfn: func(t *testing.T, e []structs.Event) {
|
|
|
|
require.Len(t, e, 1)
|
|
|
|
require.Equal(t, e[0].Topic, structs.TopicACLPolicy)
|
|
|
|
require.Equal(t, e[0].Type, structs.TypeACLPolicyDeleted)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Setup any state necessary
|
|
|
|
if tc.setupfn != nil {
|
|
|
|
tc.setupfn(t, fsm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the log
|
|
|
|
resp := fsm.Apply(makeLog(tc.raftReq(t)))
|
|
|
|
require.Nil(t, resp)
|
|
|
|
|
|
|
|
broker, err := fsm.State().EventBroker()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
subReq := &stream.SubscribeRequest{
|
|
|
|
Topics: map[structs.Topic][]string{
|
|
|
|
tc.reqTopic: {"*"},
|
|
|
|
},
|
2021-09-02 16:36:55 +00:00
|
|
|
Namespace: "default",
|
2020-12-11 15:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := broker.Subscribe(subReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var events []structs.Event
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
out, err := sub.NextNoBlock()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if out == nil {
|
|
|
|
return false, fmt.Errorf("expected events got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
events = out
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.Fail(t, err.Error())
|
|
|
|
})
|
|
|
|
|
|
|
|
tc.eventfn(t, events)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 14:18:17 +00:00
|
|
|
|
|
|
|
// TestFSM_EventBroker_JobRegisterFSMEvents asserts that only a single job
|
|
|
|
// register event is emitted when registering a job
|
|
|
|
func TestFSM_EventBroker_JobRegisterFSMEvents(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2021-01-22 14:18:17 +00:00
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
job := mock.Job()
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.JobID = job.ID
|
|
|
|
|
|
|
|
req := structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
Eval: eval,
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.JobRegisterRequestType, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := fsm.Apply(makeLog(buf))
|
|
|
|
require.Nil(t, resp)
|
|
|
|
|
|
|
|
broker, err := fsm.State().EventBroker()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
subReq := &stream.SubscribeRequest{
|
|
|
|
Topics: map[structs.Topic][]string{
|
|
|
|
structs.TopicJob: {"*"},
|
|
|
|
},
|
2021-09-02 16:36:55 +00:00
|
|
|
Namespace: "default",
|
2021-01-22 14:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := broker.Subscribe(subReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(500*time.Millisecond))
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// consume the queue
|
|
|
|
var events []structs.Event
|
|
|
|
for {
|
|
|
|
out, err := sub.Next(ctx)
|
|
|
|
if len(out.Events) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// consume the queue until the deadline has exceeded or until we've
|
|
|
|
// received more events than expected
|
|
|
|
if err == context.DeadlineExceeded || len(events) > 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
events = append(events, out.Events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(t, events, 1)
|
|
|
|
require.Equal(t, structs.TypeJobRegistered, events[0].Type)
|
|
|
|
}
|
2022-11-21 09:15:39 +00:00
|
|
|
|
|
|
|
func TestFSM_UpsertACLAuthMethods(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
am1 := mock.ACLAuthMethod()
|
|
|
|
am2 := mock.ACLAuthMethod()
|
|
|
|
req := structs.ACLAuthMethodUpsertRequest{
|
|
|
|
AuthMethods: []*structs.ACLAuthMethod{am1, am2},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLAuthMethodsUpsertRequestType, req)
|
|
|
|
must.Nil(t, err)
|
|
|
|
must.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Verify we are registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().GetACLAuthMethodByName(ws, am1.Name)
|
|
|
|
must.Nil(t, err)
|
|
|
|
must.NotNil(t, out)
|
|
|
|
|
|
|
|
out, err = fsm.State().GetACLAuthMethodByName(ws, am2.Name)
|
|
|
|
must.Nil(t, err)
|
|
|
|
must.NotNil(t, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeleteACLAuthMethods(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
am1 := mock.ACLAuthMethod()
|
|
|
|
am2 := mock.ACLAuthMethod()
|
|
|
|
must.Nil(t, fsm.State().UpsertACLAuthMethods(1000, []*structs.ACLAuthMethod{am1, am2}))
|
|
|
|
|
|
|
|
req := structs.ACLAuthMethodDeleteRequest{
|
|
|
|
Names: []string{am1.Name, am2.Name},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLAuthMethodsDeleteRequestType, req)
|
|
|
|
must.Nil(t, err)
|
|
|
|
must.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Verify we are NOT registered
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().GetACLAuthMethodByName(ws, am1.Name)
|
|
|
|
must.Nil(t, err)
|
|
|
|
must.Nil(t, out)
|
|
|
|
|
|
|
|
out, err = fsm.State().GetACLAuthMethodByName(ws, am2.Name)
|
|
|
|
must.Nil(t, err)
|
|
|
|
must.Nil(t, out)
|
|
|
|
}
|
2022-12-14 07:48:18 +00:00
|
|
|
|
|
|
|
func TestFSM_UpsertACLBindingRules(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
// Create an auth method and upsert so the binding rules can link to this.
|
|
|
|
authMethod := mock.ACLAuthMethod()
|
|
|
|
must.NoError(t, fsm.state.UpsertACLAuthMethods(10, []*structs.ACLAuthMethod{authMethod}))
|
|
|
|
|
|
|
|
aclBindingRule1 := mock.ACLBindingRule()
|
|
|
|
aclBindingRule1.AuthMethod = authMethod.Name
|
|
|
|
aclBindingRule2 := mock.ACLBindingRule()
|
|
|
|
aclBindingRule2.AuthMethod = authMethod.Name
|
|
|
|
|
|
|
|
req := structs.ACLBindingRulesUpsertRequest{
|
|
|
|
ACLBindingRules: []*structs.ACLBindingRule{aclBindingRule1, aclBindingRule2},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLBindingRulesUpsertRequestType, req)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Ensure the ACL binding rules have been upserted correctly.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().GetACLBindingRule(ws, aclBindingRule1.ID)
|
|
|
|
must.Nil(t, err)
|
|
|
|
must.Eq(t, aclBindingRule1, out)
|
|
|
|
|
|
|
|
out, err = fsm.State().GetACLBindingRule(ws, aclBindingRule2.ID)
|
|
|
|
must.Nil(t, err)
|
|
|
|
must.Eq(t, aclBindingRule2, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSM_DeleteACLBindingRules(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
fsm := testFSM(t)
|
|
|
|
|
|
|
|
aclBindingRule1 := mock.ACLBindingRule()
|
|
|
|
aclBindingRule2 := mock.ACLBindingRule()
|
|
|
|
must.NoError(t, fsm.State().UpsertACLBindingRules(
|
|
|
|
10, []*structs.ACLBindingRule{aclBindingRule1, aclBindingRule2}, true))
|
|
|
|
|
|
|
|
req := structs.ACLBindingRulesDeleteRequest{
|
|
|
|
ACLBindingRuleIDs: []string{aclBindingRule1.ID, aclBindingRule2.ID},
|
|
|
|
}
|
|
|
|
buf, err := structs.Encode(structs.ACLBindingRulesDeleteRequestType, req)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.Nil(t, fsm.Apply(makeLog(buf)))
|
|
|
|
|
|
|
|
// Ensure neither ACL binding rule is now found.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
out, err := fsm.State().GetACLBindingRule(ws, aclBindingRule1.ID)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.Nil(t, out)
|
|
|
|
|
|
|
|
out, err = fsm.State().GetACLBindingRule(ws, aclBindingRule2.ID)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.Nil(t, out)
|
|
|
|
}
|