Skip to content

Commit

Permalink
Don't display node name if output isn't verbose. Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
arshjohar committed Jan 22, 2019
1 parent bdd3eeb commit 59dec15
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions command/alloc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions command/job_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions command/job_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down

0 comments on commit 59dec15

Please sign in to comment.