github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/service/migrate_service_instances_test.go (about)

     1  package service_test
     2  
     3  import (
     4  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     5  	"github.com/cloudfoundry/cli/cf/api/resources"
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	"github.com/cloudfoundry/cli/cf/errors"
     9  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    10  	testconfig "github.com/cloudfoundry/cli/testhelpers/configuration"
    11  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    12  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  
    16  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    17  )
    18  
    19  var _ = Describe("migrating service instances from v1 to v2", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		serviceRepo         *testapi.FakeServiceRepo
    23  		config              core_config.Repository
    24  		requirementsFactory *testreq.FakeReqFactory
    25  		args                []string
    26  		deps                command_registry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.Ui = ui
    31  		deps.RepoLocator = deps.RepoLocator.SetServiceRepository(serviceRepo)
    32  		deps.Config = config
    33  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("migrate-service-instances").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		ui = &testterm.FakeUI{}
    38  		config = testconfig.NewRepository()
    39  		serviceRepo = &testapi.FakeServiceRepo{}
    40  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: false}
    41  		args = []string{}
    42  	})
    43  
    44  	Describe("requirements", func() {
    45  		It("requires you to be logged in", func() {
    46  			Expect(testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)).To(BeFalse())
    47  		})
    48  
    49  		It("requires five arguments to run", func() {
    50  			requirementsFactory.LoginSuccess = true
    51  			args = []string{"one", "two", "three"}
    52  
    53  			Expect(testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)).To(BeFalse())
    54  		})
    55  
    56  		It("passes requirements if user is logged in and provided five args to run", func() {
    57  			requirementsFactory.LoginSuccess = true
    58  			args = []string{"one", "two", "three", "four", "five"}
    59  			ui.Inputs = append(ui.Inputs, "no")
    60  
    61  			Expect(testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)).To(BeTrue())
    62  		})
    63  	})
    64  
    65  	Describe("migrating service instances", func() {
    66  		BeforeEach(func() {
    67  			requirementsFactory.LoginSuccess = true
    68  			args = []string{"v1-service-label", "v1-provider-name", "v1-plan-name", "v2-service-label", "v2-plan-name"}
    69  			serviceRepo.ServiceInstanceCountForServicePlan = 1
    70  		})
    71  
    72  		It("displays the warning and the prompt including info about the instances and plan to migrate", func() {
    73  			ui.Inputs = []string{""}
    74  			testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
    75  
    76  			Expect(ui.Outputs).To(ContainSubstrings([]string{"WARNING:", "this operation is to replace a service broker"}))
    77  			Expect(ui.Prompts).To(ContainSubstrings(
    78  				[]string{"Really migrate", "1 service instance",
    79  					"from plan", "v1-service-label", "v1-provider-name", "v1-plan-name",
    80  					"to", "v2-service-label", "v2-plan-name"},
    81  			))
    82  		})
    83  
    84  		Context("when the user confirms", func() {
    85  			BeforeEach(func() {
    86  				ui.Inputs = []string{"yes"}
    87  			})
    88  
    89  			Context("when the v1 and v2 service instances exists", func() {
    90  				BeforeEach(func() {
    91  					serviceRepo.FindServicePlanByDescriptionResultGuids = []string{"v1-guid", "v2-guid"}
    92  					serviceRepo.MigrateServicePlanFromV1ToV2ReturnedCount = 1
    93  				})
    94  
    95  				It("makes a request to migrate the v1 service instance", func() {
    96  					testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
    97  
    98  					Expect(serviceRepo.V1GuidToMigrate).To(Equal("v1-guid"))
    99  					Expect(serviceRepo.V2GuidToMigrate).To(Equal("v2-guid"))
   100  				})
   101  
   102  				It("finds the v1 service plan by its name, provider and service label", func() {
   103  					testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   104  
   105  					expectedV1 := resources.ServicePlanDescription{
   106  						ServicePlanName: "v1-plan-name",
   107  						ServiceProvider: "v1-provider-name",
   108  						ServiceLabel:    "v1-service-label",
   109  					}
   110  					Expect(serviceRepo.FindServicePlanByDescriptionArguments[0]).To(Equal(expectedV1))
   111  				})
   112  
   113  				It("finds the v2 service plan by its name and service label", func() {
   114  					testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   115  
   116  					expectedV2 := resources.ServicePlanDescription{
   117  						ServicePlanName: "v2-plan-name",
   118  						ServiceLabel:    "v2-service-label",
   119  					}
   120  					Expect(serviceRepo.FindServicePlanByDescriptionArguments[1]).To(Equal(expectedV2))
   121  				})
   122  
   123  				It("notifies the user that the migration was successful", func() {
   124  					serviceRepo.ServiceInstanceCountForServicePlan = 2
   125  					testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   126  
   127  					Expect(ui.Outputs).To(ContainSubstrings(
   128  						[]string{"Attempting to migrate", "2", "service instances"},
   129  						[]string{"1", "service instance", "migrated"},
   130  						[]string{"OK"},
   131  					))
   132  				})
   133  			})
   134  
   135  			Context("when finding the v1 plan fails", func() {
   136  				Context("because the plan does not exist", func() {
   137  					BeforeEach(func() {
   138  						serviceRepo.FindServicePlanByDescriptionResponses = []error{errors.NewModelNotFoundError("Service Plan", "")}
   139  					})
   140  
   141  					It("notifies the user of the failure", func() {
   142  						testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   143  
   144  						Expect(ui.Outputs).To(ContainSubstrings(
   145  							[]string{"FAILED"},
   146  							[]string{"Plan", "v1-service-label", "v1-provider-name", "v1-plan-name", "cannot be found"},
   147  						))
   148  					})
   149  
   150  					It("does not display the warning", func() {
   151  						testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   152  
   153  						Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"WARNING:", "this operation is to replace a service broker"}))
   154  					})
   155  				})
   156  
   157  				Context("because there was an http error", func() {
   158  					BeforeEach(func() {
   159  						serviceRepo.FindServicePlanByDescriptionResponses = []error{errors.New("uh oh")}
   160  					})
   161  
   162  					It("notifies the user of the failure", func() {
   163  						testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   164  
   165  						Expect(ui.Outputs).To(ContainSubstrings(
   166  							[]string{"FAILED"},
   167  							[]string{"uh oh"},
   168  						))
   169  					})
   170  
   171  					It("does not display the warning", func() {
   172  						testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   173  
   174  						Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"WARNING:", "this operation is to replace a service broker"}))
   175  					})
   176  				})
   177  			})
   178  
   179  			Context("when finding the v2 plan fails", func() {
   180  				Context("because the plan does not exist", func() {
   181  					BeforeEach(func() {
   182  						serviceRepo.FindServicePlanByDescriptionResponses = []error{nil, errors.NewModelNotFoundError("Service Plan", "")}
   183  					})
   184  
   185  					It("notifies the user of the failure", func() {
   186  						testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   187  
   188  						Expect(ui.Outputs).To(ContainSubstrings(
   189  							[]string{"FAILED"},
   190  							[]string{"Plan", "v2-service-label", "v2-plan-name", "cannot be found"},
   191  						))
   192  					})
   193  
   194  					It("does not display the warning", func() {
   195  						testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   196  
   197  						Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"WARNING:", "this operation is to replace a service broker"}))
   198  					})
   199  				})
   200  
   201  				Context("because there was an http error", func() {
   202  					BeforeEach(func() {
   203  						serviceRepo.FindServicePlanByDescriptionResponses = []error{nil, errors.New("uh oh")}
   204  					})
   205  
   206  					It("notifies the user of the failure", func() {
   207  						testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   208  
   209  						Expect(ui.Outputs).To(ContainSubstrings(
   210  							[]string{"FAILED"},
   211  							[]string{"uh oh"},
   212  						))
   213  					})
   214  
   215  					It("does not display the warning", func() {
   216  						testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   217  
   218  						Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"WARNING:", "this operation is to replace a service broker"}))
   219  					})
   220  				})
   221  			})
   222  
   223  			Context("when migrating the plans fails", func() {
   224  				BeforeEach(func() {
   225  					serviceRepo.MigrateServicePlanFromV1ToV2Response = errors.New("ruh roh")
   226  				})
   227  
   228  				It("notifies the user of the failure", func() {
   229  					testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   230  
   231  					Expect(ui.Outputs).To(ContainSubstrings(
   232  						[]string{"FAILED"},
   233  						[]string{"ruh roh"},
   234  					))
   235  				})
   236  			})
   237  
   238  			Context("when there are no instances to migrate", func() {
   239  				BeforeEach(func() {
   240  					serviceRepo.FindServicePlanByDescriptionResultGuids = []string{"v1-guid", "v2-guid"}
   241  					serviceRepo.ServiceInstanceCountForServicePlan = 0
   242  				})
   243  
   244  				It("returns a meaningful error", func() {
   245  					testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   246  
   247  					Expect(ui.Outputs).To(ContainSubstrings(
   248  						[]string{"FAILED"},
   249  						[]string{"no service instances to migrate"},
   250  					))
   251  				})
   252  
   253  				It("does not show the user the warning", func() {
   254  					testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   255  
   256  					Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"WARNING:", "this operation is to replace a service broker"}))
   257  				})
   258  			})
   259  
   260  			Context("when it cannot fetch the number of instances", func() {
   261  				BeforeEach(func() {
   262  					serviceRepo.ServiceInstanceCountApiResponse = errors.New("service instance fetch is very bad")
   263  				})
   264  
   265  				It("notifies the user of the failure", func() {
   266  					testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   267  
   268  					Expect(ui.Outputs).To(ContainSubstrings(
   269  						[]string{"FAILED"},
   270  						[]string{"service instance fetch is very bad"},
   271  					))
   272  				})
   273  			})
   274  		})
   275  
   276  		Context("when the user does not confirm", func() {
   277  			BeforeEach(func() {
   278  				ui.Inputs = append(ui.Inputs, "no")
   279  			})
   280  
   281  			It("does not continue the migration", func() {
   282  				testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   283  
   284  				Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"Migrating"}))
   285  				Expect(serviceRepo.MigrateServicePlanFromV1ToV2Called).To(BeFalse())
   286  			})
   287  		})
   288  
   289  		Context("when the user ignores confirmation using the force flag", func() {
   290  			It("does not prompt the user for confirmation", func() {
   291  				args = []string{"-f", "v1-service-label", "v1-provider-name", "v1-plan-name", "v2-service-label", "v2-plan-name"}
   292  
   293  				testcmd.RunCliCommand("migrate-service-instances", args, requirementsFactory, updateCommandDependency, false)
   294  
   295  				Expect(ui.Outputs).ToNot(ContainSubstrings([]string{"Really migrate"}))
   296  				Expect(serviceRepo.MigrateServicePlanFromV1ToV2Called).To(BeTrue())
   297  			})
   298  		})
   299  	})
   300  })