code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/org_quota_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/v7action"
     6  	"code.cloudfoundry.org/cli/command/commandfakes"
     7  	. "code.cloudfoundry.org/cli/command/v7"
     8  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
     9  	"code.cloudfoundry.org/cli/resources"
    10  	"code.cloudfoundry.org/cli/types"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("Org Quota Command", func() {
    19  	var (
    20  		cmd             OrgQuotaCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeActor
    25  		executeErr      error
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    30  		fakeConfig = new(commandfakes.FakeConfig)
    31  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    32  		fakeActor = new(v7fakes.FakeActor)
    33  
    34  		cmd = OrgQuotaCommand{
    35  			BaseCommand: BaseCommand{
    36  				UI:          testUI,
    37  				Config:      fakeConfig,
    38  				SharedActor: fakeSharedActor,
    39  				Actor:       fakeActor,
    40  			},
    41  		}
    42  
    43  		cmd.RequiredArgs.OrganizationQuotaName = "some-org-quota"
    44  	})
    45  
    46  	JustBeforeEach(func() {
    47  		executeErr = cmd.Execute(nil)
    48  	})
    49  
    50  	When("checking the target fails", func() {
    51  		BeforeEach(func() {
    52  			fakeSharedActor.CheckTargetReturns(
    53  				actionerror.NotLoggedInError{BinaryName: "binaryName"})
    54  		})
    55  
    56  		It("returns an error", func() {
    57  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "binaryName"}))
    58  
    59  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    60  			targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    61  			Expect(targetedOrganizationRequired).To(Equal(false))
    62  			Expect(targetedSpaceRequired).To(Equal(false))
    63  		})
    64  	})
    65  
    66  	When("getting the org quota fails", func() {
    67  		BeforeEach(func() {
    68  			fakeConfig.CurrentUserReturns(
    69  				configv3.User{
    70  					Name: "some-user",
    71  				},
    72  				nil)
    73  
    74  			fakeActor.GetOrganizationQuotaByNameReturns(
    75  				resources.OrganizationQuota{},
    76  				v7action.Warnings{"warning-1", "warning-2"},
    77  				actionerror.OrganizationQuotaNotFoundError{})
    78  		})
    79  
    80  		It("returns a translatable error and outputs all warnings", func() {
    81  
    82  			Expect(testUI.Out).To(Say("Getting org quota some-org-quota as some-user..."))
    83  
    84  			Expect(executeErr).To(MatchError(actionerror.OrganizationQuotaNotFoundError{}))
    85  			Expect(fakeActor.GetOrganizationQuotaByNameCallCount()).To(Equal(1))
    86  			Expect(testUI.Err).To(Say("warning-1"))
    87  			Expect(testUI.Err).To(Say("warning-2"))
    88  		})
    89  	})
    90  
    91  	When("getting the org quota succeeds", func() {
    92  		BeforeEach(func() {
    93  			fakeConfig.CurrentUserReturns(
    94  				configv3.User{
    95  					Name: "some-user",
    96  				},
    97  				nil)
    98  
    99  			falseValue := false
   100  			fakeActor.GetOrganizationQuotaByNameReturns(
   101  				resources.OrganizationQuota{
   102  					Quota: resources.Quota{
   103  						Name: "some-org-quota",
   104  						Apps: resources.AppLimit{
   105  							TotalMemory:       &types.NullInt{IsSet: true, Value: 2048},
   106  							InstanceMemory:    &types.NullInt{IsSet: true, Value: 1024},
   107  							TotalAppInstances: &types.NullInt{IsSet: true, Value: 2},
   108  						},
   109  						Services: resources.ServiceLimit{
   110  							TotalServiceInstances: &types.NullInt{IsSet: false},
   111  							PaidServicePlans:      &falseValue,
   112  						},
   113  						Routes: resources.RouteLimit{
   114  							TotalRoutes:        &types.NullInt{IsSet: true, Value: 4},
   115  							TotalReservedPorts: &types.NullInt{IsSet: false},
   116  						},
   117  					},
   118  				},
   119  				v7action.Warnings{"warning-1", "warning-2"},
   120  				nil)
   121  		})
   122  
   123  		It("displays the quota and all warnings", func() {
   124  			Expect(executeErr).ToNot(HaveOccurred())
   125  			Expect(fakeActor.GetOrganizationQuotaByNameCallCount()).To(Equal(1))
   126  			Expect(fakeActor.GetOrganizationQuotaByNameCallCount()).To(Equal(1))
   127  			orgQuotaName := fakeActor.GetOrganizationQuotaByNameArgsForCall(0)
   128  			Expect(orgQuotaName).To(Equal("some-org-quota"))
   129  
   130  			Expect(testUI.Out).To(Say("Getting org quota some-org-quota as some-user..."))
   131  			Expect(testUI.Err).To(Say("warning-1"))
   132  			Expect(testUI.Err).To(Say("warning-2"))
   133  
   134  			Expect(testUI.Out).To(Say(`total memory:\s+2G`))
   135  			Expect(testUI.Out).To(Say(`instance memory:\s+1G`))
   136  			Expect(testUI.Out).To(Say(`routes:\s+4`))
   137  			Expect(testUI.Out).To(Say(`service instances:\s+unlimited`))
   138  			Expect(testUI.Out).To(Say(`paid service plans:\s+disallowed`))
   139  			Expect(testUI.Out).To(Say(`app instances:\s+2`))
   140  			Expect(testUI.Out).To(Say(`route ports:\s+unlimited`))
   141  		})
   142  	})
   143  })