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