2015-09-11 23:51:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-03-28 21:01:54 +00:00
|
|
|
"context"
|
2015-09-11 23:51:18 +00:00
|
|
|
"fmt"
|
2015-09-17 19:40:51 +00:00
|
|
|
"sort"
|
2015-09-11 23:51:18 +00:00
|
|
|
"testing"
|
2016-07-27 20:44:30 +00:00
|
|
|
"time"
|
2015-09-11 23:51:18 +00:00
|
|
|
|
2019-03-29 18:47:40 +00:00
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2022-12-16 18:25:28 +00:00
|
|
|
"github.com/shoenig/test/must"
|
|
|
|
"github.com/shoenig/test/wait"
|
2015-09-11 23:51:18 +00:00
|
|
|
)
|
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
func queryNodeList(t *testing.T, nodes *Nodes) ([]*NodeListStub, *QueryMeta) {
|
|
|
|
var (
|
|
|
|
nodeListStub []*NodeListStub
|
|
|
|
queryMeta *QueryMeta
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
f := func() error {
|
|
|
|
nodeListStub, queryMeta, err = nodes.List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to list nodes: %w", err)
|
|
|
|
}
|
2022-12-22 14:21:00 +00:00
|
|
|
if len(nodeListStub) == 0 {
|
|
|
|
return fmt.Errorf("no nodes yet")
|
|
|
|
}
|
2022-12-16 18:25:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 13:52:10 +00:00
|
|
|
must.Wait(t, wait.InitialSuccess(
|
2022-12-16 18:25:28 +00:00
|
|
|
wait.ErrorFunc(f),
|
|
|
|
wait.Timeout(10*time.Second),
|
|
|
|
wait.Gap(1*time.Second),
|
|
|
|
))
|
|
|
|
|
|
|
|
return nodeListStub, queryMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
func oneNodeFromNodeList(t *testing.T, nodes *Nodes) *NodeListStub {
|
|
|
|
nodeListStub, _ := queryNodeList(t, nodes)
|
|
|
|
must.Len(t, 1, nodeListStub, must.Sprint("expected 1 node"))
|
|
|
|
return nodeListStub[0]
|
|
|
|
}
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
func TestNodes_List(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
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()
|
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
nodeListStub, queryMeta := queryNodeList(t, nodes)
|
|
|
|
must.Len(t, 1, nodeListStub)
|
2015-09-11 23:51:18 +00:00
|
|
|
|
|
|
|
// Check that we got valid QueryMeta.
|
2022-12-16 18:25:28 +00:00
|
|
|
assertQueryMeta(t, queryMeta)
|
2015-09-11 23:51:18 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
func TestNodes_PrefixList(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2015-12-24 10:46:59 +00:00
|
|
|
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.DevMode = true
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
nodes := c.Nodes()
|
|
|
|
|
|
|
|
// Get the node ID
|
2022-12-16 18:25:28 +00:00
|
|
|
nodeID := oneNodeFromNodeList(t, nodes).ID
|
2015-12-24 10:46:59 +00:00
|
|
|
|
|
|
|
// Find node based on four character prefix
|
2022-12-16 18:25:28 +00:00
|
|
|
out, qm, err := nodes.PrefixList(nodeID[:4])
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.Len(t, 1, out, must.Sprint("expected only 1 node"))
|
2015-12-24 10:46:59 +00:00
|
|
|
|
|
|
|
// Check that we got valid QueryMeta.
|
|
|
|
assertQueryMeta(t, qm)
|
|
|
|
}
|
|
|
|
|
2020-10-09 05:21:41 +00:00
|
|
|
// TestNodes_List_Resources asserts that ?resources=true includes allocated and
|
|
|
|
// reserved resources in the response.
|
|
|
|
func TestNodes_List_Resources(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2020-10-09 05:21:41 +00:00
|
|
|
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.DevMode = true
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
nodes := c.Nodes()
|
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
node := oneNodeFromNodeList(t, nodes)
|
2020-10-09 05:21:41 +00:00
|
|
|
|
|
|
|
// By default resources should *not* be included
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Nil(t, node.NodeResources)
|
|
|
|
must.Nil(t, node.ReservedResources)
|
2020-10-09 05:21:41 +00:00
|
|
|
|
|
|
|
qo := &QueryOptions{
|
|
|
|
Params: map[string]string{"resources": "true"},
|
|
|
|
}
|
2022-12-16 18:25:28 +00:00
|
|
|
|
|
|
|
out, _, err := nodes.List(qo)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, out[0].NodeResources)
|
|
|
|
must.NotNil(t, out[0].ReservedResources)
|
2020-10-09 05:21:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
func TestNodes_Info(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
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()
|
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Retrieving a nonexistent node returns error
|
2022-12-16 18:25:28 +00:00
|
|
|
_, _, infoErr := nodes.Info("12345678-abcd-efab-cdef-123456789abc", nil)
|
|
|
|
must.ErrorContains(t, infoErr, "not found")
|
2015-09-11 23:51:18 +00:00
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
// Get the node ID and DC
|
|
|
|
node := oneNodeFromNodeList(t, nodes)
|
|
|
|
nodeID, dc := node.ID, node.Datacenter
|
2015-09-11 23:51:18 +00:00
|
|
|
|
|
|
|
// Querying for existing nodes returns properly
|
|
|
|
result, qm, err := nodes.Info(nodeID, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
assertQueryMeta(t, qm)
|
|
|
|
|
|
|
|
// Check that the result is what we expect
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, nodeID, result.ID)
|
|
|
|
must.Eq(t, dc, result.Datacenter)
|
2016-07-27 20:44:30 +00:00
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, 20000, result.NodeResources.MinDynamicPort)
|
|
|
|
must.Eq(t, 32000, result.NodeResources.MaxDynamicPort)
|
2021-10-01 00:05:10 +00:00
|
|
|
|
2016-07-27 20:44:30 +00:00
|
|
|
// Check that the StatusUpdatedAt field is being populated correctly
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Less(t, result.StatusUpdatedAt, startTime)
|
2018-03-08 14:34:08 +00:00
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
// check we have at least one event
|
|
|
|
must.GreaterEq(t, 1, len(result.Events))
|
2015-09-11 23:51:18 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 17:55:34 +00:00
|
|
|
func TestNodes_NoSecretID(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2021-03-23 17:55:34 +00:00
|
|
|
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.DevMode = true
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
nodes := c.Nodes()
|
|
|
|
|
|
|
|
// Get the node ID
|
2022-12-16 18:25:28 +00:00
|
|
|
nodeID := oneNodeFromNodeList(t, nodes).ID
|
2021-03-23 17:55:34 +00:00
|
|
|
|
2021-03-26 17:03:15 +00:00
|
|
|
// perform a raw http call and make sure that:
|
|
|
|
// - "ID" to make sure that raw decoding is working correctly
|
|
|
|
// - "SecretID" to make sure it's not present
|
2021-03-23 17:55:34 +00:00
|
|
|
resp := make(map[string]interface{})
|
|
|
|
_, err := c.query("/v1/node/"+nodeID, &resp, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, nodeID, resp["ID"].(string))
|
|
|
|
must.Eq(t, "", resp["SecretID"])
|
2021-03-23 17:55:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
func TestNodes_ToggleDrain(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
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
|
2022-12-16 18:25:28 +00:00
|
|
|
nodeID := oneNodeFromNodeList(t, nodes).ID
|
2015-09-11 23:51:18 +00:00
|
|
|
|
|
|
|
// Check for drain mode
|
|
|
|
out, _, err := nodes.Info(nodeID, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.False(t, out.Drain)
|
|
|
|
must.Nil(t, out.LastDrain)
|
2015-09-11 23:51:18 +00:00
|
|
|
|
|
|
|
// Toggle it on
|
2021-05-07 17:58:40 +00:00
|
|
|
timeBeforeDrain := time.Now().Add(-1 * time.Second)
|
2018-02-23 23:56:36 +00:00
|
|
|
spec := &DrainSpec{
|
|
|
|
Deadline: 10 * time.Second,
|
|
|
|
}
|
2021-05-07 17:58:40 +00:00
|
|
|
drainMeta := map[string]string{
|
|
|
|
"reason": "this node needs to go",
|
|
|
|
}
|
|
|
|
drainOut, err := nodes.UpdateDrainOpts(nodeID, &DrainOptions{
|
|
|
|
DrainSpec: spec,
|
|
|
|
MarkEligible: false,
|
|
|
|
Meta: drainMeta,
|
|
|
|
}, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2018-03-27 22:53:24 +00:00
|
|
|
assertWriteMeta(t, &drainOut.WriteMeta)
|
2015-09-11 23:51:18 +00:00
|
|
|
|
2021-03-20 10:30:31 +00:00
|
|
|
// Drain may have completed before we can check, use event stream
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
streamCh, err := c.EventStream().Stream(ctx, map[Topic][]string{
|
|
|
|
TopicNode: {nodeID},
|
|
|
|
}, 0, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2021-03-20 10:30:31 +00:00
|
|
|
|
|
|
|
// we expect to see the node change to Drain:true and then back to Drain:false+ineligible
|
|
|
|
var sawDraining, sawDrainComplete uint64
|
|
|
|
for sawDrainComplete == 0 {
|
|
|
|
select {
|
|
|
|
case events := <-streamCh:
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, events.Err)
|
2021-03-20 10:30:31 +00:00
|
|
|
for _, e := range events.Events {
|
|
|
|
node, err := e.Node()
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, node.DrainStrategy != nil, node.Drain)
|
|
|
|
must.True(t, !node.Drain || node.SchedulingEligibility == NodeSchedulingIneligible) // node.Drain => "ineligible"
|
2021-03-20 10:30:31 +00:00
|
|
|
if node.Drain && node.SchedulingEligibility == NodeSchedulingIneligible {
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NotNil(t, node.LastDrain)
|
|
|
|
must.Eq(t, DrainStatusDraining, node.LastDrain.Status)
|
2021-05-07 17:58:40 +00:00
|
|
|
now := time.Now()
|
2022-12-16 18:25:28 +00:00
|
|
|
must.False(t, node.LastDrain.StartedAt.Before(timeBeforeDrain))
|
|
|
|
must.False(t, node.LastDrain.StartedAt.After(now))
|
|
|
|
must.Eq(t, drainMeta, node.LastDrain.Meta)
|
2021-03-20 10:30:31 +00:00
|
|
|
sawDraining = node.ModifyIndex
|
2021-05-07 17:58:40 +00:00
|
|
|
} else if sawDraining != 0 && !node.Drain && node.SchedulingEligibility == NodeSchedulingIneligible {
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NotNil(t, node.LastDrain)
|
|
|
|
must.Eq(t, DrainStatusComplete, node.LastDrain.Status)
|
|
|
|
must.True(t, !node.LastDrain.UpdatedAt.Before(node.LastDrain.StartedAt))
|
|
|
|
must.Eq(t, drainMeta, node.LastDrain.Meta)
|
2021-03-20 10:30:31 +00:00
|
|
|
sawDrainComplete = node.ModifyIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Unreachable(t, must.Sprint("waiting on stream event that never happened"))
|
2021-03-20 10:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-11 23:51:18 +00:00
|
|
|
|
|
|
|
// Toggle off again
|
2018-03-27 22:53:24 +00:00
|
|
|
drainOut, err = nodes.UpdateDrain(nodeID, nil, true, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2018-03-27 22:53:24 +00:00
|
|
|
assertWriteMeta(t, &drainOut.WriteMeta)
|
2015-09-11 23:51:18 +00:00
|
|
|
|
|
|
|
// Check again
|
|
|
|
out, _, err = nodes.Info(nodeID, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.False(t, out.Drain)
|
|
|
|
must.Nil(t, out.DrainStrategy)
|
|
|
|
must.Eq(t, NodeSchedulingEligible, out.SchedulingEligibility)
|
2015-09-11 23:51:18 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 20:59:27 +00:00
|
|
|
func TestNodes_ToggleEligibility(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2018-02-27 20:59:27 +00:00
|
|
|
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.DevMode = true
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
nodes := c.Nodes()
|
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
// Get node ID
|
|
|
|
nodeID := oneNodeFromNodeList(t, nodes).ID
|
2018-02-27 20:59:27 +00:00
|
|
|
|
|
|
|
// Check for eligibility
|
|
|
|
out, _, err := nodes.Info(nodeID, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, NodeSchedulingEligible, out.SchedulingEligibility)
|
2018-02-27 20:59:27 +00:00
|
|
|
|
|
|
|
// Toggle it off
|
2018-03-27 22:53:24 +00:00
|
|
|
eligOut, err := nodes.ToggleEligibility(nodeID, false, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2018-03-27 22:53:24 +00:00
|
|
|
assertWriteMeta(t, &eligOut.WriteMeta)
|
2018-02-27 20:59:27 +00:00
|
|
|
|
|
|
|
// Check again
|
|
|
|
out, _, err = nodes.Info(nodeID, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, NodeSchedulingIneligible, out.SchedulingEligibility)
|
2018-02-27 20:59:27 +00:00
|
|
|
|
|
|
|
// Toggle on
|
2018-03-27 22:53:24 +00:00
|
|
|
eligOut, err = nodes.ToggleEligibility(nodeID, true, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2018-03-27 22:53:24 +00:00
|
|
|
assertWriteMeta(t, &eligOut.WriteMeta)
|
2018-02-27 20:59:27 +00:00
|
|
|
|
|
|
|
// Check again
|
|
|
|
out, _, err = nodes.Info(nodeID, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, NodeSchedulingEligible, out.SchedulingEligibility)
|
|
|
|
must.Nil(t, out.DrainStrategy)
|
2018-02-27 20:59:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 23:51:18 +00:00
|
|
|
func TestNodes_Allocations(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +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()
|
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// 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.
|
2022-12-16 18:25:28 +00:00
|
|
|
allocations, _, err := nodes.Allocations("nope", nil)
|
|
|
|
must.NoError(t, err)
|
|
|
|
must.Len(t, 0, allocations)
|
2015-09-11 23:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodes_ForceEvaluate(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
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()
|
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Force-eval on a nonexistent node fails
|
2016-01-14 20:57:43 +00:00
|
|
|
_, _, err := nodes.ForceEvaluate("12345678-abcd-efab-cdef-123456789abc", nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.ErrorContains(t, err, "not found")
|
2015-09-11 23:51:18 +00:00
|
|
|
|
|
|
|
// Wait for node registration and get the ID
|
2022-12-16 18:25:28 +00:00
|
|
|
nodeID := oneNodeFromNodeList(t, nodes).ID
|
2015-09-11 23:51:18 +00:00
|
|
|
|
|
|
|
// 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)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
2015-09-11 23:51:18 +00:00
|
|
|
}
|
2015-09-17 19:40:51 +00:00
|
|
|
|
|
|
|
func TestNodes_Sort(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
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
|
|
|
}
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, expect, nodes)
|
2015-09-17 19:40:51 +00:00
|
|
|
}
|
2018-02-07 02:51:34 +00:00
|
|
|
|
2018-03-28 21:01:54 +00:00
|
|
|
// Unittest monitorDrainMultiplex when an error occurs
|
|
|
|
func TestNodes_MonitorDrain_Multiplex_Bad(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
multiplexCtx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
// monitorDrainMultiplex doesn't require anything on *Nodes, so we
|
|
|
|
// don't need to use a full Client
|
|
|
|
var nodeClient *Nodes
|
|
|
|
|
2018-03-30 18:07:40 +00:00
|
|
|
outCh := make(chan *MonitorMessage, 8)
|
|
|
|
nodeCh := make(chan *MonitorMessage, 1)
|
|
|
|
allocCh := make(chan *MonitorMessage, 8)
|
2018-03-28 21:01:54 +00:00
|
|
|
exitedCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(exitedCh)
|
2018-03-30 18:07:40 +00:00
|
|
|
nodeClient.monitorDrainMultiplex(ctx, cancel, outCh, nodeCh, allocCh)
|
2018-03-28 21:01:54 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Fake an alloc update
|
2018-03-30 18:07:40 +00:00
|
|
|
msg := Messagef(0, "alloc update")
|
2018-03-28 21:01:54 +00:00
|
|
|
allocCh <- msg
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, msg, <-outCh)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// Fake a node update
|
2018-03-30 18:07:40 +00:00
|
|
|
msg = Messagef(0, "node update")
|
2018-03-28 21:01:54 +00:00
|
|
|
nodeCh <- msg
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, msg, <-outCh)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// Fake an error that should shut everything down
|
2018-03-30 18:07:40 +00:00
|
|
|
msg = Messagef(MonitorMsgLevelError, "fake error")
|
|
|
|
nodeCh <- msg
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, msg, <-outCh)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
_, ok := <-exitedCh
|
2022-12-16 18:25:28 +00:00
|
|
|
must.False(t, ok)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
_, ok = <-outCh
|
2022-12-16 18:25:28 +00:00
|
|
|
must.False(t, ok)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// Exiting should also cancel the context that would be passed to the
|
|
|
|
// node & alloc watchers
|
|
|
|
select {
|
|
|
|
case <-multiplexCtx.Done():
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Unreachable(t, must.Sprint("multiplex context was not cancelled"))
|
2018-03-28 21:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unittest monitorDrainMultiplex when drain finishes
|
|
|
|
func TestNodes_MonitorDrain_Multiplex_Good(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
multiplexCtx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
// monitorDrainMultiplex doesn't require anything on *Nodes, so we
|
|
|
|
// don't need to use a full Client
|
|
|
|
var nodeClient *Nodes
|
|
|
|
|
2018-03-30 18:07:40 +00:00
|
|
|
outCh := make(chan *MonitorMessage, 8)
|
|
|
|
nodeCh := make(chan *MonitorMessage, 1)
|
|
|
|
allocCh := make(chan *MonitorMessage, 8)
|
2018-03-28 21:01:54 +00:00
|
|
|
exitedCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(exitedCh)
|
2018-03-30 18:07:40 +00:00
|
|
|
nodeClient.monitorDrainMultiplex(ctx, cancel, outCh, nodeCh, allocCh)
|
2018-03-28 21:01:54 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Fake a node updating and finishing
|
2018-03-30 18:07:40 +00:00
|
|
|
msg := Messagef(MonitorMsgLevelInfo, "node update")
|
2018-03-28 21:01:54 +00:00
|
|
|
nodeCh <- msg
|
|
|
|
close(nodeCh)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, msg, <-outCh)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// Nothing else should have exited yet
|
|
|
|
select {
|
2022-12-16 18:25:28 +00:00
|
|
|
case badMsg, ok := <-outCh:
|
|
|
|
must.False(t, ok, must.Sprintf("unexpected output %v", badMsg))
|
|
|
|
must.Unreachable(t, must.Sprint("out channel closed unexpectedly"))
|
2018-03-28 21:01:54 +00:00
|
|
|
case <-exitedCh:
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Unreachable(t, must.Sprint("multiplexer exited unexpectedly"))
|
2018-03-28 21:01:54 +00:00
|
|
|
case <-multiplexCtx.Done():
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Unreachable(t, must.Sprint("multiplexer context canceled unexpectedly"))
|
2018-03-28 21:01:54 +00:00
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
t.Logf("multiplexer still running as expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fake an alloc update coming in after the node monitor has finished
|
2018-03-30 18:07:40 +00:00
|
|
|
msg = Messagef(0, "alloc update")
|
2018-03-28 21:01:54 +00:00
|
|
|
allocCh <- msg
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, msg, <-outCh)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// Closing the allocCh should cause everything to exit
|
|
|
|
close(allocCh)
|
|
|
|
|
|
|
|
_, ok := <-exitedCh
|
2022-12-16 18:25:28 +00:00
|
|
|
must.False(t, ok)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
_, ok = <-outCh
|
2022-12-16 18:25:28 +00:00
|
|
|
must.False(t, ok)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// Exiting should also cancel the context that would be passed to the
|
|
|
|
// node & alloc watchers
|
|
|
|
select {
|
|
|
|
case <-multiplexCtx.Done():
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Unreachable(t, must.Sprint("context was not cancelled"))
|
2018-03-28 21:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodes_DrainStrategy_Equal(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// nil
|
|
|
|
var d *DrainStrategy
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Equal(t, nil, d)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
o := &DrainStrategy{}
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NotEqual(t, d, o)
|
|
|
|
must.NotEqual(t, o, d)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
d = &DrainStrategy{}
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Equal(t, d, o)
|
|
|
|
must.Equal(t, o, d)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// ForceDeadline
|
|
|
|
d.ForceDeadline = time.Now()
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NotEqual(t, d, o)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
o.ForceDeadline = d.ForceDeadline
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Equal(t, d, o)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// Deadline
|
|
|
|
d.Deadline = 1
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NotEqual(t, d, o)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
o.Deadline = 1
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Equal(t, d, o)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
// IgnoreSystemJobs
|
|
|
|
d.IgnoreSystemJobs = true
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NotEqual(t, d, o)
|
2018-03-28 21:01:54 +00:00
|
|
|
|
|
|
|
o.IgnoreSystemJobs = true
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Equal(t, d, o)
|
2018-03-28 21:01:54 +00:00
|
|
|
}
|
2018-11-14 16:51:50 +00:00
|
|
|
|
2020-08-14 07:40:03 +00:00
|
|
|
func TestNodes_Purge(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2022-12-16 18:25:28 +00:00
|
|
|
|
2020-08-14 07:40:03 +00:00
|
|
|
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
|
|
|
|
c.DevMode = true
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
2022-12-16 18:25:28 +00:00
|
|
|
nodes := c.Nodes()
|
2020-08-14 07:40:03 +00:00
|
|
|
|
|
|
|
// Purge on a nonexistent node fails.
|
|
|
|
_, _, err := c.Nodes().Purge("12345678-abcd-efab-cdef-123456789abc", nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.ErrorContains(t, err, "not found")
|
2020-08-14 07:40:03 +00:00
|
|
|
|
2022-12-16 18:25:28 +00:00
|
|
|
// Wait for nodeID
|
|
|
|
nodeID := oneNodeFromNodeList(t, nodes).ID
|
2020-08-14 07:40:03 +00:00
|
|
|
|
|
|
|
// Perform the node purge and check the response objects.
|
|
|
|
out, meta, err := c.Nodes().Purge(nodeID, nil)
|
2022-12-16 18:25:28 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.NotNil(t, out)
|
2020-08-14 07:40:03 +00:00
|
|
|
|
|
|
|
// We can't use assertQueryMeta here, as the RPC response does not populate
|
|
|
|
// the known leader field.
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Positive(t, meta.LastIndex)
|
2020-08-14 07:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 16:51:50 +00:00
|
|
|
func TestNodeStatValueFormatting(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2018-11-14 16:51:50 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
expected string
|
|
|
|
value StatValue
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"true",
|
2022-08-17 16:26:34 +00:00
|
|
|
StatValue{BoolVal: pointerOf(true)},
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"false",
|
2022-08-17 16:26:34 +00:00
|
|
|
StatValue{BoolVal: pointerOf(false)},
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"myvalue",
|
2022-08-17 16:26:34 +00:00
|
|
|
StatValue{StringVal: pointerOf("myvalue")},
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"2.718",
|
|
|
|
StatValue{
|
2019-01-18 18:28:35 +00:00
|
|
|
FloatNumeratorVal: float64ToPtr(2.718),
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"2.718 / 3.14",
|
|
|
|
StatValue{
|
2019-01-18 18:28:35 +00:00
|
|
|
FloatNumeratorVal: float64ToPtr(2.718),
|
|
|
|
FloatDenominatorVal: float64ToPtr(3.14),
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"2.718 MHz",
|
|
|
|
StatValue{
|
2019-01-18 18:28:35 +00:00
|
|
|
FloatNumeratorVal: float64ToPtr(2.718),
|
2018-11-14 16:51:50 +00:00
|
|
|
Unit: "MHz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"2.718 / 3.14 MHz",
|
|
|
|
StatValue{
|
2019-01-18 18:28:35 +00:00
|
|
|
FloatNumeratorVal: float64ToPtr(2.718),
|
|
|
|
FloatDenominatorVal: float64ToPtr(3.14),
|
2018-11-14 16:51:50 +00:00
|
|
|
Unit: "MHz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"2",
|
|
|
|
StatValue{
|
2022-08-17 16:26:34 +00:00
|
|
|
IntNumeratorVal: pointerOf(int64(2)),
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"2 / 3",
|
|
|
|
StatValue{
|
2022-08-17 16:26:34 +00:00
|
|
|
IntNumeratorVal: pointerOf(int64(2)),
|
|
|
|
IntDenominatorVal: pointerOf(int64(3)),
|
2018-11-14 16:51:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"2 MHz",
|
|
|
|
StatValue{
|
2022-08-17 16:26:34 +00:00
|
|
|
IntNumeratorVal: pointerOf(int64(2)),
|
2018-11-14 16:51:50 +00:00
|
|
|
Unit: "MHz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"2 / 3 MHz",
|
|
|
|
StatValue{
|
2022-08-17 16:26:34 +00:00
|
|
|
IntNumeratorVal: pointerOf(int64(2)),
|
|
|
|
IntDenominatorVal: pointerOf(int64(3)),
|
2018-11-14 16:51:50 +00:00
|
|
|
Unit: "MHz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
t.Run(fmt.Sprintf("case %d %v", i, c.expected), func(t *testing.T) {
|
|
|
|
formatted := c.value.String()
|
2022-12-16 18:25:28 +00:00
|
|
|
must.Eq(t, c.expected, formatted)
|
2018-11-14 16:51:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|