Skip to content

Commit

Permalink
command: deployment status without a prefix lists deployments (#7821)
Browse files Browse the repository at this point in the history
  • Loading branch information
langmartin authored Apr 28, 2020
1 parent 67c1b93 commit 1bcb8f5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
17 changes: 14 additions & 3 deletions command/deployment_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ func (c *DeploymentStatusCommand) Run(args []string) int {

// Check that we got exactly one argument
args = flags.Args()
if l := len(args); l != 1 {
if l := len(args); l > 1 {
c.Ui.Error("This command takes one argument: <deployment id>")
c.Ui.Error(commandErrorText(c))
return 1
}

dID := args[0]

// Truncate the id unless full length is requested
length := shortId
if verbose {
Expand All @@ -106,7 +104,20 @@ func (c *DeploymentStatusCommand) Run(args []string) int {
return 1
}

// List if no arguments are provided
if len(args) == 0 {
deploys, _, err := client.Deployments().List(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving deployments: %s", err))
return 1
}

c.Ui.Output(formatDeployments(deploys, length))
return 0
}

// Do a prefix lookup
dID := args[0]
deploy, possible, err := getDeployment(client.Deployments(), dID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving deployment: %s", err))
Expand Down
29 changes: 16 additions & 13 deletions command/deployment_status_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package command

import (
"strings"
"testing"

"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDeploymentStatusCommand_Implements(t *testing.T) {
Expand All @@ -21,20 +21,23 @@ func TestDeploymentStatusCommand_Fails(t *testing.T) {
cmd := &DeploymentStatusCommand{Meta: Meta{Ui: ui}}

// Fails on misuse
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
t.Fatalf("expected help output, got: %s", out)
}
code := cmd.Run([]string{"some", "bad", "args"})
require.Equal(t, 1, code)
out := ui.ErrorWriter.String()
require.Contains(t, out, commandErrorText(cmd))
ui.ErrorWriter.Reset()

if code := cmd.Run([]string{"-address=nope", "12"}); code != 1 {
t.Fatalf("expected exit code 1, got: %d", code)
}
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error retrieving deployment") {
t.Fatalf("expected failed query error, got: %s", out)
}
code = cmd.Run([]string{"-address=nope", "12"})
require.Equal(t, 1, code)
out = ui.ErrorWriter.String()
require.Contains(t, out, "Error retrieving deployment")
ui.ErrorWriter.Reset()

code = cmd.Run([]string{"-address=nope"})
require.Equal(t, 1, code)
out = ui.ErrorWriter.String()
// "deployments" indicates that we attempted to list all deployments
require.Contains(t, out, "Error retrieving deployments")
ui.ErrorWriter.Reset()
}

Expand Down

0 comments on commit 1bcb8f5

Please sign in to comment.