github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/alloc_restart_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 TestAllocRestartCommand_Implements(t *testing.T) {
    15  	var _ cli.Command = &AllocRestartCommand{}
    16  }
    17  
    18  func TestAllocRestartCommand_Fails(t *testing.T) {
    19  	ci.Parallel(t)
    20  
    21  	srv, client, url := testServer(t, true, nil)
    22  	defer stopTestAgent(srv)
    23  
    24  	ui := cli.NewMockUi()
    25  	cmd := &AllocRestartCommand{Meta: Meta{Ui: ui}}
    26  
    27  	// Fails on misuse
    28  	code := cmd.Run([]string{"some", "garbage", "args"})
    29  	must.One(t, code)
    30  
    31  	out := ui.ErrorWriter.String()
    32  	must.StrContains(t, out, commandErrorText(cmd))
    33  
    34  	ui.ErrorWriter.Reset()
    35  
    36  	// Fails on connection failure
    37  	code = cmd.Run([]string{"-address=nope", "foobar"})
    38  	must.One(t, code)
    39  
    40  	out = ui.ErrorWriter.String()
    41  	must.StrContains(t, out, "Error querying allocation")
    42  
    43  	ui.ErrorWriter.Reset()
    44  
    45  	// Fails on missing alloc
    46  	code = cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"})
    47  	must.One(t, code)
    48  
    49  	out = ui.ErrorWriter.String()
    50  	must.StrContains(t, out, "No allocation(s) with prefix or id")
    51  
    52  	ui.ErrorWriter.Reset()
    53  
    54  	// Fail on identifier with too few characters
    55  	code = cmd.Run([]string{"-address=" + url, "2"})
    56  	must.One(t, code)
    57  
    58  	out = ui.ErrorWriter.String()
    59  	must.StrContains(t, out, "must contain at least two characters")
    60  
    61  	ui.ErrorWriter.Reset()
    62  
    63  	// Identifiers with uneven length should produce a query result
    64  	code = cmd.Run([]string{"-address=" + url, "123"})
    65  	must.One(t, code)
    66  
    67  	out = ui.ErrorWriter.String()
    68  	must.StrContains(t, out, "No allocation(s) with prefix or id")
    69  
    70  	ui.ErrorWriter.Reset()
    71  
    72  	// Wait for a node to be ready
    73  	waitForNodes(t, client)
    74  
    75  	jobID := "job1_sfx"
    76  	job1 := testJob(jobID)
    77  	resp, _, err := client.Jobs().Register(job1, nil)
    78  	must.NoError(t, err)
    79  
    80  	code = waitForSuccess(ui, client, fullId, t, resp.EvalID)
    81  	must.Zero(t, code)
    82  
    83  	// get an alloc id
    84  	allocId1 := ""
    85  	if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
    86  		if len(allocs) > 0 {
    87  			allocId1 = allocs[0].ID
    88  		}
    89  	}
    90  	must.NotEq(t, "", allocId1)
    91  
    92  	// Fails on not found task
    93  	code = cmd.Run([]string{"-address=" + url, allocId1, "fooooobarrr"})
    94  	must.One(t, code)
    95  
    96  	out = ui.ErrorWriter.String()
    97  	must.StrContains(t, out, "Could not find task named")
    98  
    99  	ui.ErrorWriter.Reset()
   100  }
   101  
   102  func TestAllocRestartCommand_Run(t *testing.T) {
   103  	ci.Parallel(t)
   104  
   105  	srv, client, url := testServer(t, true, nil)
   106  	defer stopTestAgent(srv)
   107  
   108  	// Wait for a node to be ready
   109  	waitForNodes(t, client)
   110  
   111  	ui := cli.NewMockUi()
   112  	cmd := &AllocRestartCommand{Meta: Meta{Ui: ui}}
   113  
   114  	jobID := "job1_sfx"
   115  	job1 := testJob(jobID)
   116  	resp, _, err := client.Jobs().Register(job1, nil)
   117  	must.NoError(t, err)
   118  
   119  	code := waitForSuccess(ui, client, fullId, t, resp.EvalID)
   120  	must.Zero(t, code)
   121  
   122  	// get an alloc id
   123  	allocID := ""
   124  	if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
   125  		if len(allocs) > 0 {
   126  			allocID = allocs[0].ID
   127  		}
   128  	}
   129  	must.NotEq(t, "", allocID)
   130  
   131  	// Wait for alloc to be running
   132  	waitForAllocRunning(t, client, allocID)
   133  
   134  	code = cmd.Run([]string{"-address=" + url, allocID})
   135  	must.Zero(t, code)
   136  
   137  	ui.OutputWriter.Reset()
   138  }
   139  
   140  func TestAllocRestartCommand_AutocompleteArgs(t *testing.T) {
   141  	ci.Parallel(t)
   142  
   143  	srv, _, url := testServer(t, true, nil)
   144  	defer stopTestAgent(srv)
   145  
   146  	ui := cli.NewMockUi()
   147  	cmd := &AllocRestartCommand{Meta: Meta{Ui: ui, flagAddress: url}}
   148  
   149  	// Create a fake alloc
   150  	state := srv.Agent.Server().State()
   151  	a := mock.Alloc()
   152  	must.NoError(t, state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{a}))
   153  
   154  	prefix := a.ID[:5]
   155  	args := complete.Args{Last: prefix}
   156  	predictor := cmd.AutocompleteArgs()
   157  
   158  	res := predictor.Predict(args)
   159  	must.Len(t, 1, res)
   160  	must.Eq(t, a.ID, res[0])
   161  }