From 75e8f28f8feee9743e77e9a5d1285e5471794c31 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 17 May 2016 13:32:47 -0700 Subject: [PATCH] tests --- command/plan.go | 15 +++---- command/plan_test.go | 103 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 command/plan_test.go diff --git a/command/plan.go b/command/plan.go index 0b7fc51c5..677bfcfb8 100644 --- a/command/plan.go +++ b/command/plan.go @@ -13,9 +13,9 @@ import ( const ( jobModifyIndexHelp = `To submit the job with version verification run: -nomad run -verify %d %s +nomad run -check-index %d %s -When running the job with the verify flag, the job will only be run if the +When running the job with the check-index flag, the job will only be run if the server side version matches the the job modify index returned. If the index has changed, another user has modified the job and the plan's results are potentially invalid.` @@ -36,14 +36,12 @@ Usage: nomad plan [options] successfully and how it would affect existing allocations. A job modify index is returned with the plan. This value can be used when - submitting the job using "nomad run -verify", which will check that the job + submitting the job using "nomad run -check-index", which will check that the job was not modified between the plan and run command before invoking the - scheduler. This ensures that the plan reflects the same modifications to the - job as the run. + scheduler. This ensures the job has not been modified since the plan. - An annotated diff between the submitted job and the remote state is also - displayed. This diff gives insight onto what the scheduler will attempt to do - and why. + A structured diff between the local and remote job is displayed to + give insight into what the scheduler will attempt to do and why. General Options: @@ -59,7 +57,6 @@ Run Options: -verbose Increase diff verbosity. - ` return strings.TrimSpace(helpText) } diff --git a/command/plan_test.go b/command/plan_test.go new file mode 100644 index 000000000..af53826d5 --- /dev/null +++ b/command/plan_test.go @@ -0,0 +1,103 @@ +package command + +import ( + "io/ioutil" + "os" + "strings" + "testing" + + "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}} + + // 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 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) + } + 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 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) + } + if code := cmd.Run([]string{fh2.Name()}); code != 1 { + t.Fatalf("expect exit 1, got: %d", code) + } + if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error validating") { + 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 + disk = 150 + memory = 512 + } + } + } +}`) + if err != nil { + t.Fatalf("err: %s", err) + } + if code := cmd.Run([]string{"-address=nope", fh3.Name()}); code != 1 { + t.Fatalf("expected exit code 1, got: %d", code) + } + if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error during plan") { + t.Fatalf("expected failed query error, got: %s", out) + } +}