2015-08-13 23:25:59 +00:00
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-16 23:35:55 +00:00
|
|
|
"log"
|
2015-08-14 00:19:09 +00:00
|
|
|
"os"
|
2015-09-07 18:23:38 +00:00
|
|
|
"reflect"
|
2015-08-13 23:25:59 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-02-21 18:58:04 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2015-08-13 23:25:59 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-08-14 00:19:09 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2015-08-13 23:25:59 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2016-05-16 19:49:18 +00:00
|
|
|
// noErr is used to assert there are no errors
|
|
|
|
func noErr(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 23:25:59 +00:00
|
|
|
func TestMaterializeTaskGroups(t *testing.T) {
|
|
|
|
job := mock.Job()
|
|
|
|
index := materializeTaskGroups(job)
|
|
|
|
if len(index) != 10 {
|
|
|
|
t.Fatalf("Bad: %#v", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
name := fmt.Sprintf("my-job.web[%d]", i)
|
|
|
|
tg, ok := index[name]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
if tg != job.TaskGroups[0] {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiffAllocs(t *testing.T) {
|
|
|
|
job := mock.Job()
|
|
|
|
required := materializeTaskGroups(job)
|
|
|
|
|
|
|
|
// The "old" job has a previous modify index
|
|
|
|
oldJob := new(structs.Job)
|
|
|
|
*oldJob = *job
|
2016-01-12 17:50:33 +00:00
|
|
|
oldJob.JobModifyIndex -= 1
|
2015-08-13 23:25:59 +00:00
|
|
|
|
2016-08-03 22:45:42 +00:00
|
|
|
drainNode := mock.Node()
|
|
|
|
drainNode.Drain = true
|
|
|
|
|
|
|
|
deadNode := mock.Node()
|
|
|
|
deadNode.Status = structs.NodeStatusDown
|
|
|
|
|
|
|
|
tainted := map[string]*structs.Node{
|
|
|
|
"dead": deadNode,
|
|
|
|
"drainNode": drainNode,
|
2015-08-13 23:47:39 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 23:25:59 +00:00
|
|
|
allocs := []*structs.Allocation{
|
|
|
|
// Update the 1st
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2015-08-13 23:25:59 +00:00
|
|
|
NodeID: "zip",
|
|
|
|
Name: "my-job.web[0]",
|
|
|
|
Job: oldJob,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Ignore the 2rd
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2015-08-13 23:25:59 +00:00
|
|
|
NodeID: "zip",
|
|
|
|
Name: "my-job.web[1]",
|
|
|
|
Job: job,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Evict 11th
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2015-08-13 23:25:59 +00:00
|
|
|
NodeID: "zip",
|
|
|
|
Name: "my-job.web[10]",
|
2016-05-25 00:47:03 +00:00
|
|
|
Job: oldJob,
|
2015-08-13 23:25:59 +00:00
|
|
|
},
|
2015-08-13 23:47:39 +00:00
|
|
|
|
|
|
|
// Migrate the 3rd
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-08-03 22:45:42 +00:00
|
|
|
NodeID: "drainNode",
|
2015-08-13 23:47:39 +00:00
|
|
|
Name: "my-job.web[2]",
|
2016-05-25 00:47:03 +00:00
|
|
|
Job: oldJob,
|
2018-02-23 01:38:44 +00:00
|
|
|
DesiredTransition: structs.DesiredTransition{
|
2018-02-21 18:58:04 +00:00
|
|
|
Migrate: helper.BoolToPtr(true),
|
|
|
|
},
|
2015-08-13 23:47:39 +00:00
|
|
|
},
|
2016-08-03 22:45:42 +00:00
|
|
|
// Mark the 4th lost
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-08-03 22:45:42 +00:00
|
|
|
NodeID: "dead",
|
|
|
|
Name: "my-job.web[3]",
|
|
|
|
Job: oldJob,
|
|
|
|
},
|
2015-08-13 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 22:36:30 +00:00
|
|
|
// Have three terminal allocs
|
|
|
|
terminalAllocs := map[string]*structs.Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
"my-job.web[4]": {
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-08-30 22:36:30 +00:00
|
|
|
NodeID: "zip",
|
|
|
|
Name: "my-job.web[4]",
|
|
|
|
Job: job,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"my-job.web[5]": {
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-08-30 22:36:30 +00:00
|
|
|
NodeID: "zip",
|
|
|
|
Name: "my-job.web[5]",
|
|
|
|
Job: job,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"my-job.web[6]": {
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-08-30 22:36:30 +00:00
|
|
|
NodeID: "zip",
|
|
|
|
Name: "my-job.web[6]",
|
|
|
|
Job: job,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := diffAllocs(job, tainted, required, allocs, terminalAllocs)
|
2015-08-14 01:28:09 +00:00
|
|
|
place := diff.place
|
|
|
|
update := diff.update
|
|
|
|
migrate := diff.migrate
|
2015-08-26 00:06:06 +00:00
|
|
|
stop := diff.stop
|
2015-08-14 01:28:09 +00:00
|
|
|
ignore := diff.ignore
|
2016-08-03 22:45:42 +00:00
|
|
|
lost := diff.lost
|
2015-08-13 23:25:59 +00:00
|
|
|
|
|
|
|
// We should update the first alloc
|
2015-08-14 01:16:32 +00:00
|
|
|
if len(update) != 1 || update[0].Alloc != allocs[0] {
|
2015-08-13 23:25:59 +00:00
|
|
|
t.Fatalf("bad: %#v", update)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should ignore the second alloc
|
2015-08-14 01:16:32 +00:00
|
|
|
if len(ignore) != 1 || ignore[0].Alloc != allocs[1] {
|
2015-08-13 23:25:59 +00:00
|
|
|
t.Fatalf("bad: %#v", ignore)
|
|
|
|
}
|
|
|
|
|
2015-08-26 00:06:06 +00:00
|
|
|
// We should stop the 3rd alloc
|
|
|
|
if len(stop) != 1 || stop[0].Alloc != allocs[2] {
|
|
|
|
t.Fatalf("bad: %#v", stop)
|
2015-08-13 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
2015-08-13 23:47:39 +00:00
|
|
|
// We should migrate the 4rd alloc
|
2015-08-14 01:16:32 +00:00
|
|
|
if len(migrate) != 1 || migrate[0].Alloc != allocs[3] {
|
2015-08-13 23:47:39 +00:00
|
|
|
t.Fatalf("bad: %#v", migrate)
|
|
|
|
}
|
|
|
|
|
2016-08-03 22:45:42 +00:00
|
|
|
// We should mark the 5th alloc as lost
|
|
|
|
if len(lost) != 1 || lost[0].Alloc != allocs[4] {
|
|
|
|
t.Fatalf("bad: %#v", migrate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should place 6
|
|
|
|
if len(place) != 6 {
|
2015-08-13 23:25:59 +00:00
|
|
|
t.Fatalf("bad: %#v", place)
|
|
|
|
}
|
2016-08-30 22:36:30 +00:00
|
|
|
|
|
|
|
// Ensure that the allocations which are replacements of terminal allocs are
|
|
|
|
// annotated
|
|
|
|
for name, alloc := range terminalAllocs {
|
|
|
|
for _, allocTuple := range diff.place {
|
|
|
|
if name == allocTuple.Name {
|
|
|
|
if !reflect.DeepEqual(alloc, allocTuple.Alloc) {
|
|
|
|
t.Fatalf("expected: %#v, actual: %#v", alloc, allocTuple.Alloc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 23:25:59 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 20:14:44 +00:00
|
|
|
func TestDiffSystemAllocs(t *testing.T) {
|
|
|
|
job := mock.SystemJob()
|
|
|
|
|
2016-08-03 22:45:42 +00:00
|
|
|
drainNode := mock.Node()
|
|
|
|
drainNode.Drain = true
|
|
|
|
|
|
|
|
deadNode := mock.Node()
|
|
|
|
deadNode.Status = structs.NodeStatusDown
|
|
|
|
|
|
|
|
tainted := map[string]*structs.Node{
|
2016-09-17 18:28:02 +00:00
|
|
|
deadNode.ID: deadNode,
|
|
|
|
drainNode.ID: drainNode,
|
2015-10-15 20:14:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 18:28:02 +00:00
|
|
|
// Create three alive nodes.
|
|
|
|
nodes := []*structs.Node{{ID: "foo"}, {ID: "bar"}, {ID: "baz"},
|
|
|
|
{ID: "pipe"}, {ID: drainNode.ID}, {ID: deadNode.ID}}
|
|
|
|
|
|
|
|
// The "old" job has a previous modify index
|
|
|
|
oldJob := new(structs.Job)
|
|
|
|
*oldJob = *job
|
|
|
|
oldJob.JobModifyIndex -= 1
|
|
|
|
|
2015-10-15 20:14:44 +00:00
|
|
|
allocs := []*structs.Allocation{
|
|
|
|
// Update allocation on baz
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2015-10-15 20:14:44 +00:00
|
|
|
NodeID: "baz",
|
|
|
|
Name: "my-job.web[0]",
|
|
|
|
Job: oldJob,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Ignore allocation on bar
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2015-10-15 20:14:44 +00:00
|
|
|
NodeID: "bar",
|
|
|
|
Name: "my-job.web[0]",
|
|
|
|
Job: job,
|
|
|
|
},
|
|
|
|
|
2016-08-03 22:45:42 +00:00
|
|
|
// Stop allocation on draining node.
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-09-17 18:28:02 +00:00
|
|
|
NodeID: drainNode.ID,
|
2016-08-03 22:45:42 +00:00
|
|
|
Name: "my-job.web[0]",
|
|
|
|
Job: oldJob,
|
2018-02-23 01:38:44 +00:00
|
|
|
DesiredTransition: structs.DesiredTransition{
|
2018-02-21 18:58:04 +00:00
|
|
|
Migrate: helper.BoolToPtr(true),
|
|
|
|
},
|
2016-08-03 22:45:42 +00:00
|
|
|
},
|
|
|
|
// Mark as lost on a dead node
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-09-17 18:28:02 +00:00
|
|
|
NodeID: deadNode.ID,
|
2015-10-15 20:14:44 +00:00
|
|
|
Name: "my-job.web[0]",
|
2016-05-25 00:47:03 +00:00
|
|
|
Job: oldJob,
|
2015-10-15 20:14:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:36:30 +00:00
|
|
|
// Have three terminal allocs
|
|
|
|
terminalAllocs := map[string]*structs.Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
"my-job.web[0]": {
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2016-08-30 22:36:30 +00:00
|
|
|
NodeID: "pipe",
|
|
|
|
Name: "my-job.web[0]",
|
|
|
|
Job: job,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
diff := diffSystemAllocs(job, nodes, tainted, allocs, terminalAllocs)
|
2015-10-15 20:14:44 +00:00
|
|
|
place := diff.place
|
|
|
|
update := diff.update
|
|
|
|
migrate := diff.migrate
|
|
|
|
stop := diff.stop
|
|
|
|
ignore := diff.ignore
|
2016-08-03 22:45:42 +00:00
|
|
|
lost := diff.lost
|
2015-10-15 20:14:44 +00:00
|
|
|
|
|
|
|
// We should update the first alloc
|
|
|
|
if len(update) != 1 || update[0].Alloc != allocs[0] {
|
|
|
|
t.Fatalf("bad: %#v", update)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should ignore the second alloc
|
|
|
|
if len(ignore) != 1 || ignore[0].Alloc != allocs[1] {
|
|
|
|
t.Fatalf("bad: %#v", ignore)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should stop the third alloc
|
2018-03-27 00:54:46 +00:00
|
|
|
if len(stop) != 0 {
|
2015-10-15 20:14:44 +00:00
|
|
|
t.Fatalf("bad: %#v", stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should be no migrates.
|
2018-03-27 00:54:46 +00:00
|
|
|
if len(migrate) != 1 || migrate[0].Alloc != allocs[2] {
|
2015-10-15 20:14:44 +00:00
|
|
|
t.Fatalf("bad: %#v", migrate)
|
|
|
|
}
|
|
|
|
|
2016-08-03 22:45:42 +00:00
|
|
|
// We should mark the 5th alloc as lost
|
|
|
|
if len(lost) != 1 || lost[0].Alloc != allocs[3] {
|
|
|
|
t.Fatalf("bad: %#v", migrate)
|
|
|
|
}
|
|
|
|
|
2015-10-15 20:14:44 +00:00
|
|
|
// We should place 1
|
2016-09-17 18:28:02 +00:00
|
|
|
if l := len(place); l != 2 {
|
|
|
|
t.Fatalf("bad: %#v", l)
|
2015-10-15 20:14:44 +00:00
|
|
|
}
|
2016-08-30 22:36:30 +00:00
|
|
|
|
|
|
|
// Ensure that the allocations which are replacements of terminal allocs are
|
|
|
|
// annotated
|
|
|
|
for _, alloc := range terminalAllocs {
|
|
|
|
for _, allocTuple := range diff.place {
|
|
|
|
if alloc.NodeID == allocTuple.Alloc.NodeID {
|
|
|
|
if !reflect.DeepEqual(alloc, allocTuple.Alloc) {
|
|
|
|
t.Fatalf("expected: %#v, actual: %#v", alloc, allocTuple.Alloc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-15 20:14:44 +00:00
|
|
|
}
|
|
|
|
|
2015-08-14 00:19:09 +00:00
|
|
|
func TestReadyNodesInDCs(t *testing.T) {
|
2017-10-13 21:36:02 +00:00
|
|
|
state := state.TestStateStore(t)
|
2015-08-14 00:19:09 +00:00
|
|
|
node1 := mock.Node()
|
|
|
|
node2 := mock.Node()
|
|
|
|
node2.Datacenter = "dc2"
|
|
|
|
node3 := mock.Node()
|
|
|
|
node3.Datacenter = "dc2"
|
|
|
|
node3.Status = structs.NodeStatusDown
|
2015-09-07 02:47:02 +00:00
|
|
|
node4 := mock.Node()
|
|
|
|
node4.Drain = true
|
2015-08-14 00:19:09 +00:00
|
|
|
|
2015-09-07 03:47:42 +00:00
|
|
|
noErr(t, state.UpsertNode(1000, node1))
|
|
|
|
noErr(t, state.UpsertNode(1001, node2))
|
|
|
|
noErr(t, state.UpsertNode(1002, node3))
|
|
|
|
noErr(t, state.UpsertNode(1003, node4))
|
2015-08-14 00:19:09 +00:00
|
|
|
|
2016-01-04 20:07:33 +00:00
|
|
|
nodes, dc, err := readyNodesInDCs(state, []string{"dc1", "dc2"})
|
2015-08-14 00:19:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nodes) != 2 {
|
2015-08-15 23:10:10 +00:00
|
|
|
t.Fatalf("bad: %v", nodes)
|
2015-08-14 00:19:09 +00:00
|
|
|
}
|
|
|
|
if nodes[0].ID == node3.ID || nodes[1].ID == node3.ID {
|
|
|
|
t.Fatalf("Bad: %#v", nodes)
|
|
|
|
}
|
2016-01-04 22:33:10 +00:00
|
|
|
if count, ok := dc["dc1"]; !ok || count != 1 {
|
2016-01-04 20:07:33 +00:00
|
|
|
t.Fatalf("Bad: dc1 count %v", count)
|
|
|
|
}
|
2016-01-04 22:33:10 +00:00
|
|
|
if count, ok := dc["dc2"]; !ok || count != 1 {
|
2016-01-04 20:07:33 +00:00
|
|
|
t.Fatalf("Bad: dc2 count %v", count)
|
|
|
|
}
|
2015-08-14 00:19:09 +00:00
|
|
|
}
|
2015-08-14 00:40:23 +00:00
|
|
|
|
|
|
|
func TestRetryMax(t *testing.T) {
|
|
|
|
calls := 0
|
|
|
|
bad := func() (bool, error) {
|
|
|
|
calls += 1
|
|
|
|
return false, nil
|
|
|
|
}
|
2016-02-10 05:24:47 +00:00
|
|
|
err := retryMax(3, bad, nil)
|
2015-08-14 00:40:23 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
if calls != 3 {
|
|
|
|
t.Fatalf("mis match")
|
|
|
|
}
|
|
|
|
|
2016-02-10 05:24:47 +00:00
|
|
|
calls = 0
|
|
|
|
first := true
|
|
|
|
reset := func() bool {
|
|
|
|
if calls == 3 && first {
|
|
|
|
first = false
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
err = retryMax(3, bad, reset)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
if calls != 6 {
|
|
|
|
t.Fatalf("mis match")
|
|
|
|
}
|
|
|
|
|
2015-08-14 00:40:23 +00:00
|
|
|
calls = 0
|
|
|
|
good := func() (bool, error) {
|
|
|
|
calls += 1
|
|
|
|
return true, nil
|
|
|
|
}
|
2016-02-10 05:24:47 +00:00
|
|
|
err = retryMax(3, good, nil)
|
2015-08-14 00:40:23 +00:00
|
|
|
if err != nil {
|
2015-08-15 23:10:10 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2015-08-14 00:40:23 +00:00
|
|
|
}
|
|
|
|
if calls != 1 {
|
|
|
|
t.Fatalf("mis match")
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 00:51:31 +00:00
|
|
|
|
|
|
|
func TestTaintedNodes(t *testing.T) {
|
2017-10-13 21:36:02 +00:00
|
|
|
state := state.TestStateStore(t)
|
2015-08-14 00:51:31 +00:00
|
|
|
node1 := mock.Node()
|
|
|
|
node2 := mock.Node()
|
|
|
|
node2.Datacenter = "dc2"
|
|
|
|
node3 := mock.Node()
|
|
|
|
node3.Datacenter = "dc2"
|
|
|
|
node3.Status = structs.NodeStatusDown
|
2015-09-07 02:47:02 +00:00
|
|
|
node4 := mock.Node()
|
|
|
|
node4.Drain = true
|
2015-09-07 03:47:42 +00:00
|
|
|
noErr(t, state.UpsertNode(1000, node1))
|
|
|
|
noErr(t, state.UpsertNode(1001, node2))
|
|
|
|
noErr(t, state.UpsertNode(1002, node3))
|
|
|
|
noErr(t, state.UpsertNode(1003, node4))
|
2015-08-14 00:51:31 +00:00
|
|
|
|
|
|
|
allocs := []*structs.Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
{NodeID: node1.ID},
|
|
|
|
{NodeID: node2.ID},
|
|
|
|
{NodeID: node3.ID},
|
|
|
|
{NodeID: node4.ID},
|
|
|
|
{NodeID: "12345678-abcd-efab-cdef-123456789abc"},
|
2015-08-14 00:51:31 +00:00
|
|
|
}
|
|
|
|
tainted, err := taintedNodes(state, allocs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-03 22:45:42 +00:00
|
|
|
if len(tainted) != 3 {
|
2015-08-15 23:10:10 +00:00
|
|
|
t.Fatalf("bad: %v", tainted)
|
2015-08-14 00:51:31 +00:00
|
|
|
}
|
2016-08-03 22:45:42 +00:00
|
|
|
|
|
|
|
if _, ok := tainted[node1.ID]; ok {
|
|
|
|
t.Fatalf("Bad: %v", tainted)
|
|
|
|
}
|
|
|
|
if _, ok := tainted[node2.ID]; ok {
|
2015-08-14 00:51:31 +00:00
|
|
|
t.Fatalf("Bad: %v", tainted)
|
|
|
|
}
|
2016-08-03 22:45:42 +00:00
|
|
|
|
|
|
|
if node, ok := tainted[node3.ID]; !ok || node == nil {
|
|
|
|
t.Fatalf("Bad: %v", tainted)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := tainted[node4.ID]; !ok || node == nil {
|
|
|
|
t.Fatalf("Bad: %v", tainted)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node, ok := tainted["12345678-abcd-efab-cdef-123456789abc"]; !ok || node != nil {
|
2015-08-14 00:51:31 +00:00
|
|
|
t.Fatalf("Bad: %v", tainted)
|
|
|
|
}
|
|
|
|
}
|
2015-09-07 18:23:38 +00:00
|
|
|
|
|
|
|
func TestShuffleNodes(t *testing.T) {
|
2015-10-17 00:05:23 +00:00
|
|
|
// Use a large number of nodes to make the probability of shuffling to the
|
|
|
|
// original order very low.
|
2015-09-07 18:23:38 +00:00
|
|
|
nodes := []*structs.Node{
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
2015-10-17 00:05:23 +00:00
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
|
|
|
mock.Node(),
|
2015-09-07 18:23:38 +00:00
|
|
|
}
|
|
|
|
orig := make([]*structs.Node, len(nodes))
|
|
|
|
copy(orig, nodes)
|
|
|
|
shuffleNodes(nodes)
|
|
|
|
if reflect.DeepEqual(nodes, orig) {
|
2015-10-17 00:05:23 +00:00
|
|
|
t.Fatalf("should not match")
|
2015-09-07 18:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-07 19:25:23 +00:00
|
|
|
|
|
|
|
func TestTasksUpdated(t *testing.T) {
|
|
|
|
j1 := mock.Job()
|
|
|
|
j2 := mock.Job()
|
2016-12-16 01:08:38 +00:00
|
|
|
name := j1.TaskGroups[0].Name
|
2015-09-07 19:25:23 +00:00
|
|
|
|
2016-12-16 01:08:38 +00:00
|
|
|
if tasksUpdated(j1, j2, name) {
|
2015-09-07 19:25:23 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j2.TaskGroups[0].Tasks[0].Config["command"] = "/bin/other"
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j2, name) {
|
2015-09-07 19:25:23 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j3 := mock.Job()
|
|
|
|
j3.TaskGroups[0].Tasks[0].Name = "foo"
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j3, name) {
|
2015-09-07 19:25:23 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j4 := mock.Job()
|
|
|
|
j4.TaskGroups[0].Tasks[0].Driver = "foo"
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j4, name) {
|
2015-09-07 19:25:23 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j5 := mock.Job()
|
|
|
|
j5.TaskGroups[0].Tasks = append(j5.TaskGroups[0].Tasks,
|
|
|
|
j5.TaskGroups[0].Tasks[0])
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j5, name) {
|
2015-09-07 19:25:23 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-09-13 23:41:53 +00:00
|
|
|
|
|
|
|
j6 := mock.Job()
|
2017-02-28 00:00:19 +00:00
|
|
|
j6.TaskGroups[0].Tasks[0].Resources.Networks[0].DynamicPorts = []structs.Port{
|
|
|
|
{Label: "http", Value: 0},
|
|
|
|
{Label: "https", Value: 0},
|
|
|
|
{Label: "admin", Value: 0},
|
|
|
|
}
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j6, name) {
|
2015-09-13 23:41:53 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2015-10-23 21:52:06 +00:00
|
|
|
|
|
|
|
j7 := mock.Job()
|
|
|
|
j7.TaskGroups[0].Tasks[0].Env["NEW_ENV"] = "NEW_VALUE"
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j7, name) {
|
2015-10-23 21:52:06 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2016-04-26 00:20:25 +00:00
|
|
|
|
|
|
|
j8 := mock.Job()
|
|
|
|
j8.TaskGroups[0].Tasks[0].User = "foo"
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j8, name) {
|
2016-04-26 00:20:25 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j9 := mock.Job()
|
|
|
|
j9.TaskGroups[0].Tasks[0].Artifacts = []*structs.TaskArtifact{
|
|
|
|
{
|
|
|
|
GetterSource: "http://foo.com/bar",
|
|
|
|
},
|
|
|
|
}
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j9, name) {
|
2016-04-26 00:20:25 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j10 := mock.Job()
|
|
|
|
j10.TaskGroups[0].Tasks[0].Meta["baz"] = "boom"
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j10, name) {
|
2016-04-26 00:20:25 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j11 := mock.Job()
|
|
|
|
j11.TaskGroups[0].Tasks[0].Resources.CPU = 1337
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j11, name) {
|
2016-04-26 00:20:25 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2016-05-06 04:32:01 +00:00
|
|
|
|
|
|
|
j12 := mock.Job()
|
|
|
|
j12.TaskGroups[0].Tasks[0].Resources.Networks[0].MBits = 100
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j12, name) {
|
2016-05-06 04:32:01 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j13 := mock.Job()
|
|
|
|
j13.TaskGroups[0].Tasks[0].Resources.Networks[0].DynamicPorts[0].Label = "foobar"
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j13, name) {
|
2016-05-06 04:32:01 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
j14 := mock.Job()
|
|
|
|
j14.TaskGroups[0].Tasks[0].Resources.Networks[0].ReservedPorts = []structs.Port{{Label: "foo", Value: 1312}}
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j14, name) {
|
2016-05-06 04:32:01 +00:00
|
|
|
t.Fatalf("bad")
|
2016-09-21 18:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
j15 := mock.Job()
|
|
|
|
j15.TaskGroups[0].Tasks[0].Vault = &structs.Vault{Policies: []string{"foo"}}
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j15, name) {
|
2016-09-21 18:29:50 +00:00
|
|
|
t.Fatalf("bad")
|
2016-09-21 21:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
j16 := mock.Job()
|
|
|
|
j16.TaskGroups[0].EphemeralDisk.Sticky = true
|
2016-12-16 01:08:38 +00:00
|
|
|
if !tasksUpdated(j1, j16, name) {
|
|
|
|
t.Fatal("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change group meta
|
|
|
|
j17 := mock.Job()
|
|
|
|
j17.TaskGroups[0].Meta["j17_test"] = "roll_baby_roll"
|
|
|
|
if !tasksUpdated(j1, j17, name) {
|
|
|
|
t.Fatal("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change job meta
|
|
|
|
j18 := mock.Job()
|
|
|
|
j18.Meta["j18_test"] = "roll_baby_roll"
|
|
|
|
if !tasksUpdated(j1, j18, name) {
|
2016-09-21 21:00:02 +00:00
|
|
|
t.Fatal("bad")
|
2016-05-06 04:32:01 +00:00
|
|
|
}
|
2015-09-07 19:25:23 +00:00
|
|
|
}
|
2015-10-16 21:00:51 +00:00
|
|
|
|
2015-10-16 23:35:55 +00:00
|
|
|
func TestEvictAndPlace_LimitLessThanAllocs(t *testing.T) {
|
|
|
|
_, ctx := testContext(t)
|
|
|
|
allocs := []allocTuple{
|
2017-09-29 16:58:48 +00:00
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
diff := &diffResult{}
|
|
|
|
|
|
|
|
limit := 2
|
|
|
|
if !evictAndPlace(ctx, diff, allocs, "", &limit) {
|
|
|
|
t.Fatal("evictAndReplace() should have returned true")
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != 0 {
|
2015-10-21 12:47:36 +00:00
|
|
|
t.Fatalf("evictAndReplace() should decremented limit; got %v; want 0", limit)
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(diff.place) != 2 {
|
2015-10-21 12:47:36 +00:00
|
|
|
t.Fatalf("evictAndReplace() didn't insert into diffResult properly: %v", diff.place)
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvictAndPlace_LimitEqualToAllocs(t *testing.T) {
|
|
|
|
_, ctx := testContext(t)
|
|
|
|
allocs := []allocTuple{
|
2017-09-29 16:58:48 +00:00
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
diff := &diffResult{}
|
|
|
|
|
|
|
|
limit := 4
|
|
|
|
if evictAndPlace(ctx, diff, allocs, "", &limit) {
|
|
|
|
t.Fatal("evictAndReplace() should have returned false")
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != 0 {
|
2015-10-21 12:47:36 +00:00
|
|
|
t.Fatalf("evictAndReplace() should decremented limit; got %v; want 0", limit)
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(diff.place) != 4 {
|
2015-10-21 12:47:36 +00:00
|
|
|
t.Fatalf("evictAndReplace() didn't insert into diffResult properly: %v", diff.place)
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetStatus(t *testing.T) {
|
|
|
|
h := NewHarness(t)
|
|
|
|
logger := log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
eval := mock.Eval()
|
|
|
|
status := "a"
|
|
|
|
desc := "b"
|
2017-07-06 00:13:45 +00:00
|
|
|
if err := setStatus(logger, h, eval, nil, nil, nil, status, desc, nil, ""); err != nil {
|
2015-10-16 23:35:55 +00:00
|
|
|
t.Fatalf("setStatus() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.Evals) != 1 {
|
|
|
|
t.Fatalf("setStatus() didn't update plan: %v", h.Evals)
|
|
|
|
}
|
|
|
|
|
|
|
|
newEval := h.Evals[0]
|
|
|
|
if newEval.ID != eval.ID || newEval.Status != status || newEval.StatusDescription != desc {
|
|
|
|
t.Fatalf("setStatus() submited invalid eval: %v", newEval)
|
|
|
|
}
|
|
|
|
|
2016-05-19 20:09:52 +00:00
|
|
|
// Test next evals
|
2015-10-16 23:35:55 +00:00
|
|
|
h = NewHarness(t)
|
|
|
|
next := mock.Eval()
|
2017-07-06 00:13:45 +00:00
|
|
|
if err := setStatus(logger, h, eval, next, nil, nil, status, desc, nil, ""); err != nil {
|
2015-10-16 23:35:55 +00:00
|
|
|
t.Fatalf("setStatus() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.Evals) != 1 {
|
|
|
|
t.Fatalf("setStatus() didn't update plan: %v", h.Evals)
|
|
|
|
}
|
|
|
|
|
|
|
|
newEval = h.Evals[0]
|
|
|
|
if newEval.NextEval != next.ID {
|
|
|
|
t.Fatalf("setStatus() didn't set nextEval correctly: %v", newEval)
|
|
|
|
}
|
2016-05-19 20:09:52 +00:00
|
|
|
|
|
|
|
// Test blocked evals
|
|
|
|
h = NewHarness(t)
|
|
|
|
blocked := mock.Eval()
|
2017-07-06 00:13:45 +00:00
|
|
|
if err := setStatus(logger, h, eval, nil, blocked, nil, status, desc, nil, ""); err != nil {
|
2016-05-19 20:09:52 +00:00
|
|
|
t.Fatalf("setStatus() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.Evals) != 1 {
|
|
|
|
t.Fatalf("setStatus() didn't update plan: %v", h.Evals)
|
|
|
|
}
|
|
|
|
|
|
|
|
newEval = h.Evals[0]
|
2016-05-25 01:12:59 +00:00
|
|
|
if newEval.BlockedEval != blocked.ID {
|
|
|
|
t.Fatalf("setStatus() didn't set BlockedEval correctly: %v", newEval)
|
2016-05-19 20:09:52 +00:00
|
|
|
}
|
2016-05-27 18:26:14 +00:00
|
|
|
|
|
|
|
// Test metrics
|
|
|
|
h = NewHarness(t)
|
|
|
|
metrics := map[string]*structs.AllocMetric{"foo": nil}
|
2017-07-06 00:13:45 +00:00
|
|
|
if err := setStatus(logger, h, eval, nil, nil, metrics, status, desc, nil, ""); err != nil {
|
2016-05-27 18:26:14 +00:00
|
|
|
t.Fatalf("setStatus() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.Evals) != 1 {
|
|
|
|
t.Fatalf("setStatus() didn't update plan: %v", h.Evals)
|
|
|
|
}
|
|
|
|
|
|
|
|
newEval = h.Evals[0]
|
|
|
|
if !reflect.DeepEqual(newEval.FailedTGAllocs, metrics) {
|
2016-07-18 22:04:05 +00:00
|
|
|
t.Fatalf("setStatus() didn't set failed task group metrics correctly: %v", newEval)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test queued allocations
|
|
|
|
h = NewHarness(t)
|
|
|
|
queuedAllocs := map[string]int{"web": 1}
|
|
|
|
|
2017-07-06 00:13:45 +00:00
|
|
|
if err := setStatus(logger, h, eval, nil, nil, metrics, status, desc, queuedAllocs, ""); err != nil {
|
2016-07-18 22:04:05 +00:00
|
|
|
t.Fatalf("setStatus() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.Evals) != 1 {
|
|
|
|
t.Fatalf("setStatus() didn't update plan: %v", h.Evals)
|
|
|
|
}
|
|
|
|
|
|
|
|
newEval = h.Evals[0]
|
|
|
|
if !reflect.DeepEqual(newEval.QueuedAllocations, queuedAllocs) {
|
2016-05-27 18:26:14 +00:00
|
|
|
t.Fatalf("setStatus() didn't set failed task group metrics correctly: %v", newEval)
|
|
|
|
}
|
2017-07-06 00:13:45 +00:00
|
|
|
|
|
|
|
h = NewHarness(t)
|
2017-09-29 16:58:48 +00:00
|
|
|
dID := uuid.Generate()
|
2017-07-06 00:13:45 +00:00
|
|
|
if err := setStatus(logger, h, eval, nil, nil, metrics, status, desc, queuedAllocs, dID); err != nil {
|
|
|
|
t.Fatalf("setStatus() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(h.Evals) != 1 {
|
|
|
|
t.Fatalf("setStatus() didn't update plan: %v", h.Evals)
|
|
|
|
}
|
|
|
|
|
|
|
|
newEval = h.Evals[0]
|
|
|
|
if newEval.DeploymentID != dID {
|
|
|
|
t.Fatalf("setStatus() didn't set deployment id correctly: %v", newEval)
|
|
|
|
}
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInplaceUpdate_ChangedTaskGroup(t *testing.T) {
|
|
|
|
state, ctx := testContext(t)
|
|
|
|
eval := mock.Eval()
|
|
|
|
job := mock.Job()
|
|
|
|
|
|
|
|
node := mock.Node()
|
2016-07-22 21:53:49 +00:00
|
|
|
noErr(t, state.UpsertNode(900, node))
|
2015-10-16 23:35:55 +00:00
|
|
|
|
|
|
|
// Register an alloc
|
|
|
|
alloc := &structs.Allocation{
|
2017-09-07 23:56:15 +00:00
|
|
|
Namespace: structs.DefaultNamespace,
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2017-09-07 23:56:15 +00:00
|
|
|
EvalID: eval.ID,
|
|
|
|
NodeID: node.ID,
|
|
|
|
JobID: job.ID,
|
|
|
|
Job: job,
|
2015-10-16 23:35:55 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 2048,
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
|
|
|
DesiredStatus: structs.AllocDesiredStatusRun,
|
2016-07-22 21:53:49 +00:00
|
|
|
TaskGroup: "web",
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
alloc.TaskResources = map[string]*structs.Resources{"web": alloc.Resources}
|
2016-07-22 21:53:49 +00:00
|
|
|
noErr(t, state.UpsertJobSummary(1000, mock.JobSummary(alloc.JobID)))
|
2015-10-16 23:35:55 +00:00
|
|
|
noErr(t, state.UpsertAllocs(1001, []*structs.Allocation{alloc}))
|
|
|
|
|
|
|
|
// Create a new task group that prevents in-place updates.
|
|
|
|
tg := &structs.TaskGroup{}
|
|
|
|
*tg = *job.TaskGroups[0]
|
|
|
|
task := &structs.Task{Name: "FOO"}
|
|
|
|
tg.Tasks = nil
|
|
|
|
tg.Tasks = append(tg.Tasks, task)
|
|
|
|
|
|
|
|
updates := []allocTuple{{Alloc: alloc, TaskGroup: tg}}
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
2015-10-16 23:35:55 +00:00
|
|
|
|
|
|
|
// Do the inplace update.
|
2016-05-17 22:37:37 +00:00
|
|
|
unplaced, inplace := inplaceUpdate(ctx, eval, job, stack, updates)
|
2015-10-16 23:35:55 +00:00
|
|
|
|
2016-05-17 22:37:37 +00:00
|
|
|
if len(unplaced) != 1 || len(inplace) != 0 {
|
2015-10-16 23:35:55 +00:00
|
|
|
t.Fatal("inplaceUpdate incorrectly did an inplace update")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.plan.NodeAllocation) != 0 {
|
|
|
|
t.Fatal("inplaceUpdate incorrectly did an inplace update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInplaceUpdate_NoMatch(t *testing.T) {
|
|
|
|
state, ctx := testContext(t)
|
|
|
|
eval := mock.Eval()
|
|
|
|
job := mock.Job()
|
|
|
|
|
|
|
|
node := mock.Node()
|
2016-07-22 21:53:49 +00:00
|
|
|
noErr(t, state.UpsertNode(900, node))
|
2015-10-16 23:35:55 +00:00
|
|
|
|
|
|
|
// Register an alloc
|
|
|
|
alloc := &structs.Allocation{
|
2017-09-07 23:56:15 +00:00
|
|
|
Namespace: structs.DefaultNamespace,
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2017-09-07 23:56:15 +00:00
|
|
|
EvalID: eval.ID,
|
|
|
|
NodeID: node.ID,
|
|
|
|
JobID: job.ID,
|
|
|
|
Job: job,
|
2015-10-16 23:35:55 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 2048,
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
|
|
|
DesiredStatus: structs.AllocDesiredStatusRun,
|
2016-07-22 21:53:49 +00:00
|
|
|
TaskGroup: "web",
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
alloc.TaskResources = map[string]*structs.Resources{"web": alloc.Resources}
|
2016-07-22 21:53:49 +00:00
|
|
|
noErr(t, state.UpsertJobSummary(1000, mock.JobSummary(alloc.JobID)))
|
2015-10-16 23:35:55 +00:00
|
|
|
noErr(t, state.UpsertAllocs(1001, []*structs.Allocation{alloc}))
|
|
|
|
|
|
|
|
// Create a new task group that requires too much resources.
|
|
|
|
tg := &structs.TaskGroup{}
|
|
|
|
*tg = *job.TaskGroups[0]
|
|
|
|
resource := &structs.Resources{CPU: 9999}
|
|
|
|
tg.Tasks[0].Resources = resource
|
|
|
|
|
|
|
|
updates := []allocTuple{{Alloc: alloc, TaskGroup: tg}}
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
2015-10-16 23:35:55 +00:00
|
|
|
|
|
|
|
// Do the inplace update.
|
2016-05-17 22:37:37 +00:00
|
|
|
unplaced, inplace := inplaceUpdate(ctx, eval, job, stack, updates)
|
2015-10-16 23:35:55 +00:00
|
|
|
|
2016-05-17 22:37:37 +00:00
|
|
|
if len(unplaced) != 1 || len(inplace) != 0 {
|
2015-10-16 23:35:55 +00:00
|
|
|
t.Fatal("inplaceUpdate incorrectly did an inplace update")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.plan.NodeAllocation) != 0 {
|
|
|
|
t.Fatal("inplaceUpdate incorrectly did an inplace update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInplaceUpdate_Success(t *testing.T) {
|
|
|
|
state, ctx := testContext(t)
|
|
|
|
eval := mock.Eval()
|
|
|
|
job := mock.Job()
|
|
|
|
|
|
|
|
node := mock.Node()
|
2016-07-22 21:53:49 +00:00
|
|
|
noErr(t, state.UpsertNode(900, node))
|
2015-10-16 23:35:55 +00:00
|
|
|
|
|
|
|
// Register an alloc
|
|
|
|
alloc := &structs.Allocation{
|
2017-09-07 23:56:15 +00:00
|
|
|
Namespace: structs.DefaultNamespace,
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
2015-12-15 01:06:58 +00:00
|
|
|
EvalID: eval.ID,
|
|
|
|
NodeID: node.ID,
|
|
|
|
JobID: job.ID,
|
|
|
|
Job: job,
|
|
|
|
TaskGroup: job.TaskGroups[0].Name,
|
2015-10-16 23:35:55 +00:00
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 2048,
|
|
|
|
MemoryMB: 2048,
|
|
|
|
},
|
|
|
|
DesiredStatus: structs.AllocDesiredStatusRun,
|
|
|
|
}
|
|
|
|
alloc.TaskResources = map[string]*structs.Resources{"web": alloc.Resources}
|
2016-07-22 21:53:49 +00:00
|
|
|
noErr(t, state.UpsertJobSummary(999, mock.JobSummary(alloc.JobID)))
|
2015-10-16 23:35:55 +00:00
|
|
|
noErr(t, state.UpsertAllocs(1001, []*structs.Allocation{alloc}))
|
|
|
|
|
|
|
|
// Create a new task group that updates the resources.
|
|
|
|
tg := &structs.TaskGroup{}
|
|
|
|
*tg = *job.TaskGroups[0]
|
|
|
|
resource := &structs.Resources{CPU: 737}
|
|
|
|
tg.Tasks[0].Resources = resource
|
2016-06-12 23:36:49 +00:00
|
|
|
newServices := []*structs.Service{
|
2015-12-15 17:14:32 +00:00
|
|
|
{
|
|
|
|
Name: "dummy-service",
|
|
|
|
PortLabel: "http",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "dummy-service2",
|
|
|
|
PortLabel: "http",
|
|
|
|
},
|
|
|
|
}
|
2015-12-15 18:43:56 +00:00
|
|
|
|
|
|
|
// Delete service 2
|
2016-06-12 23:36:49 +00:00
|
|
|
tg.Tasks[0].Services = tg.Tasks[0].Services[:1]
|
2015-12-15 18:43:56 +00:00
|
|
|
|
|
|
|
// Add the new services
|
2016-06-12 23:36:49 +00:00
|
|
|
tg.Tasks[0].Services = append(tg.Tasks[0].Services, newServices...)
|
2015-10-16 23:35:55 +00:00
|
|
|
|
|
|
|
updates := []allocTuple{{Alloc: alloc, TaskGroup: tg}}
|
2015-10-17 00:05:23 +00:00
|
|
|
stack := NewGenericStack(false, ctx)
|
2015-10-22 23:45:03 +00:00
|
|
|
stack.SetJob(job)
|
2015-10-16 23:35:55 +00:00
|
|
|
|
|
|
|
// Do the inplace update.
|
2016-05-17 22:37:37 +00:00
|
|
|
unplaced, inplace := inplaceUpdate(ctx, eval, job, stack, updates)
|
2015-10-16 23:35:55 +00:00
|
|
|
|
2016-05-17 22:37:37 +00:00
|
|
|
if len(unplaced) != 0 || len(inplace) != 1 {
|
2015-10-16 23:35:55 +00:00
|
|
|
t.Fatal("inplaceUpdate did not do an inplace update")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.plan.NodeAllocation) != 1 {
|
|
|
|
t.Fatal("inplaceUpdate did not do an inplace update")
|
|
|
|
}
|
2015-12-15 16:35:26 +00:00
|
|
|
|
2016-05-17 22:37:37 +00:00
|
|
|
if inplace[0].Alloc.ID != alloc.ID {
|
|
|
|
t.Fatalf("inplaceUpdate returned the wrong, inplace updated alloc: %#v", inplace)
|
|
|
|
}
|
|
|
|
|
2015-12-15 16:35:26 +00:00
|
|
|
// Get the alloc we inserted.
|
2016-06-09 06:51:12 +00:00
|
|
|
a := inplace[0].Alloc // TODO(sean@): Verify this is correct vs: ctx.plan.NodeAllocation[alloc.NodeID][0]
|
|
|
|
if a.Job == nil {
|
|
|
|
t.Fatalf("bad")
|
2015-12-15 17:14:32 +00:00
|
|
|
}
|
|
|
|
|
2016-06-09 06:51:12 +00:00
|
|
|
if len(a.Job.TaskGroups) != 1 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.Job.TaskGroups[0].Tasks) != 1 {
|
|
|
|
t.Fatalf("bad")
|
2015-12-15 16:35:26 +00:00
|
|
|
}
|
2015-12-15 18:43:56 +00:00
|
|
|
|
2016-06-12 23:36:49 +00:00
|
|
|
if len(a.Job.TaskGroups[0].Tasks[0].Services) != 3 {
|
|
|
|
t.Fatalf("Expected number of services: %v, Actual: %v", 3, len(a.Job.TaskGroups[0].Tasks[0].Services))
|
2016-06-09 06:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serviceNames := make(map[string]struct{}, 3)
|
2016-06-12 23:36:49 +00:00
|
|
|
for _, consulService := range a.Job.TaskGroups[0].Tasks[0].Services {
|
2016-06-09 06:51:12 +00:00
|
|
|
serviceNames[consulService.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
if len(serviceNames) != 3 {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range []string{"dummy-service", "dummy-service2", "web-frontend"} {
|
|
|
|
if _, found := serviceNames[name]; !found {
|
|
|
|
t.Errorf("Expected consul service name missing: %v", name)
|
|
|
|
}
|
2015-12-15 18:43:56 +00:00
|
|
|
}
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvictAndPlace_LimitGreaterThanAllocs(t *testing.T) {
|
|
|
|
_, ctx := testContext(t)
|
|
|
|
allocs := []allocTuple{
|
2017-09-29 16:58:48 +00:00
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
|
|
|
{Alloc: &structs.Allocation{ID: uuid.Generate()}},
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
diff := &diffResult{}
|
|
|
|
|
|
|
|
limit := 6
|
|
|
|
if evictAndPlace(ctx, diff, allocs, "", &limit) {
|
|
|
|
t.Fatal("evictAndReplace() should have returned false")
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit != 2 {
|
2015-10-21 12:47:36 +00:00
|
|
|
t.Fatalf("evictAndReplace() should decremented limit; got %v; want 2", limit)
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(diff.place) != 4 {
|
2015-10-21 12:47:36 +00:00
|
|
|
t.Fatalf("evictAndReplace() didn't insert into diffResult properly: %v", diff.place)
|
2015-10-16 23:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 21:00:51 +00:00
|
|
|
func TestTaskGroupConstraints(t *testing.T) {
|
2015-10-27 21:31:14 +00:00
|
|
|
constr := &structs.Constraint{RTarget: "bar"}
|
2015-10-16 21:00:51 +00:00
|
|
|
constr2 := &structs.Constraint{LTarget: "foo"}
|
2015-10-27 21:31:14 +00:00
|
|
|
constr3 := &structs.Constraint{Operand: "<"}
|
2015-10-16 21:00:51 +00:00
|
|
|
|
|
|
|
tg := &structs.TaskGroup{
|
2016-09-14 22:43:42 +00:00
|
|
|
Name: "web",
|
|
|
|
Count: 10,
|
|
|
|
Constraints: []*structs.Constraint{constr},
|
|
|
|
EphemeralDisk: &structs.EphemeralDisk{},
|
2015-10-16 21:00:51 +00:00
|
|
|
Tasks: []*structs.Task{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-10-16 21:00:51 +00:00
|
|
|
Driver: "exec",
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
},
|
|
|
|
Constraints: []*structs.Constraint{constr2},
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2015-10-16 21:00:51 +00:00
|
|
|
Driver: "docker",
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
},
|
|
|
|
Constraints: []*structs.Constraint{constr3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the expected values.
|
|
|
|
expConstr := []*structs.Constraint{constr, constr2, constr3}
|
2017-09-26 22:26:33 +00:00
|
|
|
expDrivers := map[string]struct{}{"exec": {}, "docker": {}}
|
2015-10-16 21:00:51 +00:00
|
|
|
expSize := &structs.Resources{
|
|
|
|
CPU: 1000,
|
|
|
|
MemoryMB: 512,
|
|
|
|
}
|
|
|
|
|
|
|
|
actConstrains := taskGroupConstraints(tg)
|
|
|
|
if !reflect.DeepEqual(actConstrains.constraints, expConstr) {
|
|
|
|
t.Fatalf("taskGroupConstraints(%v) returned %v; want %v", tg, actConstrains.constraints, expConstr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actConstrains.drivers, expDrivers) {
|
|
|
|
t.Fatalf("taskGroupConstraints(%v) returned %v; want %v", tg, actConstrains.drivers, expDrivers)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actConstrains.size, expSize) {
|
|
|
|
t.Fatalf("taskGroupConstraints(%v) returned %v; want %v", tg, actConstrains.size, expSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-11-13 01:47:51 +00:00
|
|
|
|
2016-02-22 18:38:04 +00:00
|
|
|
func TestProgressMade(t *testing.T) {
|
|
|
|
noopPlan := &structs.PlanResult{}
|
|
|
|
if progressMade(nil) || progressMade(noopPlan) {
|
|
|
|
t.Fatal("no progress plan marked as making progress")
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string][]*structs.Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
"foo": {mock.Alloc()},
|
2016-02-22 18:38:04 +00:00
|
|
|
}
|
|
|
|
both := &structs.PlanResult{
|
|
|
|
NodeAllocation: m,
|
2016-02-22 21:24:26 +00:00
|
|
|
NodeUpdate: m,
|
2016-02-22 18:38:04 +00:00
|
|
|
}
|
2016-02-22 21:24:26 +00:00
|
|
|
update := &structs.PlanResult{NodeUpdate: m}
|
|
|
|
alloc := &structs.PlanResult{NodeAllocation: m}
|
2017-07-06 16:55:39 +00:00
|
|
|
deployment := &structs.PlanResult{Deployment: mock.Deployment()}
|
|
|
|
deploymentUpdates := &structs.PlanResult{
|
|
|
|
DeploymentUpdates: []*structs.DeploymentStatusUpdate{
|
2017-09-29 16:58:48 +00:00
|
|
|
{DeploymentID: uuid.Generate()},
|
2017-07-06 16:55:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if !(progressMade(both) && progressMade(update) && progressMade(alloc) &&
|
|
|
|
progressMade(deployment) && progressMade(deploymentUpdates)) {
|
2016-02-22 18:38:04 +00:00
|
|
|
t.Fatal("bad")
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 18:21:58 +00:00
|
|
|
|
|
|
|
func TestDesiredUpdates(t *testing.T) {
|
|
|
|
tg1 := &structs.TaskGroup{Name: "foo"}
|
|
|
|
tg2 := &structs.TaskGroup{Name: "bar"}
|
2016-05-13 18:53:11 +00:00
|
|
|
a2 := &structs.Allocation{TaskGroup: "bar"}
|
2016-05-05 18:21:58 +00:00
|
|
|
|
|
|
|
place := []allocTuple{
|
2017-09-26 22:26:33 +00:00
|
|
|
{TaskGroup: tg1},
|
|
|
|
{TaskGroup: tg1},
|
|
|
|
{TaskGroup: tg1},
|
|
|
|
{TaskGroup: tg2},
|
2016-05-05 18:21:58 +00:00
|
|
|
}
|
|
|
|
stop := []allocTuple{
|
2017-09-26 22:26:33 +00:00
|
|
|
{TaskGroup: tg2, Alloc: a2},
|
|
|
|
{TaskGroup: tg2, Alloc: a2},
|
2016-05-05 18:21:58 +00:00
|
|
|
}
|
|
|
|
ignore := []allocTuple{
|
2017-09-26 22:26:33 +00:00
|
|
|
{TaskGroup: tg1},
|
2016-05-05 18:21:58 +00:00
|
|
|
}
|
|
|
|
migrate := []allocTuple{
|
2017-09-26 22:26:33 +00:00
|
|
|
{TaskGroup: tg2},
|
2016-05-05 18:21:58 +00:00
|
|
|
}
|
|
|
|
inplace := []allocTuple{
|
2017-09-26 22:26:33 +00:00
|
|
|
{TaskGroup: tg1},
|
|
|
|
{TaskGroup: tg1},
|
2016-05-05 18:21:58 +00:00
|
|
|
}
|
|
|
|
destructive := []allocTuple{
|
2017-09-26 22:26:33 +00:00
|
|
|
{TaskGroup: tg1},
|
|
|
|
{TaskGroup: tg2},
|
|
|
|
{TaskGroup: tg2},
|
2016-05-05 18:21:58 +00:00
|
|
|
}
|
|
|
|
diff := &diffResult{
|
|
|
|
place: place,
|
|
|
|
stop: stop,
|
|
|
|
ignore: ignore,
|
|
|
|
migrate: migrate,
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]*structs.DesiredUpdates{
|
|
|
|
"foo": {
|
|
|
|
Place: 3,
|
|
|
|
Ignore: 1,
|
|
|
|
InPlaceUpdate: 2,
|
|
|
|
DestructiveUpdate: 1,
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
Place: 1,
|
|
|
|
Stop: 2,
|
|
|
|
Migrate: 1,
|
|
|
|
DestructiveUpdate: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
desired := desiredUpdates(diff, inplace, destructive)
|
|
|
|
if !reflect.DeepEqual(desired, expected) {
|
|
|
|
t.Fatalf("desiredUpdates() returned %#v; want %#v", desired, expected)
|
|
|
|
}
|
|
|
|
}
|
2016-07-25 21:56:38 +00:00
|
|
|
|
|
|
|
func TestUtil_AdjustQueuedAllocations(t *testing.T) {
|
|
|
|
logger := log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.CreateIndex = 4
|
2017-01-08 22:14:35 +00:00
|
|
|
alloc2.ModifyIndex = 4
|
2016-07-25 21:56:38 +00:00
|
|
|
alloc3 := mock.Alloc()
|
|
|
|
alloc3.CreateIndex = 3
|
2017-01-08 22:14:35 +00:00
|
|
|
alloc3.ModifyIndex = 5
|
2016-07-25 21:56:38 +00:00
|
|
|
alloc4 := mock.Alloc()
|
|
|
|
alloc4.CreateIndex = 6
|
2017-01-08 22:14:35 +00:00
|
|
|
alloc4.ModifyIndex = 8
|
2016-07-25 21:56:38 +00:00
|
|
|
|
|
|
|
planResult := structs.PlanResult{
|
|
|
|
NodeUpdate: map[string][]*structs.Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
"node-1": {alloc1},
|
2016-07-25 21:56:38 +00:00
|
|
|
},
|
|
|
|
NodeAllocation: map[string][]*structs.Allocation{
|
2017-09-26 22:26:33 +00:00
|
|
|
"node-1": {
|
2016-07-25 21:56:38 +00:00
|
|
|
alloc2,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"node-2": {
|
2016-07-25 21:56:38 +00:00
|
|
|
alloc3, alloc4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RefreshIndex: 3,
|
2017-01-08 22:14:35 +00:00
|
|
|
AllocIndex: 16, // Should not be considered
|
2016-07-25 21:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
queuedAllocs := map[string]int{"web": 2}
|
|
|
|
adjustQueuedAllocations(logger, &planResult, queuedAllocs)
|
|
|
|
|
|
|
|
if queuedAllocs["web"] != 1 {
|
|
|
|
t.Fatalf("expected: %v, actual: %v", 1, queuedAllocs["web"])
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 20:11:58 +00:00
|
|
|
|
|
|
|
func TestUtil_UpdateNonTerminalAllocsToLost(t *testing.T) {
|
|
|
|
node := mock.Node()
|
2018-03-30 21:17:41 +00:00
|
|
|
node.Status = structs.NodeStatusDown
|
2016-08-09 20:11:58 +00:00
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.NodeID = node.ID
|
|
|
|
alloc1.DesiredStatus = structs.AllocDesiredStatusStop
|
|
|
|
|
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.NodeID = node.ID
|
|
|
|
alloc2.DesiredStatus = structs.AllocDesiredStatusStop
|
|
|
|
alloc2.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
|
|
|
|
alloc3 := mock.Alloc()
|
|
|
|
alloc3.NodeID = node.ID
|
|
|
|
alloc3.DesiredStatus = structs.AllocDesiredStatusStop
|
|
|
|
alloc3.ClientStatus = structs.AllocClientStatusComplete
|
|
|
|
|
|
|
|
alloc4 := mock.Alloc()
|
|
|
|
alloc4.NodeID = node.ID
|
|
|
|
alloc4.DesiredStatus = structs.AllocDesiredStatusStop
|
|
|
|
alloc4.ClientStatus = structs.AllocClientStatusFailed
|
|
|
|
|
|
|
|
allocs := []*structs.Allocation{alloc1, alloc2, alloc3, alloc4}
|
|
|
|
plan := structs.Plan{
|
|
|
|
NodeUpdate: make(map[string][]*structs.Allocation),
|
|
|
|
}
|
|
|
|
tainted := map[string]*structs.Node{node.ID: node}
|
|
|
|
|
|
|
|
updateNonTerminalAllocsToLost(&plan, tainted, allocs)
|
|
|
|
|
|
|
|
allocsLost := make([]string, 0, 2)
|
|
|
|
for _, alloc := range plan.NodeUpdate[node.ID] {
|
|
|
|
allocsLost = append(allocsLost, alloc.ID)
|
|
|
|
}
|
|
|
|
expected := []string{alloc1.ID, alloc2.ID}
|
|
|
|
if !reflect.DeepEqual(allocsLost, expected) {
|
|
|
|
t.Fatalf("actual: %v, expected: %v", allocsLost, expected)
|
|
|
|
}
|
2018-03-30 21:17:41 +00:00
|
|
|
|
|
|
|
// Update the node status to ready and try again
|
|
|
|
plan = structs.Plan{
|
|
|
|
NodeUpdate: make(map[string][]*structs.Allocation),
|
|
|
|
}
|
|
|
|
node.Status = structs.NodeStatusReady
|
|
|
|
updateNonTerminalAllocsToLost(&plan, tainted, allocs)
|
|
|
|
|
|
|
|
allocsLost = make([]string, 0, 2)
|
|
|
|
for _, alloc := range plan.NodeUpdate[node.ID] {
|
|
|
|
allocsLost = append(allocsLost, alloc.ID)
|
|
|
|
}
|
|
|
|
expected = []string{}
|
|
|
|
if !reflect.DeepEqual(allocsLost, expected) {
|
|
|
|
t.Fatalf("actual: %v, expected: %v", allocsLost, expected)
|
|
|
|
}
|
2016-08-09 20:11:58 +00:00
|
|
|
}
|