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

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  
     9  	"github.com/hashicorp/nomad/nomad/mock"
    10  	"github.com/mitchellh/cli"
    11  	"github.com/posener/complete"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestJobDispatchCommand_Implements(t *testing.T) {
    16  	t.Parallel()
    17  	var _ cli.Command = &JobDispatchCommand{}
    18  }
    19  
    20  func TestJobDispatchCommand_Fails(t *testing.T) {
    21  	t.Parallel()
    22  	ui := cli.NewMockUi()
    23  	cmd := &JobDispatchCommand{Meta: Meta{Ui: ui}}
    24  
    25  	// Fails on misuse
    26  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    27  		t.Fatalf("expected exit code 1, got: %d", code)
    28  	}
    29  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    30  		t.Fatalf("expected help output, got: %s", out)
    31  	}
    32  	ui.ErrorWriter.Reset()
    33  
    34  	// Fails when specified file does not exist
    35  	if code := cmd.Run([]string{"foo", "/unicorns/leprechauns"}); code != 1 {
    36  		t.Fatalf("expect exit 1, got: %d", code)
    37  	}
    38  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error reading input data") {
    39  		t.Fatalf("expect error reading input data: %v", out)
    40  	}
    41  	ui.ErrorWriter.Reset()
    42  
    43  	if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
    44  		t.Fatalf("expected exit code 1, got: %d", code)
    45  	}
    46  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Failed to dispatch") {
    47  		t.Fatalf("expected failed query error, got: %s", out)
    48  	}
    49  	ui.ErrorWriter.Reset()
    50  }
    51  
    52  func TestJobDispatchCommand_AutocompleteArgs(t *testing.T) {
    53  	assert := assert.New(t)
    54  	t.Parallel()
    55  
    56  	srv, _, url := testServer(t, true, nil)
    57  	defer srv.Shutdown()
    58  
    59  	ui := cli.NewMockUi()
    60  	cmd := &JobDispatchCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    61  
    62  	// Create a fake job
    63  	state := srv.Agent.Server().State()
    64  	j := mock.Job()
    65  	assert.Nil(state.UpsertJob(structs.MsgTypeTestSetup, 1000, j))
    66  
    67  	prefix := j.ID[:len(j.ID)-5]
    68  	args := complete.Args{Last: prefix}
    69  	predictor := cmd.AutocompleteArgs()
    70  
    71  	res := predictor.Predict(args)
    72  	assert.Equal(1, len(res))
    73  	assert.Equal(j.ID, res[0])
    74  }