github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/deployment_pause_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/ci"
     8  	"github.com/hashicorp/nomad/nomad/mock"
     9  	"github.com/mitchellh/cli"
    10  	"github.com/posener/complete"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestDeploymentPauseCommand_Implements(t *testing.T) {
    15  	ci.Parallel(t)
    16  	var _ cli.Command = &DeploymentPauseCommand{}
    17  }
    18  
    19  func TestDeploymentPauseCommand_Fails(t *testing.T) {
    20  	ci.Parallel(t)
    21  	ui := cli.NewMockUi()
    22  	cmd := &DeploymentPauseCommand{Meta: Meta{Ui: ui}}
    23  
    24  	// Fails on misuse
    25  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    26  		t.Fatalf("expected exit code 1, got: %d", code)
    27  	}
    28  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    29  		t.Fatalf("expected help output, got: %s", out)
    30  	}
    31  	ui.ErrorWriter.Reset()
    32  
    33  	if code := cmd.Run([]string{"-address=nope", "12"}); code != 1 {
    34  		t.Fatalf("expected exit code 1, got: %d", code)
    35  	}
    36  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error retrieving deployment") {
    37  		t.Fatalf("expected failed query error, got: %s", out)
    38  	}
    39  	ui.ErrorWriter.Reset()
    40  }
    41  
    42  func TestDeploymentPauseCommand_AutocompleteArgs(t *testing.T) {
    43  	ci.Parallel(t)
    44  	assert := assert.New(t)
    45  
    46  	srv, _, url := testServer(t, true, nil)
    47  	defer srv.Shutdown()
    48  
    49  	ui := cli.NewMockUi()
    50  	cmd := &DeploymentPauseCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    51  
    52  	// Create a fake deployment
    53  	state := srv.Agent.Server().State()
    54  	d := mock.Deployment()
    55  	assert.Nil(state.UpsertDeployment(1000, d))
    56  
    57  	prefix := d.ID[:5]
    58  	args := complete.Args{Last: prefix}
    59  	predictor := cmd.AutocompleteArgs()
    60  
    61  	res := predictor.Predict(args)
    62  	assert.Equal(1, len(res))
    63  	assert.Equal(d.ID, res[0])
    64  }