1package command
2
3import (
4	"strings"
5	"testing"
6
7	"github.com/hashicorp/nomad/nomad/mock"
8	"github.com/hashicorp/nomad/nomad/structs"
9	"github.com/mitchellh/cli"
10	"github.com/posener/complete"
11	"github.com/stretchr/testify/assert"
12)
13
14func TestEvalStatusCommand_Implements(t *testing.T) {
15	t.Parallel()
16	var _ cli.Command = &EvalStatusCommand{}
17}
18
19func TestEvalStatusCommand_Fails(t *testing.T) {
20	t.Parallel()
21	srv, _, url := testServer(t, false, nil)
22	defer srv.Shutdown()
23
24	ui := new(cli.MockUi)
25	cmd := &EvalStatusCommand{Meta: Meta{Ui: ui}}
26
27	// Fails on misuse
28	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
29		t.Fatalf("expected exit code 1, got: %d", code)
30	}
31	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
32		t.Fatalf("expected help output, got: %s", out)
33	}
34	ui.ErrorWriter.Reset()
35
36	// Fails on eval lookup failure
37	if code := cmd.Run([]string{"-address=" + url, "3E55C771-76FC-423B-BCED-3E5314F433B1"}); code != 1 {
38		t.Fatalf("expect exit 1, got: %d", code)
39	}
40	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No evaluation(s) with prefix or id") {
41		t.Fatalf("expect not found error, got: %s", out)
42	}
43	ui.ErrorWriter.Reset()
44
45	// Fails on connection failure
46	if code := cmd.Run([]string{"-address=nope", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 {
47		t.Fatalf("expected exit code 1, got: %d", code)
48	}
49	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying evaluation") {
50		t.Fatalf("expected failed query error, got: %s", out)
51	}
52	ui.ErrorWriter.Reset()
53
54	// Failed on both -json and -t options are specified
55	if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
56		t.Fatalf("expected exit 1, got: %d", code)
57	}
58	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both json and template formatting are not allowed") {
59		t.Fatalf("expected getting formatter error, got: %s", out)
60	}
61
62}
63
64func TestEvalStatusCommand_AutocompleteArgs(t *testing.T) {
65	assert := assert.New(t)
66	t.Parallel()
67
68	srv, _, url := testServer(t, true, nil)
69	defer srv.Shutdown()
70
71	ui := new(cli.MockUi)
72	cmd := &EvalStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
73
74	// Create a fake eval
75	state := srv.Agent.Server().State()
76	e := mock.Eval()
77	assert.Nil(state.UpsertEvals(1000, []*structs.Evaluation{e}))
78
79	prefix := e.ID[:5]
80	args := complete.Args{Last: prefix}
81	predictor := cmd.AutocompleteArgs()
82
83	res := predictor.Predict(args)
84	assert.Equal(1, len(res))
85	assert.Equal(e.ID, res[0])
86}
87