github.com/uchennaokeke444/nomad@v0.11.8/command/job_dispatch_test.go (about)

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