github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/alloc_checks_test.go (about) 1 package command 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/nomad/ci" 7 "github.com/hashicorp/nomad/nomad/mock" 8 "github.com/hashicorp/nomad/nomad/structs" 9 "github.com/mitchellh/cli" 10 "github.com/posener/complete" 11 "github.com/shoenig/test/must" 12 ) 13 14 func TestAllocChecksCommand_Implements(t *testing.T) { 15 ci.Parallel(t) 16 var _ cli.Command = (*AllocChecksCommand)(nil) 17 } 18 19 func TestAllocChecksCommand_Fails(t *testing.T) { 20 ci.Parallel(t) 21 srv, _, url := testServer(t, false, nil) 22 t.Cleanup(func() { 23 _ = srv.Shutdown() 24 }) 25 26 ui := cli.NewMockUi() 27 cmd := &AllocChecksCommand{Meta: Meta{Ui: ui}} 28 29 // fails on misuse t.Run("fails on misuse", func(t *testing.T) { 30 code := cmd.Run([]string{"some", "bad", "args"}) 31 must.One(t, code) 32 out := ui.ErrorWriter.String() 33 must.StrContains(t, out, commandErrorText(cmd)) 34 35 ui.ErrorWriter.Reset() 36 37 // fails on connection failure 38 code = cmd.Run([]string{"-address=nope", "foobar"}) 39 must.One(t, code) 40 out = ui.ErrorWriter.String() 41 must.StrContains(t, out, "Error querying allocation") 42 43 ui.ErrorWriter.Reset() 44 45 // fails on missing allocation 46 code = cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}) 47 must.One(t, code) 48 out = ui.ErrorWriter.String() 49 must.StrContains(t, out, "No allocation(s) with prefix or id") 50 51 ui.ErrorWriter.Reset() 52 53 // fails on prefix with too few characters 54 code = cmd.Run([]string{"-address=" + url, "2"}) 55 must.One(t, code) 56 out = ui.ErrorWriter.String() 57 must.StrContains(t, out, "must contain at least two characters.") 58 59 ui.ErrorWriter.Reset() 60 } 61 62 func TestAllocChecksCommand_AutocompleteArgs(t *testing.T) { 63 ci.Parallel(t) 64 65 srv, _, url := testServer(t, true, nil) 66 defer stopTestAgent(srv) 67 68 ui := cli.NewMockUi() 69 cmd := &AllocChecksCommand{Meta: Meta{Ui: ui, flagAddress: url}} 70 71 // Create a fake alloc 72 state := srv.Agent.Server().State() 73 a := mock.Alloc() 74 must.NoError(t, state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{a})) 75 76 prefix := a.ID[:5] 77 args := complete.Args{Last: prefix} 78 predictor := cmd.AutocompleteArgs() 79 80 res := predictor.Predict(args) 81 must.Len(t, 1, res) 82 must.Eq(t, a.ID, res[0]) 83 } 84 85 func TestAllocChecksCommand_Run(t *testing.T) { 86 ci.Parallel(t) 87 srv, client, url := testServer(t, true, nil) 88 defer stopTestAgent(srv) 89 90 // wait for nodes 91 waitForNodes(t, client) 92 93 jobID := "job1_checks" 94 job1 := testNomadServiceJob(jobID) 95 96 resp, _, err := client.Jobs().Register(job1, nil) 97 must.NoError(t, err) 98 99 // wait for registration success 100 ui := cli.NewMockUi() 101 code := waitForSuccess(ui, client, fullId, t, resp.EvalID) 102 must.Zero(t, code) 103 104 // Get an alloc id 105 allocID := getAllocFromJob(t, client, jobID) 106 107 // do not wait for alloc running - it will stay pending because the 108 // health-check will never pass 109 110 // Run command 111 cmd := &AllocChecksCommand{Meta: Meta{Ui: ui, flagAddress: url}} 112 code = cmd.Run([]string{"-address=" + url, allocID}) 113 must.Zero(t, code) 114 115 // check output 116 out := ui.OutputWriter.String() 117 must.StrContains(t, out, `Name = check1`) 118 must.StrContains(t, out, `Group = job1_checks.group1[0]`) 119 must.StrContains(t, out, `Task = (group)`) 120 must.StrContains(t, out, `Service = service1`) 121 must.StrContains(t, out, `Mode = healthiness`) 122 }