github.com/bigcommerce/nomad@v0.9.3-bc/command/quota_inspect_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/posener/complete"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestQuotaInspectCommand_Implements(t *testing.T) {
    15  	t.Parallel()
    16  	var _ cli.Command = &QuotaInspectCommand{}
    17  }
    18  
    19  func TestQuotaInspectCommand_Fails(t *testing.T) {
    20  	t.Parallel()
    21  	ui := new(cli.MockUi)
    22  	cmd := &QuotaInspectCommand{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, commandErrorText(cmd)) {
    29  		t.Fatalf("expected help output, got: %s", out)
    30  	}
    31  	ui.ErrorWriter.Reset()
    32  
    33  	if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
    34  		t.Fatalf("expected exit code 1, got: %d", code)
    35  	}
    36  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "retrieving quota") {
    37  		t.Fatalf("connection error, got: %s", out)
    38  	}
    39  	ui.ErrorWriter.Reset()
    40  }
    41  
    42  func TestQuotaInspectCommand_Good(t *testing.T) {
    43  	t.Parallel()
    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 := &QuotaInspectCommand{Meta: Meta{Ui: ui}}
    51  
    52  	// Create a quota to delete
    53  	qs := testQuotaSpec()
    54  	_, err := client.Quotas().Register(qs, nil)
    55  	assert.Nil(t, err)
    56  
    57  	// Delete a namespace
    58  	if code := cmd.Run([]string{"-address=" + url, qs.Name}); code != 0 {
    59  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    60  	}
    61  
    62  	out := ui.OutputWriter.String()
    63  	if !strings.Contains(out, "Usages") || !strings.Contains(out, qs.Name) {
    64  		t.Fatalf("expected quota, got: %s", out)
    65  	}
    66  }
    67  
    68  func TestQuotaInspectCommand_AutocompleteArgs(t *testing.T) {
    69  	assert := assert.New(t)
    70  	t.Parallel()
    71  
    72  	srv, client, url := testServer(t, true, nil)
    73  	defer srv.Shutdown()
    74  
    75  	ui := new(cli.MockUi)
    76  	cmd := &QuotaInspectCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    77  
    78  	// Create a quota
    79  	qs := testQuotaSpec()
    80  	_, err := client.Quotas().Register(qs, nil)
    81  	assert.Nil(err)
    82  
    83  	args := complete.Args{Last: "t"}
    84  	predictor := cmd.AutocompleteArgs()
    85  
    86  	res := predictor.Predict(args)
    87  	assert.Equal(1, len(res))
    88  	assert.Equal(qs.Name, res[0])
    89  }