github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v6/update_service_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	"code.cloudfoundry.org/cli/command/flag"
    10  	"code.cloudfoundry.org/cli/command/translatableerror"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("update-service Command", func() {
    21  
    22  	const (
    23  		serviceInstanceName = "my-service"
    24  		spaceGUID           = "space-guid"
    25  		instanceGUID        = "instance-guid"
    26  		planGUID            = "plan-guid"
    27  	)
    28  
    29  	var (
    30  		cmd             UpdateServiceCommand
    31  		fakeActor       *v6fakes.FakeUpdateServiceActor
    32  		fakeSharedActor *commandfakes.FakeSharedActor
    33  		fakeConfig      *commandfakes.FakeConfig
    34  		testUI          *ui.UI
    35  		input           *Buffer
    36  		executeErr      error
    37  		extraArgs       []string
    38  
    39  		space = configv3.Space{Name: "space-a", GUID: spaceGUID}
    40  	)
    41  
    42  	BeforeEach(func() {
    43  		input = NewBuffer()
    44  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    45  		fakeActor = new(v6fakes.FakeUpdateServiceActor)
    46  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    47  		fakeConfig = new(commandfakes.FakeConfig)
    48  
    49  		fakeConfig.TargetedSpaceReturns(space)
    50  
    51  		extraArgs = []string{}
    52  
    53  		cmd = UpdateServiceCommand{
    54  			UI:           testUI,
    55  			Actor:        fakeActor,
    56  			SharedActor:  fakeSharedActor,
    57  			Config:       fakeConfig,
    58  			RequiredArgs: flag.ServiceInstance{ServiceInstance: serviceInstanceName},
    59  		}
    60  	})
    61  
    62  	JustBeforeEach(func() {
    63  		executeErr = cmd.Execute(extraArgs)
    64  	})
    65  
    66  	When("not upgrading", func() {
    67  		It("returns UnrefactoredCommandError", func() {
    68  			// delegates non-upgrades to legacy code
    69  			Expect(executeErr).To(MatchError(translatableerror.UnrefactoredCommandError{}))
    70  		})
    71  	})
    72  
    73  	When("combining upgrade with other flags", func() {
    74  		BeforeEach(func() {
    75  			cmd.Upgrade = true
    76  		})
    77  
    78  		When("tags provided", func() {
    79  			BeforeEach(func() {
    80  				cmd.Tags = "tags"
    81  			})
    82  
    83  			It("returns UpgradeArgumentCombinationError", func() {
    84  				Expect(executeErr).To(MatchError(translatableerror.ArgumentCombinationError{
    85  					Args: []string{"--upgrade", "-t", "-c", "-p"},
    86  				}))
    87  			})
    88  		})
    89  
    90  		When("parameters provided", func() {
    91  			BeforeEach(func() {
    92  				cmd.ParametersAsJSON = "{\"some\": \"stuff\"}"
    93  			})
    94  
    95  			It("returns UpgradeArgumentCombinationError", func() {
    96  				Expect(executeErr).To(MatchError(translatableerror.ArgumentCombinationError{
    97  					Args: []string{"--upgrade", "-t", "-c", "-p"},
    98  				}))
    99  			})
   100  		})
   101  
   102  		When("plan provided", func() {
   103  			BeforeEach(func() {
   104  				cmd.Plan = "new-plan"
   105  			})
   106  
   107  			It("returns UpgradeArgumentCombinationError", func() {
   108  				Expect(executeErr).To(MatchError(translatableerror.ArgumentCombinationError{
   109  					Args: []string{"--upgrade", "-t", "-c", "-p"},
   110  				}))
   111  			})
   112  		})
   113  	})
   114  
   115  	When("upgrading", func() {
   116  		BeforeEach(func() {
   117  			cmd.Upgrade = true
   118  		})
   119  
   120  		It("checks the user is logged in, and targeting an org and space", func() {
   121  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   122  			orgChecked, spaceChecked := fakeSharedActor.CheckTargetArgsForCall(0)
   123  			Expect(orgChecked).To(BeTrue())
   124  			Expect(spaceChecked).To(BeTrue())
   125  		})
   126  
   127  		When("checking the target succeeds", func() {
   128  			When("getting the service instance succeeds", func() {
   129  				BeforeEach(func() {
   130  					fakeActor.GetServiceInstanceByNameAndSpaceReturns(
   131  						v2action.ServiceInstance{GUID: instanceGUID, ServicePlanGUID: planGUID},
   132  						v2action.Warnings{"warning"},
   133  						nil)
   134  				})
   135  
   136  				It("displays any warnings", func() {
   137  					Expect(testUI.Err).To(Say("warning"))
   138  				})
   139  
   140  				It("mentions that the command is experimental", func() {
   141  					Expect(testUI.Out).To(Say("This command is in EXPERIMENTAL stage and may change without notice\\."))
   142  				})
   143  
   144  				It("prompts the user about the upgrade", func() {
   145  					Expect(testUI.Out).To(Say("You are about to update %s\\.", serviceInstanceName))
   146  					Expect(testUI.Out).To(Say("Warning: This operation may be long running and will block further operations on the service until complete\\."))
   147  					Expect(testUI.Out).To(Say("Really update service %s\\? \\[yN\\]:", serviceInstanceName))
   148  				})
   149  
   150  				When("user refuses to proceed with the upgrade", func() {
   151  					BeforeEach(func() {
   152  						input.Write([]byte("n\n"))
   153  					})
   154  
   155  					It("does not send an upgrade request", func() {
   156  						Expect(fakeActor.UpgradeServiceInstanceCallCount()).To(Equal(0))
   157  					})
   158  
   159  					It("cancels the update", func() {
   160  						Expect(executeErr).NotTo(HaveOccurred())
   161  						Expect(testUI.Out).To(Say("Update cancelled"))
   162  					})
   163  				})
   164  
   165  				When("user goes ahead with the upgrade", func() {
   166  					BeforeEach(func() {
   167  						input.Write([]byte("y\n"))
   168  					})
   169  
   170  					It("sends an upgrade request", func() {
   171  						Expect(fakeActor.UpgradeServiceInstanceCallCount()).To(Equal(1), "upgrade should be requested")
   172  
   173  						serviceInstanceGUID, servicePlanGUID := fakeActor.UpgradeServiceInstanceArgsForCall(0)
   174  						Expect(serviceInstanceGUID).To(Equal(instanceGUID))
   175  						Expect(servicePlanGUID).To(Equal(planGUID))
   176  					})
   177  
   178  					When("the update request succeeds", func() {
   179  						It("says that the update was successful", func() {
   180  							Expect(executeErr).NotTo(HaveOccurred())
   181  							Expect(testUI.Out).To(Say("OK"))
   182  						})
   183  					})
   184  
   185  					When("the update request fails", func() {
   186  						BeforeEach(func() {
   187  							fakeActor.UpgradeServiceInstanceReturns(
   188  								v2action.Warnings{},
   189  								fmt.Errorf("bad things happened"),
   190  							)
   191  						})
   192  
   193  						It("says that the update has failed", func() {
   194  							Expect(executeErr).To(MatchError("bad things happened"))
   195  						})
   196  					})
   197  
   198  					When("there are warnings", func() {
   199  						BeforeEach(func() {
   200  							fakeActor.UpgradeServiceInstanceReturns(
   201  								v2action.Warnings{"fake upgrade warning 1", "fake upgrade warning 2"},
   202  								nil,
   203  							)
   204  						})
   205  
   206  						It("outputs the warnings", func() {
   207  							Expect(testUI.Err).To(Say("fake upgrade warning 1"))
   208  							Expect(testUI.Err).To(Say("fake upgrade warning 2"))
   209  						})
   210  
   211  						It("can still output OK", func() {
   212  							Expect(testUI.Out).To(Say("OK"))
   213  						})
   214  					})
   215  				})
   216  
   217  				When("user presses return", func() {
   218  					BeforeEach(func() {
   219  						input.Write([]byte("\n"))
   220  					})
   221  
   222  					It("cancels the update", func() {
   223  						Expect(testUI.Out).To(Say("Update cancelled"))
   224  						Expect(executeErr).NotTo(HaveOccurred())
   225  					})
   226  				})
   227  
   228  				When("user does not answer", func() {
   229  					It("fails", func() {
   230  						Expect(executeErr).To(MatchError("EOF"))
   231  					})
   232  				})
   233  			})
   234  
   235  			When("getting the service instance fails", func() {
   236  				BeforeEach(func() {
   237  					fakeActor.GetServiceInstanceByNameAndSpaceReturns(v2action.ServiceInstance{}, v2action.Warnings{"warning"}, errors.New("explode"))
   238  				})
   239  
   240  				It("propagates the error", func() {
   241  					Expect(executeErr).To(MatchError("explode"))
   242  				})
   243  
   244  				It("displays any warnings", func() {
   245  					Expect(testUI.Err).To(Say("warning"))
   246  				})
   247  			})
   248  		})
   249  
   250  		When("too many arguments are provided", func() {
   251  			BeforeEach(func() {
   252  				extraArgs = []string{"extra"}
   253  			})
   254  
   255  			It("returns a TooManyArgumentsError", func() {
   256  				Expect(executeErr).To(MatchError(translatableerror.TooManyArgumentsError{
   257  					ExtraArgument: "extra",
   258  				}))
   259  			})
   260  		})
   261  
   262  		When("checking the target returns an error", func() {
   263  			BeforeEach(func() {
   264  				fakeSharedActor.CheckTargetReturns(errors.New("explode"))
   265  			})
   266  
   267  			It("returns an error", func() {
   268  				Expect(executeErr).To(MatchError("explode"))
   269  			})
   270  		})
   271  	})
   272  })