2015-09-25 01:29:46 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2017-02-16 23:18:38 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-09-25 01:29:46 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2022-04-21 20:20:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-25 01:29:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateCommand_Implements(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-21 00:37:28 +00:00
|
|
|
var _ cli.Command = &JobValidateCommand{}
|
2015-09-25 01:29:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommand(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-02-16 23:18:38 +00:00
|
|
|
// Create a server
|
|
|
|
s := testutil.NewTestServer(t, nil)
|
|
|
|
defer s.Stop()
|
2020-06-25 16:51:21 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2020-06-25 16:51:21 +00:00
|
|
|
cmd := &JobValidateCommand{Meta: Meta{Ui: ui, flagAddress: "http://" + s.HTTPAddr}}
|
2017-02-16 23:18:38 +00:00
|
|
|
|
2015-09-25 01:29:46 +00:00
|
|
|
fh, err := ioutil.TempFile("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(fh.Name())
|
|
|
|
_, err = fh.WriteString(`
|
|
|
|
job "job1" {
|
2015-12-18 20:17:50 +00:00
|
|
|
type = "service"
|
2015-09-25 01:29:46 +00:00
|
|
|
datacenters = [ "dc1" ]
|
|
|
|
group "group1" {
|
|
|
|
count = 1
|
|
|
|
task "task1" {
|
|
|
|
driver = "exec"
|
2017-02-16 23:18:38 +00:00
|
|
|
config {
|
|
|
|
command = "/bin/sleep"
|
|
|
|
}
|
2020-10-21 14:17:27 +00:00
|
|
|
resources {
|
2015-09-25 01:29:46 +00:00
|
|
|
cpu = 1000
|
2016-03-10 18:16:35 +00:00
|
|
|
memory = 512
|
2015-09-25 01:29:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if code := cmd.Run([]string{fh.Name()}); code != 0 {
|
|
|
|
t.Fatalf("expect exit 0, got: %d: %s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateCommand_Fails(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-03-21 00:37:28 +00:00
|
|
|
cmd := &JobValidateCommand{Meta: Meta{Ui: ui}}
|
2015-09-25 01:29:46 +00:00
|
|
|
|
|
|
|
// Fails on misuse
|
|
|
|
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
2018-04-18 17:51:17 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
|
2015-09-25 01:29:46 +00:00
|
|
|
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)
|
|
|
|
}
|
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)
|
2015-09-25 01:29:46 +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)
|
|
|
|
}
|
|
|
|
if code := cmd.Run([]string{fh1.Name()}); code != 1 {
|
|
|
|
t.Fatalf("expect exit 1, got: %d", code)
|
|
|
|
}
|
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)
|
2015-09-25 01:29:46 +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)
|
|
|
|
}
|
|
|
|
if code := cmd.Run([]string{fh2.Name()}); code != 1 {
|
|
|
|
t.Fatalf("expect exit 1, got: %d", code)
|
|
|
|
}
|
2017-03-03 23:00:39 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Job validation errors") {
|
2015-09-25 01:29:46 +00:00
|
|
|
t.Fatalf("expect validation error, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|
2016-07-22 20:48:14 +00:00
|
|
|
|
|
|
|
func TestValidateCommand_From_STDIN(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-07-22 20:48:14 +00:00
|
|
|
stdinR, stdinW, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:51:21 +00:00
|
|
|
// Create a server
|
|
|
|
s := testutil.NewTestServer(t, nil)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-03-21 00:37:28 +00:00
|
|
|
cmd := &JobValidateCommand{
|
2020-06-25 16:51:21 +00:00
|
|
|
Meta: Meta{Ui: ui, flagAddress: "http://" + s.HTTPAddr},
|
2016-08-16 10:49:14 +00:00
|
|
|
JobGetter: JobGetter{testStdin: stdinR},
|
2016-07-22 20:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.WriteString(`
|
|
|
|
job "job1" {
|
|
|
|
type = "service"
|
|
|
|
datacenters = [ "dc1" ]
|
|
|
|
group "group1" {
|
|
|
|
count = 1
|
|
|
|
task "task1" {
|
|
|
|
driver = "exec"
|
2017-02-16 23:18:38 +00:00
|
|
|
config {
|
|
|
|
command = "/bin/echo"
|
|
|
|
}
|
2020-10-21 14:17:27 +00:00
|
|
|
resources {
|
2016-07-22 20:48:14 +00:00
|
|
|
cpu = 1000
|
|
|
|
memory = 512
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
args := []string{"-"}
|
|
|
|
if code := cmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("expected exit code 0, got %d: %q", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
}
|
2016-08-06 15:17:34 +00:00
|
|
|
|
|
|
|
func TestValidateCommand_From_URL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-03-21 00:37:28 +00:00
|
|
|
cmd := &JobRunCommand{
|
2016-08-06 15:17:34 +00:00
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"https://example.com/foo/bar"}
|
|
|
|
if code := cmd.Run(args); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got %d: %q", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
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-06 15:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-21 20:20:36 +00:00
|
|
|
|
|
|
|
func TestValidateCommand_JSON(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
_, _, addr := testServer(t, false, nil)
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobValidateCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run([]string{"-address", addr, "-json", "testdata/example-short.json"})
|
|
|
|
|
|
|
|
require.Zerof(t, code, "stdout: %s\nstdout: %s\n",
|
|
|
|
ui.OutputWriter.String(), ui.ErrorWriter.String())
|
|
|
|
|
|
|
|
code = cmd.Run([]string{"-address", addr, "-json", "testdata/example-short-bad.json"})
|
|
|
|
|
|
|
|
require.Equalf(t, 1, code, "stdout: %s\nstdout: %s\n",
|
|
|
|
ui.OutputWriter.String(), ui.ErrorWriter.String())
|
|
|
|
}
|