2019-04-28 21:35:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2019-04-28 21:35:17 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/posener/complete"
|
2022-08-18 13:51:53 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2019-04-28 21:35:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// static check
|
|
|
|
var _ cli.Command = &AllocExecCommand{}
|
|
|
|
|
|
|
|
func TestAllocExecCommand_Fails(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:35:17 +00:00
|
|
|
srv, client, url := testServer(t, true, nil)
|
2022-08-18 13:51:53 +00:00
|
|
|
defer stopTestAgent(srv)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
2021-01-05 17:33:04 +00:00
|
|
|
"alloc id missing",
|
|
|
|
[]string{},
|
|
|
|
`An allocation ID is required`,
|
2019-04-28 21:35:17 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-05 17:33:04 +00:00
|
|
|
"alloc id too short",
|
|
|
|
[]string{"-address=" + url, "2", "/bin/bash"},
|
|
|
|
`Alloc ID must contain at least two characters`,
|
2019-04-28 21:35:17 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-05 17:33:04 +00:00
|
|
|
"alloc not found",
|
2019-04-28 21:35:17 +00:00
|
|
|
[]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C", "/bin/bash"},
|
2021-01-05 17:33:04 +00:00
|
|
|
`No allocation(s) with prefix or id "26470238-5CF2-438F-8772-DC67CFB0705C"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"alloc not found with odd-length prefix",
|
|
|
|
[]string{"-address=" + url, "26470238-5CF", "/bin/bash"},
|
|
|
|
`No allocation(s) with prefix or id "26470238-5CF"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"job id missing",
|
|
|
|
[]string{"-job"},
|
|
|
|
`A job ID is required`,
|
2019-04-28 21:35:17 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-05 17:33:04 +00:00
|
|
|
"job not found",
|
2019-04-28 21:35:17 +00:00
|
|
|
[]string{"-address=" + url, "-job", "example", "/bin/bash"},
|
|
|
|
`job "example" doesn't exist`,
|
|
|
|
},
|
|
|
|
{
|
2021-01-05 17:33:04 +00:00
|
|
|
"command missing",
|
|
|
|
[]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"},
|
|
|
|
`A command is required`,
|
2019-04-28 21:35:17 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-05 17:33:04 +00:00
|
|
|
"connection failure",
|
|
|
|
[]string{"-address=nope", "26470238-5CF2-438F-8772-DC67CFB0705C", "/bin/bash"},
|
|
|
|
`Error querying allocation`,
|
2019-04-28 21:35:17 +00:00
|
|
|
},
|
2019-05-16 19:50:46 +00:00
|
|
|
{
|
2021-01-05 17:33:04 +00:00
|
|
|
"escape char too long",
|
|
|
|
[]string{"-address=" + url, "-e", "es", "26470238-5CF2-438F-8772-DC67CFB0705C", "/bin/bash"},
|
|
|
|
`-e requires 'none' or a single character`,
|
2019-05-16 19:50:46 +00:00
|
|
|
},
|
2019-04-28 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2019-04-28 21:35:17 +00:00
|
|
|
cmd := &AllocExecCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
code := cmd.Run(c.args)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.One(t, code)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
2022-08-18 13:51:53 +00:00
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
must.StrContains(t, out, c.expectedError)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a node to be ready
|
2022-08-18 13:51:53 +00:00
|
|
|
waitForNodes(t, client)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
t.Run("non existent task", func(t *testing.T) {
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2019-04-28 21:35:17 +00:00
|
|
|
cmd := &AllocExecCommand{Meta: Meta{Ui: ui}}
|
|
|
|
|
|
|
|
jobID := "job1_sfx"
|
|
|
|
job1 := testJob(jobID)
|
2022-08-18 13:51:53 +00:00
|
|
|
|
2019-04-28 21:35:17 +00:00
|
|
|
resp, _, err := client.Jobs().Register(job1, nil)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
|
2019-04-28 21:35:17 +00:00
|
|
|
code := waitForSuccess(ui, client, fullId, t, resp.EvalID)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Zero(t, code)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
// get an alloc id
|
|
|
|
allocId1 := ""
|
|
|
|
if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
|
|
|
|
if len(allocs) > 0 {
|
|
|
|
allocId1 = allocs[0].ID
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 13:51:53 +00:00
|
|
|
must.NotEq(t, "", allocId1)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
// by alloc
|
2022-08-18 13:51:53 +00:00
|
|
|
code = cmd.Run([]string{"-address=" + url, "-task=nonexistenttask1", allocId1, "/bin/bash"})
|
|
|
|
must.One(t, code)
|
|
|
|
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
must.StrContains(t, out, "Could not find task named: nonexistenttask1")
|
|
|
|
|
2019-04-28 21:35:17 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// by jobID
|
2022-08-18 13:51:53 +00:00
|
|
|
code = cmd.Run([]string{"-address=" + url, "-task=nonexistenttask2", "-job", jobID, "/bin/bash"})
|
|
|
|
must.One(t, code)
|
|
|
|
|
|
|
|
out = ui.ErrorWriter.String()
|
|
|
|
must.StrContains(t, out, "Could not find task named: nonexistenttask2")
|
|
|
|
|
2019-04-28 21:35:17 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocExecCommand_AutocompleteArgs(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
srv, _, url := testServer(t, true, nil)
|
2022-08-18 13:51:53 +00:00
|
|
|
defer stopTestAgent(srv)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2019-04-28 21:35:17 +00:00
|
|
|
cmd := &AllocExecCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
|
|
|
|
|
|
|
// Create a fake alloc
|
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
a := mock.Alloc()
|
2022-08-18 13:51:53 +00:00
|
|
|
must.NoError(t, state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{a}))
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
prefix := a.ID[:5]
|
|
|
|
args := complete.Args{Last: prefix}
|
|
|
|
predictor := cmd.AutocompleteArgs()
|
|
|
|
|
|
|
|
res := predictor.Predict(args)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Len(t, 1, res)
|
|
|
|
must.Eq(t, a.ID, res[0])
|
2019-04-28 21:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllocExecCommand_Run(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:35:17 +00:00
|
|
|
srv, client, url := testServer(t, true, nil)
|
2022-08-18 13:51:53 +00:00
|
|
|
defer stopTestAgent(srv)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
// Wait for a node to be ready
|
2022-08-18 13:51:53 +00:00
|
|
|
waitForNodes(t, client)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
jobID := uuid.Generate()
|
|
|
|
job := testJob(jobID)
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "10s",
|
|
|
|
"exec_command": map[string]interface{}{
|
|
|
|
"run_for": "1ms",
|
|
|
|
"exit_code": 21,
|
|
|
|
"stdout_string": "sample stdout output\n",
|
|
|
|
"stderr_string": "sample stderr output\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, _, err := client.Jobs().Register(job, nil)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.NoError(t, err)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
evalUi := cli.NewMockUi()
|
2019-04-28 21:35:17 +00:00
|
|
|
code := waitForSuccess(evalUi, client, fullId, t, resp.EvalID)
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Zero(t, code)
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
allocId := ""
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
allocs, _, err := client.Jobs().Allocations(jobID, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to get allocations: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(allocs) < 0 {
|
|
|
|
return false, fmt.Errorf("no allocations yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc := allocs[0]
|
|
|
|
if alloc.ClientStatus != "running" {
|
|
|
|
return false, fmt.Errorf("alloc is not running yet: %v", alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
allocId = alloc.ID
|
|
|
|
return true, nil
|
2022-08-18 13:51:53 +00:00
|
|
|
}, func(err error) { must.NoError(t, err) })
|
2019-04-28 21:35:17 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
command string
|
|
|
|
stdin string
|
|
|
|
|
|
|
|
stdout string
|
|
|
|
stderr string
|
|
|
|
exitCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic stdout/err",
|
|
|
|
command: "simplecommand",
|
|
|
|
stdin: "",
|
|
|
|
stdout: "sample stdout output",
|
|
|
|
stderr: "sample stderr output",
|
|
|
|
exitCode: 21,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "notty: streamining input",
|
|
|
|
command: "showinput",
|
|
|
|
stdin: "hello from stdin",
|
|
|
|
stdout: "TTY: false\nStdin:\nhello from stdin",
|
|
|
|
exitCode: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run("by id: "+c.name, func(t *testing.T) {
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2019-04-28 21:35:17 +00:00
|
|
|
var stdout, stderr bufferCloser
|
|
|
|
|
|
|
|
cmd := &AllocExecCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
Stdin: strings.NewReader(c.stdin),
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
code = cmd.Run([]string{"-address=" + url, allocId, c.command})
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Eq(t, c.exitCode, code)
|
|
|
|
must.Eq(t, c.stdout, strings.TrimSpace(stdout.String()))
|
|
|
|
must.Eq(t, c.stderr, strings.TrimSpace(stderr.String()))
|
2019-04-28 21:35:17 +00:00
|
|
|
})
|
|
|
|
t.Run("by job: "+c.name, func(t *testing.T) {
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2019-04-28 21:35:17 +00:00
|
|
|
var stdout, stderr bufferCloser
|
|
|
|
|
|
|
|
cmd := &AllocExecCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
Stdin: strings.NewReader(c.stdin),
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
code = cmd.Run([]string{"-address=" + url, "-job", jobID, c.command})
|
2022-08-18 13:51:53 +00:00
|
|
|
must.Eq(t, c.exitCode, code)
|
|
|
|
must.Eq(t, c.stdout, strings.TrimSpace(stdout.String()))
|
|
|
|
must.Eq(t, c.stderr, strings.TrimSpace(stderr.String()))
|
2019-04-28 21:35:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bufferCloser struct {
|
|
|
|
bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bufferCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|