github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/org_quotas_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v7"
    10  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	"code.cloudfoundry.org/cli/types"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("org-quotas command", func() {
    22  	var (
    23  		cmd             OrgQuotasCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    28  		executeErr      error
    29  		args            []string
    30  		binaryName      string
    31  		trueValue       = true
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v7fakes.FakeActor)
    39  		args = nil
    40  
    41  		cmd = OrgQuotasCommand{
    42  			BaseCommand: BaseCommand{
    43  				UI:          testUI,
    44  				Config:      fakeConfig,
    45  				SharedActor: fakeSharedActor,
    46  				Actor:       fakeActor,
    47  			},
    48  		}
    49  
    50  		binaryName = "faceman"
    51  		fakeConfig.BinaryNameReturns(binaryName)
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(args)
    56  	})
    57  
    58  	When("running the command successfully", func() {
    59  		BeforeEach(func() {
    60  			fakeActor.GetCurrentUserReturns(configv3.User{Name: "apple"}, nil)
    61  			orgQuotas := []resources.OrganizationQuota{
    62  				{
    63  					Quota: resources.Quota{
    64  						Name: "org-quota-1",
    65  						Apps: resources.AppLimit{
    66  							TotalMemory:       &types.NullInt{Value: 1048576, IsSet: true},
    67  							InstanceMemory:    &types.NullInt{Value: 32, IsSet: true},
    68  							TotalAppInstances: &types.NullInt{Value: 3, IsSet: true},
    69  							TotalLogVolume:    &types.NullInt{Value: 512, IsSet: true},
    70  						},
    71  						Services: resources.ServiceLimit{
    72  							TotalServiceInstances: &types.NullInt{Value: 3, IsSet: true},
    73  							PaidServicePlans:      &trueValue,
    74  						},
    75  						Routes: resources.RouteLimit{
    76  							TotalRoutes:        &types.NullInt{Value: 5, IsSet: true},
    77  							TotalReservedPorts: &types.NullInt{Value: 2, IsSet: true},
    78  						},
    79  					},
    80  				},
    81  			}
    82  			fakeActor.GetOrganizationQuotasReturns(orgQuotas, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
    83  		})
    84  
    85  		It("should print text indicating the command status", func() {
    86  			Expect(executeErr).NotTo(HaveOccurred())
    87  			Expect(testUI.Out).To(Say(`Getting org quotas as apple\.\.\.`))
    88  			Expect(testUI.Err).To(Say("some-warning-1"))
    89  			Expect(testUI.Err).To(Say("some-warning-2"))
    90  		})
    91  
    92  		It("retrieves and displays all quotas", func() {
    93  			Expect(executeErr).NotTo(HaveOccurred())
    94  			Expect(fakeActor.GetOrganizationQuotasCallCount()).To(Equal(1))
    95  			Expect(testUI.Out).To(Say(`name\s+total memory\s+instance memory\s+routes\s+service instances\s+paid service plans\s+app instances\s+route ports\s+log volume per second`))
    96  			Expect(testUI.Out).To(Say(`org-quota-1\s+1T\s+32M\s+5\s+3\s+allowed\s+3\s+2\s+512B`))
    97  		})
    98  
    99  		When("there are limits that have not been configured", func() {
   100  			BeforeEach(func() {
   101  				orgQuotas := []resources.OrganizationQuota{
   102  					{
   103  						Quota: resources.Quota{
   104  							Name: "default",
   105  							Apps: resources.AppLimit{
   106  								TotalMemory:       &types.NullInt{Value: 0, IsSet: false},
   107  								InstanceMemory:    &types.NullInt{Value: 0, IsSet: false},
   108  								TotalAppInstances: &types.NullInt{Value: 0, IsSet: false},
   109  								TotalLogVolume:    &types.NullInt{Value: 0, IsSet: false},
   110  							},
   111  							Services: resources.ServiceLimit{
   112  								TotalServiceInstances: &types.NullInt{Value: 0, IsSet: false},
   113  								PaidServicePlans:      &trueValue,
   114  							},
   115  							Routes: resources.RouteLimit{
   116  								TotalRoutes:        &types.NullInt{Value: 0, IsSet: false},
   117  								TotalReservedPorts: &types.NullInt{Value: 0, IsSet: false},
   118  							},
   119  						},
   120  					},
   121  				}
   122  				fakeActor.GetOrganizationQuotasReturns(orgQuotas, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   123  
   124  			})
   125  
   126  			It("should convert default values from the API into readable outputs", func() {
   127  				Expect(executeErr).NotTo(HaveOccurred())
   128  				Expect(testUI.Out).To(Say(`name\s+total memory\s+instance memory\s+routes\s+service instances\s+paid service plans\s+app instances\s+route ports\s+log volume per second`))
   129  				Expect(testUI.Out).To(Say(`default\s+unlimited\s+unlimited\s+unlimited\s+unlimited\s+allowed\s+unlimited\s+unlimited\s+unlimited`))
   130  			})
   131  		})
   132  	})
   133  
   134  	When("the environment is not set up correctly", func() {
   135  		BeforeEach(func() {
   136  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
   137  		})
   138  
   139  		It("returns an error", func() {
   140  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
   141  
   142  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   143  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   144  			Expect(checkTargetedOrg).To(BeFalse())
   145  			Expect(checkTargetedSpace).To(BeFalse())
   146  		})
   147  	})
   148  
   149  	When("getting quotas fails", func() {
   150  		BeforeEach(func() {
   151  			fakeActor.GetOrganizationQuotasReturns(nil, v7action.Warnings{"some-warning-1", "some-warning-2"}, errors.New("some-error"))
   152  		})
   153  
   154  		It("prints warnings and returns error", func() {
   155  			Expect(executeErr).To(MatchError("some-error"))
   156  
   157  			Expect(testUI.Err).To(Say("some-warning-1"))
   158  			Expect(testUI.Err).To(Say("some-warning-2"))
   159  		})
   160  	})
   161  
   162  	When("the quota list is empty", func() {
   163  		BeforeEach(func() {
   164  			fakeActor.GetCurrentUserReturns(configv3.User{Name: "apple"}, nil)
   165  			fakeActor.GetOrganizationQuotasReturns([]resources.OrganizationQuota{}, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   166  		})
   167  
   168  		It("prints warnings and returns error", func() {
   169  			Expect(executeErr).NotTo(HaveOccurred())
   170  
   171  			Expect(testUI.Err).To(Say("some-warning-1"))
   172  			Expect(testUI.Err).To(Say("some-warning-2"))
   173  			Expect(testUI.Out).To(Say(`Getting org quotas as apple\.\.\.`))
   174  			Expect(testUI.Out).To(Say("No organization quotas found."))
   175  		})
   176  	})
   177  })