github.com/quite/nomad@v0.8.6/command/quota_delete_test.go (about)

     1  // +build ent
     2  
     3  package command
     4  
     5  import (
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/nomad/api"
    10  	"github.com/hashicorp/nomad/helper"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/posener/complete"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestQuotaDeleteCommand_Implements(t *testing.T) {
    17  	t.Parallel()
    18  	var _ cli.Command = &QuotaDeleteCommand{}
    19  }
    20  
    21  func TestQuotaDeleteCommand_Fails(t *testing.T) {
    22  	t.Parallel()
    23  	ui := new(cli.MockUi)
    24  	cmd := &QuotaDeleteCommand{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, "deleting quota") {
    39  		t.Fatalf("connection error, got: %s", out)
    40  	}
    41  	ui.ErrorWriter.Reset()
    42  }
    43  
    44  func TestQuotaDeleteCommand_Good(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	// Create a server
    48  	srv, client, url := testServer(t, true, nil)
    49  	defer srv.Shutdown()
    50  
    51  	ui := new(cli.MockUi)
    52  	cmd := &QuotaDeleteCommand{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  	quotas, _, err := client.Quotas().List(nil)
    65  	assert.Nil(t, err)
    66  	assert.Len(t, quotas, 0)
    67  }
    68  
    69  func TestQuotaDeleteCommand_AutocompleteArgs(t *testing.T) {
    70  	assert := assert.New(t)
    71  	t.Parallel()
    72  
    73  	srv, client, url := testServer(t, true, nil)
    74  	defer srv.Shutdown()
    75  
    76  	ui := new(cli.MockUi)
    77  	cmd := &QuotaDeleteCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    78  
    79  	// Create a quota
    80  	qs := testQuotaSpec()
    81  	_, err := client.Quotas().Register(qs, nil)
    82  	assert.Nil(err)
    83  
    84  	args := complete.Args{Last: "t"}
    85  	predictor := cmd.AutocompleteArgs()
    86  
    87  	res := predictor.Predict(args)
    88  	assert.Equal(1, len(res))
    89  	assert.Equal(qs.Name, res[0])
    90  }
    91  
    92  // testQuotaSpec returns a test quota specification
    93  func testQuotaSpec() *api.QuotaSpec {
    94  	return &api.QuotaSpec{
    95  		Name: "test",
    96  		Limits: []*api.QuotaLimit{
    97  			{
    98  				Region: "global",
    99  				RegionLimit: &api.Resources{
   100  					CPU: helper.IntToPtr(100),
   101  				},
   102  			},
   103  		},
   104  	}
   105  }