github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/alloc_stop_test.go (about) 1 package command 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/nomad/ci" 7 "github.com/mitchellh/cli" 8 "github.com/shoenig/test/must" 9 ) 10 11 func TestAllocStopCommand_Implements(t *testing.T) { 12 ci.Parallel(t) 13 var _ cli.Command = &AllocStopCommand{} 14 } 15 16 func TestAllocStop_Fails(t *testing.T) { 17 srv, _, url := testServer(t, false, nil) 18 defer stopTestAgent(srv) 19 20 ui := cli.NewMockUi() 21 cmd := &AllocStopCommand{Meta: Meta{Ui: ui}} 22 23 // Fails on misuse 24 code := cmd.Run([]string{"some", "garbage", "args"}) 25 must.One(t, code) 26 27 out := ui.ErrorWriter.String() 28 must.StrContains(t, out, commandErrorText(cmd)) 29 30 ui.ErrorWriter.Reset() 31 32 // Fails on connection failure 33 code = cmd.Run([]string{"-address=nope", "foobar"}) 34 must.One(t, code) 35 36 out = ui.ErrorWriter.String() 37 must.StrContains(t, out, "Error querying allocation") 38 39 ui.ErrorWriter.Reset() 40 41 // Fails on missing alloc 42 code = cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}) 43 must.One(t, code) 44 45 out = ui.ErrorWriter.String() 46 must.StrContains(t, out, "No allocation(s) with prefix or id") 47 ui.ErrorWriter.Reset() 48 49 // Fail on identifier with too few characters 50 code = cmd.Run([]string{"-address=" + url, "2"}) 51 must.One(t, code) 52 53 out = ui.ErrorWriter.String() 54 must.StrContains(t, out, "must contain at least two characters") 55 ui.ErrorWriter.Reset() 56 57 // Identifiers with uneven length should produce a query result 58 code = cmd.Run([]string{"-address=" + url, "123"}) 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 65 func TestAllocStop_Run(t *testing.T) { 66 ci.Parallel(t) 67 68 srv, client, url := testServer(t, true, nil) 69 defer stopTestAgent(srv) 70 71 // Wait for a node to be ready 72 waitForNodes(t, client) 73 74 ui := cli.NewMockUi() 75 cmd := &AllocStopCommand{Meta: Meta{Ui: ui}} 76 77 jobID := "job1_sfx" 78 job1 := testJob(jobID) 79 resp, _, err := client.Jobs().Register(job1, nil) 80 must.NoError(t, err) 81 82 code := waitForSuccess(ui, client, fullId, t, resp.EvalID) 83 must.Zero(t, code) 84 85 // get an alloc id 86 allocID := "" 87 if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil { 88 if len(allocs) > 0 { 89 allocID = allocs[0].ID 90 } 91 } 92 must.NotEq(t, "", allocID) 93 94 // Wait for alloc to be running 95 waitForAllocRunning(t, client, allocID) 96 97 code = cmd.Run([]string{"-address=" + url, allocID}) 98 must.Zero(t, code) 99 }