github.com/elopio/cli@v6.21.2-0.20160902224010-ea909d1fdb2f+incompatible/cf/commands/service/purge_service_offering_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/commandregistry"
     5  	"code.cloudfoundry.org/cli/cf/commands/service"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/errors"
     8  	"code.cloudfoundry.org/cli/cf/flags"
     9  	"code.cloudfoundry.org/cli/cf/models"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  
    13  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    14  	testconfig "code.cloudfoundry.org/cli/testhelpers/configuration"
    15  	testterm "code.cloudfoundry.org/cli/testhelpers/terminal"
    16  
    17  	. "code.cloudfoundry.org/cli/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("PurgeServiceOffering", func() {
    23  	var (
    24  		ui          *testterm.FakeUI
    25  		configRepo  coreconfig.Repository
    26  		serviceRepo *apifakes.FakeServiceRepository
    27  
    28  		cmd         commandregistry.Command
    29  		deps        commandregistry.Dependency
    30  		factory     *requirementsfakes.FakeFactory
    31  		flagContext flags.FlagContext
    32  
    33  		loginRequirement         requirements.Requirement
    34  		maxAPIVersionRequirement requirements.Requirement
    35  	)
    36  
    37  	BeforeEach(func() {
    38  		ui = &testterm.FakeUI{}
    39  		configRepo = testconfig.NewRepositoryWithDefaults()
    40  		serviceRepo = new(apifakes.FakeServiceRepository)
    41  		repoLocator := deps.RepoLocator.SetServiceRepository(serviceRepo)
    42  
    43  		deps = commandregistry.Dependency{
    44  			UI:          ui,
    45  			Config:      configRepo,
    46  			RepoLocator: repoLocator,
    47  		}
    48  
    49  		cmd = &service.PurgeServiceOffering{}
    50  		cmd.SetDependency(deps, false)
    51  
    52  		flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    53  		factory = new(requirementsfakes.FakeFactory)
    54  
    55  		loginRequirement = &passingRequirement{Name: "login-requirement"}
    56  		factory.NewLoginRequirementReturns(loginRequirement)
    57  
    58  		maxAPIVersionRequirement = &passingRequirement{Name: "max-api-version-requirement"}
    59  		factory.NewMaxAPIVersionRequirementReturns(maxAPIVersionRequirement)
    60  	})
    61  
    62  	Describe("Requirements", func() {
    63  		Context("when not provided exactly one arg", func() {
    64  			BeforeEach(func() {
    65  				flagContext.Parse("service", "extra-arg")
    66  			})
    67  
    68  			It("fails with usage", func() {
    69  				_, err := cmd.Requirements(factory, flagContext)
    70  				Expect(err).To(HaveOccurred())
    71  				Expect(ui.Outputs()).To(ContainSubstrings(
    72  					[]string{"FAILED"},
    73  					[]string{"Incorrect Usage. Requires an argument"},
    74  				))
    75  			})
    76  		})
    77  
    78  		Context("when provided exactly one arg", func() {
    79  			BeforeEach(func() {
    80  				flagContext.Parse("service")
    81  			})
    82  
    83  			It("returns a LoginRequirement", func() {
    84  				actualRequirements, err := cmd.Requirements(factory, flagContext)
    85  				Expect(err).NotTo(HaveOccurred())
    86  				Expect(factory.NewLoginRequirementCallCount()).To(Equal(1))
    87  				Expect(actualRequirements).To(ContainElement(loginRequirement))
    88  			})
    89  		})
    90  
    91  		Context("when the -p flag is passed", func() {
    92  			BeforeEach(func() {
    93  				flagContext.Parse("service", "-p", "provider-name")
    94  			})
    95  
    96  			It("returns a MaxAPIVersion requirement", func() {
    97  				actualRequirements, err := cmd.Requirements(factory, flagContext)
    98  				Expect(err).NotTo(HaveOccurred())
    99  				Expect(actualRequirements).To(ContainElement(maxAPIVersionRequirement))
   100  			})
   101  		})
   102  	})
   103  
   104  	Describe("Execute", func() {
   105  		var runCLIErr error
   106  
   107  		BeforeEach(func() {
   108  			err := flagContext.Parse("service-name")
   109  			Expect(err).NotTo(HaveOccurred())
   110  			_, err = cmd.Requirements(factory, flagContext)
   111  			Expect(err).NotTo(HaveOccurred())
   112  			ui.Inputs = []string{"n"}
   113  			serviceRepo.FindServiceOfferingsByLabelReturns([]models.ServiceOffering{{}}, nil)
   114  		})
   115  
   116  		JustBeforeEach(func() {
   117  			runCLIErr = cmd.Execute(flagContext)
   118  		})
   119  
   120  		It("tries to find the service offering by label", func() {
   121  			Expect(runCLIErr).NotTo(HaveOccurred())
   122  			Expect(serviceRepo.FindServiceOfferingsByLabelCallCount()).To(Equal(1))
   123  			name := serviceRepo.FindServiceOfferingsByLabelArgsForCall(0)
   124  			Expect(name).To(Equal("service-name"))
   125  		})
   126  
   127  		Context("when finding the service offering succeeds", func() {
   128  			BeforeEach(func() {
   129  				serviceOffering := models.ServiceOffering{}
   130  				serviceOffering.GUID = "service-offering-guid"
   131  				serviceRepo.FindServiceOfferingsByLabelReturns([]models.ServiceOffering{serviceOffering}, nil)
   132  				ui.Inputs = []string{"n"}
   133  			})
   134  
   135  			It("asks the user to confirm", func() {
   136  				Expect(runCLIErr).NotTo(HaveOccurred())
   137  				Expect(ui.Outputs()).To(ContainSubstrings([]string{"WARNING"}))
   138  				Expect(ui.Prompts).To(ContainSubstrings([]string{"Really purge service offering service-name from Cloud Foundry?"}))
   139  			})
   140  
   141  			Context("when the user confirms", func() {
   142  				BeforeEach(func() {
   143  					ui.Inputs = []string{"y"}
   144  				})
   145  
   146  				It("tells the user it will purge the service offering", func() {
   147  					Expect(runCLIErr).NotTo(HaveOccurred())
   148  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"Purging service service-name..."}))
   149  				})
   150  
   151  				It("tries to purge the service offering", func() {
   152  					Expect(runCLIErr).NotTo(HaveOccurred())
   153  					Expect(serviceRepo.PurgeServiceOfferingCallCount()).To(Equal(1))
   154  				})
   155  
   156  				Context("when purging succeeds", func() {
   157  					BeforeEach(func() {
   158  						serviceRepo.PurgeServiceOfferingReturns(nil)
   159  					})
   160  
   161  					It("says OK", func() {
   162  						Expect(runCLIErr).NotTo(HaveOccurred())
   163  						Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   164  					})
   165  				})
   166  
   167  				Context("when purging fails", func() {
   168  					BeforeEach(func() {
   169  						serviceRepo.PurgeServiceOfferingReturns(errors.New("purge-err"))
   170  					})
   171  
   172  					It("fails with error", func() {
   173  						Expect(runCLIErr).To(HaveOccurred())
   174  						Expect(runCLIErr.Error()).To(Equal("purge-err"))
   175  					})
   176  				})
   177  			})
   178  
   179  			Context("when the user does not confirm", func() {
   180  				BeforeEach(func() {
   181  					ui.Inputs = []string{"n"}
   182  				})
   183  
   184  				It("does not try to purge the service offering", func() {
   185  					Expect(runCLIErr).NotTo(HaveOccurred())
   186  					Expect(serviceRepo.PurgeServiceOfferingCallCount()).To(BeZero())
   187  				})
   188  			})
   189  		})
   190  
   191  		Context("when finding the service offering fails with an error other than 404", func() {
   192  			BeforeEach(func() {
   193  				serviceRepo.FindServiceOfferingsByLabelReturns([]models.ServiceOffering{}, errors.New("find-err"))
   194  			})
   195  
   196  			It("fails with error", func() {
   197  				Expect(runCLIErr).To(HaveOccurred())
   198  				Expect(runCLIErr.Error()).To(Equal("find-err"))
   199  			})
   200  		})
   201  
   202  		Context("when finding the service offering fails with 404 not found", func() {
   203  			BeforeEach(func() {
   204  				serviceRepo.FindServiceOfferingsByLabelReturns(
   205  					[]models.ServiceOffering{{}},
   206  					errors.NewModelNotFoundError("model-type", "find-err"),
   207  				)
   208  			})
   209  
   210  			It("warns the user", func() {
   211  				Expect(runCLIErr).NotTo(HaveOccurred())
   212  				Expect(ui.Outputs()).To(ContainSubstrings(
   213  					[]string{"Service offering does not exist"},
   214  				))
   215  			})
   216  
   217  			It("does not try to purge the service offering", func() {
   218  				Expect(runCLIErr).NotTo(HaveOccurred())
   219  				Expect(serviceRepo.PurgeServiceOfferingCallCount()).To(BeZero())
   220  			})
   221  		})
   222  
   223  		Context("when the -p flag is passed", func() {
   224  			var origAPIVersion string
   225  
   226  			BeforeEach(func() {
   227  				origAPIVersion = configRepo.APIVersion()
   228  				configRepo.SetAPIVersion("2.46.0")
   229  
   230  				flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
   231  				err := flagContext.Parse("service-name", "-p", "provider-name")
   232  				Expect(err).NotTo(HaveOccurred())
   233  				cmd.Requirements(factory, flagContext)
   234  				ui.Inputs = []string{"n"}
   235  			})
   236  
   237  			AfterEach(func() {
   238  				configRepo.SetAPIVersion(origAPIVersion)
   239  			})
   240  
   241  			It("tries to find the service offering by label and provider", func() {
   242  				Expect(runCLIErr).NotTo(HaveOccurred())
   243  				Expect(serviceRepo.FindServiceOfferingByLabelAndProviderCallCount()).To(Equal(1))
   244  				name, provider := serviceRepo.FindServiceOfferingByLabelAndProviderArgsForCall(0)
   245  				Expect(name).To(Equal("service-name"))
   246  				Expect(provider).To(Equal("provider-name"))
   247  			})
   248  
   249  			Context("when finding the service offering succeeds", func() {
   250  				BeforeEach(func() {
   251  					serviceOffering := models.ServiceOffering{}
   252  					serviceOffering.GUID = "service-offering-guid"
   253  					serviceRepo.FindServiceOfferingByLabelAndProviderReturns(serviceOffering, nil)
   254  					ui.Inputs = []string{"n"}
   255  				})
   256  
   257  				It("asks the user to confirm", func() {
   258  					Expect(runCLIErr).NotTo(HaveOccurred())
   259  					Expect(ui.Outputs()).To(ContainSubstrings([]string{"WARNING"}))
   260  					Expect(ui.Prompts).To(ContainSubstrings([]string{"Really purge service offering service-name from Cloud Foundry?"}))
   261  				})
   262  
   263  				Context("when the user confirms", func() {
   264  					BeforeEach(func() {
   265  						ui.Inputs = []string{"y"}
   266  					})
   267  
   268  					It("tells the user it will purge the service offering", func() {
   269  						Expect(runCLIErr).NotTo(HaveOccurred())
   270  						Expect(ui.Outputs()).To(ContainSubstrings([]string{"Purging service service-name..."}))
   271  					})
   272  
   273  					It("tries to purge the service offering", func() {
   274  						Expect(runCLIErr).NotTo(HaveOccurred())
   275  						Expect(serviceRepo.PurgeServiceOfferingCallCount()).To(Equal(1))
   276  					})
   277  
   278  					Context("when purging succeeds", func() {
   279  						BeforeEach(func() {
   280  							serviceRepo.PurgeServiceOfferingReturns(nil)
   281  						})
   282  
   283  						It("says OK", func() {
   284  							Expect(runCLIErr).NotTo(HaveOccurred())
   285  							Expect(ui.Outputs()).To(ContainSubstrings([]string{"OK"}))
   286  						})
   287  					})
   288  
   289  					Context("when purging fails", func() {
   290  						BeforeEach(func() {
   291  							serviceRepo.PurgeServiceOfferingReturns(errors.New("purge-err"))
   292  						})
   293  
   294  						It("fails with error", func() {
   295  							Expect(runCLIErr).To(HaveOccurred())
   296  							Expect(runCLIErr.Error()).To(Equal("purge-err"))
   297  						})
   298  					})
   299  				})
   300  
   301  				Context("when the user does not confirm", func() {
   302  					BeforeEach(func() {
   303  						ui.Inputs = []string{"n"}
   304  					})
   305  
   306  					It("does not try to purge the service offering", func() {
   307  						Expect(runCLIErr).NotTo(HaveOccurred())
   308  						Expect(serviceRepo.PurgeServiceOfferingCallCount()).To(BeZero())
   309  					})
   310  				})
   311  			})
   312  
   313  			Context("when finding the service offering fails with an error other than 404", func() {
   314  				BeforeEach(func() {
   315  					serviceRepo.FindServiceOfferingByLabelAndProviderReturns(models.ServiceOffering{}, errors.New("find-err"))
   316  				})
   317  
   318  				It("fails with error", func() {
   319  					Expect(runCLIErr).To(HaveOccurred())
   320  					Expect(runCLIErr.Error()).To(Equal("find-err"))
   321  				})
   322  			})
   323  
   324  			Context("when finding the service offering fails with 404 not found", func() {
   325  				BeforeEach(func() {
   326  					serviceRepo.FindServiceOfferingByLabelAndProviderReturns(
   327  						models.ServiceOffering{},
   328  						errors.NewModelNotFoundError("model-type", "find-err"),
   329  					)
   330  				})
   331  
   332  				It("warns the user", func() {
   333  					Expect(runCLIErr).NotTo(HaveOccurred())
   334  					Expect(ui.Outputs()).To(ContainSubstrings(
   335  						[]string{"Service offering does not exist"},
   336  					))
   337  				})
   338  
   339  				It("does not try to purge the service offering", func() {
   340  					Expect(runCLIErr).NotTo(HaveOccurred())
   341  					Expect(serviceRepo.PurgeServiceOfferingCallCount()).To(BeZero())
   342  				})
   343  			})
   344  		})
   345  	})
   346  })