71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
|
package command
|
||
|
|
||
|
import (
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/mitchellh/cli"
|
||
|
)
|
||
|
|
||
|
func TestRunCommand_Implements(t *testing.T) {
|
||
|
var _ cli.Command = &RunCommand{}
|
||
|
}
|
||
|
|
||
|
func TestRunCommand_Fails(t *testing.T) {
|
||
|
ui := new(cli.MockUi)
|
||
|
cmd := &RunCommand{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 when specified file does not exist
|
||
|
if code := cmd.Run([]string{"/unicorns/leprechauns"}); code != 1 {
|
||
|
t.Fatalf("expect exit 1, got: %d", code)
|
||
|
}
|
||
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error parsing") {
|
||
|
t.Fatalf("expect parsing error, got: %s", out)
|
||
|
}
|
||
|
ui.ErrorWriter.Reset()
|
||
|
|
||
|
// Fails on invalid job
|
||
|
fh1, err := ioutil.TempFile("", "nomad")
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %s", err)
|
||
|
}
|
||
|
defer os.Remove(fh1.Name())
|
||
|
if _, err := fh1.WriteString("nope"); err != nil {
|
||
|
t.Fatalf("err: %s", err)
|
||
|
}
|
||
|
if code := cmd.Run([]string{fh1.Name()}); code != 1 {
|
||
|
t.Fatalf("expect exit 1, got: %d", code)
|
||
|
}
|
||
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error parsing") {
|
||
|
t.Fatalf("expect parsing error, got: %s", err)
|
||
|
}
|
||
|
ui.ErrorWriter.Reset()
|
||
|
|
||
|
// Fails on connection failure
|
||
|
fh2, err := ioutil.TempFile("", "nomad")
|
||
|
if err != nil {
|
||
|
t.Fatalf("err: %s", err)
|
||
|
}
|
||
|
defer os.Remove(fh2.Name())
|
||
|
if _, err := fh2.WriteString(`job "hello" {}`); err != nil {
|
||
|
t.Fatalf("err: %s", err)
|
||
|
}
|
||
|
if code := cmd.Run([]string{"-address=nope", fh2.Name()}); code != 1 {
|
||
|
t.Fatalf("expected exit code 1, got: %d", code)
|
||
|
}
|
||
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error submitting job") {
|
||
|
t.Fatalf("expected failed query error, got: %s", out)
|
||
|
}
|
||
|
}
|