nomad stop

This commit is contained in:
Alex Dadgar
2017-08-22 13:22:29 -07:00
parent fcacdd5c73
commit 7e0302376c
3 changed files with 56 additions and 1 deletions

View File

@@ -67,7 +67,7 @@ func TestInspectCommand_AutocompleteArgs(t *testing.T) {
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &JobStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
cmd := &InspectCommand{Meta: Meta{Ui: ui, flagAddress: url}}
state := srv.Agent.Server().State()
j := mock.Job()

View File

@@ -3,6 +3,9 @@ package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type StopCommand struct {
@@ -48,6 +51,25 @@ func (c *StopCommand) Synopsis() string {
return "Stop a running job"
}
func (c *StopCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *StopCommand) AutocompleteArgs() complete.Predictor {
client, _ := c.Meta.Client()
return complete.PredictFunc(func(a complete.Args) []string {
if len(a.Completed) > 1 {
return nil
}
resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
if err != nil {
return []string{}
}
return resp.Matches[contexts.Jobs]
})
}
func (c *StopCommand) Run(args []string) int {
var detach, purge, verbose, autoYes bool

View File

@@ -4,7 +4,10 @@ import (
"strings"
"testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
)
func TestStopCommand_Implements(t *testing.T) {
@@ -46,3 +49,33 @@ func TestStopCommand_Fails(t *testing.T) {
t.Fatalf("expected failed query error, got: %s", out)
}
}
func TestStopCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &StopCommand{Meta: Meta{Ui: ui, flagAddress: url}}
state := srv.Agent.Server().State()
j := mock.Job()
assert.Nil(state.UpsertJob(1000, j))
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])
// Autocomplete should only complete once
args = complete.Args{Last: prefix, Completed: []string{prefix, "a", "b"}}
predictor = cmd.AutocompleteArgs()
res = predictor.Predict(args)
assert.Nil(res)
}