github.com/rohankumardubey/nomad@v0.11.8/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, commandErrorText(cmd)) {
    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  	d.JobCreateIndex = job.CreateIndex
    73  	assert.Nil(state.UpsertDeployment(200, d))
    74  
    75  	// Should now display the deployment
    76  	if code := cmd.Run([]string{"-address=" + url, "-verbose", job.ID}); code != 0 {
    77  		t.Fatalf("expected exit 0, got: %d", code)
    78  	}
    79  	if out := ui.OutputWriter.String(); !strings.Contains(out, d.ID) {
    80  		t.Fatalf("expected deployment output, got: %s", out)
    81  	}
    82  	ui.OutputWriter.Reset()
    83  }
    84  
    85  func TestJobDeploymentsCommand_Run_Latest(t *testing.T) {
    86  	t.Parallel()
    87  	assert := assert.New(t)
    88  	srv, _, url := testServer(t, true, nil)
    89  	defer srv.Shutdown()
    90  
    91  	ui := new(cli.MockUi)
    92  	cmd := &JobDeploymentsCommand{Meta: Meta{Ui: ui}}
    93  
    94  	// Should return an error message for no job match
    95  	if code := cmd.Run([]string{"-address=" + url, "-latest", "foo"}); code != 1 {
    96  		t.Fatalf("expected exit 1, got: %d", code)
    97  	}
    98  
    99  	// Create a job without a deployment
   100  	job := mock.Job()
   101  	state := srv.Agent.Server().State()
   102  	assert.Nil(state.UpsertJob(100, job))
   103  
   104  	// Should display no match if the job doesn't have deployments
   105  	if code := cmd.Run([]string{"-address=" + url, "-latest", job.ID}); code != 0 {
   106  		t.Fatalf("expected exit 0, got: %d", code)
   107  	}
   108  	if out := ui.OutputWriter.String(); !strings.Contains(out, "No deployment found") {
   109  		t.Fatalf("expected no deployments output, got: %s", out)
   110  	}
   111  	ui.OutputWriter.Reset()
   112  
   113  	// Inject a deployment
   114  	d := mock.Deployment()
   115  	d.JobID = job.ID
   116  	d.JobCreateIndex = job.CreateIndex
   117  	assert.Nil(state.UpsertDeployment(200, d))
   118  
   119  	// Should now display the deployment
   120  	if code := cmd.Run([]string{"-address=" + url, "-verbose", "-latest", job.ID}); code != 0 {
   121  		t.Fatalf("expected exit 0, got: %d", code)
   122  	}
   123  	if out := ui.OutputWriter.String(); !strings.Contains(out, d.ID) {
   124  		t.Fatalf("expected deployment output, got: %s", out)
   125  	}
   126  	ui.OutputWriter.Reset()
   127  }
   128  
   129  func TestJobDeploymentsCommand_AutocompleteArgs(t *testing.T) {
   130  	assert := assert.New(t)
   131  	t.Parallel()
   132  
   133  	srv, _, url := testServer(t, true, nil)
   134  	defer srv.Shutdown()
   135  
   136  	ui := new(cli.MockUi)
   137  	cmd := &JobDeploymentsCommand{Meta: Meta{Ui: ui, flagAddress: url}}
   138  
   139  	// Create a fake job
   140  	state := srv.Agent.Server().State()
   141  	j := mock.Job()
   142  	assert.Nil(state.UpsertJob(1000, j))
   143  
   144  	prefix := j.ID[:len(j.ID)-5]
   145  	args := complete.Args{Last: prefix}
   146  	predictor := cmd.AutocompleteArgs()
   147  
   148  	res := predictor.Predict(args)
   149  	assert.Equal(1, len(res))
   150  	assert.Equal(j.ID, res[0])
   151  }