2015-09-09 20:48:56 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2015-09-17 19:40:51 +00:00
|
|
|
"sort"
|
2015-09-09 20:48:56 +00:00
|
|
|
"testing"
|
2021-12-10 18:43:03 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2022-03-01 20:36:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-09 20:48:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEvaluations_List(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
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)
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(0), qm.LastIndex, "bad index")
|
|
|
|
require.Equal(t, 0, len(result), "expected 0 evaluations")
|
2015-09-09 20:48:56 +00:00
|
|
|
|
|
|
|
// 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)
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
2015-09-09 20:48:56 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Check the evaluations again
|
|
|
|
result, qm, err = e.List(nil)
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
2015-09-09 20:48:56 +00:00
|
|
|
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
|
2022-03-01 20:36:49 +00:00
|
|
|
require.Greater(t, len(result), 0, "expected eval (%s), got none", resp.EvalID)
|
2016-08-13 02:48:49 +00:00
|
|
|
idx := len(result) - 1
|
2022-03-01 20:36:49 +00:00
|
|
|
require.Equal(t, resp.EvalID, result[idx].ID, "expected eval (%s), got: %#v", resp.EvalID, result[idx])
|
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)
|
|
|
|
})
|
|
|
|
|
2022-03-01 20:36:49 +00:00
|
|
|
// query first page
|
|
|
|
result, qm, err = e.List(&QueryOptions{
|
|
|
|
PerPage: int32(1),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(result), "expected no evals after last one but got %d: %#v", len(result), result)
|
|
|
|
|
|
|
|
// query second page
|
|
|
|
result, qm, err = e.List(&QueryOptions{
|
|
|
|
PerPage: int32(1),
|
|
|
|
NextToken: qm.NextToken,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(result), "expected no evals after last one but got %d: %#v", len(result), result)
|
2022-02-16 16:40:30 +00:00
|
|
|
|
|
|
|
// Query evaluations using a filter.
|
|
|
|
results, _, err = e.List(&QueryOptions{
|
|
|
|
Filter: `TriggeredBy == "job-register"`,
|
|
|
|
})
|
2022-03-01 20:36:49 +00:00
|
|
|
require.Equal(t, 1, len(result), "expected 1 eval, got %d", len(result))
|
2015-09-09 20:48:56 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
func TestEvaluations_PrefixList(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
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")
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(0), qm.LastIndex, "bad index")
|
|
|
|
require.Equal(t, 0, len(result), "expected 0 evaluations")
|
2015-12-24 10:46:59 +00:00
|
|
|
|
|
|
|
// 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)
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-24 10:46:59 +00:00
|
|
|
assertWriteMeta(t, wm)
|
|
|
|
|
|
|
|
// Check the evaluations again
|
2017-05-10 03:52:47 +00:00
|
|
|
result, qm, err = e.PrefixList(resp.EvalID[:4])
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
2015-12-24 10:46:59 +00:00
|
|
|
assertQueryMeta(t, qm)
|
|
|
|
|
|
|
|
// Check if we have the right list
|
2022-03-01 20:36:49 +00:00
|
|
|
require.Equal(t, 1, len(result))
|
|
|
|
require.Equal(t, resp.EvalID, result[0].ID)
|
2015-12-24 10:46:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 20:48:56 +00:00
|
|
|
func TestEvaluations_Info(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
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)
|
2022-03-01 20:36:49 +00:00
|
|
|
require.Error(t, err)
|
2015-09-09 20:48:56 +00:00
|
|
|
|
|
|
|
// 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)
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
2015-09-09 20:48:56 +00:00
|
|
|
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)
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
2015-09-09 20:48:56 +00:00
|
|
|
assertQueryMeta(t, qm)
|
|
|
|
|
|
|
|
// Check that we got the right result
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NotNil(t, result)
|
|
|
|
require.Equal(t, resp.EvalID, result.ID)
|
2022-03-17 17:56:14 +00:00
|
|
|
|
|
|
|
// Register the job again to get a related eval
|
|
|
|
resp, wm, err = jobs.Register(job, nil)
|
|
|
|
evals, _, err := e.List(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Find an eval that should have related evals
|
|
|
|
for _, eval := range evals {
|
|
|
|
if eval.NextEval != "" || eval.PreviousEval != "" || eval.BlockedEval != "" {
|
|
|
|
result, qm, err := e.Info(eval.ID, &QueryOptions{
|
|
|
|
Params: map[string]string{
|
|
|
|
"related": "true",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertQueryMeta(t, qm)
|
|
|
|
require.NotNil(t, result.RelatedEvals)
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 20:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvaluations_Allocations(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
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)
|
2022-03-01 20:36:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(0), qm.LastIndex, "bad index")
|
|
|
|
require.Equal(t, 0, len(allocs), "expected 0 evaluations")
|
2015-09-09 20:48:56 +00:00
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
|
|
|
|
func TestEvaluations_Sort(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
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
|
|
|
}
|
2022-03-01 20:36:49 +00:00
|
|
|
require.Equal(t, expect, evals)
|
2015-09-17 19:40:51 +00:00
|
|
|
}
|