github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/quota_delete_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/api"
    11  	"github.com/hashicorp/nomad/ci"
    12  	"github.com/hashicorp/nomad/helper/pointer"
    13  	"github.com/mitchellh/cli"
    14  	"github.com/posener/complete"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestQuotaDeleteCommand_Implements(t *testing.T) {
    19  	ci.Parallel(t)
    20  	var _ cli.Command = &QuotaDeleteCommand{}
    21  }
    22  
    23  func TestQuotaDeleteCommand_Fails(t *testing.T) {
    24  	ci.Parallel(t)
    25  	ui := cli.NewMockUi()
    26  	cmd := &QuotaDeleteCommand{Meta: Meta{Ui: ui}}
    27  
    28  	// Fails on misuse
    29  	if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 {
    30  		t.Fatalf("expected exit code 1, got: %d", code)
    31  	}
    32  	if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) {
    33  		t.Fatalf("expected help output, got: %s", out)
    34  	}
    35  	ui.ErrorWriter.Reset()
    36  
    37  	if code := cmd.Run([]string{"-address=nope", "foo"}); code != 1 {
    38  		t.Fatalf("expected exit code 1, got: %d", code)
    39  	}
    40  	if out := ui.ErrorWriter.String(); !strings.Contains(out, "deleting quota") {
    41  		t.Fatalf("connection error, got: %s", out)
    42  	}
    43  	ui.ErrorWriter.Reset()
    44  }
    45  
    46  func TestQuotaDeleteCommand_Good(t *testing.T) {
    47  	ci.Parallel(t)
    48  
    49  	// Create a server
    50  	srv, client, url := testServer(t, true, nil)
    51  	defer srv.Shutdown()
    52  
    53  	ui := cli.NewMockUi()
    54  	cmd := &QuotaDeleteCommand{Meta: Meta{Ui: ui}}
    55  
    56  	// Create a quota to delete
    57  	qs := testQuotaSpec()
    58  	_, err := client.Quotas().Register(qs, nil)
    59  	assert.Nil(t, err)
    60  
    61  	// Delete a namespace
    62  	if code := cmd.Run([]string{"-address=" + url, qs.Name}); code != 0 {
    63  		t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    64  	}
    65  
    66  	quotas, _, err := client.Quotas().List(nil)
    67  	assert.Nil(t, err)
    68  	assert.Len(t, quotas, 0)
    69  }
    70  
    71  func TestQuotaDeleteCommand_AutocompleteArgs(t *testing.T) {
    72  	ci.Parallel(t)
    73  	assert := assert.New(t)
    74  
    75  	srv, client, url := testServer(t, true, nil)
    76  	defer srv.Shutdown()
    77  
    78  	ui := cli.NewMockUi()
    79  	cmd := &QuotaDeleteCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    80  
    81  	// Create a quota
    82  	qs := testQuotaSpec()
    83  	_, err := client.Quotas().Register(qs, nil)
    84  	assert.Nil(err)
    85  
    86  	args := complete.Args{Last: "t"}
    87  	predictor := cmd.AutocompleteArgs()
    88  
    89  	res := predictor.Predict(args)
    90  	assert.Equal(1, len(res))
    91  	assert.Equal(qs.Name, res[0])
    92  }
    93  
    94  // testQuotaSpec returns a test quota specification
    95  func testQuotaSpec() *api.QuotaSpec {
    96  	return &api.QuotaSpec{
    97  		Name: "test",
    98  		Limits: []*api.QuotaLimit{
    99  			{
   100  				Region: "global",
   101  				RegionLimit: &api.Resources{
   102  					CPU: pointer.Of(100),
   103  				},
   104  			},
   105  		},
   106  	}
   107  }