From 1272fcb9e107869cf149589d65ec39c3f8e90a97 Mon Sep 17 00:00:00 2001 From: Arshneet Singh Date: Fri, 18 Jan 2019 03:55:17 -0800 Subject: [PATCH] Don't display node name if output isn't verbose. Add tests. --- command/alloc_status_test.go | 12 ++++++++++++ command/job_status.go | 5 ++--- command/job_status_test.go | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/command/alloc_status_test.go b/command/alloc_status_test.go index fbd199424..e4b5c239c 100644 --- a/command/alloc_status_test.go +++ b/command/alloc_status_test.go @@ -120,9 +120,11 @@ func TestAllocStatusCommand_Run(t *testing.T) { } // get an alloc id allocId1 := "" + nodeName := "" if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil { if len(allocs) > 0 { allocId1 = allocs[0].ID + nodeName = allocs[0].NodeName } } if allocId1 == "" { @@ -141,6 +143,16 @@ func TestAllocStatusCommand_Run(t *testing.T) { t.Fatalf("expected to have 'Modified' but saw: %s", out) } + if !strings.Contains(out, "Modified") { + t.Fatalf("expected to have 'Modified' but saw: %s", out) + } + + nodeNameRegexpStr := fmt.Sprintf(`\nNode Name\s+= %s\n`, regexp.QuoteMeta(nodeName)) + nodeNameRegexp := regexp.MustCompile(nodeNameRegexpStr) + if !nodeNameRegexp.MatchString(out) { + t.Fatalf("expected to have 'Node Name' but saw: %s", out) + } + ui.OutputWriter.Reset() if code := cmd.Run([]string{"-address=" + url, "-verbose", allocId1}); code != 0 { diff --git a/command/job_status.go b/command/job_status.go index fb8336e2a..63fd163e2 100644 --- a/command/job_status.go +++ b/command/job_status.go @@ -428,15 +428,14 @@ func formatAllocListStubs(stubs []*api.AllocationListStub, verbose bool, uuidLen formatUnixNanoTime(alloc.ModifyTime)) } } else { - allocs[0] = "ID|Node ID|Node Name|Task Group|Version|Desired|Status|Created|Modified" + allocs[0] = "ID|Node ID|Task Group|Version|Desired|Status|Created|Modified" for i, alloc := range stubs { now := time.Now() createTimePretty := prettyTimeDiff(time.Unix(0, alloc.CreateTime), now) modTimePretty := prettyTimeDiff(time.Unix(0, alloc.ModifyTime), now) - allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%d|%s|%s|%s|%s", + allocs[i+1] = fmt.Sprintf("%s|%s|%s|%d|%s|%s|%s|%s", limit(alloc.ID, uuidLength), limit(alloc.NodeID, uuidLength), - alloc.NodeName, alloc.TaskGroup, alloc.JobVersion, alloc.DesiredStatus, diff --git a/command/job_status_test.go b/command/job_status_test.go index e0bc40b56..f4a71c7b8 100644 --- a/command/job_status_test.go +++ b/command/job_status_test.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "regexp" "strings" "testing" "time" @@ -123,6 +124,14 @@ func TestJobStatusCommand_Run(t *testing.T) { if code := cmd.Run([]string{"-address=" + url, "-verbose", "job2_sfx"}); code != 0 { t.Fatalf("expected exit 0, got: %d", code) } + + nodeName := "" + if allocs, _, err := client.Jobs().Allocations("job2_sfx", false, nil); err == nil { + if len(allocs) > 0 { + nodeName = allocs[0].NodeName + } + } + out = ui.OutputWriter.String() if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") { t.Fatalf("expected only job2_sfx, got: %s", out) @@ -139,6 +148,18 @@ func TestJobStatusCommand_Run(t *testing.T) { if !strings.Contains(out, "Modified") { t.Fatal("should have modified header") } + + // string calculations based on 1-byte chars, not using runes + allocationsTableName := "Allocations\n" + allocationsTableStr := strings.Split(out, allocationsTableName)[1] + nodeNameHeaderStr := "Node Name" + nodeNameHeaderIndex := strings.Index(allocationsTableStr, nodeNameHeaderStr) + nodeNameRegexpStr := fmt.Sprintf(`.*%s.*\n.{%d}%s`, nodeNameHeaderStr, nodeNameHeaderIndex, regexp.QuoteMeta(nodeName)) + nodeNameRegexp := regexp.MustCompile(nodeNameRegexpStr) + if !nodeNameRegexp.MatchString(out) { + t.Fatalf("expected to have 'Node Name' but saw: %s", out) + } + ui.ErrorWriter.Reset() ui.OutputWriter.Reset()