code.cloudfoundry.org/cli@v7.1.0+incompatible/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  			fakeConfig.CurrentUserReturns(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  						},
    73  						Services: resources.ServiceLimit{
    74  							TotalServiceInstances: &types.NullInt{Value: 3, IsSet: true},
    75  							PaidServicePlans:      &trueValue,
    76  						},
    77  						Routes: resources.RouteLimit{
    78  							TotalRoutes:        &types.NullInt{Value: 5, IsSet: true},
    79  							TotalReservedPorts: &types.NullInt{Value: 2, IsSet: true},
    80  						},
    81  					},
    82  				},
    83  			}
    84  			fakeActor.GetSpaceQuotasByOrgGUIDReturns(spaceQuotas, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
    85  		})
    86  
    87  		It("should print text indicating the command status", func() {
    88  			Expect(executeErr).NotTo(HaveOccurred())
    89  			Expect(testUI.Out).To(Say(`Getting space quotas for org %s as apple\.\.\.`, orgName))
    90  			Expect(testUI.Err).To(Say("some-warning-1"))
    91  			Expect(testUI.Err).To(Say("some-warning-2"))
    92  		})
    93  
    94  		It("retrieves and displays all quotas", func() {
    95  			Expect(executeErr).NotTo(HaveOccurred())
    96  
    97  			Expect(fakeActor.GetSpaceQuotasByOrgGUIDCallCount()).To(Equal(1))
    98  			orgGUID := fakeActor.GetSpaceQuotasByOrgGUIDArgsForCall(0)
    99  			Expect(orgGUID).To(Equal(fakeConfig.TargetedOrganization().GUID))
   100  
   101  			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`))
   102  			Expect(testUI.Out).To(Say(`space-quota-1\s+1T\s+32M\s+5\s+3\s+allowed\s+3\s+2`))
   103  		})
   104  
   105  		When("there are limits that have not been configured", func() {
   106  			BeforeEach(func() {
   107  				spaceQuotas := []resources.SpaceQuota{
   108  					{
   109  						Quota: resources.Quota{
   110  							Name: "default",
   111  							Apps: resources.AppLimit{
   112  								TotalMemory:       &types.NullInt{Value: 0, IsSet: false},
   113  								InstanceMemory:    &types.NullInt{Value: 0, IsSet: false},
   114  								TotalAppInstances: &types.NullInt{Value: 0, IsSet: false},
   115  							},
   116  							Services: resources.ServiceLimit{
   117  								TotalServiceInstances: &types.NullInt{Value: 0, IsSet: false},
   118  								PaidServicePlans:      &trueValue,
   119  							},
   120  							Routes: resources.RouteLimit{
   121  								TotalRoutes:        &types.NullInt{Value: 0, IsSet: false},
   122  								TotalReservedPorts: &types.NullInt{Value: 0, IsSet: false},
   123  							},
   124  						},
   125  					},
   126  				}
   127  				fakeActor.GetSpaceQuotasByOrgGUIDReturns(spaceQuotas, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   128  
   129  			})
   130  
   131  			It("should convert default values from the API into readable outputs", func() {
   132  				Expect(executeErr).NotTo(HaveOccurred())
   133  				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`))
   134  				Expect(testUI.Out).To(Say(`default\s+unlimited\s+unlimited\s+unlimited\s+unlimited\s+allowed\s+unlimited\s+unlimited`))
   135  
   136  			})
   137  		})
   138  	})
   139  
   140  	When("the environment is not set up correctly", func() {
   141  		BeforeEach(func() {
   142  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
   143  		})
   144  
   145  		It("returns an error", func() {
   146  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
   147  
   148  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   149  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   150  			Expect(checkTargetedOrg).To(BeTrue())
   151  			Expect(checkTargetedSpace).To(BeFalse())
   152  		})
   153  	})
   154  
   155  	When("getting quotas fails", func() {
   156  		BeforeEach(func() {
   157  			fakeActor.GetSpaceQuotasByOrgGUIDReturns(nil, v7action.Warnings{"some-warning-1", "some-warning-2"}, errors.New("some-error"))
   158  		})
   159  
   160  		It("prints warnings and returns error", func() {
   161  			Expect(executeErr).To(MatchError("some-error"))
   162  
   163  			Expect(testUI.Err).To(Say("some-warning-1"))
   164  			Expect(testUI.Err).To(Say("some-warning-2"))
   165  		})
   166  	})
   167  
   168  	When("the quota list is empty", func() {
   169  		BeforeEach(func() {
   170  			fakeConfig.CurrentUserReturns(configv3.User{Name: "apple"}, nil)
   171  			fakeActor.GetSpaceQuotasByOrgGUIDReturns([]resources.SpaceQuota{}, v7action.Warnings{"some-warning-1", "some-warning-2"}, nil)
   172  		})
   173  
   174  		It("prints warnings and returns error", func() {
   175  			Expect(executeErr).NotTo(HaveOccurred())
   176  
   177  			Expect(testUI.Err).To(Say("some-warning-1"))
   178  			Expect(testUI.Err).To(Say("some-warning-2"))
   179  			Expect(testUI.Out).To(Say(`Getting space quotas for org %s as apple\.\.\.`, orgName))
   180  			Expect(testUI.Out).To(Say("No space quotas found."))
   181  		})
   182  	})
   183  })