github.com/hernad/nomad@v1.6.112/command/quota_list_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 //go:build ent 5 // +build ent 6 7 package command 8 9 import ( 10 "strings" 11 "testing" 12 13 "github.com/hernad/nomad/ci" 14 "github.com/mitchellh/cli" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 func TestQuotaListCommand_Implements(t *testing.T) { 19 ci.Parallel(t) 20 var _ cli.Command = &QuotaListCommand{} 21 } 22 23 func TestQuotaListCommand_Fails(t *testing.T) { 24 ci.Parallel(t) 25 ui := cli.NewMockUi() 26 cmd := &QuotaListCommand{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"}); code != 1 { 38 t.Fatalf("expected exit code 1, got: %d", code) 39 } 40 if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error retrieving quotas") { 41 t.Fatalf("expected failed query error, got: %s", out) 42 } 43 ui.ErrorWriter.Reset() 44 } 45 46 func TestQuotaListCommand_List(t *testing.T) { 47 ci.Parallel(t) 48 assert := assert.New(t) 49 50 // Create a server 51 srv, client, url := testServer(t, true, nil) 52 defer srv.Shutdown() 53 54 ui := cli.NewMockUi() 55 cmd := &QuotaListCommand{Meta: Meta{Ui: ui}} 56 57 // Create a quota 58 qs := testQuotaSpec() 59 _, err := client.Quotas().Register(qs, nil) 60 assert.Nil(err) 61 62 // List should contain the new quota 63 if code := cmd.Run([]string{"-address=" + url}); code != 0 { 64 t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String()) 65 } 66 out := ui.OutputWriter.String() 67 if !strings.Contains(out, qs.Name) || !strings.Contains(out, qs.Description) { 68 t.Fatalf("expected quota, got: %s", out) 69 } 70 ui.OutputWriter.Reset() 71 72 // List json 73 t.Log(url) 74 if code := cmd.Run([]string{"-address=" + url, "-json"}); code != 0 { 75 t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String()) 76 } 77 out = ui.OutputWriter.String() 78 if !strings.Contains(out, "CreateIndex") { 79 t.Fatalf("expected json output, got: %s", out) 80 } 81 ui.OutputWriter.Reset() 82 }