github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/quota_status_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 TestQuotaStatusCommand_Implements(t *testing.T) {
    17  	ci.Parallel(t)
    18  	var _ cli.Command = &QuotaStatusCommand{}
    19  }
    20  
    21  func TestQuotaStatusCommand_Fails(t *testing.T) {
    22  	ci.Parallel(t)
    23  	ui := cli.NewMockUi()
    24  	cmd := &QuotaStatusCommand{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 TestQuotaStatusCommand_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 := &QuotaStatusCommand{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  	// Check for basic spec
    65  	out := ui.OutputWriter.String()
    66  	if !strings.Contains(out, "= test") {
    67  		t.Fatalf("expected quota, got: %s", out)
    68  	}
    69  
    70  	// Check for usage
    71  	if !strings.Contains(out, "0 / 100") {
    72  		t.Fatalf("expected quota, got: %s", out)
    73  	}
    74  }
    75  
    76  func TestQuotaStatusCommand_AutocompleteArgs(t *testing.T) {
    77  	ci.Parallel(t)
    78  	assert := assert.New(t)
    79  
    80  	srv, client, url := testServer(t, true, nil)
    81  	defer srv.Shutdown()
    82  
    83  	ui := cli.NewMockUi()
    84  	cmd := &QuotaStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    85  
    86  	// Create a quota
    87  	qs := testQuotaSpec()
    88  	_, err := client.Quotas().Register(qs, nil)
    89  	assert.Nil(err)
    90  
    91  	args := complete.Args{Last: "t"}
    92  	predictor := cmd.AutocompleteArgs()
    93  
    94  	res := predictor.Predict(args)
    95  	assert.Equal(1, len(res))
    96  	assert.Equal(qs.Name, res[0])
    97  }