github.com/hernad/nomad@v1.6.112/command/alloc_logs_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "testing" 8 9 "github.com/hernad/nomad/ci" 10 "github.com/hernad/nomad/nomad/mock" 11 "github.com/hernad/nomad/nomad/structs" 12 "github.com/mitchellh/cli" 13 "github.com/posener/complete" 14 "github.com/shoenig/test/must" 15 ) 16 17 var _ cli.Command = (*AllocLogsCommand)(nil) 18 19 func TestLogsCommand_Fails(t *testing.T) { 20 ci.Parallel(t) 21 srv, _, url := testServer(t, false, nil) 22 defer srv.Shutdown() 23 24 ui := cli.NewMockUi() 25 cmd := &AllocLogsCommand{Meta: Meta{Ui: ui}} 26 27 // Fails on misuse 28 code := cmd.Run([]string{"some", "bad", "args"}) 29 must.One(t, code) 30 31 out := ui.ErrorWriter.String() 32 must.StrContains(t, out, commandErrorText(cmd)) 33 34 ui.ErrorWriter.Reset() 35 36 // Fails on connection failure 37 code = cmd.Run([]string{"-address=nope", "foobar"}) 38 must.One(t, code) 39 40 out = ui.ErrorWriter.String() 41 must.StrContains(t, out, "Error querying allocation") 42 43 ui.ErrorWriter.Reset() 44 45 // Fails on missing alloc 46 code = cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}) 47 must.One(t, code) 48 49 out = ui.ErrorWriter.String() 50 must.StrContains(t, out, "No allocation(s) with prefix or id") 51 52 ui.ErrorWriter.Reset() 53 54 // Fail on identifier with too few characters 55 code = cmd.Run([]string{"-address=" + url, "2"}) 56 must.One(t, code) 57 58 out = ui.ErrorWriter.String() 59 must.StrContains(t, out, "must contain at least two characters.") 60 61 ui.ErrorWriter.Reset() 62 63 // Identifiers with uneven length should produce a query result 64 code = cmd.Run([]string{"-address=" + url, "123"}) 65 must.One(t, code) 66 67 out = ui.ErrorWriter.String() 68 must.StrContains(t, out, "No allocation(s) with prefix or id") 69 } 70 71 func TestLogsCommand_AutocompleteArgs(t *testing.T) { 72 ci.Parallel(t) 73 74 srv, _, url := testServer(t, true, nil) 75 defer srv.Shutdown() 76 77 ui := cli.NewMockUi() 78 cmd := &AllocLogsCommand{Meta: Meta{Ui: ui, flagAddress: url}} 79 80 // Create a fake alloc 81 state := srv.Agent.Server().State() 82 a := mock.Alloc() 83 must.NoError(t, state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{a})) 84 85 prefix := a.ID[:5] 86 args := complete.Args{Last: prefix} 87 predictor := cmd.AutocompleteArgs() 88 89 res := predictor.Predict(args) 90 must.Len(t, 1, res) 91 must.Eq(t, a.ID, res[0]) 92 }