2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-17 00:35:58 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-12-21 22:21:48 +00:00
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-09-17 00:35:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-09 20:00:04 +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"
|
2022-12-21 22:21:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2022-12-16 23:46:58 +00:00
|
|
|
"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"
|
2022-12-21 22:21:48 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2015-09-17 00:35:58 +00:00
|
|
|
)
|
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
var _ cli.Command = (*JobStopCommand)(nil)
|
2015-09-17 00:35:58 +00:00
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
func TestStopCommand_multi(t *testing.T) {
|
2022-12-16 23:46:58 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
srv, _, addr := testServer(t, true, func(c *agent.Config) {
|
|
|
|
c.DevMode = true
|
|
|
|
})
|
2022-12-16 23:46:58 +00:00
|
|
|
defer srv.Shutdown()
|
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
// the number of jobs we want to run
|
|
|
|
numJobs := 10
|
2022-12-16 23:46:58 +00:00
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
// create and run a handful of jobs
|
|
|
|
jobIDs := make([]string, 0, numJobs)
|
|
|
|
for i := 0; i < numJobs; i++ {
|
2022-12-16 23:46:58 +00:00
|
|
|
jobID := uuid.Generate()
|
|
|
|
jobIDs = append(jobIDs, jobID)
|
2022-12-21 22:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jobFilePath := func(jobID string) string {
|
|
|
|
return filepath.Join(os.TempDir(), jobID+".nomad")
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup job files we will create
|
|
|
|
t.Cleanup(func() {
|
|
|
|
for _, jobID := range jobIDs {
|
|
|
|
_ = os.Remove(jobFilePath(jobID))
|
|
|
|
}
|
|
|
|
})
|
2022-12-16 23:46:58 +00:00
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
// record cli output
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
|
|
|
|
for _, jobID := range jobIDs {
|
2022-12-16 23:46:58 +00:00
|
|
|
job := testJob(jobID)
|
2022-12-21 22:21:48 +00:00
|
|
|
job.TaskGroups[0].Tasks[0].Resources.MemoryMB = pointer.Of(16)
|
|
|
|
job.TaskGroups[0].Tasks[0].Resources.DiskMB = pointer.Of(32)
|
|
|
|
job.TaskGroups[0].Tasks[0].Resources.CPU = pointer.Of(10)
|
2022-12-16 23:46:58 +00:00
|
|
|
job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "30s",
|
|
|
|
}
|
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
jobJSON, err := json.MarshalIndent(job, "", " ")
|
|
|
|
must.NoError(t, err)
|
|
|
|
|
|
|
|
jobFile := jobFilePath(jobID)
|
|
|
|
err = os.WriteFile(jobFile, []byte(jobJSON), 0o644)
|
|
|
|
must.NoError(t, err)
|
2022-12-16 23:46:58 +00:00
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
cmd := &JobRunCommand{Meta: Meta{Ui: ui}}
|
|
|
|
code := cmd.Run([]string{"-address", addr, "-json", jobFile})
|
|
|
|
must.Zero(t, code,
|
|
|
|
must.Sprintf("job stop stdout: %s", ui.OutputWriter.String()),
|
|
|
|
must.Sprintf("job stop stderr: %s", ui.ErrorWriter.String()),
|
|
|
|
)
|
2022-12-16 23:46:58 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 22:21:48 +00:00
|
|
|
// helper for stopping a list of jobs
|
|
|
|
stop := func(args ...string) (stdout string, stderr string, code int) {
|
|
|
|
cmd := &JobStopCommand{Meta: Meta{Ui: ui}}
|
|
|
|
code = cmd.Run(args)
|
|
|
|
return ui.OutputWriter.String(), ui.ErrorWriter.String(), code
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop all jobs in one command
|
|
|
|
args := []string{"-address", addr, "-detach"}
|
2022-12-16 23:46:58 +00:00
|
|
|
args = append(args, jobIDs...)
|
|
|
|
stdout, stderr, code := stop(args...)
|
2022-12-21 22:21:48 +00:00
|
|
|
must.Zero(t, code,
|
|
|
|
must.Sprintf("job stop stdout: %s", stdout),
|
|
|
|
must.Sprintf("job stop stderr: %s", stderr),
|
|
|
|
)
|
2022-12-16 23:46:58 +00:00
|
|
|
}
|
|
|
|
|
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
|
2022-12-21 22:21:48 +00:00
|
|
|
code := cmd.Run([]string{"-some", "-bad", "-args"})
|
|
|
|
must.One(t, code)
|
|
|
|
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
must.StrContains(t, out, "flag provided but not defined: -some")
|
|
|
|
|
2015-09-17 00:35:58 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
2018-03-12 18:26:37 +00:00
|
|
|
// Fails on nonexistent job ID
|
2022-12-21 22:21:48 +00:00
|
|
|
code = cmd.Run([]string{"-address=" + url, "nope"})
|
|
|
|
must.One(t, code)
|
|
|
|
|
|
|
|
out = ui.ErrorWriter.String()
|
2023-03-03 19:43:20 +00:00
|
|
|
must.StrContains(t, out, "No job(s) with prefix or ID")
|
2022-12-21 22:21:48 +00:00
|
|
|
|
2015-09-17 00:35:58 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fails on connection failure
|
2022-12-21 22:21:48 +00:00
|
|
|
code = cmd.Run([]string{"-address=nope", "nope"})
|
|
|
|
must.One(t, code)
|
|
|
|
|
|
|
|
out = ui.ErrorWriter.String()
|
2023-03-03 19:43:20 +00:00
|
|
|
must.StrContains(t, out, "Error querying job prefix")
|
2015-09-17 00:35:58 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
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()
|
2022-12-21 22:21:48 +00:00
|
|
|
must.NoError(t, 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)
|
2022-12-21 22:21:48 +00:00
|
|
|
must.Len(t, 1, res)
|
|
|
|
must.Eq(t, j.ID, res[0])
|
2017-08-22 20:22:29 +00:00
|
|
|
}
|
2023-03-09 20:00:04 +00:00
|
|
|
|
|
|
|
func TestJobStopCommand_ACL(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
// Start server with ACL enabled.
|
|
|
|
srv, client, url := testServer(t, true, func(c *agent.Config) {
|
|
|
|
c.ACL.Enabled = true
|
|
|
|
})
|
|
|
|
defer srv.Shutdown()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
jobPrefix bool
|
|
|
|
aclPolicy string
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no token",
|
|
|
|
aclPolicy: "",
|
|
|
|
expectedErr: api.PermissionDeniedErrorContent,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing submit-job",
|
|
|
|
aclPolicy: `
|
|
|
|
namespace "default" {
|
|
|
|
capabilities = ["read-job"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
expectedErr: api.PermissionDeniedErrorContent,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing read-job",
|
|
|
|
aclPolicy: `
|
|
|
|
namespace "default" {
|
|
|
|
capabilities = ["submit-job"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
expectedErr: api.PermissionDeniedErrorContent,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "read-job and submit-job allowed",
|
|
|
|
aclPolicy: `
|
|
|
|
namespace "default" {
|
|
|
|
capabilities = ["read-job", "submit-job"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "job prefix requires list-job",
|
|
|
|
jobPrefix: true,
|
|
|
|
aclPolicy: `
|
|
|
|
namespace "default" {
|
|
|
|
capabilities = ["read-job", "submit-job"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
expectedErr: "job not found",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "job prefix works with list-job",
|
|
|
|
jobPrefix: true,
|
|
|
|
aclPolicy: `
|
|
|
|
namespace "default" {
|
|
|
|
capabilities = ["list-jobs", "read-job", "submit-job"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := &JobStopCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{
|
|
|
|
"-address", url,
|
|
|
|
"-yes",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a job.
|
|
|
|
job := mock.MinJob()
|
|
|
|
state := srv.Agent.Server().State()
|
|
|
|
err := state.UpsertJob(structs.MsgTypeTestSetup, uint64(300+i), job)
|
|
|
|
must.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
client.Jobs().Deregister(job.ID, true, &api.WriteOptions{
|
|
|
|
AuthToken: srv.RootToken.SecretID,
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
if tc.aclPolicy != "" {
|
|
|
|
// Create ACL token with test case policy and add it to the
|
|
|
|
// command.
|
|
|
|
policyName := nonAlphaNum.ReplaceAllString(tc.name, "-")
|
|
|
|
token := mock.CreatePolicyAndToken(t, state, uint64(302+i), policyName, tc.aclPolicy)
|
|
|
|
args = append(args, "-token", token.SecretID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add job ID or job ID prefix to the command.
|
|
|
|
if tc.jobPrefix {
|
|
|
|
args = append(args, job.ID[:3])
|
|
|
|
} else {
|
|
|
|
args = append(args, job.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run command.
|
|
|
|
code := cmd.Run(args)
|
|
|
|
if tc.expectedErr == "" {
|
|
|
|
must.Zero(t, code)
|
|
|
|
} else {
|
|
|
|
must.One(t, code)
|
|
|
|
must.StrContains(t, ui.ErrorWriter.String(), tc.expectedErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|