github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/command/status_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/hashicorp/nomad/testutil"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func TestStatusCommand_Implements(t *testing.T) {
    13  	var _ cli.Command = &StatusCommand{}
    14  }
    15  
    16  func TestStatusCommand_Run(t *testing.T) {
    17  	srv, client, url := testServer(t, func(c *testutil.TestServerConfig) {
    18  		c.DevMode = true
    19  	})
    20  	defer srv.Stop()
    21  
    22  	ui := new(cli.MockUi)
    23  	cmd := &StatusCommand{Meta: Meta{Ui: ui}}
    24  
    25  	// Should return blank for no jobs
    26  	if code := cmd.Run([]string{"-address=" + url}); code != 0 {
    27  		t.Fatalf("expected exit 0, got: %d", code)
    28  	}
    29  
    30  	// Check for this awkward nil string, since a nil bytes.Buffer
    31  	// returns this purposely, and mitchellh/cli has a nil pointer
    32  	// if nothing was ever output.
    33  	exp := "No running jobs"
    34  	if out := strings.TrimSpace(ui.OutputWriter.String()); out != exp {
    35  		t.Fatalf("expected %q; got: %q", exp, out)
    36  	}
    37  
    38  	// Register two jobs
    39  	job1 := testJob("job1_sfx")
    40  	evalId1, _, err := client.Jobs().Register(job1, nil)
    41  	if err != nil {
    42  		t.Fatalf("err: %s", err)
    43  	}
    44  	if code := waitForSuccess(ui, client, fullId, t, evalId1); code != 0 {
    45  		t.Fatalf("status code non zero saw %d", code)
    46  	}
    47  
    48  	job2 := testJob("job2_sfx")
    49  	evalId2, _, err := client.Jobs().Register(job2, nil)
    50  	if err != nil {
    51  		t.Fatalf("err: %s", err)
    52  	}
    53  	if code := waitForSuccess(ui, client, fullId, t, evalId2); code != 0 {
    54  		t.Fatalf("status code non zero saw %d", code)
    55  	}
    56  
    57  	// Query again and check the result
    58  	if code := cmd.Run([]string{"-address=" + url}); code != 0 {
    59  		t.Fatalf("expected exit 0, got: %d", code)
    60  	}
    61  	out := ui.OutputWriter.String()
    62  	if !strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") {
    63  		t.Fatalf("expected job1_sfx and job2_sfx, got: %s", out)
    64  	}
    65  	ui.OutputWriter.Reset()
    66  
    67  	// Query a single job
    68  	if code := cmd.Run([]string{"-address=" + url, "job2_sfx"}); code != 0 {
    69  		t.Fatalf("expected exit 0, got: %d", code)
    70  	}
    71  	out = ui.OutputWriter.String()
    72  	if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") {
    73  		t.Fatalf("expected only job2_sfx, got: %s", out)
    74  	}
    75  	if !strings.Contains(out, "Allocations") {
    76  		t.Fatalf("should dump allocations")
    77  	}
    78  	if !strings.Contains(out, "Summary") {
    79  		t.Fatalf("should dump summary")
    80  	}
    81  	ui.OutputWriter.Reset()
    82  
    83  	// Query a single job showing evals
    84  	if code := cmd.Run([]string{"-address=" + url, "-evals", "job2_sfx"}); code != 0 {
    85  		t.Fatalf("expected exit 0, got: %d", code)
    86  	}
    87  	out = ui.OutputWriter.String()
    88  	if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") {
    89  		t.Fatalf("expected only job2_sfx, got: %s", out)
    90  	}
    91  	if !strings.Contains(out, "Evaluations") {
    92  		t.Fatalf("should dump evaluations")
    93  	}
    94  	if !strings.Contains(out, "Allocations") {
    95  		t.Fatalf("should dump allocations")
    96  	}
    97  	ui.OutputWriter.Reset()
    98  
    99  	// Query a single job in verbose mode
   100  	if code := cmd.Run([]string{"-address=" + url, "-verbose", "job2_sfx"}); code != 0 {
   101  		t.Fatalf("expected exit 0, got: %d", code)
   102  	}
   103  	out = ui.OutputWriter.String()
   104  	if strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") {
   105  		t.Fatalf("expected only job2_sfx, got: %s", out)
   106  	}
   107  	if !strings.Contains(out, "Evaluations") {
   108  		t.Fatalf("should dump evaluations")
   109  	}
   110  	if !strings.Contains(out, "Allocations") {
   111  		t.Fatalf("should dump allocations")
   112  	}
   113  	if !strings.Contains(out, "Created At") {
   114  		t.Fatal("should have created header")
   115  	}
   116  	ui.OutputWriter.Reset()
   117  
   118  	// Query jobs with prefix match
   119  	if code := cmd.Run([]string{"-address=" + url, "job"}); code != 0 {
   120  		t.Fatalf("expected exit 0, got: %d", code)
   121  	}
   122  	out = ui.OutputWriter.String()
   123  	if !strings.Contains(out, "job1_sfx") || !strings.Contains(out, "job2_sfx") {
   124  		t.Fatalf("expected job1_sfx and job2_sfx, got: %s", out)
   125  	}
   126  	ui.OutputWriter.Reset()
   127  
   128  	// Query a single job with prefix match
   129  	if code := cmd.Run([]string{"-address=" + url, "job1"}); code != 0 {
   130  		t.Fatalf("expected exit 0, got: %d", code)
   131  	}
   132  	out = ui.OutputWriter.String()
   133  	if !strings.Contains(out, "job1_sfx") || strings.Contains(out, "job2_sfx") {
   134  		t.Fatalf("expected only job1_sfx, got: %s", out)
   135  	}
   136  	ui.OutputWriter.Reset()
   137  
   138  	// Query in short view mode
   139  	if code := cmd.Run([]string{"-address=" + url, "-short", "job2"}); code != 0 {
   140  		t.Fatalf("expected exit 0, got: %d", code)
   141  	}
   142  	out = ui.OutputWriter.String()
   143  	if !strings.Contains(out, "job2") {
   144  		t.Fatalf("expected job2, got: %s", out)
   145  	}
   146  	if strings.Contains(out, "Evaluations") {
   147  		t.Fatalf("should not dump evaluations")
   148  	}
   149  	if strings.Contains(out, "Allocations") {
   150  		t.Fatalf("should not dump allocations")
   151  	}
   152  	if strings.Contains(out, evalId1) {
   153  		t.Fatalf("should not contain full identifiers, got %s", out)
   154  	}
   155  	ui.OutputWriter.Reset()
   156  
   157  	// Request full identifiers
   158  	if code := cmd.Run([]string{"-address=" + url, "-verbose", "job1"}); code != 0 {
   159  		t.Fatalf("expected exit 0, got: %d", code)
   160  	}
   161  	out = ui.OutputWriter.String()
   162  	if !strings.Contains(out, evalId1) {
   163  		t.Fatalf("should contain full identifiers, got %s", out)
   164  	}
   165  }
   166  
   167  func TestStatusCommand_Fails(t *testing.T) {
   168  	ui := new(cli.MockUi)
   169  	cmd := &StatusCommand{Meta: Meta{Ui: ui}}
   170  
   171  	// Fails on misuse
   172  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
   173  		t.Fatalf("expected exit code 1, got: %d", code)
   174  	}
   175  	if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) {
   176  		t.Fatalf("expected help output, got: %s", out)
   177  	}
   178  	ui.ErrorWriter.Reset()
   179  
   180  	// Fails on connection failure
   181  	if code := cmd.Run([]string{"-address=nope"}); code != 1 {
   182  		t.Fatalf("expected exit code 1, got: %d", code)
   183  	}
   184  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error querying jobs") {
   185  		t.Fatalf("expected failed query error, got: %s", out)
   186  	}
   187  }
   188  
   189  func waitForSuccess(ui cli.Ui, client *api.Client, length int, t *testing.T, evalId string) int {
   190  	mon := newMonitor(ui, client, length)
   191  	monErr := mon.monitor(evalId, false)
   192  	return monErr
   193  }