github.com/ThomasObenaus/nomad@v0.11.1/command/quota_list_test.go (about)

     1  // +build ent
     2  
     3  package command
     4  
     5  import (
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/mitchellh/cli"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestQuotaListCommand_Implements(t *testing.T) {
    14  	t.Parallel()
    15  	var _ cli.Command = &QuotaListCommand{}
    16  }
    17  
    18  func TestQuotaListCommand_Fails(t *testing.T) {
    19  	t.Parallel()
    20  	ui := new(cli.MockUi)
    21  	cmd := &QuotaListCommand{Meta: Meta{Ui: ui}}
    22  
    23  	// Fails on misuse
    24  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    25  		t.Fatalf("expected exit code 1, got: %d", code)
    26  	}
    27  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    28  		t.Fatalf("expected help output, got: %s", out)
    29  	}
    30  	ui.ErrorWriter.Reset()
    31  
    32  	if code := cmd.Run([]string{"-address=nope"}); code != 1 {
    33  		t.Fatalf("expected exit code 1, got: %d", code)
    34  	}
    35  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error retrieving quotas") {
    36  		t.Fatalf("expected failed query error, got: %s", out)
    37  	}
    38  	ui.ErrorWriter.Reset()
    39  }
    40  
    41  func TestQuotaListCommand_List(t *testing.T) {
    42  	t.Parallel()
    43  	assert := assert.New(t)
    44  
    45  	// Create a server
    46  	srv, client, url := testServer(t, true, nil)
    47  	defer srv.Shutdown()
    48  
    49  	ui := new(cli.MockUi)
    50  	cmd := &QuotaListCommand{Meta: Meta{Ui: ui}}
    51  
    52  	// Create a quota
    53  	qs := testQuotaSpec()
    54  	_, err := client.Quotas().Register(qs, nil)
    55  	assert.Nil(err)
    56  
    57  	// List should contain the new quota
    58  	if code := cmd.Run([]string{"-address=" + url}); code != 0 {
    59  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    60  	}
    61  	out := ui.OutputWriter.String()
    62  	if !strings.Contains(out, qs.Name) || !strings.Contains(out, qs.Description) {
    63  		t.Fatalf("expected quota, got: %s", out)
    64  	}
    65  	ui.OutputWriter.Reset()
    66  
    67  	// List json
    68  	t.Log(url)
    69  	if code := cmd.Run([]string{"-address=" + url, "-json"}); code != 0 {
    70  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    71  	}
    72  	out = ui.OutputWriter.String()
    73  	if !strings.Contains(out, "CreateIndex") {
    74  		t.Fatalf("expected json output, got: %s", out)
    75  	}
    76  	ui.OutputWriter.Reset()
    77  }