2016-05-17 20:32:47 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-02-16 23:18:38 +00:00
|
|
|
"fmt"
|
2016-05-17 20:32:47 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-16 23:18:38 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2016-05-17 20:32:47 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPlanCommand_Implements(t *testing.T) {
|
|
|
|
var _ cli.Command = &RunCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlanCommand_Fails(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &PlanCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
2017-02-16 23:18:38 +00:00
|
|
|
// Create a server
|
|
|
|
s := testutil.NewTestServer(t, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
os.Setenv("NOMAD_ADDR", fmt.Sprintf("http://%s", s.HTTPAddr))
|
|
|
|
|
2016-05-17 20:32:47 +00:00
|
|
|
// Fails on misuse
|
2016-08-03 18:09:50 +00:00
|
|
|
if code := cmd.Run([]string{"some", "bad", "args"}); code != 255 {
|
2016-05-17 20:32:47 +00:00
|
|
|
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
|
2016-08-03 18:09:50 +00:00
|
|
|
if code := cmd.Run([]string{"/unicorns/leprechauns"}); code != 255 {
|
2016-08-17 23:26:23 +00:00
|
|
|
t.Fatalf("expect exit 255, got: %d", code)
|
2016-05-17 20:32:47 +00:00
|
|
|
}
|
2016-08-17 08:02:43 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error getting job struct") {
|
|
|
|
t.Fatalf("expect getting job struct error, got: %s", out)
|
2016-05-17 20:32:47 +00:00
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fails on invalid HCL
|
|
|
|
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)
|
|
|
|
}
|
2016-08-03 18:09:50 +00:00
|
|
|
if code := cmd.Run([]string{fh1.Name()}); code != 255 {
|
2016-08-17 23:26:23 +00:00
|
|
|
t.Fatalf("expect exit 255, got: %d", code)
|
2016-05-17 20:32:47 +00:00
|
|
|
}
|
2016-08-17 08:02:43 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error getting job struct") {
|
|
|
|
t.Fatalf("expect parsing error, got: %s", out)
|
2016-05-17 20:32:47 +00:00
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fails on invalid job spec
|
|
|
|
fh2, err := ioutil.TempFile("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(fh2.Name())
|
|
|
|
if _, err := fh2.WriteString(`job "job1" {}`); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-08-03 18:09:50 +00:00
|
|
|
if code := cmd.Run([]string{fh2.Name()}); code != 255 {
|
2016-08-17 23:26:23 +00:00
|
|
|
t.Fatalf("expect exit 255, got: %d", code)
|
2016-05-17 20:32:47 +00:00
|
|
|
}
|
2017-02-16 23:18:38 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error during plan") {
|
2016-05-17 20:32:47 +00:00
|
|
|
t.Fatalf("expect validation error, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fails on connection failure (requires a valid job)
|
|
|
|
fh3, err := ioutil.TempFile("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(fh3.Name())
|
|
|
|
_, err = fh3.WriteString(`
|
|
|
|
job "job1" {
|
|
|
|
type = "service"
|
|
|
|
datacenters = [ "dc1" ]
|
|
|
|
group "group1" {
|
|
|
|
count = 1
|
|
|
|
task "task1" {
|
|
|
|
driver = "exec"
|
|
|
|
resources = {
|
|
|
|
cpu = 1000
|
|
|
|
memory = 512
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2016-08-03 18:09:50 +00:00
|
|
|
if code := cmd.Run([]string{"-address=nope", fh3.Name()}); code != 255 {
|
2016-08-17 23:26:23 +00:00
|
|
|
t.Fatalf("expected exit code 255, got: %d", code)
|
2016-05-17 20:32:47 +00:00
|
|
|
}
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error during plan") {
|
|
|
|
t.Fatalf("expected failed query error, got: %s", out)
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 02:59:55 +00:00
|
|
|
|
|
|
|
func TestPlanCommand_From_STDIN(t *testing.T) {
|
|
|
|
stdinR, stdinW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
cmd := &PlanCommand{
|
2016-08-16 10:49:14 +00:00
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
JobGetter: JobGetter{testStdin: stdinR},
|
2016-07-22 02:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.WriteString(`
|
|
|
|
job "job1" {
|
|
|
|
type = "service"
|
|
|
|
datacenters = [ "dc1" ]
|
|
|
|
group "group1" {
|
|
|
|
count = 1
|
|
|
|
task "task1" {
|
|
|
|
driver = "exec"
|
|
|
|
resources = {
|
|
|
|
cpu = 1000
|
|
|
|
memory = 512
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := []string{"-"}
|
2016-08-03 18:09:50 +00:00
|
|
|
if code := cmd.Run(args); code != 255 {
|
2016-08-17 08:02:43 +00:00
|
|
|
t.Fatalf("expected exit code 255, got %d: %q", code, ui.ErrorWriter.String())
|
2016-07-22 02:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "connection refused") {
|
2016-08-17 08:02:43 +00:00
|
|
|
t.Fatalf("expected connection refused error, got: %s", out)
|
2016-07-22 02:59:55 +00:00
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|
2016-08-03 10:59:06 +00:00
|
|
|
|
|
|
|
func TestPlanCommand_From_URL(t *testing.T) {
|
|
|
|
ui := new(cli.MockUi)
|
2016-08-17 23:26:23 +00:00
|
|
|
cmd := &PlanCommand{
|
2016-08-03 10:59:06 +00:00
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"https://example.com/foo/bar"}
|
2016-08-17 23:26:23 +00:00
|
|
|
if code := cmd.Run(args); code != 255 {
|
|
|
|
t.Fatalf("expected exit code 255, got %d: %q", code, ui.ErrorWriter.String())
|
2016-08-03 10:59:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 04:28:17 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error getting jobfile") {
|
|
|
|
t.Fatalf("expected error getting jobfile, got: %s", out)
|
2016-08-03 10:59:06 +00:00
|
|
|
}
|
|
|
|
}
|