Add logs command test

This commit is contained in:
Alex Dadgar 2016-07-20 15:18:54 -07:00
parent 1f600252e7
commit 6e15bdbe36
4 changed files with 84 additions and 11 deletions

View File

@ -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()
}()
@ -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

View File

@ -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.

View File

@ -24,7 +24,7 @@ Usage: nomad logs [options] <alloc-id> <task>
General Options:
` + generalOptionsUsage() + `
` + generalOptionsUsage() + `
Logs Specific Options:
@ -32,7 +32,7 @@ Logs 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.
-f
Causes the output to not stop when the end of the logs are reached, but
@ -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, "")
@ -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
}

65
command/logs_test.go Normal file
View File

@ -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)
}
}