github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/alloc_signal_test.go (about)

     1  package command
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/ci"
     7  	"github.com/hashicorp/nomad/nomad/mock"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  	"github.com/mitchellh/cli"
    10  	"github.com/posener/complete"
    11  	"github.com/shoenig/test/must"
    12  )
    13  
    14  func TestAllocSignalCommand_Implements(t *testing.T) {
    15  	ci.Parallel(t)
    16  	var _ cli.Command = &AllocSignalCommand{}
    17  }
    18  
    19  func TestAllocSignalCommand_Fails(t *testing.T) {
    20  	ci.Parallel(t)
    21  	srv, _, url := testServer(t, false, nil)
    22  	defer stopTestAgent(srv)
    23  
    24  	ui := cli.NewMockUi()
    25  	cmd := &AllocSignalCommand{Meta: Meta{Ui: ui}}
    26  
    27  	// Fails on lack of alloc ID
    28  	code := cmd.Run([]string{})
    29  	must.One(t, code)
    30  
    31  	out := ui.ErrorWriter.String()
    32  	must.StrContains(t, out, "This command takes up to two arguments")
    33  
    34  	ui.ErrorWriter.Reset()
    35  
    36  	// Fails on misuse
    37  	code = cmd.Run([]string{"some", "bad", "args"})
    38  	must.One(t, code)
    39  
    40  	out = ui.ErrorWriter.String()
    41  	must.StrContains(t, out, "This command takes up to two arguments")
    42  
    43  	ui.ErrorWriter.Reset()
    44  
    45  	// Fails on connection failure
    46  	code = cmd.Run([]string{"-address=nope", "foobar"})
    47  	must.One(t, code)
    48  
    49  	out = ui.ErrorWriter.String()
    50  	must.StrContains(t, out, "Error querying allocation")
    51  
    52  	ui.ErrorWriter.Reset()
    53  
    54  	// Fails on missing alloc
    55  	code = cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"})
    56  	must.One(t, code)
    57  
    58  	out = ui.ErrorWriter.String()
    59  	must.StrContains(t, out, "No allocation(s) with prefix or id")
    60  
    61  	ui.ErrorWriter.Reset()
    62  
    63  	// Fail on identifier with too few characters
    64  	code = cmd.Run([]string{"-address=" + url, "2"})
    65  	must.One(t, code)
    66  
    67  	out = ui.ErrorWriter.String()
    68  	must.StrContains(t, out, "must contain at least two characters.")
    69  
    70  	ui.ErrorWriter.Reset()
    71  }
    72  
    73  func TestAllocSignalCommand_AutocompleteArgs(t *testing.T) {
    74  	ci.Parallel(t)
    75  
    76  	srv, _, url := testServer(t, true, nil)
    77  	defer stopTestAgent(srv)
    78  
    79  	ui := cli.NewMockUi()
    80  	cmd := &AllocSignalCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    81  
    82  	// Create a fake alloc
    83  	state := srv.Agent.Server().State()
    84  	a := mock.Alloc()
    85  	must.NoError(t, state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{a}))
    86  
    87  	prefix := a.ID[:5]
    88  	args := complete.Args{All: []string{"signal", prefix}, Last: prefix}
    89  	predictor := cmd.AutocompleteArgs()
    90  
    91  	// Match Allocs
    92  	res := predictor.Predict(args)
    93  	must.Len(t, 1, res)
    94  	must.Eq(t, a.ID, res[0])
    95  }
    96  
    97  func TestAllocSignalCommand_Run(t *testing.T) {
    98  	ci.Parallel(t)
    99  
   100  	srv, client, url := testServer(t, true, nil)
   101  	defer stopTestAgent(srv)
   102  
   103  	// Wait for a node to be ready
   104  	waitForNodes(t, client)
   105  
   106  	ui := cli.NewMockUi()
   107  	cmd := &AllocSignalCommand{Meta: Meta{Ui: ui}}
   108  
   109  	jobID := "job1_sfx"
   110  	job1 := testJob(jobID)
   111  	resp, _, err := client.Jobs().Register(job1, nil)
   112  	must.NoError(t, err)
   113  
   114  	code := waitForSuccess(ui, client, fullId, t, resp.EvalID)
   115  	must.Zero(t, code)
   116  
   117  	// Get an alloc id
   118  	allocID := getAllocFromJob(t, client, jobID)
   119  
   120  	// Wait for alloc to be running
   121  	waitForAllocRunning(t, client, allocID)
   122  
   123  	code = cmd.Run([]string{"-address=" + url, allocID})
   124  	must.Zero(t, code)
   125  }