github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/job_stop_test.go (about) 1 package command 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/nomad/ci" 8 "github.com/hashicorp/nomad/nomad/mock" 9 "github.com/hashicorp/nomad/nomad/structs" 10 "github.com/mitchellh/cli" 11 "github.com/posener/complete" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestStopCommand_Implements(t *testing.T) { 16 ci.Parallel(t) 17 var _ cli.Command = &JobStopCommand{} 18 } 19 20 func TestStopCommand_Fails(t *testing.T) { 21 ci.Parallel(t) 22 srv, _, url := testServer(t, false, nil) 23 defer srv.Shutdown() 24 25 ui := cli.NewMockUi() 26 cmd := &JobStopCommand{Meta: Meta{Ui: ui}} 27 28 // Fails on misuse 29 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 30 t.Fatalf("expected exit code 1, got: %d", code) 31 } 32 if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) { 33 t.Fatalf("expected help output, got: %s", out) 34 } 35 ui.ErrorWriter.Reset() 36 37 // Fails on nonexistent job ID 38 if code := cmd.Run([]string{"-address=" + url, "nope"}); code != 1 { 39 t.Fatalf("expect exit 1, got: %d", code) 40 } 41 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No job(s) with prefix or id") { 42 t.Fatalf("expect not found error, got: %s", out) 43 } 44 ui.ErrorWriter.Reset() 45 46 // Fails on connection failure 47 if code := cmd.Run([]string{"-address=nope", "nope"}); code != 1 { 48 t.Fatalf("expected exit code 1, got: %d", code) 49 } 50 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error deregistering job") { 51 t.Fatalf("expected failed query error, got: %s", out) 52 } 53 } 54 55 func TestStopCommand_AutocompleteArgs(t *testing.T) { 56 ci.Parallel(t) 57 assert := assert.New(t) 58 59 srv, _, url := testServer(t, true, nil) 60 defer srv.Shutdown() 61 62 ui := cli.NewMockUi() 63 cmd := &JobStopCommand{Meta: Meta{Ui: ui, flagAddress: url}} 64 65 // Create a fake job 66 state := srv.Agent.Server().State() 67 j := mock.Job() 68 assert.Nil(state.UpsertJob(structs.MsgTypeTestSetup, 1000, j)) 69 70 prefix := j.ID[:len(j.ID)-5] 71 args := complete.Args{Last: prefix} 72 predictor := cmd.AutocompleteArgs() 73 74 res := predictor.Predict(args) 75 assert.Equal(1, len(res)) 76 assert.Equal(j.ID, res[0]) 77 }