small fixups

This commit is contained in:
Chelsea Holland Komlo 2017-08-17 21:37:38 +00:00
parent d231b4aa0f
commit 3d743a476a
2 changed files with 38 additions and 67 deletions

View File

@ -15,12 +15,11 @@ type StatusCommand struct {
// Check that the last argument provided is not setting a flag
func lastArgIsFlag(args []string) bool {
lastArg := args[len(args)-1]
// strip leading '-' from what is potentially a flag
lastArg := strings.Replace(args[len(args)-1], "-", "", 1)
for _, flag := range flagOptions {
arg := strings.Replace(lastArg, "-", "", 1) // strip leading '-' from flag
if strings.HasPrefix(arg, flag) {
if strings.HasPrefix(lastArg, flag) {
return true
}
}
@ -35,15 +34,15 @@ func (c *StatusCommand) Run(args []string) int {
return 1
}
// If no identifier is provided, default to listing jobs
if len(args) == 0 || lastArgIsFlag(args) {
cmd := &JobStatusCommand{Meta: c.Meta}
return cmd.Run(args)
}
// Assume the last argument will be the id to search
id := args[len(args)-1]
// Try querying for the context associated with the id
// Query for the context associated with the id
res, err := client.Search().PrefixSearch(id, contexts.All)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying search with id: %s", err))
@ -63,7 +62,7 @@ func (c *StatusCommand) Run(args []string) int {
matchCount++
}
// Only a single match should return, as this is a match against a full id
// Only a single result should return, as this is a match against a full id
if matchCount > 1 || len(vers) > 1 {
c.Ui.Error(fmt.Sprintf("Multiple matches found for id %s", err))
return 1
@ -89,8 +88,7 @@ func (c *StatusCommand) Run(args []string) int {
}
func (s *StatusCommand) Help() string {
helpText := `
Usage: nomad status <identifier>
helpText := `Usage: nomad status <identifier>
Display information about an existing resource. Job names, node ids,
allocation ids, and evaluation ids are all valid identifiers.

View File

@ -2,11 +2,9 @@ package command
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/nomad/command/agent"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/mitchellh/cli"
"github.com/posener/complete"
@ -14,7 +12,9 @@ import (
)
func TestStatusCommand_Run_JobStatus(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
@ -24,9 +24,8 @@ func TestStatusCommand_Run_JobStatus(t *testing.T) {
// Register a job
job1 := testJob("job1_sfx")
resp, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assert.Nil(err)
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}
@ -35,15 +34,15 @@ func TestStatusCommand_Run_JobStatus(t *testing.T) {
if code := cmd.Run([]string{"-address=" + url, "job1_sfx"}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out := ui.OutputWriter.String()
if !strings.Contains(out, "job1_sfx") {
t.Fatalf("expected job1_sfx, got: %s", out)
}
out := ui.OutputWriter.String()
assert.Contains(out, "job1_sfx")
ui.OutputWriter.Reset()
}
func TestStatusCommand_Run_EvalStatus(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, client, url := testServer(t, true, nil)
@ -55,9 +54,8 @@ func TestStatusCommand_Run_EvalStatus(t *testing.T) {
jobID := "job1_sfx"
job1 := testJob(jobID)
resp, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assert.Nil(err)
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}
@ -69,24 +67,22 @@ func TestStatusCommand_Run_EvalStatus(t *testing.T) {
evalID = evals[0].ID
}
}
if evalID == "" {
t.Fatal("unable to find an evaluation")
}
assert.NotEqual("", evalID)
// Query to check the eval status
if code := cmd.Run([]string{"-address=" + url, evalID}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out := ui.OutputWriter.String()
if !strings.Contains(out, evalID) {
t.Fatalf("expected eval id, got: %s", out)
}
out := ui.OutputWriter.String()
assert.Contains(out, evalID)
ui.OutputWriter.Reset()
}
func TestStatusCommand_Run_NodeStatus(t *testing.T) {
assert := assert.New(t)
t.Parallel()
// Start in dev mode so we get a node registration
@ -118,46 +114,28 @@ func TestStatusCommand_Run_NodeStatus(t *testing.T) {
if code := cmd.Run([]string{"-address=" + url, nodeID}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out := ui.OutputWriter.String()
if !strings.Contains(out, "mynode") {
t.Fatalf("expected node id (mynode), got: %s", out)
}
out := ui.OutputWriter.String()
assert.Contains(out, "mynode")
ui.OutputWriter.Reset()
}
func TestStatusCommand_Run_AllocStatus(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
// Wait for a node to be ready
testutil.WaitForResult(func() (bool, error) {
nodes, _, err := client.Nodes().List(nil)
if err != nil {
return false, err
}
for _, node := range nodes {
if node.Status == structs.NodeStatusReady {
return true, nil
}
}
return false, fmt.Errorf("no ready nodes")
}, func(err error) {
t.Fatalf("err: %v", err)
})
ui := new(cli.MockUi)
cmd := &StatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
jobID := "job1_sfx"
job1 := testJob(jobID)
resp, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assert.Nil(err)
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}
@ -169,23 +147,22 @@ func TestStatusCommand_Run_AllocStatus(t *testing.T) {
allocId1 = allocs[0].ID
}
}
if allocId1 == "" {
t.Fatal("unable to find an allocation")
}
assert.NotEqual("", allocId1)
if code := cmd.Run([]string{"-address=" + url, allocId1}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out := ui.OutputWriter.String()
if !strings.Contains(out, allocId1) {
t.Fatal("expected to find alloc id in output")
}
assert.Contains(out, allocId1)
ui.OutputWriter.Reset()
}
func TestStatusCommand_Run_NoPrefix(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
@ -195,9 +172,8 @@ func TestStatusCommand_Run_NoPrefix(t *testing.T) {
// Register a job
job1 := testJob("job1_sfx")
resp, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assert.Nil(err)
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}
@ -206,11 +182,9 @@ func TestStatusCommand_Run_NoPrefix(t *testing.T) {
if code := cmd.Run([]string{"-address=" + url}); code != 0 {
t.Fatalf("expected exit 0, got: %d", code)
}
out := ui.OutputWriter.String()
if !strings.Contains(out, "job1_sfx") {
t.Fatalf("expected job1_sfx, got: %s", out)
}
out := ui.OutputWriter.String()
assert.Contains(out, "job1_sfx")
ui.OutputWriter.Reset()
}
@ -228,9 +202,8 @@ func TestStatusCommand_AutocompleteArgs(t *testing.T) {
jobID := "job1_sfx"
job1 := testJob(jobID)
resp, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
assert.Nil(err)
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}