2015-09-17 00:35:58 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-12-16 23:46:58 +00:00
|
|
|
"time"
|
2015-09-17 00:35:58 +00:00
|
|
|
|
2022-12-16 23:46:58 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-12-16 23:46:58 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2017-08-22 20:22:29 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2020-10-19 13:30:15 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-09-17 00:35:58 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-08-22 20:22:29 +00:00
|
|
|
"github.com/posener/complete"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-12-16 23:46:58 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-09-17 00:35:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStopCommand_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 = &JobStopCommand{}
|
2015-09-17 00:35:58 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:46:58 +00:00
|
|
|
func TestStopCommand_JSON(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
stop := func(args ...string) (stdout string, stderr string, code int) {
|
|
|
|
cmd := &JobStopCommand{
|
|
|
|
Meta: Meta{Ui: ui},
|
|
|
|
}
|
|
|
|
t.Logf("run: nomad job stop %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)
|
|
|
|
var srv *agent.TestAgent
|
|
|
|
var client *api.Client
|
|
|
|
go func() {
|
|
|
|
var addr string
|
|
|
|
srv, client, addr = testServer(t, false, nil)
|
|
|
|
agentReady <- addr
|
|
|
|
}()
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
// Wait for agent to start and get its address
|
|
|
|
select {
|
|
|
|
case <-agentReady:
|
|
|
|
case <-time.After(20 * time.Second):
|
|
|
|
t.Fatalf("timed out waiting for agent to start")
|
|
|
|
}
|
|
|
|
|
|
|
|
// create and run 10 jobs
|
|
|
|
jobIDs := make([]string, 10)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
jobID := uuid.Generate()
|
|
|
|
jobIDs = append(jobIDs, jobID)
|
|
|
|
|
|
|
|
job := testJob(jobID)
|
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "30s",
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _, err := client.Jobs().Register(job, nil)
|
|
|
|
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
|
|
|
|
t.Fatalf("[DEBUG] waiting for job to register; status code non zero saw %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop all jobs
|
|
|
|
var args []string
|
|
|
|
args = append(args, "-detach")
|
|
|
|
args = append(args, jobIDs...)
|
|
|
|
stdout, stderr, code := stop(args...)
|
|
|
|
t.Logf("[DEBUG] run: nomad job stop stdout/stderr: %s/%s", stdout, stderr)
|
|
|
|
require.Zero(t, code)
|
|
|
|
require.Empty(t, stderr)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:35:58 +00:00
|
|
|
func TestStopCommand_Fails(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-07-21 04:07:32 +00:00
|
|
|
srv, _, url := testServer(t, false, nil)
|
|
|
|
defer srv.Shutdown()
|
2015-09-17 00:35:58 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-03-21 00:37:28 +00:00
|
|
|
cmd := &JobStopCommand{Meta: Meta{Ui: ui}}
|
2015-09-17 00:35:58 +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-17 00:35:58 +00:00
|
|
|
t.Fatalf("expected help output, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Fails on nonexistent job ID
|
2015-09-17 00:35:58 +00:00
|
|
|
if code := cmd.Run([]string{"-address=" + url, "nope"}); code != 1 {
|
|
|
|
t.Fatalf("expect exit 1, got: %d", code)
|
|
|
|
}
|
2015-12-24 10:46:59 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No job(s) with prefix or id") {
|
2015-09-17 00:35:58 +00:00
|
|
|
t.Fatalf("expect not found error, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fails on connection failure
|
|
|
|
if code := cmd.Run([]string{"-address=nope", "nope"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error deregistering job") {
|
|
|
|
t.Fatalf("expected failed query error, got: %s", out)
|
|
|
|
}
|
|
|
|
}
|
2017-08-22 20:22:29 +00:00
|
|
|
|
|
|
|
func TestStopCommand_AutocompleteArgs(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-08-22 20:22:29 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
srv, _, url := testServer(t, true, nil)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-03-21 00:37:28 +00:00
|
|
|
cmd := &JobStopCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
2017-08-22 20:22:29 +00:00
|
|
|
|
2017-08-22 20:33:12 +00:00
|
|
|
// Create a fake job
|
2017-08-22 20:22:29 +00:00
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
j := mock.Job()
|
2020-10-19 13:30:15 +00:00
|
|
|
assert.Nil(state.UpsertJob(structs.MsgTypeTestSetup, 1000, j))
|
2017-08-22 20:22:29 +00:00
|
|
|
|
|
|
|
prefix := j.ID[:len(j.ID)-5]
|
|
|
|
args := complete.Args{Last: prefix}
|
|
|
|
predictor := cmd.AutocompleteArgs()
|
|
|
|
|
|
|
|
res := predictor.Predict(args)
|
|
|
|
assert.Equal(1, len(res))
|
|
|
|
assert.Equal(j.ID, res[0])
|
|
|
|
}
|