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 TestInspectCommand_Implements(t *testing.T) {
14	t.Parallel()
15	var _ cli.Command = &JobInspectCommand{}
16}
17
18func TestInspectCommand_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 := &JobInspectCommand{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 inspecting job") {
49		t.Fatalf("expected failed query error, got: %s", out)
50	}
51	ui.ErrorWriter.Reset()
52
53	// Failed on both -json and -t options are specified
54	if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
55		t.Fatalf("expected exit 1, got: %d", code)
56	}
57	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both json and template formatting are not allowed") {
58		t.Fatalf("expected getting formatter error, got: %s", out)
59	}
60}
61
62func TestInspectCommand_AutocompleteArgs(t *testing.T) {
63	assert := assert.New(t)
64	t.Parallel()
65
66	srv, _, url := testServer(t, true, nil)
67	defer srv.Shutdown()
68
69	ui := new(cli.MockUi)
70	cmd := &JobInspectCommand{Meta: Meta{Ui: ui, flagAddress: url}}
71
72	state := srv.Agent.Server().State()
73	j := mock.Job()
74	assert.Nil(state.UpsertJob(1000, j))
75
76	prefix := j.ID[:len(j.ID)-5]
77	args := complete.Args{Last: prefix}
78	predictor := cmd.AutocompleteArgs()
79
80	res := predictor.Predict(args)
81	assert.Equal(1, len(res))
82	assert.Equal(j.ID, res[0])
83}
84