github.com/djenriquez/nomad-1@v0.8.1/command/alloc_fs_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 TestFSCommand_Implements(t *testing.T) { 15 t.Parallel() 16 var _ cli.Command = &AllocFSCommand{} 17 } 18 19 func TestFSCommand_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 := &AllocFSCommand{Meta: Meta{Ui: ui}} 26 27 // Fails on lack of job ID 28 if code := cmd.Run([]string{"-job"}); code != 1 { 29 t.Fatalf("expected exit code 1, got: %d", code) 30 } 31 if out := ui.ErrorWriter.String(); !strings.Contains(out, "job ID is required") { 32 t.Fatalf("expected help output, got: %s", out) 33 } 34 ui.ErrorWriter.Reset() 35 36 // Fails on lack of allocation ID 37 if code := cmd.Run([]string{}); code != 1 { 38 t.Fatalf("expected exit code 1, got: %d", code) 39 } 40 if out := ui.ErrorWriter.String(); !strings.Contains(out, "allocation ID is required") { 41 t.Fatalf("expected help output, got: %s", out) 42 } 43 ui.ErrorWriter.Reset() 44 45 // Fails on misuse 46 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 47 t.Fatalf("expected exit code 1, got: %d", code) 48 } 49 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 50 t.Fatalf("expected help output, got: %s", out) 51 } 52 ui.ErrorWriter.Reset() 53 54 // Fails on connection failure 55 if code := cmd.Run([]string{"-address=nope", "foobar"}); code != 1 { 56 t.Fatalf("expected exit code 1, got: %d", code) 57 } 58 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying allocation") { 59 t.Fatalf("expected failed query error, got: %s", out) 60 } 61 ui.ErrorWriter.Reset() 62 63 // Fails on missing alloc 64 if code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}); 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 ui.ErrorWriter.Reset() 71 72 // Fail on identifier with too few characters 73 if code := cmd.Run([]string{"-address=" + url, "2"}); code != 1 { 74 t.Fatalf("expected exit 1, got: %d", code) 75 } 76 if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") { 77 t.Fatalf("expected too few characters error, got: %s", out) 78 } 79 ui.ErrorWriter.Reset() 80 81 // Identifiers with uneven length should produce a query result 82 if code := cmd.Run([]string{"-address=" + url, "123"}); code != 1 { 83 t.Fatalf("expected exit 1, got: %d", code) 84 } 85 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") { 86 t.Fatalf("expected not found error, got: %s", out) 87 } 88 } 89 90 func TestFSCommand_AutocompleteArgs(t *testing.T) { 91 assert := assert.New(t) 92 t.Parallel() 93 94 srv, _, url := testServer(t, true, nil) 95 defer srv.Shutdown() 96 97 ui := new(cli.MockUi) 98 cmd := &AllocFSCommand{Meta: Meta{Ui: ui, flagAddress: url}} 99 100 // Create a fake alloc 101 state := srv.Agent.Server().State() 102 a := mock.Alloc() 103 assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a})) 104 105 prefix := a.ID[:5] 106 args := complete.Args{Last: prefix} 107 predictor := cmd.AutocompleteArgs() 108 109 res := predictor.Predict(args) 110 assert.Equal(1, len(res)) 111 assert.Equal(a.ID, res[0]) 112 }