open-nomad/command/alloc_exec_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

283 lines
6.7 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2019-04-28 21:35:17 +00:00
package command
import (
"bytes"
"fmt"
"strings"
"testing"
"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"
"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) {
ci.Parallel(t)
2019-04-28 21:35:17 +00:00
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
2019-04-28 21:35:17 +00:00
cases := []struct {
name string
args []string
expectedError string
}{
{
"alloc id missing",
[]string{},
`An allocation ID is required`,
2019-04-28 21:35:17 +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
},
{
"alloc not found",
2019-04-28 21:35:17 +00:00
[]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C", "/bin/bash"},
`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
},
{
"job not found",
2019-04-28 21:35:17 +00:00
[]string{"-address=" + url, "-job", "example", "/bin/bash"},
`job "example" doesn't exist`,
},
{
"command missing",
[]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"},
`A command is required`,
2019-04-28 21:35:17 +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
},
{
"escape char too long",
[]string{"-address=" + url, "-e", "es", "26470238-5CF2-438F-8772-DC67CFB0705C", "/bin/bash"},
`-e requires 'none' or a single character`,
},
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)
must.One(t, code)
2019-04-28 21:35:17 +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
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)
2019-04-28 21:35:17 +00:00
resp, _, err := client.Jobs().Register(job1, nil)
must.NoError(t, err)
2019-04-28 21:35:17 +00:00
code := waitForSuccess(ui, client, fullId, t, resp.EvalID)
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
}
}
must.NotEq(t, "", allocId1)
2019-04-28 21:35:17 +00:00
// by alloc
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
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) {
ci.Parallel(t)
2019-04-28 21:35:17 +00:00
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
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()
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)
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) {
ci.Parallel(t)
2019-04-28 21:35:17 +00:00
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
2019-04-28 21:35:17 +00:00
// Wait for a node to be ready
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)
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)
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
}, 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})
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})
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
}