github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/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  
    79  func TestNodeStatusCommand_Fails(t *testing.T) {
    80  	srv, _, url := testServer(t, nil)
    81  	defer srv.Stop()
    82  
    83  	ui := new(cli.MockUi)
    84  	cmd := &NodeStatusCommand{Meta: Meta{Ui: ui}}
    85  
    86  	// Fails on misuse
    87  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    88  		t.Fatalf("expected exit code 1, got: %d", code)
    89  	}
    90  	if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
    91  		t.Fatalf("expected help output, got: %s", out)
    92  	}
    93  	ui.ErrorWriter.Reset()
    94  
    95  	// Fails on connection failure
    96  	if code := cmd.Run([]string{"-address=nope"}); code != 1 {
    97  		t.Fatalf("expected exit code 1, got: %d", code)
    98  	}
    99  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying node status") {
   100  		t.Fatalf("expected failed query error, got: %s", out)
   101  	}
   102  
   103  	// Fails on non-existent node
   104  	if code := cmd.Run([]string{"-address=" + url, "nope"}); code != 1 {
   105  		t.Fatalf("expected exit 1, got: %d", code)
   106  	}
   107  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "not found") {
   108  		t.Fatalf("expected not found error, got: %s", out)
   109  	}
   110  }