github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/job_promote_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/structs"
     9  
    10  	"github.com/hashicorp/nomad/nomad/mock"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/posener/complete"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestJobPromoteCommand_Implements(t *testing.T) {
    17  	ci.Parallel(t)
    18  	var _ cli.Command = &JobPromoteCommand{}
    19  }
    20  
    21  func TestJobPromoteCommand_Fails(t *testing.T) {
    22  	ci.Parallel(t)
    23  	ui := cli.NewMockUi()
    24  	cmd := &JobPromoteCommand{Meta: Meta{Ui: ui}}
    25  
    26  	// Fails on misuse
    27  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    28  		t.Fatalf("expected exit code 1, got: %d", code)
    29  	}
    30  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    31  		t.Fatalf("expected help output, got: %s", out)
    32  	}
    33  	ui.ErrorWriter.Reset()
    34  
    35  	if code := cmd.Run([]string{"-address=nope", "12"}); code != 1 {
    36  		t.Fatalf("expected exit code 1, got: %d", code)
    37  	}
    38  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error promoting") {
    39  		t.Fatalf("expected failed to promote error, got: %s", out)
    40  	}
    41  	ui.ErrorWriter.Reset()
    42  }
    43  
    44  func TestJobPromoteCommand_AutocompleteArgs(t *testing.T) {
    45  	ci.Parallel(t)
    46  	assert := assert.New(t)
    47  
    48  	srv, _, url := testServer(t, true, nil)
    49  	defer srv.Shutdown()
    50  
    51  	ui := cli.NewMockUi()
    52  	cmd := &JobPromoteCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    53  
    54  	// Create a fake job
    55  	state := srv.Agent.Server().State()
    56  	j := mock.Job()
    57  	assert.Nil(state.UpsertJob(structs.MsgTypeTestSetup, 1000, j))
    58  
    59  	prefix := j.ID[:len(j.ID)-5]
    60  	args := complete.Args{Last: prefix}
    61  	predictor := cmd.AutocompleteArgs()
    62  
    63  	res := predictor.Predict(args)
    64  	assert.Equal(1, len(res))
    65  	assert.Equal(j.ID, res[0])
    66  }