github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/node_drain_test.go (about) 1 package command 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/mitchellh/cli" 8 ) 9 10 func TestNodeDrainCommand_Implements(t *testing.T) { 11 t.Parallel() 12 var _ cli.Command = &NodeDrainCommand{} 13 } 14 15 func TestNodeDrainCommand_Fails(t *testing.T) { 16 t.Parallel() 17 srv, _, url := testServer(t, false, nil) 18 defer srv.Shutdown() 19 20 ui := new(cli.MockUi) 21 cmd := &NodeDrainCommand{Meta: Meta{Ui: ui}} 22 23 // Fails on misuse 24 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 25 t.Fatalf("expected exit code 1, got: %d", code) 26 } 27 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 28 t.Fatalf("expected help output, got: %s", out) 29 } 30 ui.ErrorWriter.Reset() 31 32 // Fails on connection failure 33 if code := cmd.Run([]string{"-address=nope", "-enable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 34 t.Fatalf("expected exit code 1, got: %d", code) 35 } 36 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error toggling") { 37 t.Fatalf("expected failed toggle error, got: %s", out) 38 } 39 ui.ErrorWriter.Reset() 40 41 // Fails on non-existent node 42 if code := cmd.Run([]string{"-address=" + url, "-enable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 43 t.Fatalf("expected exit 1, got: %d", code) 44 } 45 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") { 46 t.Fatalf("expected not exist error, got: %s", out) 47 } 48 ui.ErrorWriter.Reset() 49 50 // Fails if both enable and disable specified 51 if code := cmd.Run([]string{"-enable", "-disable", "12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 52 t.Fatalf("expected exit 1, got: %d", code) 53 } 54 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 55 t.Fatalf("expected help output, got: %s", out) 56 } 57 ui.ErrorWriter.Reset() 58 59 // Fails if neither enable or disable specified 60 if code := cmd.Run([]string{"12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 61 t.Fatalf("expected exit 1, got: %d", code) 62 } 63 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 64 t.Fatalf("expected help output, got: %s", out) 65 } 66 ui.ErrorWriter.Reset() 67 68 // Fail on identifier with too few characters 69 if code := cmd.Run([]string{"-address=" + url, "-enable", "1"}); code != 1 { 70 t.Fatalf("expected exit 1, got: %d", code) 71 } 72 if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") { 73 t.Fatalf("expected too few characters error, got: %s", out) 74 } 75 ui.ErrorWriter.Reset() 76 77 // Identifiers with uneven length should produce a query result 78 if code := cmd.Run([]string{"-address=" + url, "-enable", "123"}); code != 1 { 79 t.Fatalf("expected exit 1, got: %d", code) 80 } 81 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix or id") { 82 t.Fatalf("expected not exist error, got: %s", out) 83 } 84 }