func (routeActor routeActor) BindRoute(app models.Application, route models.Route) error { if !app.HasRoute(route) { routeActor.ui.Say(T( "Binding {{.URL}} to {{.AppName}}...", map[string]interface{}{ "URL": terminal.EntityNameColor(route.URL()), "AppName": terminal.EntityNameColor(app.Name), }), ) err := routeActor.routeRepo.Bind(route.GUID, app.GUID) switch err := err.(type) { case nil: routeActor.ui.Ok() routeActor.ui.Say("") return nil case errors.HTTPError: if err.ErrorCode() == errors.InvalidRelation { return errors.New(T( "The route {{.URL}} is already in use.\nTIP: Change the hostname with -n HOSTNAME or use --random-route to generate a new route and then push again.", map[string]interface{}{ "URL": route.URL(), }), ) } } return err } return nil }
func (resource ApplicationFromSummary) ToModel() models.Application { var app models.Application app.ApplicationFields = resource.ToFields() routes := []models.RouteSummary{} for _, route := range resource.Routes { routes = append(routes, route.ToModel()) } app.Routes = routes services := []models.ServicePlanSummary{} for _, service := range resource.Services { services = append(services, service.ToModel()) } app.Routes = routes app.Services = services return app }
cmd = &application.Files{} cmd.SetDependency(deps, false) flagContext = flags.NewFlagContext(cmd.MetaData().Flags) factory = new(requirementsfakes.FakeFactory) loginRequirement = &passingRequirement{Name: "login-requirement"} factory.NewLoginRequirementReturns(loginRequirement) targetedSpaceRequirement = &passingRequirement{} factory.NewTargetedSpaceRequirementReturns(targetedSpaceRequirement) deaApplicationRequirement = new(requirementsfakes.FakeDEAApplicationRequirement) factory.NewDEAApplicationRequirementReturns(deaApplicationRequirement) app := models.Application{} app.InstanceCount = 1 app.GUID = "app-guid" app.Name = "app-name" deaApplicationRequirement.GetApplicationReturns(app) }) Describe("Requirements", func() { Context("when not provided one or two args", func() { BeforeEach(func() { flagContext.Parse("app-name", "the-path", "extra-arg") }) It("fails with usage", func() { _, err := cmd.Requirements(factory, flagContext) Expect(err).To(HaveOccurred())
} cmd = &route.UnmapRoute{} cmd.SetDependency(deps, false) flagContext = flags.NewFlagContext(cmd.MetaData().Flags) factory = new(requirementsfakes.FakeFactory) loginRequirement = &passingRequirement{Name: "login-requirement"} factory.NewLoginRequirementReturns(loginRequirement) applicationRequirement = new(requirementsfakes.FakeApplicationRequirement) factory.NewApplicationRequirementReturns(applicationRequirement) fakeApplication := models.Application{} fakeApplication.GUID = "fake-app-guid" applicationRequirement.GetApplicationReturns(fakeApplication) domainRequirement = new(requirementsfakes.FakeDomainRequirement) factory.NewDomainRequirementReturns(domainRequirement) fakeDomain = models.DomainFields{ GUID: "fake-domain-guid", Name: "fake-domain-name", } domainRequirement.GetDomainReturns(fakeDomain) minAPIVersionRequirement = &passingRequirement{Name: "min-api-version-requirement"} factory.NewMinAPIVersionRequirementReturns(minAPIVersionRequirement) })
It("returns an ApplicationRequirement", func() { actualRequirements, err := cmd.Requirements(factory, flagContext) Expect(err).NotTo(HaveOccurred()) Expect(factory.NewApplicationRequirementCallCount()).To(Equal(1)) Expect(factory.NewApplicationRequirementArgsForCall(0)).To(Equal("app-name")) Expect(actualRequirements).To(ContainElement(applicationRequirement)) }) }) }) Describe("Execute", func() { var ( getApplicationModel models.Application getAppSummaryModel models.Application appStackModel models.Stack appInstanceFields []models.AppInstanceFields getAppSummaryErr error err error ) BeforeEach(func() { err := flagContext.Parse("app-name") Expect(err).NotTo(HaveOccurred()) cmd.Requirements(factory, flagContext) paginatedApplicationResources := resources.PaginatedApplicationResources{} err = json.Unmarshal([]byte(getApplicationJSON), &paginatedApplicationResources) Expect(err).NotTo(HaveOccurred()) getApplicationModel = paginatedApplicationResources.Resources[0].ToModel()
"code.cloudfoundry.org/cli/cf/requirements" "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" testcmd "code.cloudfoundry.org/cli/testhelpers/commands" testconfig "code.cloudfoundry.org/cli/testhelpers/configuration" testterm "code.cloudfoundry.org/cli/testhelpers/terminal" . "code.cloudfoundry.org/cli/testhelpers/matchers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("unset-env command", func() { var ( ui *testterm.FakeUI app models.Application appRepo *applicationsfakes.FakeRepository configRepo coreconfig.Repository requirementsFactory *requirementsfakes.FakeFactory deps commandregistry.Dependency ) updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.Config = configRepo deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo) commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("unset-env").SetDependency(deps, pluginCall)) } BeforeEach(func() { ui = &testterm.FakeUI{} app = models.Application{} app.Name = "my-app"
"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" testcmd "code.cloudfoundry.org/cli/testhelpers/commands" testconfig "code.cloudfoundry.org/cli/testhelpers/configuration" testterm "code.cloudfoundry.org/cli/testhelpers/terminal" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "code.cloudfoundry.org/cli/testhelpers/matchers" ) var _ = Describe("delete app command", func() { var ( ui *testterm.FakeUI app models.Application configRepo coreconfig.Repository appRepo *applicationsfakes.FakeRepository routeRepo *apifakes.FakeRouteRepository requirementsFactory *requirementsfakes.FakeFactory deps commandregistry.Dependency ) updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.Config = configRepo deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo) deps.RepoLocator = deps.RepoLocator.SetRouteRepository(routeRepo) commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("delete").SetDependency(deps, pluginCall)) } BeforeEach(func() { app = models.Application{}
testterm "code.cloudfoundry.org/cli/testhelpers/terminal" "code.cloudfoundry.org/cli/cf/commandregistry" "code.cloudfoundry.org/cli/cf/configuration/coreconfig" . "code.cloudfoundry.org/cli/testhelpers/matchers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("restart command", func() { var ( ui *testterm.FakeUI requirementsFactory *requirementsfakes.FakeFactory starter *applicationfakes.FakeStarter stopper *applicationfakes.FakeStopper config coreconfig.Repository app models.Application originalStop commandregistry.Command originalStart commandregistry.Command deps commandregistry.Dependency applicationReq *requirementsfakes.FakeApplicationRequirement ) updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.Config = config //inject fake 'stopper and starter' into registry commandregistry.Register(starter) commandregistry.Register(stopper) commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("restart").SetDependency(deps, pluginCall))
) BeforeEach(func() { appName = "fake-app-name" appRepo = new(applicationsfakes.FakeRepository) req = requirements.NewDEAApplicationRequirement(appName, appRepo) }) Describe("GetApplication", func() { It("returns an empty application", func() { Expect(req.GetApplication()).To(Equal(models.Application{})) }) Context("when the requirement has been executed", func() { BeforeEach(func() { app := models.Application{} app.GUID = "fake-app-guid" appRepo.ReadReturns(app, nil) req.Execute() }) It("returns the application", func() { Expect(req.GetApplication().GUID).To(Equal("fake-app-guid")) }) }) }) Describe("Execute", func() { Context("when the returned application is a Diego application", func() { BeforeEach(func() {
. "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("SSH", func() { var ( fakeTerminalHelper *terminalfakes.FakeTerminalHelper fakeListenerFactory *sshfakes.FakeListenerFactory fakeConnection *fake_ssh.FakeConn fakeSecureClient *sshfakes.FakeSecureClient fakeSecureDialer *sshfakes.FakeSecureDialer fakeSecureSession *sshfakes.FakeSecureSession terminalHelper terminal.TerminalHelper keepAliveDuration time.Duration secureShell sshCmd.SecureShell stdinPipe *fake_io.FakeWriteCloser currentApp models.Application sshEndpointFingerprint string sshEndpoint string token string ) BeforeEach(func() { fakeTerminalHelper = new(terminalfakes.FakeTerminalHelper) terminalHelper = terminal.DefaultHelper() fakeListenerFactory = new(sshfakes.FakeListenerFactory)
requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) }) Context("when application is not found", func() { It("Fails", func() { appRequirement := new(requirementsfakes.FakeApplicationRequirement) appRequirement.ExecuteReturns(errors.New("no app")) requirementsFactory.NewApplicationRequirementReturns(appRequirement) Expect(runCommand("non-exist-app")).To(BeFalse()) }) }) Context("when application exists", func() { BeforeEach(func() { app := models.Application{} app.Name = "my-app" app.GUID = "my-app-guid" app.HealthCheckType = "port" applicationReq := new(requirementsfakes.FakeApplicationRequirement) applicationReq.GetApplicationReturns(app) requirementsFactory.NewApplicationRequirementReturns(applicationReq) }) It("shows the health_check_type", func() { runCommand("my-app") Expect(ui.Outputs()).To(ContainSubstrings([]string{"Getting", "my-app", "health_check_type"})) Expect(ui.Outputs()).To(ContainSubstrings([]string{"port"})) })
Expect(domain).To(Equal(expectedDomain)) Expect(path).To(Equal(expectedPath)) Expect(port).To(Equal(0)) Expect(randomPort).To(BeFalse()) Expect(fakeUI.SayCallCount()).To(Equal(2)) output, _ := fakeUI.SayArgsForCall(0) Expect(output).To(MatchRegexp("Creating route.*hostname.foo.com/path")) }) }) }) }) Describe("BindRoute", func() { var ( expectedApp models.Application ) BeforeEach(func() { expectedRoute = models.Route{ GUID: "route-guid", } expectedApp = models.Application{ ApplicationFields: models.ApplicationFields{ Name: "app-name", GUID: "app-guid", }, } }) Context("when the app has the route", func() {
"code.cloudfoundry.org/cli/cf/requirements" "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" testcmd "code.cloudfoundry.org/cli/testhelpers/commands" testconfig "code.cloudfoundry.org/cli/testhelpers/configuration" testterm "code.cloudfoundry.org/cli/testhelpers/terminal" . "code.cloudfoundry.org/cli/testhelpers/matchers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("set-env command", func() { var ( ui *testterm.FakeUI configRepo coreconfig.Repository app models.Application appRepo *applicationsfakes.FakeRepository requirementsFactory *requirementsfakes.FakeFactory deps commandregistry.Dependency ) updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.Config = configRepo deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo) commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("set-env").SetDependency(deps, pluginCall)) } BeforeEach(func() { ui = &testterm.FakeUI{} app = models.Application{} app.Name = "my-app"
"metadata": { "guid": "my-cool-app-guid" }, "entity": { "name": "my-cool-app" } }`), ), ) }) It("returns the application", func() { createdApp, err := repo.Create(appParams) Expect(err).NotTo(HaveOccurred()) app := models.Application{} app.Name = "my-cool-app" app.GUID = "my-cool-app-guid" Expect(createdApp).To(Equal(app)) }) }) Context("when the create fails", func() { BeforeEach(func() { h := ccServer.GetHandler(0) ccServer.SetHandler(0, ghttp.CombineHandlers( h, ghttp.RespondWith(http.StatusInternalServerError, ""), ), )
}) It("fails requirements when not logged in", func() { Expect(runCommand("my-app", "none")).To(BeFalse()) }) It("fails if a space is not targeted", func() { requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"}) Expect(runCommand("my-app", "none")).To(BeFalse()) }) }) Describe("disable-ssh", func() { var ( app models.Application ) BeforeEach(func() { requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) app = models.Application{} app.Name = "my-app" app.GUID = "my-app-guid" app.EnableSSH = true applicationReq := new(requirementsfakes.FakeApplicationRequirement) applicationReq.GetApplicationReturns(app) requirementsFactory.NewApplicationRequirementReturns(applicationReq) })
It("shows error and prints command usage", func() { Expect(runCommand("app_name", "-L", "[9999:localhost...")).To(BeFalse()) Expect(ui.Outputs()).To(ContainSubstrings( []string{"Incorrect Usage"}, []string{"USAGE:"}, )) }) }) }) }) Describe("ssh", func() { var ( currentApp models.Application ) BeforeEach(func() { requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) currentApp = models.Application{} currentApp.Name = "my-app" currentApp.State = "started" currentApp.GUID = "my-app-guid" currentApp.EnableSSH = true currentApp.Diego = true applicationReq := new(requirementsfakes.FakeApplicationRequirement) applicationReq.GetApplicationReturns(currentApp) requirementsFactory.NewApplicationRequirementReturns(applicationReq)
"sync/atomic" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("start command", func() { var ( ui *testterm.FakeUI configRepo coreconfig.Repository defaultAppForStart models.Application defaultInstanceResponses [][]models.AppInstanceFields defaultInstanceErrorCodes []string requirementsFactory *requirementsfakes.FakeFactory logMessages atomic.Value logRepo *logsfakes.FakeRepository appInstancesRepo *appinstancesfakes.FakeAppInstancesRepository appRepo *applicationsfakes.FakeRepository originalAppCommand commandregistry.Command deps commandregistry.Dependency displayApp *applicationfakes.FakeAppDisplayer ) updateCommandDependency := func(logsRepo logs.Repository) { deps.UI = ui deps.Config = configRepo deps.RepoLocator = deps.RepoLocator.SetLogsRepository(logsRepo) deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo) deps.RepoLocator = deps.RepoLocator.SetAppInstancesRepository(appInstancesRepo)
testcmd "code.cloudfoundry.org/cli/testhelpers/commands" testconfig "code.cloudfoundry.org/cli/testhelpers/configuration" testterm "code.cloudfoundry.org/cli/testhelpers/terminal" "code.cloudfoundry.org/cli/cf/commandregistry" "code.cloudfoundry.org/cli/cf/commands/application" . "code.cloudfoundry.org/cli/testhelpers/matchers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("stop command", func() { var ( ui *testterm.FakeUI app models.Application appRepo *applicationsfakes.FakeRepository requirementsFactory *requirementsfakes.FakeFactory config coreconfig.Repository deps commandregistry.Dependency ) updateCommandDependency := func(pluginCall bool) { deps.UI = ui deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo) deps.Config = config commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("stop").SetDependency(deps, pluginCall)) } BeforeEach(func() { ui = &testterm.FakeUI{} config = testconfig.NewRepositoryWithDefaults() appRepo = new(applicationsfakes.FakeRepository)
"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" testcmd "code.cloudfoundry.org/cli/testhelpers/commands" testconfig "code.cloudfoundry.org/cli/testhelpers/configuration" testterm "code.cloudfoundry.org/cli/testhelpers/terminal" "code.cloudfoundry.org/cli/cf/commandregistry" . "code.cloudfoundry.org/cli/testhelpers/matchers" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("restart-app-instance", func() { var ( ui *testterm.FakeUI config coreconfig.Repository appInstancesRepo *appinstancesfakes.FakeAppInstancesRepository requirementsFactory *requirementsfakes.FakeFactory application models.Application deps commandregistry.Dependency ) BeforeEach(func() { ui = &testterm.FakeUI{} appInstancesRepo = new(appinstancesfakes.FakeAppInstancesRepository) config = testconfig.NewRepositoryWithDefaults() requirementsFactory = new(requirementsfakes.FakeFactory) requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) application = models.Application{} application.Name = "my-app"
}, }, { Host: "app1", Domain: models.DomainFields{ Name: "example.com", }, }} app2Routes := []models.RouteSummary{ { Host: "app2", Domain: models.DomainFields{Name: "cfapps.io"}, }} app := models.Application{} app.Name = "Application-1" app.GUID = "Application-1-guid" app.State = "started" app.RunningInstances = 1 app.InstanceCount = 1 app.Memory = 512 app.DiskQuota = 1024 app.Routes = app1Routes app.AppPorts = []int{8080, 9090} app2 := models.Application{} app2.Name = "Application-2" app2.GUID = "Application-2-guid" app2.State = "started" app2.RunningInstances = 1
} cmd = &commands.CreateAppManifest{} cmd.SetDependency(deps, false) flagContext = flags.NewFlagContext(cmd.MetaData().Flags) factory = new(requirementsfakes.FakeFactory) loginRequirement = &passingRequirement{Name: "login-requirement"} factory.NewLoginRequirementReturns(loginRequirement) targetedSpaceRequirement = &passingRequirement{Name: "targeted-space-requirement"} factory.NewTargetedSpaceRequirementReturns(targetedSpaceRequirement) applicationRequirement = new(requirementsfakes.FakeApplicationRequirement) application := models.Application{} application.GUID = "app-guid" applicationRequirement.GetApplicationReturns(application) factory.NewApplicationRequirementReturns(applicationRequirement) }) Describe("Requirements", func() { Context("when not provided exactly one arg", func() { BeforeEach(func() { flagContext.Parse("app-name", "extra-arg") }) It("fails with usage", func() { _, err := cmd.Requirements(factory, flagContext) Expect(err).To(HaveOccurred()) Expect(ui.Outputs()).To(ContainSubstrings(