github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/job_stop_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/nomad/mock"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  	"github.com/mitchellh/cli"
    10  	"github.com/posener/complete"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestStopCommand_Implements(t *testing.T) {
    15  	t.Parallel()
    16  	var _ cli.Command = &JobStopCommand{}
    17  }
    18  
    19  func TestStopCommand_Fails(t *testing.T) {
    20  	t.Parallel()
    21  	srv, _, url := testServer(t, false, nil)
    22  	defer srv.Shutdown()
    23  
    24  	ui := cli.NewMockUi()
    25  	cmd := &JobStopCommand{Meta: Meta{Ui: ui}}
    26  
    27  	// Fails 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  	// Fails on nonexistent job ID
    37  	if code := cmd.Run([]string{"-address=" + url, "nope"}); code != 1 {
    38  		t.Fatalf("expect exit 1, got: %d", code)
    39  	}
    40  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No job(s) with prefix or id") {
    41  		t.Fatalf("expect not found error, got: %s", out)
    42  	}
    43  	ui.ErrorWriter.Reset()
    44  
    45  	// Fails on connection failure
    46  	if code := cmd.Run([]string{"-address=nope", "nope"}); code != 1 {
    47  		t.Fatalf("expected exit code 1, got: %d", code)
    48  	}
    49  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error deregistering job") {
    50  		t.Fatalf("expected failed query error, got: %s", out)
    51  	}
    52  }
    53  
    54  func TestStopCommand_AutocompleteArgs(t *testing.T) {
    55  	assert := assert.New(t)
    56  	t.Parallel()
    57  
    58  	srv, _, url := testServer(t, true, nil)
    59  	defer srv.Shutdown()
    60  
    61  	ui := cli.NewMockUi()
    62  	cmd := &JobStopCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    63  
    64  	// Create a fake job
    65  	state := srv.Agent.Server().State()
    66  	j := mock.Job()
    67  	assert.Nil(state.UpsertJob(structs.MsgTypeTestSetup, 1000, j))
    68  
    69  	prefix := j.ID[:len(j.ID)-5]
    70  	args := complete.Args{Last: prefix}
    71  	predictor := cmd.AutocompleteArgs()
    72  
    73  	res := predictor.Predict(args)
    74  	assert.Equal(1, len(res))
    75  	assert.Equal(j.ID, res[0])
    76  }