github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/quota_list_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/stretchr/testify/assert" 13 ) 14 15 func TestQuotaListCommand_Implements(t *testing.T) { 16 ci.Parallel(t) 17 var _ cli.Command = &QuotaListCommand{} 18 } 19 20 func TestQuotaListCommand_Fails(t *testing.T) { 21 ci.Parallel(t) 22 ui := cli.NewMockUi() 23 cmd := &QuotaListCommand{Meta: Meta{Ui: ui}} 24 25 // Fails on misuse 26 if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { 27 t.Fatalf("expected exit code 1, got: %d", code) 28 } 29 if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) { 30 t.Fatalf("expected help output, got: %s", out) 31 } 32 ui.ErrorWriter.Reset() 33 34 if code := cmd.Run([]string{"-address=nope"}); code != 1 { 35 t.Fatalf("expected exit code 1, got: %d", code) 36 } 37 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error retrieving quotas") { 38 t.Fatalf("expected failed query error, got: %s", out) 39 } 40 ui.ErrorWriter.Reset() 41 } 42 43 func TestQuotaListCommand_List(t *testing.T) { 44 ci.Parallel(t) 45 assert := assert.New(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 := &QuotaListCommand{Meta: Meta{Ui: ui}} 53 54 // Create a quota 55 qs := testQuotaSpec() 56 _, err := client.Quotas().Register(qs, nil) 57 assert.Nil(err) 58 59 // List should contain the new quota 60 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 61 t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String()) 62 } 63 out := ui.OutputWriter.String() 64 if !strings.Contains(out, qs.Name) || !strings.Contains(out, qs.Description) { 65 t.Fatalf("expected quota, got: %s", out) 66 } 67 ui.OutputWriter.Reset() 68 69 // List json 70 t.Log(url) 71 if code := cmd.Run([]string{"-address=" + url, "-json"}); code != 0 { 72 t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String()) 73 } 74 out = ui.OutputWriter.String() 75 if !strings.Contains(out, "CreateIndex") { 76 t.Fatalf("expected json output, got: %s", out) 77 } 78 ui.OutputWriter.Reset() 79 }