2015-09-08 22:37:07 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-09-17 19:40:51 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2015-09-08 22:37:07 +00:00
|
|
|
"testing"
|
2018-01-25 17:46:12 +00:00
|
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-08 22:37:07 +00:00
|
|
|
)
|
|
|
|
|
2015-09-09 00:49:31 +00:00
|
|
|
func TestAllocations_List(t *testing.T) {
|
2017-07-21 23:33:04 +00:00
|
|
|
t.Parallel()
|
2015-09-08 22:37:07 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
2015-09-09 00:49:31 +00:00
|
|
|
a := c.Allocations()
|
2015-09-08 22:37:07 +00:00
|
|
|
|
|
|
|
// Querying when no allocs exist returns nothing
|
2015-09-09 20:18:50 +00:00
|
|
|
allocs, qm, err := a.List(nil)
|
2015-09-08 22:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-09-08 23:45:16 +00:00
|
|
|
if qm.LastIndex != 0 {
|
|
|
|
t.Fatalf("bad index: %d", qm.LastIndex)
|
|
|
|
}
|
2015-09-08 22:37:07 +00:00
|
|
|
if n := len(allocs); n != 0 {
|
|
|
|
t.Fatalf("expected 0 allocs, got: %d", n)
|
|
|
|
}
|
2015-09-09 00:20:52 +00:00
|
|
|
|
|
|
|
// TODO: do something that causes an allocation to actually happen
|
|
|
|
// so we can query for them.
|
|
|
|
return
|
|
|
|
|
2017-02-28 00:00:19 +00:00
|
|
|
//job := &Job{
|
2019-01-18 18:28:35 +00:00
|
|
|
//ID: stringToPtr("job1"),
|
|
|
|
//Name: stringToPtr("Job #1"),
|
|
|
|
//Type: stringToPtr(JobTypeService),
|
2017-02-28 00:00:19 +00:00
|
|
|
//}
|
|
|
|
//eval, _, err := c.Jobs().Register(job, nil)
|
|
|
|
//if err != nil {
|
|
|
|
//t.Fatalf("err: %s", err)
|
|
|
|
//}
|
|
|
|
|
|
|
|
//// List the allocations again
|
|
|
|
//allocs, qm, err = a.List(nil)
|
|
|
|
//if err != nil {
|
|
|
|
//t.Fatalf("err: %s", err)
|
|
|
|
//}
|
|
|
|
//if qm.LastIndex == 0 {
|
|
|
|
//t.Fatalf("bad index: %d", qm.LastIndex)
|
|
|
|
//}
|
|
|
|
|
|
|
|
//// Check that we got the allocation back
|
|
|
|
//if len(allocs) == 0 || allocs[0].EvalID != eval {
|
|
|
|
//t.Fatalf("bad: %#v", allocs)
|
|
|
|
//}
|
2015-09-08 22:37:07 +00:00
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
func TestAllocations_PrefixList(t *testing.T) {
|
2017-07-21 23:33:04 +00:00
|
|
|
t.Parallel()
|
2015-12-24 10:46:59 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
a := c.Allocations()
|
|
|
|
|
|
|
|
// Querying when no allocs exist returns nothing
|
|
|
|
allocs, qm, err := a.PrefixList("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if qm.LastIndex != 0 {
|
|
|
|
t.Fatalf("bad index: %d", qm.LastIndex)
|
|
|
|
}
|
|
|
|
if n := len(allocs); n != 0 {
|
|
|
|
t.Fatalf("expected 0 allocs, got: %d", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do something that causes an allocation to actually happen
|
|
|
|
// so we can query for them.
|
|
|
|
return
|
|
|
|
|
2017-02-28 00:00:19 +00:00
|
|
|
//job := &Job{
|
2019-01-18 18:28:35 +00:00
|
|
|
//ID: stringToPtr("job1"),
|
|
|
|
//Name: stringToPtr("Job #1"),
|
|
|
|
//Type: stringToPtr(JobTypeService),
|
2017-02-28 00:00:19 +00:00
|
|
|
//}
|
|
|
|
|
|
|
|
//eval, _, err := c.Jobs().Register(job, nil)
|
|
|
|
//if err != nil {
|
|
|
|
//t.Fatalf("err: %s", err)
|
|
|
|
//}
|
|
|
|
|
|
|
|
//// List the allocations by prefix
|
|
|
|
//allocs, qm, err = a.PrefixList("foobar")
|
|
|
|
//if err != nil {
|
|
|
|
//t.Fatalf("err: %s", err)
|
|
|
|
//}
|
|
|
|
//if qm.LastIndex == 0 {
|
|
|
|
//t.Fatalf("bad index: %d", qm.LastIndex)
|
|
|
|
//}
|
|
|
|
|
|
|
|
//// Check that we got the allocation back
|
|
|
|
//if len(allocs) == 0 || allocs[0].EvalID != eval {
|
|
|
|
//t.Fatalf("bad: %#v", allocs)
|
|
|
|
//}
|
2015-12-24 10:46:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 19:40:51 +00:00
|
|
|
func TestAllocations_CreateIndexSort(t *testing.T) {
|
2017-07-21 23:33:04 +00:00
|
|
|
t.Parallel()
|
2015-09-17 19:40:51 +00:00
|
|
|
allocs := []*AllocationListStub{
|
2017-09-26 22:26:33 +00:00
|
|
|
{CreateIndex: 2},
|
|
|
|
{CreateIndex: 1},
|
|
|
|
{CreateIndex: 5},
|
2015-09-17 19:40:51 +00:00
|
|
|
}
|
|
|
|
sort.Sort(AllocIndexSort(allocs))
|
|
|
|
|
|
|
|
expect := []*AllocationListStub{
|
2017-09-26 22:26:33 +00:00
|
|
|
{CreateIndex: 5},
|
|
|
|
{CreateIndex: 2},
|
|
|
|
{CreateIndex: 1},
|
2015-09-17 19:40:51 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(allocs, expect) {
|
|
|
|
t.Fatalf("\n\n%#v\n\n%#v", allocs, expect)
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 17:46:12 +00:00
|
|
|
|
|
|
|
func TestAllocations_RescheduleInfo(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
// Create a job, task group and alloc
|
|
|
|
job := &Job{
|
2019-01-18 18:28:35 +00:00
|
|
|
Name: stringToPtr("foo"),
|
|
|
|
Namespace: stringToPtr(DefaultNamespace),
|
|
|
|
ID: stringToPtr("bar"),
|
|
|
|
ParentID: stringToPtr("lol"),
|
2018-01-25 17:46:12 +00:00
|
|
|
TaskGroups: []*TaskGroup{
|
|
|
|
{
|
2019-01-18 18:28:35 +00:00
|
|
|
Name: stringToPtr("bar"),
|
2018-01-25 17:46:12 +00:00
|
|
|
Tasks: []*Task{
|
|
|
|
{
|
|
|
|
Name: "task1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
job.Canonicalize()
|
|
|
|
|
|
|
|
alloc := &Allocation{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
Namespace: DefaultNamespace,
|
|
|
|
EvalID: uuid.Generate(),
|
|
|
|
Name: "foo-bar[1]",
|
|
|
|
NodeID: uuid.Generate(),
|
|
|
|
TaskGroup: *job.TaskGroups[0].Name,
|
|
|
|
JobID: *job.ID,
|
|
|
|
Job: job,
|
|
|
|
}
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
desc string
|
|
|
|
reschedulePolicy *ReschedulePolicy
|
|
|
|
rescheduleTracker *RescheduleTracker
|
|
|
|
time time.Time
|
|
|
|
expAttempted int
|
|
|
|
expTotal int
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []testCase{
|
|
|
|
{
|
|
|
|
desc: "no reschedule policy",
|
|
|
|
expAttempted: 0,
|
|
|
|
expTotal: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no reschedule events",
|
|
|
|
reschedulePolicy: &ReschedulePolicy{
|
2019-01-18 18:28:35 +00:00
|
|
|
Attempts: intToPtr(3),
|
|
|
|
Interval: timeToPtr(15 * time.Minute),
|
2018-01-25 17:46:12 +00:00
|
|
|
},
|
|
|
|
expAttempted: 0,
|
|
|
|
expTotal: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "all reschedule events within interval",
|
|
|
|
reschedulePolicy: &ReschedulePolicy{
|
2019-01-18 18:28:35 +00:00
|
|
|
Attempts: intToPtr(3),
|
|
|
|
Interval: timeToPtr(15 * time.Minute),
|
2018-01-25 17:46:12 +00:00
|
|
|
},
|
|
|
|
time: time.Now(),
|
|
|
|
rescheduleTracker: &RescheduleTracker{
|
|
|
|
Events: []*RescheduleEvent{
|
|
|
|
{
|
|
|
|
RescheduleTime: time.Now().Add(-5 * time.Minute).UTC().UnixNano(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expAttempted: 1,
|
|
|
|
expTotal: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "some reschedule events outside interval",
|
|
|
|
reschedulePolicy: &ReschedulePolicy{
|
2019-01-18 18:28:35 +00:00
|
|
|
Attempts: intToPtr(3),
|
|
|
|
Interval: timeToPtr(15 * time.Minute),
|
2018-01-25 17:46:12 +00:00
|
|
|
},
|
|
|
|
time: time.Now(),
|
|
|
|
rescheduleTracker: &RescheduleTracker{
|
|
|
|
Events: []*RescheduleEvent{
|
|
|
|
{
|
|
|
|
RescheduleTime: time.Now().Add(-45 * time.Minute).UTC().UnixNano(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RescheduleTime: time.Now().Add(-30 * time.Minute).UTC().UnixNano(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RescheduleTime: time.Now().Add(-10 * time.Minute).UTC().UnixNano(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
RescheduleTime: time.Now().Add(-5 * time.Minute).UTC().UnixNano(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expAttempted: 2,
|
|
|
|
expTotal: 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
alloc.RescheduleTracker = tc.rescheduleTracker
|
|
|
|
job.TaskGroups[0].ReschedulePolicy = tc.reschedulePolicy
|
|
|
|
attempted, total := alloc.RescheduleInfo(tc.time)
|
|
|
|
require.Equal(tc.expAttempted, attempted)
|
|
|
|
require.Equal(tc.expTotal, total)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-03-07 00:23:21 +00:00
|
|
|
|
|
|
|
func TestAllocations_ShouldMigrate(t *testing.T) {
|
|
|
|
t.Parallel()
|
2019-01-18 18:28:35 +00:00
|
|
|
require.True(t, DesiredTransition{Migrate: boolToPtr(true)}.ShouldMigrate())
|
2018-03-07 00:23:21 +00:00
|
|
|
require.False(t, DesiredTransition{}.ShouldMigrate())
|
2019-01-18 18:28:35 +00:00
|
|
|
require.False(t, DesiredTransition{Migrate: boolToPtr(false)}.ShouldMigrate())
|
2018-03-07 00:23:21 +00:00
|
|
|
}
|