2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-05-17 20:32:47 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-11-06 15:52:32 +00:00
|
|
|
"strconv"
|
2016-05-17 20:32:47 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-30 23:26:23 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
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"
|
2016-05-17 20:32:47 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2022-08-11 13:51:45 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2018-11-06 15:52:32 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-05-17 20:32:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPlanCommand_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 = &JobRunCommand{}
|
2016-05-17 20:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlanCommand_Fails(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-05-17 20:32:47 +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 := &JobPlanCommand{Meta: Meta{Ui: ui, flagAddress: "http://" + s.HTTPAddr}}
|
2017-02-16 23:18:38 +00:00
|
|
|
|
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)
|
|
|
|
}
|
2018-04-18 17:51:17 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
|
2016-05-17 20:32:47 +00:00
|
|
|
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
|
2023-03-08 15:20:04 +00:00
|
|
|
fh1, err := os.CreateTemp("", "nomad")
|
2016-05-17 20:32:47 +00:00
|
|
|
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
|
2023-03-08 15:20:04 +00:00
|
|
|
fh2, err := os.CreateTemp("", "nomad")
|
2016-05-17 20:32:47 +00:00
|
|
|
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)
|
2023-03-08 15:20:04 +00:00
|
|
|
fh3, err := os.CreateTemp("", "nomad")
|
2016-05-17 20:32:47 +00:00
|
|
|
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"
|
2020-10-21 14:17:27 +00:00
|
|
|
resources {
|
2016-05-17 20:32:47 +00:00
|
|
|
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
|
|
|
|
2022-09-01 14:42:08 +00:00
|
|
|
func TestPlanCommand_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 := &JobPlanCommand{Meta: Meta{Ui: ui}}
|
|
|
|
got := cmd.Run([]string{
|
|
|
|
"-hcl1", "-hcl2-strict",
|
|
|
|
"-address", addr,
|
2023-02-10 17:02:29 +00:00
|
|
|
"asset/example-short.nomad.hcl",
|
2022-09-01 14:42:08 +00:00
|
|
|
})
|
|
|
|
// Exit code 1 here means that an alloc will be created, which is
|
|
|
|
// expected.
|
|
|
|
require.Equal(t, 1, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-22 02:59:55 +00:00
|
|
|
func TestPlanCommand_From_STDIN(t *testing.T) {
|
2023-02-10 17:02:29 +00:00
|
|
|
_, _, addr := testServer(t, false, nil)
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-07-22 02:59:55 +00:00
|
|
|
stdinR, stdinW, err := os.Pipe()
|
|
|
|
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 := &JobPlanCommand{
|
2023-02-10 17:02:29 +00:00
|
|
|
Meta: Meta{
|
|
|
|
Ui: ui,
|
|
|
|
flagAddress: addr,
|
|
|
|
},
|
2016-08-16 10:49:14 +00:00
|
|
|
JobGetter: JobGetter{testStdin: stdinR},
|
2016-07-22 02:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
stdinW.WriteString(`
|
|
|
|
job "job1" {
|
2023-02-10 17:02:29 +00:00
|
|
|
datacenters = ["dc1"]
|
2016-07-22 02:59:55 +00:00
|
|
|
type = "service"
|
2023-02-10 17:02:29 +00:00
|
|
|
group "group1" {
|
|
|
|
count = 1
|
|
|
|
task "task1" {
|
|
|
|
driver = "exec"
|
|
|
|
resources {
|
|
|
|
cpu = 100
|
|
|
|
memory = 100
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-22 02:59:55 +00:00
|
|
|
}`)
|
|
|
|
stdinW.Close()
|
|
|
|
}()
|
|
|
|
|
2023-02-10 17:02:29 +00:00
|
|
|
args := []string{"-address", addr, "-"}
|
|
|
|
code := cmd.Run(args)
|
|
|
|
must.Eq(t, 1, code, must.Sprintf("expected exit code 1, got %d: %q", code, ui.ErrorWriter.String()))
|
|
|
|
must.Eq(t, "", ui.ErrorWriter.String(), must.Sprintf("expected no stderr output, got:\n%s", ui.ErrorWriter.String()))
|
2016-07-22 02:59:55 +00:00
|
|
|
}
|
2016-08-03 10:59:06 +00:00
|
|
|
|
2022-08-11 13:51:45 +00:00
|
|
|
func TestPlanCommand_From_Files(t *testing.T) {
|
|
|
|
|
|
|
|
// Create a Vault server
|
|
|
|
v := testutil.NewTestVault(t)
|
|
|
|
defer v.Stop()
|
|
|
|
|
|
|
|
// Create a Nomad server
|
|
|
|
s := testutil.NewTestServer(t, func(c *testutil.TestServerConfig) {
|
|
|
|
c.Vault.Address = v.HTTPAddr
|
|
|
|
c.Vault.Enabled = true
|
|
|
|
c.Vault.AllowUnauthenticated = false
|
|
|
|
c.Vault.Token = v.RootToken
|
|
|
|
})
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
t.Run("fail to place", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobPlanCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{"-address", "http://" + s.HTTPAddr, "testdata/example-basic.nomad"}
|
|
|
|
code := cmd.Run(args)
|
|
|
|
require.Equal(t, 1, code) // no client running, fail to place
|
|
|
|
must.StrContains(t, ui.OutputWriter.String(), "WARNING: Failed to place all allocations.")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("vault no token", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobPlanCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{"-address", "http://" + s.HTTPAddr, "testdata/example-vault.nomad"}
|
|
|
|
code := cmd.Run(args)
|
|
|
|
must.Eq(t, 255, code)
|
|
|
|
must.StrContains(t, ui.ErrorWriter.String(), "* Vault used in the job but missing Vault token")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("vault bad token via flag", func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobPlanCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{"-address", "http://" + s.HTTPAddr, "-vault-token=abc123", "testdata/example-vault.nomad"}
|
|
|
|
code := cmd.Run(args)
|
|
|
|
must.Eq(t, 255, code)
|
|
|
|
must.StrContains(t, ui.ErrorWriter.String(), "* bad token")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("vault bad token via env", func(t *testing.T) {
|
|
|
|
t.Setenv("VAULT_TOKEN", "abc123")
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobPlanCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{"-address", "http://" + s.HTTPAddr, "testdata/example-vault.nomad"}
|
|
|
|
code := cmd.Run(args)
|
|
|
|
must.Eq(t, 255, code)
|
|
|
|
must.StrContains(t, ui.ErrorWriter.String(), "* bad token")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-03 10:59:06 +00:00
|
|
|
func TestPlanCommand_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 := &JobPlanCommand{
|
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
|
|
|
}
|
|
|
|
}
|
2018-10-30 23:26:23 +00:00
|
|
|
|
2023-02-10 17:02:29 +00:00
|
|
|
func TestPlanCommand_Preemptions(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-10-30 23:26:23 +00:00
|
|
|
cmd := &JobPlanCommand{Meta: Meta{Ui: ui}}
|
2018-11-06 15:52:32 +00:00
|
|
|
require := require.New(t)
|
2018-10-30 23:26:23 +00:00
|
|
|
|
|
|
|
// Only one preempted alloc
|
|
|
|
resp1 := &api.JobPlanResponse{
|
|
|
|
Annotations: &api.PlanAnnotations{
|
|
|
|
PreemptedAllocs: []*api.AllocationListStub{
|
|
|
|
{
|
|
|
|
ID: "alloc1",
|
|
|
|
JobID: "jobID1",
|
|
|
|
TaskGroup: "meta",
|
|
|
|
JobType: "batch",
|
|
|
|
Namespace: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.addPreemptions(resp1)
|
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
require.Contains(out, "Alloc ID")
|
|
|
|
require.Contains(out, "alloc1")
|
|
|
|
|
|
|
|
// Less than 10 unique job ids
|
|
|
|
var preemptedAllocs []*api.AllocationListStub
|
|
|
|
for i := 0; i < 12; i++ {
|
|
|
|
job_id := "job" + strconv.Itoa(i%4)
|
|
|
|
alloc := &api.AllocationListStub{
|
|
|
|
ID: "alloc",
|
|
|
|
JobID: job_id,
|
|
|
|
TaskGroup: "meta",
|
|
|
|
JobType: "batch",
|
|
|
|
Namespace: "test",
|
|
|
|
}
|
|
|
|
preemptedAllocs = append(preemptedAllocs, alloc)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp2 := &api.JobPlanResponse{
|
|
|
|
Annotations: &api.PlanAnnotations{
|
|
|
|
PreemptedAllocs: preemptedAllocs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
cmd.addPreemptions(resp2)
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
require.Contains(out, "Job ID")
|
|
|
|
require.Contains(out, "Namespace")
|
|
|
|
|
|
|
|
// More than 10 unique job IDs
|
|
|
|
preemptedAllocs = make([]*api.AllocationListStub, 0)
|
2018-11-08 15:04:49 +00:00
|
|
|
var job_type string
|
2018-10-30 23:26:23 +00:00
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
job_id := "job" + strconv.Itoa(i)
|
|
|
|
if i%2 == 0 {
|
|
|
|
job_type = "service"
|
|
|
|
} else {
|
|
|
|
job_type = "batch"
|
|
|
|
}
|
|
|
|
alloc := &api.AllocationListStub{
|
|
|
|
ID: "alloc",
|
|
|
|
JobID: job_id,
|
|
|
|
TaskGroup: "meta",
|
|
|
|
JobType: job_type,
|
|
|
|
Namespace: "test",
|
|
|
|
}
|
|
|
|
preemptedAllocs = append(preemptedAllocs, alloc)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp3 := &api.JobPlanResponse{
|
|
|
|
Annotations: &api.PlanAnnotations{
|
|
|
|
PreemptedAllocs: preemptedAllocs,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
cmd.addPreemptions(resp3)
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
require.Contains(out, "Job Type")
|
|
|
|
require.Contains(out, "batch")
|
|
|
|
require.Contains(out, "service")
|
|
|
|
}
|
2022-04-21 20:20:36 +00:00
|
|
|
|
2023-02-10 17:02:29 +00:00
|
|
|
func TestPlanCommand_JSON(t *testing.T) {
|
2022-04-21 20:20:36 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobPlanCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-address=http://nope",
|
|
|
|
"-json",
|
|
|
|
"testdata/example-short.json",
|
|
|
|
}
|
|
|
|
code := cmd.Run(args)
|
|
|
|
require.Equal(t, 255, code)
|
|
|
|
require.Contains(t, ui.ErrorWriter.String(), "Error during plan: Put")
|
|
|
|
}
|