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