github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/space_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("space-quotas command", func() {
    22  	var (
    23  		cmd             SpaceQuotasCommand
    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  		orgName         = "some-org-name"
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    37  		fakeConfig = new(commandfakes.FakeConfig)
    38  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    39  		fakeActor = new(v7fakes.FakeActor)
    40  		args = nil
    41  
    42  		cmd = SpaceQuotasCommand{
    43  			BaseCommand: BaseCommand{
    44  				UI:          testUI,
    45  				Config:      fakeConfig,
    46  				SharedActor: fakeSharedActor,
    47  				Actor:       fakeActor,
    48  			},
    49  		}
    50  
    51  		binaryName = "faceman"
    52  		fakeConfig.BinaryNameReturns(binaryName)
    53  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{GUID: "targeted-org-guid"})
    54  		fakeConfig.TargetedOrganizationNameReturns(orgName)
    55  	})
    56  
    57  	JustBeforeEach(func() {
    58  		executeErr = cmd.Execute(args)
    59  	})
    60  
    61  	When("running the command successfully", func() {
    62  		BeforeEach(func() {
    63  			fakeActor.GetCurrentUserReturns(configv3.User{Name: "apple"}, nil)
    64  			spaceQuotas := []resources.SpaceQuota{
    65  				{
    66  					Quota: resources.Quota{
    67  						Name: "space-quota-1",
    68  						Apps: resources.AppLimit{
    69  							TotalMemory:       &types.NullInt{Value: 1048576, IsSet: true},
    70  							InstanceMemory:    &types.NullInt{Value: 32, IsSet: true},
    71  							TotalAppInstances: &types.NullInt{Value: 3, IsSet: true},
    72  							TotalLogVolume:    &types.NullInt{Value: 512, IsSet: true},
    73  						},
    74  						Services: resources.ServiceLimit{
    75  							TotalServiceInstances: &types.NullInt{Value: 3, IsSet: true},
    76  							PaidServicePlans:      &trueValue,
    77  						},
    78  						Routes: resources.RouteLimit{
    79  							TotalRoutes:        &types.NullInt{Value: 5, IsSet: true},
    80  							TotalReservedPorts: &types.NullInt{Value: 2, IsSet: true},
    81  						},
    82  					},
    83  				},
    84  			}
    85  			fakeActor.GetSpaceQuotasByOrgGUIDReturns(spaceQuotas, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
    86  		})
    87  
    88  		It("should print text indicating the command status", func() {
    89  			Expect(executeErr).NotTo(HaveOccurred())
    90  			Expect(testUI.Out).To(Say(`Getting space quotas for org %s as apple\.\.\.`, orgName))
    91  			Expect(testUI.Err).To(Say("some-warning-1"))
    92  			Expect(testUI.Err).To(Say("some-warning-2"))
    93  		})
    94  
    95  		It("retrieves and displays all quotas", func() {
    96  			Expect(executeErr).NotTo(HaveOccurred())
    97  
    98  			Expect(fakeActor.GetSpaceQuotasByOrgGUIDCallCount()).To(Equal(1))
    99  			orgGUID := fakeActor.GetSpaceQuotasByOrgGUIDArgsForCall(0)
   100  			Expect(orgGUID).To(Equal(fakeConfig.TargetedOrganization().GUID))
   101  
   102  			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`))
   103  			Expect(testUI.Out).To(Say(`space-quota-1\s+1T\s+32M\s+5\s+3\s+allowed\s+3\s+2\s+512B`))
   104  		})
   105  
   106  		When("there are limits that have not been configured", func() {
   107  			BeforeEach(func() {
   108  				spaceQuotas := []resources.SpaceQuota{
   109  					{
   110  						Quota: resources.Quota{
   111  							Name: "default",
   112  							Apps: resources.AppLimit{
   113  								TotalMemory:       &types.NullInt{Value: 0, IsSet: false},
   114  								InstanceMemory:    &types.NullInt{Value: 0, IsSet: false},
   115  								TotalAppInstances: &types.NullInt{Value: 0, IsSet: false},
   116  								TotalLogVolume:    &types.NullInt{Value: 0, IsSet: false},
   117  							},
   118  							Services: resources.ServiceLimit{
   119  								TotalServiceInstances: &types.NullInt{Value: 0, IsSet: false},
   120  								PaidServicePlans:      &trueValue,
   121  							},
   122  							Routes: resources.RouteLimit{
   123  								TotalRoutes:        &types.NullInt{Value: 0, IsSet: false},
   124  								TotalReservedPorts: &types.NullInt{Value: 0, IsSet: false},
   125  							},
   126  						},
   127  					},
   128  				}
   129  				fakeActor.GetSpaceQuotasByOrgGUIDReturns(spaceQuotas, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   130  
   131  			})
   132  
   133  			It("should convert default values from the API into readable outputs", func() {
   134  				Expect(executeErr).NotTo(HaveOccurred())
   135  				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`))
   136  				Expect(testUI.Out).To(Say(`default\s+unlimited\s+unlimited\s+unlimited\s+unlimited\s+allowed\s+unlimited\s+unlimited\s+unlimited`))
   137  
   138  			})
   139  		})
   140  	})
   141  
   142  	When("the environment is not set up correctly", func() {
   143  		BeforeEach(func() {
   144  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
   145  		})
   146  
   147  		It("returns an error", func() {
   148  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
   149  
   150  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   151  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   152  			Expect(checkTargetedOrg).To(BeTrue())
   153  			Expect(checkTargetedSpace).To(BeFalse())
   154  		})
   155  	})
   156  
   157  	When("getting quotas fails", func() {
   158  		BeforeEach(func() {
   159  			fakeActor.GetSpaceQuotasByOrgGUIDReturns(nil, v7action.Warnings{"some-warning-1", "some-warning-2"}, errors.New("some-error"))
   160  		})
   161  
   162  		It("prints warnings and returns error", func() {
   163  			Expect(executeErr).To(MatchError("some-error"))
   164  
   165  			Expect(testUI.Err).To(Say("some-warning-1"))
   166  			Expect(testUI.Err).To(Say("some-warning-2"))
   167  		})
   168  	})
   169  
   170  	When("the quota list is empty", func() {
   171  		BeforeEach(func() {
   172  			fakeActor.GetCurrentUserReturns(configv3.User{Name: "apple"}, nil)
   173  			fakeActor.GetSpaceQuotasByOrgGUIDReturns([]resources.SpaceQuota{}, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   174  		})
   175  
   176  		It("prints warnings and returns error", func() {
   177  			Expect(executeErr).NotTo(HaveOccurred())
   178  
   179  			Expect(testUI.Err).To(Say("some-warning-1"))
   180  			Expect(testUI.Err).To(Say("some-warning-2"))
   181  			Expect(testUI.Out).To(Say(`Getting space quotas for org %s as apple\.\.\.`, orgName))
   182  			Expect(testUI.Out).To(Say("No space quotas found."))
   183  		})
   184  	})
   185  })