github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/node_drain_test.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/nomad/testutil" 9 "github.com/mitchellh/cli" 10 "github.com/posener/complete" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestNodeDrainCommand_Implements(t *testing.T) { 15 t.Parallel() 16 var _ cli.Command = &NodeDrainCommand{} 17 } 18 19 func TestNodeDrainCommand_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 := &NodeDrainCommand{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", "-enable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 38 t.Fatalf("expected exit code 1, got: %d", code) 39 } 40 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error toggling") { 41 t.Fatalf("expected failed toggle error, got: %s", out) 42 } 43 ui.ErrorWriter.Reset() 44 45 // Fails on non-existent node 46 if code := cmd.Run([]string{"-address=" + url, "-enable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 47 t.Fatalf("expected exit 1, got: %d", code) 48 } 49 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") { 50 t.Fatalf("expected not exist error, got: %s", out) 51 } 52 ui.ErrorWriter.Reset() 53 54 // Fails if both enable and disable specified 55 if code := cmd.Run([]string{"-enable", "-disable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 56 t.Fatalf("expected exit 1, got: %d", code) 57 } 58 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 59 t.Fatalf("expected help output, got: %s", out) 60 } 61 ui.ErrorWriter.Reset() 62 63 // Fails if neither enable or disable specified 64 if code := cmd.Run([]string{"12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 65 t.Fatalf("expected exit 1, got: %d", code) 66 } 67 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 68 t.Fatalf("expected help output, 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, "-enable", "1"}); 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, "-enable", "123"}); code != 1 { 83 t.Fatalf("expected exit 1, got: %d", code) 84 } 85 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") { 86 t.Fatalf("expected not exist error, got: %s", out) 87 } 88 } 89 90 func TestNodeDrainCommand_AutocompleteArgs(t *testing.T) { 91 assert := assert.New(t) 92 t.Parallel() 93 94 srv, client, url := testServer(t, true, nil) 95 defer srv.Shutdown() 96 97 // Wait for a node to appear 98 var nodeID string 99 testutil.WaitForResult(func() (bool, error) { 100 nodes, _, err := client.Nodes().List(nil) 101 if err != nil { 102 return false, err 103 } 104 if len(nodes) == 0 { 105 return false, fmt.Errorf("missing node") 106 } 107 nodeID = nodes[0].ID 108 return true, nil 109 }, func(err error) { 110 t.Fatalf("err: %s", err) 111 }) 112 113 ui := new(cli.MockUi) 114 cmd := &NodeDrainCommand{Meta: Meta{Ui: ui, flagAddress: url}} 115 116 prefix := nodeID[:len(nodeID)-5] 117 args := complete.Args{Last: prefix} 118 predictor := cmd.AutocompleteArgs() 119 120 res := predictor.Predict(args) 121 assert.Equal(1, len(res)) 122 assert.Equal(nodeID, res[0]) 123 }