github.com/hernad/nomad@v1.6.112/command/alloc_signal_test.go (about)

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