2015-09-12 20:55:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-09-15 18:20:08 +00:00
|
|
|
"fmt"
|
2015-09-12 23:36:44 +00:00
|
|
|
"strings"
|
2015-09-12 20:55:51 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-09-15 18:20:08 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2015-09-12 20:55:51 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeStatusCommand_Implements(t *testing.T) {
|
|
|
|
var _ cli.Command = &NodeStatusCommand{}
|
|
|
|
}
|
2015-09-12 23:36:44 +00:00
|
|
|
|
|
|
|
func TestNodeStatusCommand_Run(t *testing.T) {
|
2015-09-15 18:20:08 +00:00
|
|
|
// Start in dev mode so we get a node registration
|
|
|
|
srv, client, url := testServer(t, func(c *testutil.TestServerConfig) {
|
|
|
|
c.DevMode = true
|
|
|
|
c.NodeName = "mynode"
|
|
|
|
})
|
2015-09-12 23:36:44 +00:00
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2015-09-14 20:13:52 +00:00
|
|
|
cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}}
|
2015-09-12 23:36:44 +00:00
|
|
|
|
2015-09-15 18:20:08 +00:00
|
|
|
// Wait for a node to appear
|
|
|
|
var nodeID string
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return false, fmt.Errorf("missing node")
|
|
|
|
}
|
|
|
|
nodeID = nodes[0].ID
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
})
|
|
|
|
|
2015-09-12 23:36:44 +00:00
|
|
|
// Query all node statuses
|
2015-09-14 20:13:52 +00:00
|
|
|
if code := cmd.Run([]string{"-address=" + url}); code != 0 {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
2015-09-15 18:20:08 +00:00
|
|
|
out := ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
|
|
|
|
|
|
|
// Query a single node
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, nodeID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
|
|
|
if !strings.Contains(out, "Allocations") {
|
|
|
|
t.Fatalf("expected allocations, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
2015-09-12 23:36:44 +00:00
|
|
|
|
2015-09-15 18:20:08 +00:00
|
|
|
// Query single node in short view
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, "-short", nodeID}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, "Allocations") {
|
|
|
|
t.Fatalf("should not dump allocations")
|
2015-09-12 23:36:44 +00:00
|
|
|
}
|
2015-12-22 22:44:33 +00:00
|
|
|
|
|
|
|
// Query a single node based on prefix
|
|
|
|
if code := cmd.Run([]string{"-address=" + url, nodeID[:4]}); code != 0 {
|
|
|
|
t.Fatalf("expected exit 0, got: %d", code)
|
|
|
|
}
|
|
|
|
out = ui.OutputWriter.String()
|
|
|
|
if !strings.Contains(out, "mynode") {
|
|
|
|
t.Fatalf("expect to find mynode, got: %s", out)
|
|
|
|
}
|
|
|
|
if !strings.Contains(out, "Allocations") {
|
|
|
|
t.Fatalf("expected allocations, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.OutputWriter.Reset()
|
2015-09-12 23:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeStatusCommand_Fails(t *testing.T) {
|
2015-09-15 18:20:08 +00:00
|
|
|
srv, _, url := testServer(t, nil)
|
2015-09-12 23:36:44 +00:00
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
2015-09-14 20:13:52 +00:00
|
|
|
cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}}
|
2015-09-12 23:36:44 +00:00
|
|
|
|
|
|
|
// Fails on misuse
|
|
|
|
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
|
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
|
|
|
|
t.Fatalf("expected help output, got: %s", out)
|
|
|
|
}
|
|
|
|
ui.ErrorWriter.Reset()
|
|
|
|
|
|
|
|
// Fails on connection failure
|
2015-09-14 20:13:52 +00:00
|
|
|
if code := cmd.Run([]string{"-address=nope"}); code != 1 {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected exit code 1, got: %d", code)
|
|
|
|
}
|
2015-09-14 20:13:52 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying node status") {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected failed query error, got: %s", out)
|
|
|
|
}
|
2015-12-22 22:44:33 +00:00
|
|
|
ui.ErrorWriter.Reset()
|
2015-09-12 23:36:44 +00:00
|
|
|
|
|
|
|
// Fails on non-existent node
|
2015-09-14 20:13:52 +00:00
|
|
|
if code := cmd.Run([]string{"-address=" + url, "nope"}); code != 1 {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected exit 1, got: %d", code)
|
|
|
|
}
|
2015-12-24 10:46:59 +00:00
|
|
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No node(s) with prefix") {
|
2015-09-12 23:36:44 +00:00
|
|
|
t.Fatalf("expected not found error, got: %s", out)
|
|
|
|
}
|
|
|
|
}
|