2018-03-27 21:11:10 +00:00
|
|
|
package drainer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-03-27 21:11:10 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testDrainingNode creates a *drainingNode with a 1h deadline but no allocs
|
|
|
|
func testDrainingNode(t *testing.T) *drainingNode {
|
|
|
|
t.Helper()
|
2018-09-15 23:42:38 +00:00
|
|
|
state := state.TestStateStore(t)
|
2018-03-27 21:11:10 +00:00
|
|
|
node := mock.Node()
|
|
|
|
node.DrainStrategy = &structs.DrainStrategy{
|
|
|
|
DrainSpec: structs.DrainSpec{
|
|
|
|
Deadline: time.Hour,
|
|
|
|
},
|
|
|
|
ForceDeadline: time.Now().Add(time.Hour),
|
|
|
|
}
|
|
|
|
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, state.UpsertNode(structs.MsgTypeTestSetup, 100, node))
|
2018-03-27 21:11:10 +00:00
|
|
|
return NewDrainingNode(node, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertDrainingNode(t *testing.T, dn *drainingNode, isDone bool, remaining, running int) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
done, err := dn.IsDone()
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, isDone, done, "IsDone mismatch")
|
|
|
|
|
|
|
|
allocs, err := dn.RemainingAllocs()
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Len(t, allocs, remaining, "RemainingAllocs mismatch")
|
|
|
|
|
2018-03-29 21:30:38 +00:00
|
|
|
jobs, err := dn.DrainingJobs()
|
2018-03-27 21:11:10 +00:00
|
|
|
require.Nil(t, err)
|
2018-03-29 21:30:38 +00:00
|
|
|
assert.Len(t, jobs, running, "DrainingJobs mismatch")
|
2018-03-27 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDrainingNode_Table(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-08-16 14:06:30 +00:00
|
|
|
|
2018-03-27 21:11:10 +00:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
isDone bool
|
|
|
|
remaining int
|
|
|
|
running int
|
|
|
|
setup func(*testing.T, *drainingNode)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Empty",
|
|
|
|
isDone: true,
|
|
|
|
remaining: 0,
|
|
|
|
running: 0,
|
|
|
|
setup: func(*testing.T, *drainingNode) {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Batch",
|
|
|
|
isDone: false,
|
|
|
|
remaining: 1,
|
2018-03-29 21:30:38 +00:00
|
|
|
running: 1,
|
2018-03-27 21:11:10 +00:00
|
|
|
setup: func(t *testing.T, dn *drainingNode) {
|
|
|
|
alloc := mock.BatchAlloc()
|
|
|
|
alloc.NodeID = dn.node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertJob(structs.MsgTypeTestSetup, 101, alloc.Job))
|
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 102, []*structs.Allocation{alloc}))
|
2018-03-27 21:11:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Service",
|
|
|
|
isDone: false,
|
|
|
|
remaining: 1,
|
|
|
|
running: 1,
|
|
|
|
setup: func(t *testing.T, dn *drainingNode) {
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = dn.node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertJob(structs.MsgTypeTestSetup, 101, alloc.Job))
|
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 102, []*structs.Allocation{alloc}))
|
2018-03-27 21:11:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "System",
|
|
|
|
isDone: true,
|
|
|
|
remaining: 1,
|
|
|
|
running: 0,
|
|
|
|
setup: func(t *testing.T, dn *drainingNode) {
|
|
|
|
alloc := mock.SystemAlloc()
|
|
|
|
alloc.NodeID = dn.node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertJob(structs.MsgTypeTestSetup, 101, alloc.Job))
|
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 102, []*structs.Allocation{alloc}))
|
2018-03-27 21:11:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AllTerminal",
|
|
|
|
isDone: true,
|
|
|
|
remaining: 0,
|
|
|
|
running: 0,
|
|
|
|
setup: func(t *testing.T, dn *drainingNode) {
|
|
|
|
allocs := []*structs.Allocation{mock.Alloc(), mock.BatchAlloc(), mock.SystemAlloc()}
|
|
|
|
for _, a := range allocs {
|
|
|
|
a.NodeID = dn.node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertJob(structs.MsgTypeTestSetup, 101, a.Job))
|
2018-03-27 21:11:10 +00:00
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 102, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
|
|
|
|
// StateStore doesn't like inserting new allocs
|
|
|
|
// with a terminal status, so set the status in
|
|
|
|
// a second pass
|
|
|
|
for _, a := range allocs {
|
|
|
|
a.ClientStatus = structs.AllocClientStatusComplete
|
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 103, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ServiceTerminal",
|
|
|
|
isDone: false,
|
|
|
|
remaining: 2,
|
2018-03-29 21:30:38 +00:00
|
|
|
running: 1,
|
2018-03-27 21:11:10 +00:00
|
|
|
setup: func(t *testing.T, dn *drainingNode) {
|
|
|
|
allocs := []*structs.Allocation{mock.Alloc(), mock.BatchAlloc(), mock.SystemAlloc()}
|
|
|
|
for _, a := range allocs {
|
|
|
|
a.NodeID = dn.node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertJob(structs.MsgTypeTestSetup, 101, a.Job))
|
2018-03-27 21:11:10 +00:00
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 102, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
|
|
|
|
// Set only the service job as terminal
|
|
|
|
allocs[0].ClientStatus = structs.AllocClientStatusComplete
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 103, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AllTerminalButBatch",
|
|
|
|
isDone: false,
|
|
|
|
remaining: 1,
|
2018-03-29 21:30:38 +00:00
|
|
|
running: 1,
|
2018-03-27 21:11:10 +00:00
|
|
|
setup: func(t *testing.T, dn *drainingNode) {
|
|
|
|
allocs := []*structs.Allocation{mock.Alloc(), mock.BatchAlloc(), mock.SystemAlloc()}
|
|
|
|
for _, a := range allocs {
|
|
|
|
a.NodeID = dn.node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertJob(structs.MsgTypeTestSetup, 101, a.Job))
|
2018-03-27 21:11:10 +00:00
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 102, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
|
|
|
|
// Set only the service and batch jobs as terminal
|
|
|
|
allocs[0].ClientStatus = structs.AllocClientStatusComplete
|
|
|
|
allocs[2].ClientStatus = structs.AllocClientStatusComplete
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 103, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "AllTerminalButSystem",
|
|
|
|
isDone: true,
|
|
|
|
remaining: 1,
|
|
|
|
running: 0,
|
|
|
|
setup: func(t *testing.T, dn *drainingNode) {
|
|
|
|
allocs := []*structs.Allocation{mock.Alloc(), mock.BatchAlloc(), mock.SystemAlloc()}
|
|
|
|
for _, a := range allocs {
|
|
|
|
a.NodeID = dn.node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertJob(structs.MsgTypeTestSetup, 101, a.Job))
|
2018-03-27 21:11:10 +00:00
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 102, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
|
|
|
|
// Set only the service and batch jobs as terminal
|
|
|
|
allocs[0].ClientStatus = structs.AllocClientStatusComplete
|
|
|
|
allocs[1].ClientStatus = structs.AllocClientStatusComplete
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 103, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "HalfTerminal",
|
|
|
|
isDone: false,
|
|
|
|
remaining: 3,
|
2018-03-29 21:30:38 +00:00
|
|
|
running: 2,
|
2018-03-27 21:11:10 +00:00
|
|
|
setup: func(t *testing.T, dn *drainingNode) {
|
|
|
|
allocs := []*structs.Allocation{
|
|
|
|
mock.Alloc(),
|
|
|
|
mock.BatchAlloc(),
|
|
|
|
mock.SystemAlloc(),
|
|
|
|
mock.Alloc(),
|
|
|
|
mock.BatchAlloc(),
|
|
|
|
mock.SystemAlloc(),
|
|
|
|
}
|
|
|
|
for _, a := range allocs {
|
|
|
|
a.NodeID = dn.node.ID
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertJob(structs.MsgTypeTestSetup, 101, a.Job))
|
2018-03-27 21:11:10 +00:00
|
|
|
}
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 102, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
|
|
|
|
// Set only the service and batch jobs as terminal
|
|
|
|
allocs[0].ClientStatus = structs.AllocClientStatusComplete
|
|
|
|
allocs[1].ClientStatus = structs.AllocClientStatusComplete
|
|
|
|
allocs[2].ClientStatus = structs.AllocClientStatusComplete
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, dn.state.UpsertAllocs(structs.MsgTypeTestSetup, 103, allocs))
|
2018-03-27 21:11:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default test drainingNode has no allocs, so it should be done and
|
|
|
|
// have no remaining allocs.
|
|
|
|
for _, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-27 21:11:10 +00:00
|
|
|
dn := testDrainingNode(t)
|
|
|
|
tc.setup(t, dn)
|
|
|
|
assertDrainingNode(t, dn, tc.isDone, tc.remaining, tc.running)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|