github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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/mock"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/hashicorp/nomad/testutil"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/posener/complete"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestAllocStatusCommand_Implements(t *testing.T) {
    17  	t.Parallel()
    18  	var _ cli.Command = &AllocStatusCommand{}
    19  }
    20  
    21  func TestAllocStatusCommand_Fails(t *testing.T) {
    22  	t.Parallel()
    23  	srv, _, url := testServer(t, false, nil)
    24  	defer srv.Shutdown()
    25  
    26  	ui := new(cli.MockUi)
    27  	cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
    28  
    29  	// Fails on misuse
    30  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    31  		t.Fatalf("expected exit code 1, got: %d", code)
    32  	}
    33  	if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
    34  		t.Fatalf("expected help output, got: %s", out)
    35  	}
    36  	ui.ErrorWriter.Reset()
    37  
    38  	// Fails on connection failure
    39  	if code := cmd.Run([]string{"-address=nope", "foobar"}); code != 1 {
    40  		t.Fatalf("expected exit code 1, got: %d", code)
    41  	}
    42  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying allocation") {
    43  		t.Fatalf("expected failed query error, got: %s", out)
    44  	}
    45  	ui.ErrorWriter.Reset()
    46  
    47  	// Fails on missing alloc
    48  	if code := cmd.Run([]string{"-address=" + url, "26470238-5CF2-438F-8772-DC67CFB0705C"}); code != 1 {
    49  		t.Fatalf("expected exit 1, got: %d", code)
    50  	}
    51  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
    52  		t.Fatalf("expected not found error, got: %s", out)
    53  	}
    54  	ui.ErrorWriter.Reset()
    55  
    56  	// Fail on identifier with too few characters
    57  	if code := cmd.Run([]string{"-address=" + url, "2"}); code != 1 {
    58  		t.Fatalf("expected exit 1, got: %d", code)
    59  	}
    60  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "must contain at least two characters.") {
    61  		t.Fatalf("expected too few characters error, got: %s", out)
    62  	}
    63  	ui.ErrorWriter.Reset()
    64  
    65  	// Identifiers with uneven length should produce a query result
    66  	if code := cmd.Run([]string{"-address=" + url, "123"}); code != 1 {
    67  		t.Fatalf("expected exit 1, got: %d", code)
    68  	}
    69  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
    70  		t.Fatalf("expected not found error, got: %s", out)
    71  	}
    72  	ui.ErrorWriter.Reset()
    73  
    74  	// Failed on both -json and -t options are specified
    75  	if code := cmd.Run([]string{"-address=" + url, "-json", "-t", "{{.ID}}"}); code != 1 {
    76  		t.Fatalf("expected exit 1, got: %d", code)
    77  	}
    78  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Both json and template formatting are not allowed") {
    79  		t.Fatalf("expected getting formatter error, got: %s", out)
    80  	}
    81  }
    82  
    83  func TestAllocStatusCommand_Run(t *testing.T) {
    84  	t.Parallel()
    85  	srv, client, url := testServer(t, true, nil)
    86  	defer srv.Shutdown()
    87  
    88  	// Wait for a node to be ready
    89  	testutil.WaitForResult(func() (bool, error) {
    90  		nodes, _, err := client.Nodes().List(nil)
    91  		if err != nil {
    92  			return false, err
    93  		}
    94  		for _, node := range nodes {
    95  			if node.Status == structs.NodeStatusReady {
    96  				return true, nil
    97  			}
    98  		}
    99  		return false, fmt.Errorf("no ready nodes")
   100  	}, func(err error) {
   101  		t.Fatalf("err: %v", err)
   102  	})
   103  
   104  	ui := new(cli.MockUi)
   105  	cmd := &AllocStatusCommand{Meta: Meta{Ui: ui}}
   106  
   107  	jobID := "job1_sfx"
   108  	job1 := testJob(jobID)
   109  	resp, _, err := client.Jobs().Register(job1, nil)
   110  	if err != nil {
   111  		t.Fatalf("err: %s", err)
   112  	}
   113  	if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
   114  		t.Fatalf("status code non zero saw %d", code)
   115  	}
   116  	// get an alloc id
   117  	allocId1 := ""
   118  	if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
   119  		if len(allocs) > 0 {
   120  			allocId1 = allocs[0].ID
   121  		}
   122  	}
   123  	if allocId1 == "" {
   124  		t.Fatal("unable to find an allocation")
   125  	}
   126  
   127  	if code := cmd.Run([]string{"-address=" + url, allocId1}); code != 0 {
   128  		t.Fatalf("expected exit 0, got: %d", code)
   129  	}
   130  	out := ui.OutputWriter.String()
   131  	if !strings.Contains(out, "Created") {
   132  		t.Fatalf("expected to have 'Created' but saw: %s", out)
   133  	}
   134  
   135  	if !strings.Contains(out, "Modified") {
   136  		t.Fatalf("expected to have 'Modified' but saw: %s", out)
   137  	}
   138  
   139  	ui.OutputWriter.Reset()
   140  
   141  	if code := cmd.Run([]string{"-address=" + url, "-verbose", allocId1}); code != 0 {
   142  		t.Fatalf("expected exit 0, got: %d", code)
   143  	}
   144  	out = ui.OutputWriter.String()
   145  	if !strings.Contains(out, allocId1) {
   146  		t.Fatal("expected to find alloc id in output")
   147  	}
   148  	if !strings.Contains(out, "Created") {
   149  		t.Fatalf("expected to have 'Created' but saw: %s", out)
   150  	}
   151  	ui.OutputWriter.Reset()
   152  
   153  	// Try the query with an even prefix that includes the hyphen
   154  	if code := cmd.Run([]string{"-address=" + url, allocId1[:13]}); code != 0 {
   155  		t.Fatalf("expected exit 0, got: %d", code)
   156  	}
   157  	out = ui.OutputWriter.String()
   158  	if !strings.Contains(out, "Created") {
   159  		t.Fatalf("expected to have 'Created' but saw: %s", out)
   160  	}
   161  	ui.OutputWriter.Reset()
   162  
   163  	if code := cmd.Run([]string{"-address=" + url, "-verbose", allocId1}); code != 0 {
   164  		t.Fatalf("expected exit 0, got: %d", code)
   165  	}
   166  	out = ui.OutputWriter.String()
   167  	if !strings.Contains(out, allocId1) {
   168  		t.Fatal("expected to find alloc id in output")
   169  	}
   170  	ui.OutputWriter.Reset()
   171  }
   172  
   173  func TestAllocStatusCommand_AutocompleteArgs(t *testing.T) {
   174  	assert := assert.New(t)
   175  	t.Parallel()
   176  
   177  	srv, _, url := testServer(t, true, nil)
   178  	defer srv.Shutdown()
   179  
   180  	ui := new(cli.MockUi)
   181  	cmd := &AllocStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
   182  
   183  	// Create a fake alloc
   184  	state := srv.Agent.Server().State()
   185  	a := mock.Alloc()
   186  	assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}))
   187  
   188  	prefix := a.ID[:5]
   189  	args := complete.Args{Last: prefix}
   190  	predictor := cmd.AutocompleteArgs()
   191  
   192  	res := predictor.Predict(args)
   193  	assert.Equal(1, len(res))
   194  	assert.Equal(a.ID, res[0])
   195  }