github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/logs_test.go (about) 1 package command 2 3 import ( 4 "strings" 5 "testing" 6 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/stretchr/testify/assert" 12 ) 13 14 func TestLogsCommand_Implements(t *testing.T) { 15 t.Parallel() 16 var _ cli.Command = &LogsCommand{} 17 } 18 19 func TestLogsCommand_Fails(t *testing.T) { 20 t.Parallel() 21 srv, _, url := testServer(t, false, nil) 22 defer srv.Shutdown() 23 24 ui := new(cli.MockUi) 25 cmd := &LogsCommand{Meta: Meta{Ui: ui}} 26 27 // Fails on misuse 28 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 29 t.Fatalf("expected exit code 1, got: %d", code) 30 } 31 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 32 t.Fatalf("expected help output, got: %s", out) 33 } 34 ui.ErrorWriter.Reset() 35 36 // Fails on connection failure 37 if code := cmd.Run([]string{"-address=nope", "foobar"}); code != 1 { 38 t.Fatalf("expected exit code 1, got: %d", code) 39 } 40 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying allocation") { 41 t.Fatalf("expected failed query error, got: %s", out) 42 } 43 ui.ErrorWriter.Reset() 44 45 // Fails on missing alloc 46 if code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}); code != 1 { 47 t.Fatalf("expected exit 1, got: %d", code) 48 } 49 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") { 50 t.Fatalf("expected not found error, got: %s", out) 51 } 52 ui.ErrorWriter.Reset() 53 54 // Fail on identifier with too few characters 55 if code := cmd.Run([]string{"-address=" + url, "2"}); code != 1 { 56 t.Fatalf("expected exit 1, got: %d", code) 57 } 58 if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") { 59 t.Fatalf("expected too few characters error, got: %s", out) 60 } 61 ui.ErrorWriter.Reset() 62 63 // Identifiers with uneven length should produce a query result 64 if code := cmd.Run([]string{"-address=" + url, "123"}); code != 1 { 65 t.Fatalf("expected exit 1, got: %d", code) 66 } 67 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") { 68 t.Fatalf("expected not found error, got: %s", out) 69 } 70 } 71 72 func TestLogsCommand_AutocompleteArgs(t *testing.T) { 73 assert := assert.New(t) 74 t.Parallel() 75 76 srv, _, url := testServer(t, true, nil) 77 defer srv.Shutdown() 78 79 ui := new(cli.MockUi) 80 cmd := &LogsCommand{Meta: Meta{Ui: ui, flagAddress: url}} 81 82 // Create a fake alloc 83 state := srv.Agent.Server().State() 84 a := mock.Alloc() 85 assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a})) 86 87 prefix := a.ID[:5] 88 args := complete.Args{Last: prefix} 89 predictor := cmd.AutocompleteArgs() 90 91 res := predictor.Predict(args) 92 assert.Equal(1, len(res)) 93 assert.Equal(a.ID, res[0]) 94 }