github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/service/purge_service_instance_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/commands/service"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	cferrors "code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    14  
    15  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    16  
    17  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    18  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    19  
    20  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    21  
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  )
    25  
    26  var _ = Describe("PurgeServiceInstance", func() {
    27  	var (
    28  		ui          *testterm.FakeUI
    29  		configRepo  coreconfig.Repository
    30  		serviceRepo *apifakes.FakeServiceRepository
    31  
    32  		cmd         commandregistry.Command
    33  		deps        commandregistry.Dependency
    34  		factory     *requirementsfakes.FakeFactory
    35  		flagContext flags.FlagContext
    36  
    37  		loginRequirement requirements.Requirement
    38  	)
    39  
    40  	BeforeEach(func() {
    41  		ui = &testterm.FakeUI{}
    42  		configRepo = testconfig.NewRepositoryWithDefaults()
    43  		serviceRepo = new(apifakes.FakeServiceRepository)
    44  		repoLocator := deps.RepoLocator.SetServiceRepository(serviceRepo)
    45  
    46  		deps = commandregistry.Dependency{
    47  			UI:          ui,
    48  			Config:      configRepo,
    49  			RepoLocator: repoLocator,
    50  		}
    51  
    52  		cmd = &service.PurgeServiceInstance{}
    53  		cmd.SetDependency(deps, false)
    54  
    55  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    56  
    57  		factory = new(requirementsfakes.FakeFactory)
    58  
    59  		loginRequirement = &passingRequirement{}
    60  		factory.NewLoginRequirementReturns(loginRequirement)
    61  	})
    62  
    63  	Describe("Requirements", func() {
    64  		Context("when not provided exactly one arg", func() {
    65  			BeforeEach(func() {
    66  				flagContext.Parse("service-instance", "extra-arg")
    67  			})
    68  
    69  			It("fails with usage", func() {
    70  				_, err := cmd.Requirements(factory, flagContext)
    71  				Expect(err).To(HaveOccurred())
    72  				Expect(ui.Outputs()).To(ContainSubstrings(
    73  					[]string{"Incorrect Usage. Requires an argument"},
    74  					[]string{"NAME"},
    75  					[]string{"USAGE"},
    76  				))
    77  			})
    78  		})
    79  
    80  		Context("when provided exactly one arg", func() {
    81  			BeforeEach(func() {
    82  				flagContext.Parse("service-instance")
    83  			})
    84  
    85  			It("returns a LoginRequirement", func() {
    86  				actualRequirements, err := cmd.Requirements(factory, flagContext)
    87  				Expect(err).NotTo(HaveOccurred())
    88  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
    89  
    90  				Expect(actualRequirements).To(ContainElement(loginRequirement))
    91  			})
    92  		})
    93  	})
    94  
    95  	Describe("Execute", func() {
    96  		BeforeEach(func() {
    97  			err := flagContext.Parse("service-instance-name")
    98  			Expect(err).NotTo(HaveOccurred())
    99  			cmd.Requirements(factory, flagContext)
   100  		})
   101  
   102  		It("finds the instance by name in the service repo", func() {
   103  			err := flagContext.Parse("service-instance-name", "-f")
   104  			Expect(err).NotTo(HaveOccurred())
   105  			cmd.Execute(flagContext)
   106  			Expect(serviceRepo.FindInstanceByNameCallCount()).To(Equal(1))
   107  		})
   108  
   109  		Context("when the instance can be found", func() {
   110  			var serviceInstance models.ServiceInstance
   111  
   112  			BeforeEach(func() {
   113  				serviceInstance = models.ServiceInstance{}
   114  				serviceInstance.Name = "service-instance-name"
   115  				serviceRepo.FindInstanceByNameReturns(serviceInstance, nil)
   116  			})
   117  
   118  			It("warns the user", func() {
   119  				ui.Inputs = []string{"n"}
   120  				cmd.Execute(flagContext)
   121  				Eventually(func() []string {
   122  					return ui.Outputs()
   123  				}).Should(ContainSubstrings(
   124  					[]string{"WARNING"},
   125  				))
   126  			})
   127  
   128  			It("asks the user if they would like to proceed", func() {
   129  				ui.Inputs = []string{"n"}
   130  				cmd.Execute(flagContext)
   131  				Eventually(func() []string { return ui.Prompts }).Should(ContainSubstrings(
   132  					[]string{"Really purge service instance service-instance-name from Cloud Foundry?"},
   133  				))
   134  			})
   135  
   136  			It("purges the service instance when the response is to proceed", func() {
   137  				ui.Inputs = []string{"y"}
   138  				cmd.Execute(flagContext)
   139  
   140  				Eventually(serviceRepo.PurgeServiceInstanceCallCount).Should(Equal(1))
   141  				Expect(serviceRepo.PurgeServiceInstanceArgsForCall(0)).To(Equal(serviceInstance))
   142  			})
   143  
   144  			It("does not purge the service instance when the response is not to proceed", func() {
   145  				ui.Inputs = []string{"n"}
   146  				cmd.Execute(flagContext)
   147  				Consistently(serviceRepo.PurgeServiceInstanceCallCount).Should(BeZero())
   148  			})
   149  
   150  			Context("when force is set", func() {
   151  				BeforeEach(func() {
   152  					err := flagContext.Parse("service-instance-name", "-f")
   153  					Expect(err).NotTo(HaveOccurred())
   154  				})
   155  
   156  				It("does not ask the user if they would like to proceed", func() {
   157  					Expect(ui.Prompts).NotTo(ContainSubstrings(
   158  						[]string{"Really purge service instance service-instance-name from Cloud Foundry?"},
   159  					))
   160  				})
   161  
   162  				It("purges the service instance", func() {
   163  					cmd.Execute(flagContext)
   164  					Expect(serviceRepo.PurgeServiceInstanceCallCount()).To(Equal(1))
   165  					Expect(serviceRepo.PurgeServiceInstanceArgsForCall(0)).To(Equal(serviceInstance))
   166  				})
   167  			})
   168  		})
   169  
   170  		Context("when the instance can not be found", func() {
   171  			BeforeEach(func() {
   172  				serviceRepo.FindInstanceByNameReturns(models.ServiceInstance{}, cferrors.NewModelNotFoundError("model-type", "model-name"))
   173  			})
   174  
   175  			It("prints a warning", func() {
   176  				cmd.Execute(flagContext)
   177  				Expect(ui.Outputs()).To(ContainSubstrings(
   178  					[]string{"Service instance service-instance-name not found"},
   179  				))
   180  			})
   181  		})
   182  
   183  		Context("when an error occurs fetching the instance", func() {
   184  			var runCLIErr error
   185  
   186  			BeforeEach(func() {
   187  				serviceRepo.FindInstanceByNameReturns(models.ServiceInstance{}, errors.New("an-error"))
   188  			})
   189  
   190  			JustBeforeEach(func() {
   191  				runCLIErr = cmd.Execute(flagContext)
   192  			})
   193  
   194  			It("prints a message with the error", func() {
   195  				Expect(runCLIErr).To(HaveOccurred())
   196  			})
   197  		})
   198  	})
   199  })