github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/command/alloc_status_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  	"github.com/hashicorp/nomad/testutil"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestAllocStatusCommand_Implements(t *testing.T) {
    14  	t.Parallel()
    15  	var _ cli.Command = &AllocStatusCommand{}
    16  }
    17  
    18  func TestAllocStatusCommand_Fails(t *testing.T) {
    19  	t.Parallel()
    20  	srv, _, url := testServer(t, false, nil)
    21  	defer srv.Shutdown()
    22  
    23  	ui := new(cli.MockUi)
    24  	cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
    25  
    26  	// Fails on misuse
    27  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    28  		t.Fatalf("expected exit code 1, got: %d", code)
    29  	}
    30  	if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
    31  		t.Fatalf("expected help output, got: %s", out)
    32  	}
    33  	ui.ErrorWriter.Reset()
    34  
    35  	// Fails on connection failure
    36  	if code := cmd.Run([]string{"-address=nope", "foobar"}); code != 1 {
    37  		t.Fatalf("expected exit code 1, got: %d", code)
    38  	}
    39  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying allocation") {
    40  		t.Fatalf("expected failed query error, got: %s", out)
    41  	}
    42  	ui.ErrorWriter.Reset()
    43  
    44  	// Fails on missing alloc
    45  	if code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}); code != 1 {
    46  		t.Fatalf("expected exit 1, got: %d", code)
    47  	}
    48  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
    49  		t.Fatalf("expected not found error, got: %s", out)
    50  	}
    51  	ui.ErrorWriter.Reset()
    52  
    53  	// Fail on identifier with too few characters
    54  	if code := cmd.Run([]string{"-address=" + url, "2"}); code != 1 {
    55  		t.Fatalf("expected exit 1, got: %d", code)
    56  	}
    57  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
    58  		t.Fatalf("expected too few characters error, got: %s", out)
    59  	}
    60  	ui.ErrorWriter.Reset()
    61  
    62  	// Identifiers with uneven length should produce a query result
    63  	if code := cmd.Run([]string{"-address=" + url, "123"}); code != 1 {
    64  		t.Fatalf("expected exit 1, got: %d", code)
    65  	}
    66  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
    67  		t.Fatalf("expected not found error, got: %s", out)
    68  	}
    69  	ui.ErrorWriter.Reset()
    70  
    71  	// Failed on both -json and -t options are specified
    72  	if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
    73  		t.Fatalf("expected exit 1, got: %d", code)
    74  	}
    75  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both json and template formatting are not allowed") {
    76  		t.Fatalf("expected getting formatter error, got: %s", out)
    77  	}
    78  }
    79  
    80  func TestAllocStatusCommand_Run(t *testing.T) {
    81  	t.Parallel()
    82  	srv, client, url := testServer(t, true, nil)
    83  	defer srv.Shutdown()
    84  
    85  	// Wait for a node to be ready
    86  	testutil.WaitForResult(func() (bool, error) {
    87  		nodes, _, err := client.Nodes().List(nil)
    88  		if err != nil {
    89  			return false, err
    90  		}
    91  		for _, node := range nodes {
    92  			if node.Status == structs.NodeStatusReady {
    93  				return true, nil
    94  			}
    95  		}
    96  		return false, fmt.Errorf("no ready nodes")
    97  	}, func(err error) {
    98  		t.Fatalf("err: %v", err)
    99  	})
   100  
   101  	ui := new(cli.MockUi)
   102  	cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
   103  
   104  	jobID := "job1_sfx"
   105  	job1 := testJob(jobID)
   106  	resp, _, err := client.Jobs().Register(job1, nil)
   107  	if err != nil {
   108  		t.Fatalf("err: %s", err)
   109  	}
   110  	if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
   111  		t.Fatalf("status code non zero saw %d", code)
   112  	}
   113  	// get an alloc id
   114  	allocId1 := ""
   115  	if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
   116  		if len(allocs) > 0 {
   117  			allocId1 = allocs[0].ID
   118  		}
   119  	}
   120  	if allocId1 == "" {
   121  		t.Fatal("unable to find an allocation")
   122  	}
   123  
   124  	if code := cmd.Run([]string{"-address=" + url, allocId1}); code != 0 {
   125  		t.Fatalf("expected exit 0, got: %d", code)
   126  	}
   127  	out := ui.OutputWriter.String()
   128  	if !strings.Contains(out, "Created At") {
   129  		t.Fatalf("expected to have 'Created At' but saw: %s", out)
   130  	}
   131  	ui.OutputWriter.Reset()
   132  
   133  	if code := cmd.Run([]string{"-address=" + url, "-verbose", allocId1}); code != 0 {
   134  		t.Fatalf("expected exit 0, got: %d", code)
   135  	}
   136  	out = ui.OutputWriter.String()
   137  	if !strings.Contains(out, allocId1) {
   138  		t.Fatal("expected to find alloc id in output")
   139  	}
   140  	if !strings.Contains(out, "Created At") {
   141  		t.Fatalf("expected to have 'Created At' but saw: %s", out)
   142  	}
   143  	ui.OutputWriter.Reset()
   144  }