2015-09-09 20:48:56 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-09-17 19:40:51 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2015-09-09 20:48:56 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-12-10 18:43:03 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2015-09-09 20:48:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEvaluations_List(t *testing.T) {
|
2017-07-21 23:33:04 +00:00
|
|
|
t.Parallel()
|
2015-09-09 20:48:56 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
e := c.Evaluations()
|
|
|
|
|
|
|
|
// Listing when nothing exists returns empty
|
|
|
|
result, qm, err := e.List(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if qm.LastIndex != 0 {
|
|
|
|
t.Fatalf("bad index: %d", qm.LastIndex)
|
|
|
|
}
|
|
|
|
if n := len(result); n != 0 {
|
|
|
|
t.Fatalf("expected 0 evaluations, got: %d", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a job. This will create an evaluation.
|
|
|
|
jobs := c.Jobs()
|
|
|
|
job := testJob()
|
2017-05-10 03:52:47 +00:00
|
|
|
resp, wm, err := jobs.Register(job, nil)
|
2015-09-09 20:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Check the evaluations again
|
|
|
|
result, qm, err = e.List(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
assertQueryMeta(t, qm)
|
|
|
|
|
2016-08-13 02:48:49 +00:00
|
|
|
// if the eval fails fast there can be more than 1
|
|
|
|
// but they are in order of most recent first, so look at the last one
|
2021-12-10 18:43:03 +00:00
|
|
|
if len(result) == 0 {
|
|
|
|
t.Fatalf("expected eval (%s), got none", resp.EvalID)
|
|
|
|
}
|
2016-08-13 02:48:49 +00:00
|
|
|
idx := len(result) - 1
|
2021-12-10 18:43:03 +00:00
|
|
|
if result[idx].ID != resp.EvalID {
|
2017-05-10 03:52:47 +00:00
|
|
|
t.Fatalf("expected eval (%s), got: %#v", resp.EvalID, result[idx])
|
2015-09-09 20:48:56 +00:00
|
|
|
}
|
2021-12-10 18:43:03 +00:00
|
|
|
|
|
|
|
// wait until the 2nd eval shows up before we try paging
|
|
|
|
results := []*Evaluation{}
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
results, _, err = e.List(nil)
|
|
|
|
if len(results) < 2 || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Check the evaluations again with paging; note that while this
|
|
|
|
// package sorts by timestamp, the actual HTTP API sorts by ID
|
|
|
|
// so we need to use that for the NextToken
|
|
|
|
ids := []string{results[0].ID, results[1].ID}
|
|
|
|
sort.Strings(ids)
|
|
|
|
result, qm, err = e.List(&QueryOptions{PerPage: int32(1), NextToken: ids[1]})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(result) != 1 {
|
|
|
|
t.Fatalf("expected no evals after last one but got %v", result[0])
|
|
|
|
}
|
2015-09-09 20:48:56 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
func TestEvaluations_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()
|
|
|
|
e := c.Evaluations()
|
|
|
|
|
|
|
|
// Listing when nothing exists returns empty
|
|
|
|
result, qm, err := e.PrefixList("abcdef")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if qm.LastIndex != 0 {
|
|
|
|
t.Fatalf("bad index: %d", qm.LastIndex)
|
|
|
|
}
|
|
|
|
if n := len(result); n != 0 {
|
|
|
|
t.Fatalf("expected 0 evaluations, got: %d", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a job. This will create an evaluation.
|
|
|
|
jobs := c.Jobs()
|
|
|
|
job := testJob()
|
2017-05-10 03:52:47 +00:00
|
|
|
resp, wm, err := jobs.Register(job, nil)
|
2015-12-24 10:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Check the evaluations again
|
2017-05-10 03:52:47 +00:00
|
|
|
result, qm, err = e.PrefixList(resp.EvalID[:4])
|
2015-12-24 10:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
assertQueryMeta(t, qm)
|
|
|
|
|
|
|
|
// Check if we have the right list
|
2017-05-10 03:52:47 +00:00
|
|
|
if len(result) != 1 || result[0].ID != resp.EvalID {
|
2015-12-24 10:46:59 +00:00
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-09 20:48:56 +00:00
|
|
|
func TestEvaluations_Info(t *testing.T) {
|
2017-07-21 23:33:04 +00:00
|
|
|
t.Parallel()
|
2015-09-09 20:48:56 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
e := c.Evaluations()
|
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Querying a nonexistent evaluation returns error
|
2015-09-09 20:48:56 +00:00
|
|
|
_, _, err := e.Info("8E231CF4-CA48-43FF-B694-5801E69E22FA", nil)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "not found") {
|
|
|
|
t.Fatalf("expected not found error, got: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a job. Creates a new evaluation.
|
|
|
|
jobs := c.Jobs()
|
|
|
|
job := testJob()
|
2017-05-10 03:52:47 +00:00
|
|
|
resp, wm, err := jobs.Register(job, nil)
|
2015-09-09 20:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Try looking up by the new eval ID
|
2017-05-10 03:52:47 +00:00
|
|
|
result, qm, err := e.Info(resp.EvalID, nil)
|
2015-09-09 20:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
assertQueryMeta(t, qm)
|
|
|
|
|
|
|
|
// Check that we got the right result
|
2017-05-10 03:52:47 +00:00
|
|
|
if result == nil || result.ID != resp.EvalID {
|
|
|
|
t.Fatalf("expected eval %q, got: %#v", resp.EvalID, result)
|
2015-09-09 20:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvaluations_Allocations(t *testing.T) {
|
2017-07-21 23:33:04 +00:00
|
|
|
t.Parallel()
|
2015-09-09 20:48:56 +00:00
|
|
|
c, s := makeClient(t, nil, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
e := c.Evaluations()
|
|
|
|
|
|
|
|
// Returns empty if no allocations
|
|
|
|
allocs, qm, err := e.Allocations("8E231CF4-CA48-43FF-B694-5801E69E22FA", nil)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
|
|
|
|
func TestEvaluations_Sort(t *testing.T) {
|
2017-07-21 23:33:04 +00:00
|
|
|
t.Parallel()
|
2015-09-17 19:40:51 +00:00
|
|
|
evals := []*Evaluation{
|
2017-09-26 22:26:33 +00:00
|
|
|
{CreateIndex: 2},
|
|
|
|
{CreateIndex: 1},
|
|
|
|
{CreateIndex: 5},
|
2015-09-17 19:40:51 +00:00
|
|
|
}
|
|
|
|
sort.Sort(EvalIndexSort(evals))
|
|
|
|
|
|
|
|
expect := []*Evaluation{
|
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(evals, expect) {
|
|
|
|
t.Fatalf("\n\n%#v\n\n%#v", evals, expect)
|
|
|
|
}
|
|
|
|
}
|