2015-09-16 01:22:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-21 20:20:36 +00:00
|
|
|
"io"
|
2015-09-16 01:22:51 +00:00
|
|
|
"io/ioutil"
|
2022-04-21 20:20:36 +00:00
|
|
|
"net/http"
|
2015-09-16 01:22:51 +00:00
|
|
|
"os"
|
2022-04-21 20:20:36 +00:00
|
|
|
"path/filepath"
|
2015-09-16 01:22:51 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-04-21 20:20:36 +00:00
|
|
|
"time"
|
2015-09-16 01:22:51 +00:00
|
|
|
|
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-16 01:22:51 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2022-04-21 20:20:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-16 01:22:51 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 20:20:36 +00:00
|
|
|
var _ cli.Command = (*JobRunCommand)(nil)
|
2015-09-16 01:22:51 +00:00
|
|
|
|
2016-03-26 01:24:51 +00:00
|
|
|
func TestRunCommand_Output_Json(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{Meta: Meta{Ui: ui}}
|
2016-03-26 01:24:51 +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" {
|
|
|
|
type = "service"
|
|
|
|
datacenters = [ "dc1" ]
|
|
|
|
group "group1" {
|
|
|
|
count = 1
|
|
|
|
task "task1" {
|
|
|
|
driver = "exec"
|
2020-10-21 14:17:27 +00:00
|
|
|
resources {
|
2016-03-26 01:24:51 +00:00
|
|
|
cpu = 1000
|
|
|
|
memory = 512
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if code := cmd.Run([]string{"-output", fh.Name()}); code != 0 {
|
|
|
|
t.Fatalf("expected exit code 0, got: %d", code)
|
|
|
|
}
|
2017-02-22 22:15:22 +00:00
|
|
|
if out := ui.OutputWriter.String(); !strings.Contains(out, `"Type": "service",`) {
|
2016-03-26 01:24:51 +00:00
|
|
|
t.Fatalf("Expected JSON output: %v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 14:42:08 +00:00
|
|
|
func TestRunCommand_hcl1_hcl2_strict(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
_, _, addr := testServer(t, false, nil)
|
|
|
|
|
|
|
|
t.Run("-hcl1 implies -hcl2-strict is false", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobRunCommand{Meta: Meta{Ui: ui}}
|
|
|
|
got := cmd.Run([]string{
|
|
|
|
"-hcl1", "-hcl2-strict",
|
|
|
|
"-address", addr,
|
|
|
|
"-detach",
|
|
|
|
"assets/example-short.nomad",
|
|
|
|
})
|
|
|
|
require.Equal(t, 0, got, ui.ErrorWriter.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
func TestRunCommand_Fails(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-09-16 01:22:51 +00:00
|
|
|
|
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 := &JobRunCommand{Meta: Meta{Ui: ui, flagAddress: "http://" + s.HTTPAddr}}
|
2017-02-16 23:18:38 +00:00
|
|
|
|
2015-09-16 01:22:51 +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-16 01:22:51 +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-16 01:22:51 +00:00
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
2015-09-21 22:08:29 +00:00
|
|
|
// Fails on invalid HCL
|
2015-09-16 01:22:51 +00:00
|
|
|
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-16 01:22:51 +00:00
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
2015-09-21 22:08:29 +00:00
|
|
|
// Fails on invalid job spec
|
2015-09-16 01:22:51 +00:00
|
|
|
fh2, err := ioutil.TempFile("", "nomad")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(fh2.Name())
|
2015-09-21 22:08:29 +00:00
|
|
|
if _, err := fh2.WriteString(`job "job1" {}`); err != nil {
|
2015-09-16 01:22:51 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-09-21 22:08:29 +00:00
|
|
|
if code := cmd.Run([]string{fh2.Name()}); code != 1 {
|
|
|
|
t.Fatalf("expect exit 1, got: %d", code)
|
|
|
|
}
|
2017-02-16 23:18:38 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error submitting job") {
|
2015-09-21 22:08:29 +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" {
|
2015-12-18 20:17:50 +00:00
|
|
|
type = "service"
|
2015-09-21 22:08:29 +00:00
|
|
|
datacenters = [ "dc1" ]
|
|
|
|
group "group1" {
|
|
|
|
count = 1
|
|
|
|
task "task1" {
|
|
|
|
driver = "exec"
|
2020-10-21 14:17:27 +00:00
|
|
|
resources {
|
2015-09-21 22:08:29 +00:00
|
|
|
cpu = 1000
|
2016-03-10 18:16:35 +00:00
|
|
|
memory = 512
|
2015-09-21 22:08:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if code := cmd.Run([]string{"-address=nope", fh3.Name()}); code != 1 {
|
2015-09-16 01:22:51 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-06-08 23:48:02 +00:00
|
|
|
|
|
|
|
// Fails on invalid check-index (requires a valid job)
|
|
|
|
if code := cmd.Run([]string{"-check-index=bad", fh3.Name()}); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "parsing check-index") {
|
|
|
|
t.Fatalf("expected parse error, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
2015-09-16 01:22:51 +00:00
|
|
|
}
|
2016-06-21 20:46:42 +00:00
|
|
|
|
|
|
|
func TestRunCommand_From_STDIN(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-06-22 02:59:04 +00:00
|
|
|
stdinR, stdinW, err := os.Pipe()
|
2016-06-21 20:46:42 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-03-21 00:37:28 +00:00
|
|
|
cmd := &JobRunCommand{
|
2016-08-16 10:49:14 +00:00
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
JobGetter: JobGetter{testStdin: stdinR},
|
2016-06-22 02:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.WriteString(`
|
2016-06-21 20:46:42 +00:00
|
|
|
job "job1" {
|
2016-06-22 02:59:04 +00:00
|
|
|
type = "service"
|
|
|
|
datacenters = [ "dc1" ]
|
|
|
|
group "group1" {
|
2016-06-21 20:46:42 +00:00
|
|
|
count = 1
|
|
|
|
task "task1" {
|
|
|
|
driver = "exec"
|
2020-10-21 14:17:27 +00:00
|
|
|
resources {
|
2016-06-21 20:46:42 +00:00
|
|
|
cpu = 1000
|
|
|
|
memory = 512
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`)
|
2016-06-22 02:59:04 +00:00
|
|
|
stdinW.Close()
|
|
|
|
}()
|
2016-06-21 20:46:42 +00:00
|
|
|
|
2017-10-24 00:57:00 +00:00
|
|
|
args := []string{"-address=nope", "-"}
|
2016-06-23 15:28:17 +00:00
|
|
|
if code := cmd.Run(args); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got %d: %q", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-10-24 00:57:00 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error submitting job") {
|
|
|
|
t.Fatalf("expected submission error, got: %s", out)
|
2016-06-21 20:46:42 +00:00
|
|
|
}
|
2016-06-23 15:28:17 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
2016-06-21 20:46:42 +00:00
|
|
|
}
|
2016-08-03 10:59:06 +00:00
|
|
|
|
|
|
|
func TestRunCommand_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-03 10:59:06 +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-03 10:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-21 20:20:36 +00:00
|
|
|
|
|
|
|
// TestRunCommand_JSON asserts that `nomad job run -json` accepts JSON jobs
|
|
|
|
// with or without a top level Job key.
|
|
|
|
func TestRunCommand_JSON(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
run := func(args ...string) (stdout string, stderr string, code int) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobRunCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
t.Logf("run: nomad job run %s", strings.Join(args, " "))
|
|
|
|
code = cmd.Run(args)
|
|
|
|
return ui.OutputWriter.String(), ui.ErrorWriter.String(), code
|
|
|
|
}
|
|
|
|
|
|
|
|
// Agent startup is slow, do some work while we wait
|
|
|
|
agentReady := make(chan string)
|
|
|
|
go func() {
|
|
|
|
_, _, addr := testServer(t, false, nil)
|
|
|
|
agentReady <- addr
|
|
|
|
}()
|
|
|
|
|
|
|
|
// First convert HCL -> JSON with -output
|
|
|
|
stdout, stderr, code := run("-output", "assets/example-short.nomad")
|
|
|
|
require.Zero(t, code, stderr)
|
|
|
|
require.Empty(t, stderr)
|
|
|
|
require.NotEmpty(t, stdout)
|
|
|
|
t.Logf("run -output==> %s...", stdout[:12])
|
|
|
|
|
|
|
|
jsonFile := filepath.Join(t.TempDir(), "redis.json")
|
|
|
|
require.NoError(t, os.WriteFile(jsonFile, []byte(stdout), 0o640))
|
|
|
|
|
|
|
|
// Wait for agent to start and get its address
|
|
|
|
addr := ""
|
|
|
|
select {
|
|
|
|
case addr = <-agentReady:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatalf("timed out waiting for agent to start")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submit JSON
|
|
|
|
stdout, stderr, code = run("-detach", "-address", addr, "-json", jsonFile)
|
|
|
|
require.Zero(t, code, stderr)
|
|
|
|
require.Empty(t, stderr)
|
|
|
|
|
|
|
|
// Read the JSON from the API as it omits the Job envelope and
|
|
|
|
// therefore differs from -output
|
|
|
|
resp, err := http.Get(addr + "/v1/job/example")
|
|
|
|
require.NoError(t, err)
|
|
|
|
buf, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, resp.Body.Close())
|
|
|
|
require.NotEmpty(t, buf)
|
|
|
|
t.Logf("/v1/job/example==> %s...", string(buf[:12]))
|
|
|
|
require.NoError(t, os.WriteFile(jsonFile, buf, 0o640))
|
|
|
|
|
|
|
|
// Submit JSON
|
|
|
|
stdout, stderr, code = run("-detach", "-address", addr, "-json", jsonFile)
|
|
|
|
require.Zerof(t, code, "stderr: %s\njson: %s\n", stderr, string(buf))
|
|
|
|
require.Empty(t, stderr)
|
|
|
|
require.NotEmpty(t, stdout)
|
|
|
|
}
|