github.com/niteshexa/cloudfoundry_cli@v7.1.0+incompatible/command/v7/create_space_quota_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/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	v7 "code.cloudfoundry.org/cli/command/v7"
    12  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    13  	"code.cloudfoundry.org/cli/types"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("create-space-quota Command", func() {
    22  	var (
    23  		cmd             v7.CreateSpaceQuotaCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    28  		binaryName      string
    29  		executeErr      error
    30  
    31  		userName       string
    32  		spaceQuotaName string
    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  
    41  		binaryName = "faceman"
    42  		fakeConfig.BinaryNameReturns(binaryName)
    43  		spaceQuotaName = "some-space-quota"
    44  		userName = "some-user-name"
    45  
    46  		cmd = v7.CreateSpaceQuotaCommand{
    47  			BaseCommand: v7.BaseCommand{
    48  				UI:          testUI,
    49  				Config:      fakeConfig,
    50  				SharedActor: fakeSharedActor,
    51  				Actor:       fakeActor,
    52  			},
    53  			RequiredArgs: flag.SpaceQuota{SpaceQuota: spaceQuotaName},
    54  		}
    55  	})
    56  
    57  	JustBeforeEach(func() {
    58  		executeErr = cmd.Execute(nil)
    59  	})
    60  
    61  	When("the environment is not set up correctly", func() {
    62  		BeforeEach(func() {
    63  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    64  		})
    65  
    66  		It("returns an error", func() {
    67  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    68  
    69  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    70  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    71  			Expect(checkTargetedOrg).To(BeTrue())
    72  			Expect(checkTargetedSpace).To(BeFalse())
    73  		})
    74  	})
    75  
    76  	When("the environment is setup correctly", func() {
    77  		BeforeEach(func() {
    78  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    79  				Name: "some-org-name",
    80  				GUID: "some-org-guid",
    81  			})
    82  			fakeConfig.CurrentUserReturns(configv3.User{
    83  				Name:   userName,
    84  				Origin: "some-user-origin",
    85  			}, nil)
    86  		})
    87  
    88  		It("prints text indicating it is creating a space quota", func() {
    89  			Expect(executeErr).NotTo(HaveOccurred())
    90  			Expect(testUI.Out).To(Say(`Creating space quota %s for org %s as %s\.\.\.`, spaceQuotaName, "some-org-name", userName))
    91  		})
    92  
    93  		When("creating the space quota errors", func() {
    94  			BeforeEach(func() {
    95  				fakeActor.CreateSpaceQuotaReturns(
    96  					v7action.Warnings{"warnings-1", "warnings-2"},
    97  					errors.New("err-create-space-quota"),
    98  				)
    99  			})
   100  
   101  			It("returns an error and displays warnings", func() {
   102  				Expect(executeErr).To(MatchError("err-create-space-quota"))
   103  				Expect(testUI.Err).To(Say("warnings-1"))
   104  				Expect(testUI.Err).To(Say("warnings-2"))
   105  			})
   106  		})
   107  
   108  		When("no flag limits are given", func() {
   109  			var (
   110  				falseValue = false
   111  			)
   112  			BeforeEach(func() {
   113  				fakeActor.CreateSpaceQuotaReturns(
   114  					v7action.Warnings{"warnings-1", "warnings-2"},
   115  					nil,
   116  				)
   117  			})
   118  
   119  			It("creates the space quota in the targeted organization", func() {
   120  				Expect(fakeActor.CreateSpaceQuotaCallCount()).To(Equal(1))
   121  				expectedSpaceQuotaName, expectedOrgGUID, expectedLimits := fakeActor.CreateSpaceQuotaArgsForCall(0)
   122  				Expect(expectedSpaceQuotaName).To(Equal(spaceQuotaName))
   123  				Expect(expectedOrgGUID).To(Equal("some-org-guid"))
   124  				Expect(expectedLimits).To(Equal(v7action.QuotaLimits{PaidServicesAllowed: &falseValue}))
   125  			})
   126  
   127  			It("prints all warnings, text indicating creation completion, ok and then a tip", func() {
   128  				Expect(executeErr).ToNot(HaveOccurred())
   129  				Expect(testUI.Err).To(Say("warnings-1"))
   130  				Expect(testUI.Err).To(Say("warnings-2"))
   131  				Expect(testUI.Out).To(Say("Creating space quota some-space-quota for org some-org-name as some-user-name..."))
   132  				Expect(testUI.Out).To(Say("OK"))
   133  			})
   134  		})
   135  
   136  		When("all flag limits are given", func() {
   137  			BeforeEach(func() {
   138  				cmd.TotalMemory = flag.MemoryWithUnlimited{IsSet: true, Value: 47}
   139  				cmd.PerProcessMemory = flag.MemoryWithUnlimited{IsSet: true, Value: 23}
   140  				cmd.NumAppInstances = flag.IntegerLimit{IsSet: true, Value: 4}
   141  				cmd.PaidServicePlans = true
   142  				cmd.TotalServiceInstances = flag.IntegerLimit{IsSet: true, Value: 9}
   143  				cmd.TotalRoutes = flag.IntegerLimit{IsSet: true, Value: 1}
   144  				cmd.TotalReservedPorts = flag.IntegerLimit{IsSet: true, Value: 7}
   145  
   146  				fakeActor.CreateSpaceQuotaReturns(
   147  					v7action.Warnings{"warnings-1", "warnings-2"},
   148  					nil,
   149  				)
   150  			})
   151  
   152  			It("creates the space quota with the specified limits in the targeted organization", func() {
   153  				Expect(fakeActor.CreateSpaceQuotaCallCount()).To(Equal(1))
   154  				expectedSpaceQuotaName, expectedOrgGUID, expectedLimits := fakeActor.CreateSpaceQuotaArgsForCall(0)
   155  				Expect(expectedSpaceQuotaName).To(Equal(spaceQuotaName))
   156  				Expect(expectedOrgGUID).To(Equal("some-org-guid"))
   157  				trueValue := true
   158  				Expect(expectedLimits).To(Equal(v7action.QuotaLimits{
   159  					TotalMemoryInMB:       &types.NullInt{IsSet: true, Value: 47},
   160  					PerProcessMemoryInMB:  &types.NullInt{IsSet: true, Value: 23},
   161  					TotalInstances:        &types.NullInt{IsSet: true, Value: 4},
   162  					PaidServicesAllowed:   &trueValue,
   163  					TotalServiceInstances: &types.NullInt{IsSet: true, Value: 9},
   164  					TotalRoutes:           &types.NullInt{IsSet: true, Value: 1},
   165  					TotalReservedPorts:    &types.NullInt{IsSet: true, Value: 7},
   166  				}))
   167  			})
   168  
   169  			It("prints all warnings, text indicating creation completion, ok and then a tip", func() {
   170  				Expect(executeErr).ToNot(HaveOccurred())
   171  				Expect(testUI.Err).To(Say("warnings-1"))
   172  				Expect(testUI.Err).To(Say("warnings-2"))
   173  				Expect(testUI.Out).To(Say("Creating space quota some-space-quota for org some-org-name as some-user-name..."))
   174  				Expect(testUI.Out).To(Say("OK"))
   175  			})
   176  		})
   177  
   178  		When("the space quota already exists", func() {
   179  			BeforeEach(func() {
   180  				fakeActor.CreateSpaceQuotaReturns(v7action.Warnings{"some-warning"}, ccerror.QuotaAlreadyExists{Message: "yikes"})
   181  			})
   182  
   183  			It("displays all warnings, that the space quota already exists, and does not error", func() {
   184  				Expect(executeErr).ToNot(HaveOccurred())
   185  
   186  				Expect(testUI.Err).To(Say("some-warning"))
   187  				Expect(testUI.Out).To(Say(`Creating space quota %s for org %s as %s\.\.\.`, spaceQuotaName, "some-org-name", userName))
   188  				Expect(testUI.Err).To(Say(`yikes`))
   189  				Expect(testUI.Out).To(Say("OK"))
   190  			})
   191  		})
   192  	})
   193  })