open-nomad/command/alloc_signal_test.go
Drew Bailey 6c788fdccd
Events/msgtype cleanup (#9117)
* use msgtype in upsert node

adds message type to signature for upsert node, update tests, remove placeholder method

* UpsertAllocs msg type test setup

* use upsertallocs with msg type in signature

update test usage of delete node

delete placeholder msgtype method

* add msgtype to upsert evals signature, update test call sites with test setup msg type

handle snapshot upsert eval outside of FSM and ignore eval event

remove placeholder upsertevalsmsgtype

handle job plan rpc and prevent event creation for plan

msgtype cleanup upsertnodeevents

updatenodedrain msgtype

msg type 0 is a node registration event, so set the default  to the ignore type

* fix named import

* fix signature ordering on upsertnode to match
2020-10-19 09:30:15 -04:00

143 lines
3.9 KiB
Go

package command
import (
"fmt"
"testing"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAllocSignalCommand_Implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &AllocSignalCommand{}
}
func TestAllocSignalCommand_Fails(t *testing.T) {
t.Parallel()
srv, _, url := testServer(t, false, nil)
defer srv.Shutdown()
require := require.New(t)
ui := cli.NewMockUi()
cmd := &AllocSignalCommand{Meta: Meta{Ui: ui}}
// Fails on lack of alloc ID
require.Equal(1, cmd.Run([]string{}))
require.Contains(ui.ErrorWriter.String(), "This command takes up to two arguments")
ui.ErrorWriter.Reset()
// Fails on misuse
require.Equal(1, cmd.Run([]string{"some", "bad", "args"}))
require.Contains(ui.ErrorWriter.String(), "This command takes up to two arguments")
ui.ErrorWriter.Reset()
// Fails on connection failure
require.Equal(1, cmd.Run([]string{"-address=nope", "foobar"}))
require.Contains(ui.ErrorWriter.String(), "Error querying allocation")
ui.ErrorWriter.Reset()
// Fails on missing alloc
code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"})
require.Equal(1, code)
require.Contains(ui.ErrorWriter.String(), "No allocation(s) with prefix or id")
ui.ErrorWriter.Reset()
// Fail on identifier with too few characters
require.Equal(1, cmd.Run([]string{"-address=" + url, "2"}))
require.Contains(ui.ErrorWriter.String(), "must contain at least two characters.")
ui.ErrorWriter.Reset()
}
func TestAllocSignalCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := cli.NewMockUi()
cmd := &AllocSignalCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a fake alloc
state := srv.Agent.Server().State()
a := mock.Alloc()
assert.Nil(state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{a}))
prefix := a.ID[:5]
args := complete.Args{All: []string{"signal", prefix}, Last: prefix}
predictor := cmd.AutocompleteArgs()
// Match Allocs
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(a.ID, res[0])
}
func TestAllocSignalCommand_Run(t *testing.T) {
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
require := require.New(t)
// Wait for a node to be ready
testutil.WaitForResult(func() (bool, error) {
nodes, _, err := client.Nodes().List(nil)
if err != nil {
return false, err
}
for _, node := range nodes {
if _, ok := node.Drivers["mock_driver"]; ok &&
node.Status == structs.NodeStatusReady {
return true, nil
}
}
return false, fmt.Errorf("no ready nodes")
}, func(err error) {
t.Fatalf("err: %v", err)
})
ui := cli.NewMockUi()
cmd := &AllocSignalCommand{Meta: Meta{Ui: ui}}
jobID := "job1_sfx"
job1 := testJob(jobID)
resp, _, err := client.Jobs().Register(job1, nil)
require.NoError(err)
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}
// get an alloc id
allocId1 := ""
if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
if len(allocs) > 0 {
allocId1 = allocs[0].ID
}
}
require.NotEmpty(allocId1, "unable to find allocation")
// Wait for alloc to be running
testutil.WaitForResult(func() (bool, error) {
alloc, _, err := client.Allocations().Info(allocId1, nil)
if err != nil {
return false, err
}
if alloc.ClientStatus == api.AllocClientStatusRunning {
return true, nil
}
return false, fmt.Errorf("alloc is not running, is: %s", alloc.ClientStatus)
}, func(err error) {
t.Fatalf("err: %v", err)
})
require.Equal(cmd.Run([]string{"-address=" + url, allocId1}), 0, "expected successful exit code")
ui.OutputWriter.Reset()
}