github.com/hernad/nomad@v1.6.112/command/deployment_unblock_test.go (about)

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