github.com/djenriquez/nomad-1@v0.8.1/command/job_deployments_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 TestJobDeploymentsCommand_Implements(t *testing.T) {
    14  	t.Parallel()
    15  	var _ cli.Command = &JobDeploymentsCommand{}
    16  }
    17  
    18  func TestJobDeploymentsCommand_Fails(t *testing.T) {
    19  	t.Parallel()
    20  	ui := new(cli.MockUi)
    21  	cmd := &JobDeploymentsCommand{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, cmd.Help()) {
    28  		t.Fatalf("expected help output, got: %s", out)
    29  	}
    30  	ui.ErrorWriter.Reset()
    31  
    32  	if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
    33  		t.Fatalf("expected exit code 1, got: %d", code)
    34  	}
    35  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error listing jobs") {
    36  		t.Fatalf("expected failed query error, got: %s", out)
    37  	}
    38  	ui.ErrorWriter.Reset()
    39  }
    40  
    41  func TestJobDeploymentsCommand_Run(t *testing.T) {
    42  	t.Parallel()
    43  	assert := assert.New(t)
    44  	srv, _, url := testServer(t, true, nil)
    45  	defer srv.Shutdown()
    46  
    47  	ui := new(cli.MockUi)
    48  	cmd := &JobDeploymentsCommand{Meta: Meta{Ui: ui}}
    49  
    50  	// Should return an error message for no job match
    51  	if code := cmd.Run([]string{"-address=" + url, "foo"}); code != 1 {
    52  		t.Fatalf("expected exit 1, got: %d", code)
    53  	}
    54  
    55  	// Create a job without a deployment
    56  	job := mock.Job()
    57  	state := srv.Agent.Server().State()
    58  	assert.Nil(state.UpsertJob(100, job))
    59  
    60  	// Should display no match if the job doesn't have deployments
    61  	if code := cmd.Run([]string{"-address=" + url, job.ID}); code != 0 {
    62  		t.Fatalf("expected exit 0, got: %d", code)
    63  	}
    64  	if out := ui.OutputWriter.String(); !strings.Contains(out, "No deployments found") {
    65  		t.Fatalf("expected no deployments output, got: %s", out)
    66  	}
    67  	ui.OutputWriter.Reset()
    68  
    69  	// Inject a deployment
    70  	d := mock.Deployment()
    71  	d.JobID = job.ID
    72  	assert.Nil(state.UpsertDeployment(200, d))
    73  
    74  	// Should now display the deployment
    75  	if code := cmd.Run([]string{"-address=" + url, "-verbose", job.ID}); code != 0 {
    76  		t.Fatalf("expected exit 0, got: %d", code)
    77  	}
    78  	if out := ui.OutputWriter.String(); !strings.Contains(out, d.ID) {
    79  		t.Fatalf("expected deployment output, got: %s", out)
    80  	}
    81  	ui.OutputWriter.Reset()
    82  }
    83  
    84  func TestJobDeploymentsCommand_Run_Latest(t *testing.T) {
    85  	t.Parallel()
    86  	assert := assert.New(t)
    87  	srv, _, url := testServer(t, true, nil)
    88  	defer srv.Shutdown()
    89  
    90  	ui := new(cli.MockUi)
    91  	cmd := &JobDeploymentsCommand{Meta: Meta{Ui: ui}}
    92  
    93  	// Should return an error message for no job match
    94  	if code := cmd.Run([]string{"-address=" + url, "-latest", "foo"}); code != 1 {
    95  		t.Fatalf("expected exit 1, got: %d", code)
    96  	}
    97  
    98  	// Create a job without a deployment
    99  	job := mock.Job()
   100  	state := srv.Agent.Server().State()
   101  	assert.Nil(state.UpsertJob(100, job))
   102  
   103  	// Should display no match if the job doesn't have deployments
   104  	if code := cmd.Run([]string{"-address=" + url, "-latest", job.ID}); code != 0 {
   105  		t.Fatalf("expected exit 0, got: %d", code)
   106  	}
   107  	if out := ui.OutputWriter.String(); !strings.Contains(out, "No deployment found") {
   108  		t.Fatalf("expected no deployments output, got: %s", out)
   109  	}
   110  	ui.OutputWriter.Reset()
   111  
   112  	// Inject a deployment
   113  	d := mock.Deployment()
   114  	d.JobID = job.ID
   115  	assert.Nil(state.UpsertDeployment(200, d))
   116  
   117  	// Should now display the deployment
   118  	if code := cmd.Run([]string{"-address=" + url, "-verbose", "-latest", job.ID}); code != 0 {
   119  		t.Fatalf("expected exit 0, got: %d", code)
   120  	}
   121  	if out := ui.OutputWriter.String(); !strings.Contains(out, d.ID) {
   122  		t.Fatalf("expected deployment output, got: %s", out)
   123  	}
   124  	ui.OutputWriter.Reset()
   125  }
   126  
   127  func TestJobDeploymentsCommand_AutocompleteArgs(t *testing.T) {
   128  	assert := assert.New(t)
   129  	t.Parallel()
   130  
   131  	srv, _, url := testServer(t, true, nil)
   132  	defer srv.Shutdown()
   133  
   134  	ui := new(cli.MockUi)
   135  	cmd := &JobDeploymentsCommand{Meta: Meta{Ui: ui, flagAddress: url}}
   136  
   137  	// Create a fake job
   138  	state := srv.Agent.Server().State()
   139  	j := mock.Job()
   140  	assert.Nil(state.UpsertJob(1000, j))
   141  
   142  	prefix := j.ID[:len(j.ID)-5]
   143  	args := complete.Args{Last: prefix}
   144  	predictor := cmd.AutocompleteArgs()
   145  
   146  	res := predictor.Predict(args)
   147  	assert.Equal(1, len(res))
   148  	assert.Equal(j.ID, res[0])
   149  }