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