github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/quota_inspect_test.go (about)

     1  //go:build ent
     2  // +build ent
     3  
     4  package command
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/nomad/ci"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/posener/complete"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestQuotaInspectCommand_Implements(t *testing.T) {
    17  	ci.Parallel(t)
    18  	var _ cli.Command = &QuotaInspectCommand{}
    19  }
    20  
    21  func TestQuotaInspectCommand_Fails(t *testing.T) {
    22  	ci.Parallel(t)
    23  	ui := cli.NewMockUi()
    24  	cmd := &QuotaInspectCommand{Meta: Meta{Ui: ui}}
    25  
    26  	// Fails on misuse
    27  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    28  		t.Fatalf("expected exit code 1, got: %d", code)
    29  	}
    30  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    31  		t.Fatalf("expected help output, got: %s", out)
    32  	}
    33  	ui.ErrorWriter.Reset()
    34  
    35  	if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
    36  		t.Fatalf("expected exit code 1, got: %d", code)
    37  	}
    38  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "retrieving quota") {
    39  		t.Fatalf("connection error, got: %s", out)
    40  	}
    41  	ui.ErrorWriter.Reset()
    42  }
    43  
    44  func TestQuotaInspectCommand_Good(t *testing.T) {
    45  	ci.Parallel(t)
    46  
    47  	// Create a server
    48  	srv, client, url := testServer(t, true, nil)
    49  	defer srv.Shutdown()
    50  
    51  	ui := cli.NewMockUi()
    52  	cmd := &QuotaInspectCommand{Meta: Meta{Ui: ui}}
    53  
    54  	// Create a quota to delete
    55  	qs := testQuotaSpec()
    56  	_, err := client.Quotas().Register(qs, nil)
    57  	assert.Nil(t, err)
    58  
    59  	// Delete a namespace
    60  	if code := cmd.Run([]string{"-address=" + url, qs.Name}); code != 0 {
    61  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    62  	}
    63  
    64  	out := ui.OutputWriter.String()
    65  	if !strings.Contains(out, "Usages") || !strings.Contains(out, qs.Name) {
    66  		t.Fatalf("expected quota, got: %s", out)
    67  	}
    68  }
    69  
    70  func TestQuotaInspectCommand_AutocompleteArgs(t *testing.T) {
    71  	ci.Parallel(t)
    72  	assert := assert.New(t)
    73  
    74  	srv, client, url := testServer(t, true, nil)
    75  	defer srv.Shutdown()
    76  
    77  	ui := cli.NewMockUi()
    78  	cmd := &QuotaInspectCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    79  
    80  	// Create a quota
    81  	qs := testQuotaSpec()
    82  	_, err := client.Quotas().Register(qs, nil)
    83  	assert.Nil(err)
    84  
    85  	args := complete.Args{Last: "t"}
    86  	predictor := cmd.AutocompleteArgs()
    87  
    88  	res := predictor.Predict(args)
    89  	assert.Equal(1, len(res))
    90  	assert.Equal(qs.Name, res[0])
    91  }