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

     1  package application_test
     2  
     3  import (
     4  	testApplication "github.com/cloudfoundry/cli/cf/api/applications/fakes"
     5  	testCopyApplication "github.com/cloudfoundry/cli/cf/api/copy_application_source/fakes"
     6  	testapi "github.com/cloudfoundry/cli/cf/api/fakes"
     7  	testorg "github.com/cloudfoundry/cli/cf/api/organizations/fakes"
     8  	"github.com/cloudfoundry/cli/cf/models"
     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  
    14  	"github.com/cloudfoundry/cli/cf/command_registry"
    15  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    16  	"github.com/cloudfoundry/cli/cf/errors"
    17  
    18  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    19  	. "github.com/onsi/ginkgo"
    20  	. "github.com/onsi/gomega"
    21  )
    22  
    23  var _ = Describe("CopySource", func() {
    24  
    25  	var (
    26  		ui                  *testterm.FakeUI
    27  		config              core_config.Repository
    28  		requirementsFactory *testreq.FakeReqFactory
    29  		authRepo            *testapi.FakeAuthenticationRepository
    30  		appRepo             *testApplication.FakeApplicationRepository
    31  		copyAppSourceRepo   *testCopyApplication.FakeCopyApplicationSourceRepository
    32  		spaceRepo           *testapi.FakeSpaceRepository
    33  		orgRepo             *testorg.FakeOrganizationRepository
    34  		appRestarter        *testcmd.FakeApplicationRestarter
    35  		OriginalCommand     command_registry.Command
    36  		deps                command_registry.Dependency
    37  	)
    38  
    39  	updateCommandDependency := func(pluginCall bool) {
    40  		deps.Ui = ui
    41  		deps.RepoLocator = deps.RepoLocator.SetAuthenticationRepository(authRepo)
    42  		deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo)
    43  		deps.RepoLocator = deps.RepoLocator.SetCopyApplicationSourceRepository(copyAppSourceRepo)
    44  		deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo)
    45  		deps.RepoLocator = deps.RepoLocator.SetOrganizationRepository(orgRepo)
    46  		deps.Config = config
    47  
    48  		//inject fake 'command dependency' into registry
    49  		command_registry.Register(appRestarter)
    50  
    51  		command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("copy-source").SetDependency(deps, pluginCall))
    52  	}
    53  
    54  	BeforeEach(func() {
    55  		ui = &testterm.FakeUI{}
    56  		requirementsFactory = &testreq.FakeReqFactory{LoginSuccess: true, TargetedSpaceSuccess: true}
    57  		authRepo = &testapi.FakeAuthenticationRepository{}
    58  		appRepo = &testApplication.FakeApplicationRepository{}
    59  		copyAppSourceRepo = &testCopyApplication.FakeCopyApplicationSourceRepository{}
    60  		spaceRepo = &testapi.FakeSpaceRepository{}
    61  		orgRepo = &testorg.FakeOrganizationRepository{}
    62  		config = testconfig.NewRepositoryWithDefaults()
    63  
    64  		//save original command and restore later
    65  		OriginalCommand = command_registry.Commands.FindCommand("restart")
    66  
    67  		appRestarter = &testcmd.FakeApplicationRestarter{}
    68  		//setup fakes to correctly interact with command_registry
    69  		appRestarter.SetDependencyStub = func(_ command_registry.Dependency, _ bool) command_registry.Command {
    70  			return appRestarter
    71  		}
    72  		appRestarter.MetaDataReturns(command_registry.CommandMetadata{Name: "restart"})
    73  	})
    74  
    75  	AfterEach(func() {
    76  		command_registry.Register(OriginalCommand)
    77  	})
    78  
    79  	runCommand := func(args ...string) bool {
    80  		return testcmd.RunCliCommand("copy-source", args, requirementsFactory, updateCommandDependency, false)
    81  	}
    82  
    83  	Describe("requirement failures", func() {
    84  		It("when not logged in", func() {
    85  			requirementsFactory.LoginSuccess = false
    86  			Expect(runCommand("source-app", "target-app")).ToNot(HavePassedRequirements())
    87  		})
    88  
    89  		It("when a space is not targeted", func() {
    90  			requirementsFactory.TargetedSpaceSuccess = false
    91  			Expect(runCommand("source-app", "target-app")).ToNot(HavePassedRequirements())
    92  		})
    93  
    94  		It("when provided too many args", func() {
    95  			Expect(runCommand("source-app", "target-app", "too-much", "app-name")).ToNot(HavePassedRequirements())
    96  		})
    97  	})
    98  
    99  	Describe("Passing requirements", func() {
   100  		BeforeEach(func() {
   101  			requirementsFactory.LoginSuccess = true
   102  			requirementsFactory.TargetedSpaceSuccess = true
   103  		})
   104  
   105  		Context("refreshing the auth token", func() {
   106  			It("makes a call for the app token", func() {
   107  				runCommand("source-app", "target-app")
   108  				Expect(authRepo.RefreshTokenCalled).To(BeTrue())
   109  			})
   110  
   111  			Context("when refreshing the auth token fails", func() {
   112  				BeforeEach(func() {
   113  					authRepo.RefreshTokenError = errors.New("I accidentally the UAA")
   114  				})
   115  
   116  				It("it displays an error", func() {
   117  					runCommand("source-app", "target-app")
   118  					Expect(ui.Outputs).To(ContainSubstrings(
   119  						[]string{"FAILED"},
   120  						[]string{"accidentally the UAA"},
   121  					))
   122  				})
   123  			})
   124  
   125  			Describe("when retrieving the app token succeeds", func() {
   126  				var (
   127  					sourceApp, targetApp models.Application
   128  				)
   129  
   130  				BeforeEach(func() {
   131  					sourceApp = models.Application{
   132  						ApplicationFields: models.ApplicationFields{
   133  							Name: "source-app",
   134  							Guid: "source-app-guid",
   135  						},
   136  					}
   137  					appRepo.ReadReturns.App = sourceApp
   138  
   139  					targetApp = models.Application{
   140  						ApplicationFields: models.ApplicationFields{
   141  							Name: "target-app",
   142  							Guid: "target-app-guid",
   143  						},
   144  					}
   145  					appRepo.ReadFromSpaceReturns(targetApp, nil)
   146  				})
   147  
   148  				Describe("when no parameters are passed", func() {
   149  					It("obtains both the source and target application from the same space", func() {
   150  						runCommand("source-app", "target-app")
   151  
   152  						targetAppName, spaceGuid := appRepo.ReadFromSpaceArgsForCall(0)
   153  						Expect(targetAppName).To(Equal("target-app"))
   154  						Expect(spaceGuid).To(Equal("my-space-guid"))
   155  
   156  						Expect(appRepo.ReadArgs.Name).To(Equal("source-app"))
   157  
   158  						sourceAppGuid, targetAppGuid := copyAppSourceRepo.CopyApplicationArgsForCall(0)
   159  						Expect(sourceAppGuid).To(Equal("source-app-guid"))
   160  						Expect(targetAppGuid).To(Equal("target-app-guid"))
   161  
   162  						appArg, orgName, spaceName := appRestarter.ApplicationRestartArgsForCall(0)
   163  						Expect(appArg).To(Equal(targetApp))
   164  						Expect(orgName).To(Equal(config.OrganizationFields().Name))
   165  						Expect(spaceName).To(Equal(config.SpaceFields().Name))
   166  
   167  						Expect(ui.Outputs).To(ContainSubstrings(
   168  							[]string{"Copying source from app", "source-app", "to target app", "target-app", "in org my-org / space my-space as my-user..."},
   169  							[]string{"Note: this may take some time"},
   170  							[]string{"OK"},
   171  						))
   172  					})
   173  
   174  					Context("Failures", func() {
   175  						It("if we cannot obtain the source application", func() {
   176  							appRepo.ReadReturns.Error = errors.New("could not find source app")
   177  							runCommand("source-app", "target-app")
   178  
   179  							Expect(ui.Outputs).To(ContainSubstrings(
   180  								[]string{"FAILED"},
   181  								[]string{"could not find source app"},
   182  							))
   183  						})
   184  
   185  						It("fails if we cannot obtain the target application", func() {
   186  							appRepo.ReadFromSpaceReturns(models.Application{}, errors.New("could not find target app"))
   187  							runCommand("source-app", "target-app")
   188  
   189  							Expect(ui.Outputs).To(ContainSubstrings(
   190  								[]string{"FAILED"},
   191  								[]string{"could not find target app"},
   192  							))
   193  						})
   194  					})
   195  				})
   196  
   197  				Describe("when a space is provided, but not an org", func() {
   198  					It("send the correct target appplication for the current org and target space", func() {
   199  						spaceRepo.Spaces = []models.Space{
   200  							{
   201  								SpaceFields: models.SpaceFields{
   202  									Name: "space-name",
   203  									Guid: "model-space-guid",
   204  								},
   205  							},
   206  						}
   207  
   208  						runCommand("-s", "space-name", "source-app", "target-app")
   209  
   210  						targetAppName, spaceGuid := appRepo.ReadFromSpaceArgsForCall(0)
   211  						Expect(targetAppName).To(Equal("target-app"))
   212  						Expect(spaceGuid).To(Equal("model-space-guid"))
   213  
   214  						Expect(ui.Outputs).To(ContainSubstrings(
   215  							[]string{"Copying source from app", "source-app", "to target app", "target-app", "in org my-org / space space-name as my-user..."},
   216  							[]string{"Note: this may take some time"},
   217  							[]string{"OK"},
   218  						))
   219  					})
   220  
   221  					Context("Failures", func() {
   222  						It("when we cannot find the provided space", func() {
   223  							spaceRepo.FindByNameErr = true
   224  
   225  							runCommand("-s", "space-name", "source-app", "target-app")
   226  							Expect(ui.Outputs).To(ContainSubstrings(
   227  								[]string{"FAILED"},
   228  								[]string{"Error finding space by name."},
   229  							))
   230  						})
   231  					})
   232  				})
   233  
   234  				Describe("when an org and space name are passed as parameters", func() {
   235  					It("send the correct target application for the space and org", func() {
   236  						orgRepo.FindByNameReturns(models.Organization{
   237  							Spaces: []models.SpaceFields{
   238  								{
   239  									Name: "space-name",
   240  									Guid: "space-guid",
   241  								},
   242  							},
   243  						}, nil)
   244  
   245  						runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app")
   246  
   247  						targetAppName, spaceGuid := appRepo.ReadFromSpaceArgsForCall(0)
   248  						Expect(targetAppName).To(Equal("target-app"))
   249  						Expect(spaceGuid).To(Equal("space-guid"))
   250  
   251  						sourceAppGuid, targetAppGuid := copyAppSourceRepo.CopyApplicationArgsForCall(0)
   252  						Expect(sourceAppGuid).To(Equal("source-app-guid"))
   253  						Expect(targetAppGuid).To(Equal("target-app-guid"))
   254  
   255  						appArg, orgName, spaceName := appRestarter.ApplicationRestartArgsForCall(0)
   256  						Expect(appArg).To(Equal(targetApp))
   257  						Expect(orgName).To(Equal("org-name"))
   258  						Expect(spaceName).To(Equal("space-name"))
   259  
   260  						Expect(ui.Outputs).To(ContainSubstrings(
   261  							[]string{"Copying source from app source-app to target app target-app in org org-name / space space-name as my-user..."},
   262  							[]string{"Note: this may take some time"},
   263  							[]string{"OK"},
   264  						))
   265  					})
   266  
   267  					Context("failures", func() {
   268  						It("cannot just accept an organization and no space", func() {
   269  							runCommand("-o", "org-name", "source-app", "target-app")
   270  
   271  							Expect(ui.Outputs).To(ContainSubstrings(
   272  								[]string{"FAILED"},
   273  								[]string{"Please provide the space within the organization containing the target application"},
   274  							))
   275  						})
   276  
   277  						It("when we cannot find the provided org", func() {
   278  							orgRepo.FindByNameReturns(models.Organization{}, errors.New("Could not find org"))
   279  							runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app")
   280  
   281  							Expect(ui.Outputs).To(ContainSubstrings(
   282  								[]string{"FAILED"},
   283  								[]string{"Could not find org"},
   284  							))
   285  						})
   286  
   287  						It("when the org does not contain the space name provide", func() {
   288  							orgRepo.FindByNameReturns(models.Organization{}, nil)
   289  							runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app")
   290  
   291  							Expect(ui.Outputs).To(ContainSubstrings(
   292  								[]string{"FAILED"},
   293  								[]string{"Could not find space space-name in organization org-name"},
   294  							))
   295  						})
   296  
   297  						It("when the targeted app does not exist in the targeted org and space", func() {
   298  							orgRepo.FindByNameReturns(models.Organization{
   299  								Spaces: []models.SpaceFields{
   300  									{
   301  										Name: "space-name",
   302  										Guid: "space-guid",
   303  									},
   304  								},
   305  							}, nil)
   306  
   307  							appRepo.ReadFromSpaceReturns(models.Application{}, errors.New("could not find app"))
   308  							runCommand("-o", "org-name", "-s", "space-name", "source-app", "target-app")
   309  
   310  							Expect(ui.Outputs).To(ContainSubstrings(
   311  								[]string{"FAILED"},
   312  								[]string{"could not find app"},
   313  							))
   314  						})
   315  					})
   316  				})
   317  
   318  				Describe("when the --no-restart flag is passed", func() {
   319  					It("does not restart the target application", func() {
   320  						runCommand("--no-restart", "source-app", "target-app")
   321  						Expect(appRestarter.ApplicationRestartCallCount()).To(Equal(0))
   322  					})
   323  				})
   324  			})
   325  		})
   326  	})
   327  })