github.com/sleungcy-sap/cli@v7.1.0+incompatible/cf/commands/application/set_env_test.go (about) 1 package application_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/cf/api/applications/applicationsfakes" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/models" 10 "code.cloudfoundry.org/cli/cf/requirements" 11 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 12 testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 15 16 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("set-env command", func() { 22 var ( 23 ui *testterm.FakeUI 24 configRepo coreconfig.Repository 25 app models.Application 26 appRepo *applicationsfakes.FakeRepository 27 requirementsFactory *requirementsfakes.FakeFactory 28 deps commandregistry.Dependency 29 ) 30 31 updateCommandDependency := func(pluginCall bool) { 32 deps.UI = ui 33 deps.Config = configRepo 34 deps.RepoLocator = deps.RepoLocator.SetApplicationRepository(appRepo) 35 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("set-env").SetDependency(deps, pluginCall)) 36 } 37 38 BeforeEach(func() { 39 ui = &testterm.FakeUI{} 40 app = models.Application{} 41 app.Name = "my-app" 42 app.GUID = "my-app-guid" 43 appRepo = new(applicationsfakes.FakeRepository) 44 requirementsFactory = new(requirementsfakes.FakeFactory) 45 configRepo = testconfig.NewRepositoryWithDefaults() 46 }) 47 48 runCommand := func(args ...string) bool { 49 return testcmd.RunCLICommand("set-env", args, requirementsFactory, updateCommandDependency, false, ui) 50 } 51 52 Describe("requirements", func() { 53 BeforeEach(func() { 54 applicationReq := new(requirementsfakes.FakeApplicationRequirement) 55 applicationReq.GetApplicationReturns(app) 56 requirementsFactory.NewApplicationRequirementReturns(applicationReq) 57 }) 58 59 It("fails when login is not successful", func() { 60 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 61 62 Expect(runCommand("hey", "gabba", "gabba")).To(BeFalse()) 63 }) 64 65 It("fails when a space is not targeted", func() { 66 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 67 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Failing{Message: "not targeting space"}) 68 69 Expect(runCommand("hey", "gabba", "gabba")).To(BeFalse()) 70 }) 71 72 It("fails with usage when not provided with exactly three args", func() { 73 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 74 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 75 76 runCommand("zomg", "too", "many", "args") 77 Expect(ui.Outputs()).To(ContainSubstrings( 78 []string{"Incorrect Usage", "Requires", "arguments"}, 79 )) 80 }) 81 }) 82 83 Context("when logged in, a space is targeted and given enough args", func() { 84 BeforeEach(func() { 85 app.EnvironmentVars = map[string]interface{}{"foo": "bar"} 86 applicationReq := new(requirementsfakes.FakeApplicationRequirement) 87 applicationReq.GetApplicationReturns(app) 88 requirementsFactory.NewApplicationRequirementReturns(applicationReq) 89 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 90 requirementsFactory.NewTargetedSpaceRequirementReturns(requirements.Passing{}) 91 }) 92 93 Context("when it is new", func() { 94 It("is created", func() { 95 runCommand("my-app", "DATABASE_URL", "mysql://new-example.com/my-db") 96 97 Expect(ui.Outputs()).To(ContainSubstrings( 98 []string{ 99 "Setting env variable", 100 "DATABASE_URL", 101 "my-app", 102 "my-org", 103 "my-space", 104 "my-user", 105 }, 106 []string{"OK"}, 107 []string{"TIP"}, 108 )) 109 110 Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"mysql://new-example.com/my-db"})) 111 112 appGUID, params := appRepo.UpdateArgsForCall(0) 113 Expect(appGUID).To(Equal(app.GUID)) 114 Expect(*params.EnvironmentVars).To(Equal(map[string]interface{}{ 115 "DATABASE_URL": "mysql://new-example.com/my-db", 116 "foo": "bar", 117 })) 118 }) 119 }) 120 121 Context("when it already exists", func() { 122 BeforeEach(func() { 123 app.EnvironmentVars["DATABASE_URL"] = "mysql://old-example.com/my-db" 124 }) 125 126 It("is updated", func() { 127 runCommand("my-app", "DATABASE_URL", "mysql://new-example.com/my-db") 128 129 Expect(ui.Outputs()).To(ContainSubstrings( 130 []string{ 131 "Setting env variable", 132 "DATABASE_URL", 133 "my-app", 134 "my-org", 135 "my-space", 136 "my-user", 137 }, 138 []string{"OK"}, 139 []string{"TIP"}, 140 )) 141 142 Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"mysql://new-example.com/my-db"})) 143 }) 144 }) 145 146 It("allows the variable value to begin with a hyphen", func() { 147 runCommand("my-app", "MY_VAR", "--has-a-cool-value") 148 149 Expect(ui.Outputs()).To(ContainSubstrings( 150 []string{ 151 "Setting env variable", 152 "MY_VAR", 153 }, 154 []string{"OK"}, 155 []string{"TIP"}, 156 )) 157 _, params := appRepo.UpdateArgsForCall(0) 158 Expect(*params.EnvironmentVars).To(Equal(map[string]interface{}{ 159 "MY_VAR": "--has-a-cool-value", 160 "foo": "bar", 161 })) 162 }) 163 164 Context("when setting fails", func() { 165 BeforeEach(func() { 166 appRepo.UpdateReturns(models.Application{}, errors.New("Error updating app.")) 167 }) 168 169 It("tells the user", func() { 170 runCommand("please", "dont", "fail") 171 172 Expect(ui.Outputs()).To(ContainSubstrings( 173 []string{"Setting env variable"}, 174 []string{"FAILED"}, 175 []string{"Error updating app."}, 176 )) 177 }) 178 }) 179 180 It("gives the appropriate tip", func() { 181 runCommand("my-app", "DATABASE_URL", "mysql://new-example.com/my-db") 182 Expect(ui.Outputs()).To(ContainSubstrings( 183 []string{"TIP: Use 'cf restage my-app' to ensure your env variable changes take effect"}, 184 )) 185 }) 186 }) 187 })