github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/command/node_status_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 ) 11 12 func TestNodeStatusCommand_Implements(t *testing.T) { 13 var _ cli.Command = &NodeStatusCommand{} 14 } 15 16 func TestNodeStatusCommand_Run(t *testing.T) { 17 // Start in dev mode so we get a node registration 18 srv, client, url := testServer(t, func(c *testutil.TestServerConfig) { 19 c.DevMode = true 20 c.NodeName = "mynode" 21 }) 22 defer srv.Stop() 23 24 ui := new(cli.MockUi) 25 cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}} 26 27 // Wait for a node to appear 28 var nodeID string 29 testutil.WaitForResult(func() (bool, error) { 30 nodes, _, err := client.Nodes().List(nil) 31 if err != nil { 32 return false, err 33 } 34 if len(nodes) == 0 { 35 return false, fmt.Errorf("missing node") 36 } 37 nodeID = nodes[0].ID 38 return true, nil 39 }, func(err error) { 40 t.Fatalf("err: %s", err) 41 }) 42 43 // Query all node statuses 44 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 45 t.Fatalf("expected exit 0, got: %d", code) 46 } 47 out := ui.OutputWriter.String() 48 if !strings.Contains(out, "mynode") { 49 t.Fatalf("expect to find mynode, got: %s", out) 50 } 51 ui.OutputWriter.Reset() 52 53 // Query a single node 54 if code := cmd.Run([]string{"-address=" + url, nodeID}); code != 0 { 55 t.Fatalf("expected exit 0, got: %d", code) 56 } 57 out = ui.OutputWriter.String() 58 if !strings.Contains(out, "mynode") { 59 t.Fatalf("expect to find mynode, got: %s", out) 60 } 61 if !strings.Contains(out, "Allocations") { 62 t.Fatalf("expected allocations, got: %s", out) 63 } 64 ui.OutputWriter.Reset() 65 66 // Query single node in short view 67 if code := cmd.Run([]string{"-address=" + url, "-short", nodeID}); code != 0 { 68 t.Fatalf("expected exit 0, got: %d", code) 69 } 70 out = ui.OutputWriter.String() 71 if !strings.Contains(out, "mynode") { 72 t.Fatalf("expect to find mynode, got: %s", out) 73 } 74 if strings.Contains(out, "Allocations") { 75 t.Fatalf("should not dump allocations") 76 } 77 78 // Query a single node based on prefix 79 if code := cmd.Run([]string{"-address=" + url, nodeID[:4]}); code != 0 { 80 t.Fatalf("expected exit 0, got: %d", code) 81 } 82 out = ui.OutputWriter.String() 83 if !strings.Contains(out, "mynode") { 84 t.Fatalf("expect to find mynode, got: %s", out) 85 } 86 if !strings.Contains(out, "Allocations") { 87 t.Fatalf("expected allocations, got: %s", out) 88 } 89 if strings.Contains(out, nodeID) { 90 t.Fatalf("expected truncated node id, got: %s", out) 91 } 92 if !strings.Contains(out, nodeID[:8]) { 93 t.Fatalf("expected node id %q, got: %s", nodeID[:8], out) 94 } 95 ui.OutputWriter.Reset() 96 97 // Request full id output 98 if code := cmd.Run([]string{"-address=" + url, "-verbose", nodeID[:4]}); code != 0 { 99 t.Fatalf("expected exit 0, got: %d", code) 100 } 101 out = ui.OutputWriter.String() 102 if !strings.Contains(out, nodeID) { 103 t.Fatalf("expected full node id %q, got: %s", nodeID, out) 104 } 105 ui.OutputWriter.Reset() 106 107 // Identifiers with uneven length should produce a query result 108 if code := cmd.Run([]string{"-address=" + url, nodeID[:3]}); code != 0 { 109 t.Fatalf("expected exit 0, got: %d", code) 110 } 111 out = ui.OutputWriter.String() 112 if !strings.Contains(out, "mynode") { 113 t.Fatalf("expect to find mynode, got: %s", out) 114 } 115 } 116 117 func TestNodeStatusCommand_Fails(t *testing.T) { 118 srv, _, url := testServer(t, nil) 119 defer srv.Stop() 120 121 ui := new(cli.MockUi) 122 cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}} 123 124 // Fails on misuse 125 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 126 t.Fatalf("expected exit code 1, got: %d", code) 127 } 128 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 129 t.Fatalf("expected help output, got: %s", out) 130 } 131 ui.ErrorWriter.Reset() 132 133 // Fails on connection failure 134 if code := cmd.Run([]string{"-address=nope"}); code != 1 { 135 t.Fatalf("expected exit code 1, got: %d", code) 136 } 137 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying node status") { 138 t.Fatalf("expected failed query error, got: %s", out) 139 } 140 ui.ErrorWriter.Reset() 141 142 // Fails on non-existent node 143 if code := cmd.Run([]string{"-address=" + url, "12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 144 t.Fatalf("expected exit 1, got: %d", code) 145 } 146 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix") { 147 t.Fatalf("expected not found error, got: %s", out) 148 } 149 ui.ErrorWriter.Reset() 150 151 // Fail on identifier with too few characters 152 if code := cmd.Run([]string{"-address=" + url, "1"}); code != 1 { 153 t.Fatalf("expected exit 1, got: %d", code) 154 } 155 if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") { 156 t.Fatalf("expected too few characters error, got: %s", out) 157 } 158 }