open-nomad/api/nodes_test.go

378 lines
8.7 KiB
Go
Raw Normal View History

2015-09-11 23:51:18 +00:00
package api
import (
"fmt"
2015-09-17 19:40:51 +00:00
"reflect"
"sort"
2015-09-11 23:51:18 +00:00
"strings"
"testing"
2016-07-27 20:44:30 +00:00
"time"
2015-09-11 23:51:18 +00:00
"github.com/hashicorp/nomad/helper/uuid"
2018-02-14 18:30:26 +00:00
"github.com/hashicorp/nomad/nomad/structs"
2015-09-11 23:51:18 +00:00
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
2015-09-11 23:51:18 +00:00
)
func TestNodes_List(t *testing.T) {
2017-07-21 23:33:04 +00:00
t.Parallel()
2015-09-12 04:28:21 +00:00
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.DevMode = true
})
2015-09-11 23:51:18 +00:00
defer s.Stop()
nodes := c.Nodes()
var qm *QueryMeta
2015-09-14 02:55:47 +00:00
var out []*NodeListStub
2015-09-11 23:51:18 +00:00
var err error
testutil.WaitForResult(func() (bool, error) {
out, qm, err = nodes.List(nil)
if err != nil {
return false, err
}
if n := len(out); n != 1 {
return false, fmt.Errorf("expected 1 node, got: %d", n)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
// Check that we got valid QueryMeta.
assertQueryMeta(t, qm)
}
func TestNodes_PrefixList(t *testing.T) {
2017-07-21 23:33:04 +00:00
t.Parallel()
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.DevMode = true
})
defer s.Stop()
nodes := c.Nodes()
var qm *QueryMeta
var out []*NodeListStub
var err error
// Get the node ID
2016-05-27 10:38:07 +00:00
var nodeID string
testutil.WaitForResult(func() (bool, error) {
out, _, err := nodes.List(nil)
if err != nil {
return false, err
}
if n := len(out); n != 1 {
return false, fmt.Errorf("expected 1 node, got: %d", n)
}
nodeID = out[0].ID
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
// Find node based on four character prefix
out, qm, err = nodes.PrefixList(nodeID[:4])
if err != nil {
t.Fatalf("err: %s", err)
}
if n := len(out); n != 1 {
t.Fatalf("expected 1 node, got: %d ", n)
}
// Check that we got valid QueryMeta.
assertQueryMeta(t, qm)
}
2015-09-11 23:51:18 +00:00
func TestNodes_Info(t *testing.T) {
2017-07-21 23:33:04 +00:00
t.Parallel()
2016-07-27 20:44:30 +00:00
startTime := time.Now().Unix()
2015-09-12 04:28:21 +00:00
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.DevMode = true
})
2015-09-11 23:51:18 +00:00
defer s.Stop()
nodes := c.Nodes()
// Retrieving a nonexistent node returns error
_, _, err := nodes.Info("12345678-abcd-efab-cdef-123456789abc", nil)
2015-09-11 23:51:18 +00:00
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("expected not found error, got: %#v", err)
}
// Get the node ID
var nodeID, dc string
testutil.WaitForResult(func() (bool, error) {
out, _, err := nodes.List(nil)
if err != nil {
return false, err
}
if n := len(out); n != 1 {
return false, fmt.Errorf("expected 1 node, got: %d", n)
}
nodeID = out[0].ID
dc = out[0].Datacenter
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
// Querying for existing nodes returns properly
result, qm, err := nodes.Info(nodeID, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assertQueryMeta(t, qm)
// Check that the result is what we expect
if result.ID != nodeID || result.Datacenter != dc {
t.Fatalf("expected %s (%s), got: %s (%s)",
nodeID, dc,
result.ID, result.Datacenter)
}
2016-07-27 20:44:30 +00:00
// Check that the StatusUpdatedAt field is being populated correctly
if result.StatusUpdatedAt < startTime {
t.Fatalf("start time: %v, status updated: %v", startTime, result.StatusUpdatedAt)
}
2018-03-14 00:59:37 +00:00
if len(result.Events) < 1 {
t.Fatalf("Expected at minimum the node register event to be populated: %+v", result)
}
2015-09-11 23:51:18 +00:00
}
func TestNodes_ToggleDrain(t *testing.T) {
2017-07-21 23:33:04 +00:00
t.Parallel()
require := require.New(t)
2015-09-12 04:28:21 +00:00
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.DevMode = true
})
2015-09-11 23:51:18 +00:00
defer s.Stop()
nodes := c.Nodes()
// Wait for node registration and get the ID
var nodeID string
testutil.WaitForResult(func() (bool, error) {
out, _, err := nodes.List(nil)
if err != nil {
return false, err
}
if n := len(out); n != 1 {
return false, fmt.Errorf("expected 1 node, got: %d", n)
}
nodeID = out[0].ID
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
// Check for drain mode
out, _, err := nodes.Info(nodeID, nil)
require.Nil(err)
2015-09-11 23:51:18 +00:00
if out.Drain {
t.Fatalf("drain mode should be off")
}
// Toggle it on
2018-02-23 23:56:36 +00:00
spec := &DrainSpec{
Deadline: 10 * time.Second,
}
wm, err := nodes.UpdateDrain(nodeID, spec, false, nil)
require.Nil(err)
2015-09-11 23:51:18 +00:00
assertWriteMeta(t, wm)
// Check again
out, _, err = nodes.Info(nodeID, nil)
require.Nil(err)
2018-02-23 23:56:36 +00:00
if out.SchedulingEligibility != structs.NodeSchedulingIneligible {
t.Fatalf("bad eligibility: %v vs %v", out.SchedulingEligibility, structs.NodeSchedulingIneligible)
2015-09-11 23:51:18 +00:00
}
// Toggle off again
wm, err = nodes.UpdateDrain(nodeID, nil, true, nil)
require.Nil(err)
2015-09-11 23:51:18 +00:00
assertWriteMeta(t, wm)
// Check again
out, _, err = nodes.Info(nodeID, nil)
require.Nil(err)
2015-09-11 23:51:18 +00:00
if out.Drain {
t.Fatalf("drain mode should be off")
}
2018-02-23 23:56:36 +00:00
if out.DrainStrategy != nil {
t.Fatalf("drain strategy should be unset")
}
if out.SchedulingEligibility != structs.NodeSchedulingEligible {
t.Fatalf("should be eligible")
}
2015-09-11 23:51:18 +00:00
}
2018-02-27 20:59:27 +00:00
func TestNodes_ToggleEligibility(t *testing.T) {
t.Parallel()
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.DevMode = true
})
defer s.Stop()
nodes := c.Nodes()
// Wait for node registration and get the ID
var nodeID string
testutil.WaitForResult(func() (bool, error) {
out, _, err := nodes.List(nil)
if err != nil {
return false, err
}
if n := len(out); n != 1 {
return false, fmt.Errorf("expected 1 node, got: %d", n)
}
nodeID = out[0].ID
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
// Check for eligibility
out, _, err := nodes.Info(nodeID, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if out.SchedulingEligibility != structs.NodeSchedulingEligible {
t.Fatalf("node should be eligible")
}
// Toggle it off
wm, err := nodes.ToggleEligibility(nodeID, false, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assertWriteMeta(t, wm)
// Check again
out, _, err = nodes.Info(nodeID, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if out.SchedulingEligibility != structs.NodeSchedulingIneligible {
t.Fatalf("bad eligibility: %v vs %v", out.SchedulingEligibility, structs.NodeSchedulingIneligible)
}
// Toggle on
wm, err = nodes.ToggleEligibility(nodeID, true, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assertWriteMeta(t, wm)
// Check again
out, _, err = nodes.Info(nodeID, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if out.SchedulingEligibility != structs.NodeSchedulingEligible {
t.Fatalf("bad eligibility: %v vs %v", out.SchedulingEligibility, structs.NodeSchedulingEligible)
}
2018-02-23 23:56:36 +00:00
if out.DrainStrategy != nil {
t.Fatalf("drain strategy should be unset")
}
2018-02-27 20:59:27 +00:00
}
2015-09-11 23:51:18 +00:00
func TestNodes_Allocations(t *testing.T) {
2017-07-21 23:33:04 +00:00
t.Parallel()
2015-09-11 23:51:18 +00:00
c, s := makeClient(t, nil, nil)
defer s.Stop()
nodes := c.Nodes()
// Looking up by a nonexistent node returns nothing. We
2015-09-11 23:51:18 +00:00
// don't check the index here because it's possible the node
// has already registered, in which case we will get a non-
// zero result anyways.
allocs, _, err := nodes.Allocations("nope", nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if n := len(allocs); n != 0 {
t.Fatalf("expected 0 allocs, got: %d", n)
}
}
func TestNodes_ForceEvaluate(t *testing.T) {
2017-07-21 23:33:04 +00:00
t.Parallel()
2015-09-12 04:28:21 +00:00
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.DevMode = true
})
2015-09-11 23:51:18 +00:00
defer s.Stop()
nodes := c.Nodes()
// Force-eval on a nonexistent node fails
_, _, err := nodes.ForceEvaluate("12345678-abcd-efab-cdef-123456789abc", nil)
2015-09-11 23:51:18 +00:00
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("expected not found error, got: %#v", err)
}
// Wait for node registration and get the ID
var nodeID string
testutil.WaitForResult(func() (bool, error) {
out, _, err := nodes.List(nil)
if err != nil {
return false, err
}
if n := len(out); n != 1 {
return false, fmt.Errorf("expected 1 node, got: %d", n)
}
nodeID = out[0].ID
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
// Try force-eval again. We don't check the WriteMeta because
// there are no allocations to process, so we would get an index
// of zero. Same goes for the eval ID.
_, _, err = nodes.ForceEvaluate(nodeID, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
}
2015-09-17 19:40:51 +00:00
func TestNodes_Sort(t *testing.T) {
2017-07-21 23:33:04 +00:00
t.Parallel()
2015-09-17 19:40:51 +00:00
nodes := []*NodeListStub{
2017-09-26 22:26:33 +00:00
{CreateIndex: 2},
{CreateIndex: 1},
{CreateIndex: 5},
2015-09-17 19:40:51 +00:00
}
sort.Sort(NodeIndexSort(nodes))
expect := []*NodeListStub{
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(nodes, expect) {
t.Fatalf("\n\n%#v\n\n%#v", nodes, expect)
}
}
func TestNodes_GC(t *testing.T) {
t.Parallel()
require := require.New(t)
c, s := makeClient(t, nil, nil)
defer s.Stop()
nodes := c.Nodes()
err := nodes.GC(uuid.Generate(), nil)
require.NotNil(err)
2018-02-14 18:30:26 +00:00
require.True(structs.IsErrUnknownNode(err))
}
func TestNodes_GcAlloc(t *testing.T) {
t.Parallel()
require := require.New(t)
c, s := makeClient(t, nil, nil)
defer s.Stop()
nodes := c.Nodes()
err := nodes.GcAlloc(uuid.Generate(), nil)
require.NotNil(err)
2018-02-14 18:30:26 +00:00
require.True(structs.IsErrUnknownAllocation(err))
}