From 14295a4d42636e92cb9578959c1d2573e99fad3c Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 20 Jul 2016 15:18:54 -0700 Subject: [PATCH] Add logs command test --- command/agent/fs_endpoint.go | 12 ++++--- command/fs.go | 2 +- command/logs.go | 16 +++++---- command/logs_test.go | 65 ++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 command/logs_test.go diff --git a/command/agent/fs_endpoint.go b/command/agent/fs_endpoint.go index ab50481072d..ccc55218112 100644 --- a/command/agent/fs_endpoint.go +++ b/command/agent/fs_endpoint.go @@ -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 diff --git a/command/fs.go b/command/fs.go index 0ab861f1f4f..a5c91b51dc1 100644 --- a/command/fs.go +++ b/command/fs.go @@ -50,7 +50,7 @@ FS Specific Options: Show full information. -job - 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. diff --git a/command/logs.go b/command/logs.go index e5aa6297a40..6f105202c0f 100644 --- a/command/logs.go +++ b/command/logs.go @@ -24,7 +24,7 @@ Usage: nomad logs [options] General Options: - ` + generalOptionsUsage() + ` + ` + generalOptionsUsage() + ` Logs Specific Options: @@ -32,7 +32,7 @@ Logs Specific Options: Show full information. -job - 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 } diff --git a/command/logs_test.go b/command/logs_test.go new file mode 100644 index 00000000000..2400b6302d0 --- /dev/null +++ b/command/logs_test.go @@ -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) + } + +}