github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/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_Self(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 self node 44 if code := cmd.Run([]string{"-address=" + url, "-self"}); 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 if strings.Contains(out, "Allocations") { 52 t.Fatalf("should not dump allocations") 53 } 54 ui.OutputWriter.Reset() 55 56 // Request full id output 57 if code := cmd.Run([]string{"-address=" + url, "-self", "-verbose"}); code != 0 { 58 t.Fatalf("expected exit 0, got: %d", code) 59 } 60 out = ui.OutputWriter.String() 61 if !strings.Contains(out, nodeID) { 62 t.Fatalf("expected full node id %q, got: %s", nodeID, out) 63 } 64 ui.OutputWriter.Reset() 65 } 66 67 func TestNodeStatusCommand_Run(t *testing.T) { 68 // Start in dev mode so we get a node registration 69 srv, client, url := testServer(t, func(c *testutil.TestServerConfig) { 70 c.DevMode = true 71 c.NodeName = "mynode" 72 }) 73 defer srv.Stop() 74 75 ui := new(cli.MockUi) 76 cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}} 77 78 // Wait for a node to appear 79 var nodeID string 80 testutil.WaitForResult(func() (bool, error) { 81 nodes, _, err := client.Nodes().List(nil) 82 if err != nil { 83 return false, err 84 } 85 if len(nodes) == 0 { 86 return false, fmt.Errorf("missing node") 87 } 88 nodeID = nodes[0].ID 89 return true, nil 90 }, func(err error) { 91 t.Fatalf("err: %s", err) 92 }) 93 94 // Query all node statuses 95 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 96 t.Fatalf("expected exit 0, got: %d", code) 97 } 98 out := ui.OutputWriter.String() 99 if !strings.Contains(out, "mynode") { 100 t.Fatalf("expect to find mynode, got: %s", out) 101 } 102 ui.OutputWriter.Reset() 103 104 // Query a single node 105 if code := cmd.Run([]string{"-address=" + url, nodeID}); code != 0 { 106 t.Fatalf("expected exit 0, got: %d", code) 107 } 108 out = ui.OutputWriter.String() 109 if !strings.Contains(out, "mynode") { 110 t.Fatalf("expect to find mynode, got: %s", out) 111 } 112 ui.OutputWriter.Reset() 113 114 // Query single node in short view 115 if code := cmd.Run([]string{"-address=" + url, "-short", nodeID}); code != 0 { 116 t.Fatalf("expected exit 0, got: %d", code) 117 } 118 out = ui.OutputWriter.String() 119 if !strings.Contains(out, "mynode") { 120 t.Fatalf("expect to find mynode, got: %s", out) 121 } 122 if strings.Contains(out, "Allocations") { 123 t.Fatalf("should not dump allocations") 124 } 125 126 // Query a single node based on prefix 127 if code := cmd.Run([]string{"-address=" + url, nodeID[:4]}); code != 0 { 128 t.Fatalf("expected exit 0, got: %d", code) 129 } 130 out = ui.OutputWriter.String() 131 if !strings.Contains(out, "mynode") { 132 t.Fatalf("expect to find mynode, got: %s", out) 133 } 134 if strings.Contains(out, nodeID) { 135 t.Fatalf("expected truncated node id, got: %s", out) 136 } 137 if !strings.Contains(out, nodeID[:8]) { 138 t.Fatalf("expected node id %q, got: %s", nodeID[:8], out) 139 } 140 ui.OutputWriter.Reset() 141 142 // Request full id output 143 if code := cmd.Run([]string{"-address=" + url, "-verbose", nodeID[:4]}); code != 0 { 144 t.Fatalf("expected exit 0, got: %d", code) 145 } 146 out = ui.OutputWriter.String() 147 if !strings.Contains(out, nodeID) { 148 t.Fatalf("expected full node id %q, got: %s", nodeID, out) 149 } 150 ui.OutputWriter.Reset() 151 152 // Identifiers with uneven length should produce a query result 153 if code := cmd.Run([]string{"-address=" + url, nodeID[:3]}); code != 0 { 154 t.Fatalf("expected exit 0, got: %d", code) 155 } 156 out = ui.OutputWriter.String() 157 if !strings.Contains(out, "mynode") { 158 t.Fatalf("expect to find mynode, got: %s", out) 159 } 160 } 161 162 func TestNodeStatusCommand_Fails(t *testing.T) { 163 srv, _, url := testServer(t, nil) 164 defer srv.Stop() 165 166 ui := new(cli.MockUi) 167 cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}} 168 169 // Fails on misuse 170 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 171 t.Fatalf("expected exit code 1, got: %d", code) 172 } 173 if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { 174 t.Fatalf("expected help output, got: %s", out) 175 } 176 ui.ErrorWriter.Reset() 177 178 // Fails on connection failure 179 if code := cmd.Run([]string{"-address=nope"}); code != 1 { 180 t.Fatalf("expected exit code 1, got: %d", code) 181 } 182 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying node status") { 183 t.Fatalf("expected failed query error, got: %s", out) 184 } 185 ui.ErrorWriter.Reset() 186 187 // Fails on non-existent node 188 if code := cmd.Run([]string{"-address=" + url, "12345678-abcd-efab-cdef-123456789abc"}); code != 1 { 189 t.Fatalf("expected exit 1, got: %d", code) 190 } 191 if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix") { 192 t.Fatalf("expected not found error, got: %s", out) 193 } 194 ui.ErrorWriter.Reset() 195 196 // Fail on identifier with too few characters 197 if code := cmd.Run([]string{"-address=" + url, "1"}); code != 1 { 198 t.Fatalf("expected exit 1, got: %d", code) 199 } 200 if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") { 201 t.Fatalf("expected too few characters error, got: %s", out) 202 } 203 ui.ErrorWriter.Reset() 204 205 // Failed on both -json and -t options are specified 206 if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 { 207 t.Fatalf("expected exit 1, got: %d", code) 208 } 209 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both -json and -t are not allowed") { 210 t.Fatalf("expected getting formatter error, got: %s", out) 211 } 212 }