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