Skip to content

Commit

Permalink
Add logs command test
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Jul 21, 2016
1 parent eb88e87 commit 14295a4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
12 changes: 8 additions & 4 deletions command/agent/fs_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,11 @@ func (s *StreamFramer) run() {
// Store any error and mark it as not running
var err error
defer func() {
s.l.Lock()
s.Err = err
close(s.exitCh)
close(s.outbound)

s.l.Lock()
s.Err = err
s.running = false
s.l.Unlock()
}()
Expand All @@ -338,8 +339,11 @@ func (s *StreamFramer) run() {

// Read the data for the frame, and send it
s.f.Data = s.readData()
s.outbound <- s.f
s.f = nil
select {
case s.outbound <- s.f:
s.f = nil
default:
}
s.l.Unlock()
case <-s.heartbeat.C:
// Send a heartbeat frame
Expand Down
2 changes: 1 addition & 1 deletion command/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ FS Specific Options:
Show full information.
-job <job-id>
Use a random allocation from a specified job-id.
Use a random allocation from the specified job ID.
-stat
Show file stat information instead of displaying the file, or listing the directory.
Expand Down
16 changes: 10 additions & 6 deletions command/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ Usage: nomad logs [options] <alloc-id> <task>
General Options:
` + generalOptionsUsage() + `
` + generalOptionsUsage() + `
Logs Specific Options:
-verbose
Show full information.
-job <job-id>
Use a random allocation from a specified job-id.
Use a random allocation from the specified job ID.
-f
Causes the output to not stop when the end of the logs are reached, but
Expand Down Expand Up @@ -60,7 +60,7 @@ func (l *LogsCommand) Run(args []string) int {
var verbose, job, tail, stderr, follow bool
var numLines, numBytes int64

flags := l.Meta.FlagSet("logs-list", FlagSetClient)
flags := l.Meta.FlagSet("logs", FlagSetClient)
flags.Usage = func() { l.Ui.Output(l.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&job, "job", false, "")
Expand All @@ -75,13 +75,17 @@ func (l *LogsCommand) Run(args []string) int {
}
args = flags.Args()

if len(args) < 1 {
if numArgs := len(args); numArgs < 1 {
if job {
l.Ui.Error("Job ID required")
l.Ui.Error("Job ID required. See help:\n")
} else {
l.Ui.Error("Allocation ID required")
l.Ui.Error("Allocation ID required. See help:\n")
}

l.Ui.Error(l.Help())
return 1
} else if numArgs > 2 {
l.Ui.Error(l.Help())
return 1
}

Expand Down
65 changes: 65 additions & 0 deletions command/logs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package command

import (
"strings"
"testing"

"github.com/mitchellh/cli"
)

func TestLogsCommand_Implements(t *testing.T) {
var _ cli.Command = &LogsCommand{}
}

func TestLogsCommand_Fails(t *testing.T) {
srv, _, url := testServer(t, nil)
defer srv.Stop()

ui := new(cli.MockUi)
cmd := &LogsCommand{Meta: Meta{Ui: ui}}

// Fails on misuse
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
t.Fatalf("expected help output, got: %s", out)
}
ui.ErrorWriter.Reset()

// Fails on connection failure
if code := cmd.Run([]string{"-address=nope", "foobar"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying allocation") {
t.Fatalf("expected failed query error, got: %s", out)
}
ui.ErrorWriter.Reset()

// Fails on missing alloc
if code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
t.Fatalf("expected not found error, got: %s", out)
}
ui.ErrorWriter.Reset()

// Fail on identifier with too few characters
if code := cmd.Run([]string{"-address=" + url, "2"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
t.Fatalf("expected too few characters error, got: %s", out)
}
ui.ErrorWriter.Reset()

// Identifiers with uneven length should produce a query result
if code := cmd.Run([]string{"-address=" + url, "123"}); code != 1 {
t.Fatalf("expected exit 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
t.Fatalf("expected not found error, got: %s", out)
}

}

0 comments on commit 14295a4

Please sign in to comment.