github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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/util/testhelpers/commands" 13 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 14 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 15 16 . "code.cloudfoundry.org/cli/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 "mysql://new-example.com/my-db", 102 "my-app", 103 "my-org", 104 "my-space", 105 "my-user", 106 }, 107 []string{"OK"}, 108 []string{"TIP"}, 109 )) 110 111 appGUID, params := appRepo.UpdateArgsForCall(0) 112 Expect(appGUID).To(Equal(app.GUID)) 113 Expect(*params.EnvironmentVars).To(Equal(map[string]interface{}{ 114 "DATABASE_URL": "mysql://new-example.com/my-db", 115 "foo": "bar", 116 })) 117 }) 118 }) 119 120 Context("when it already exists", func() { 121 BeforeEach(func() { 122 app.EnvironmentVars["DATABASE_URL"] = "mysql://old-example.com/my-db" 123 }) 124 125 It("is updated", func() { 126 runCommand("my-app", "DATABASE_URL", "mysql://new-example.com/my-db") 127 128 Expect(ui.Outputs()).To(ContainSubstrings( 129 []string{ 130 "Setting env variable", 131 "DATABASE_URL", 132 "mysql://new-example.com/my-db", 133 "my-app", 134 "my-org", 135 "my-space", 136 "my-user", 137 }, 138 []string{"OK"}, 139 []string{"TIP"}, 140 )) 141 }) 142 }) 143 144 It("allows the variable value to begin with a hyphen", func() { 145 runCommand("my-app", "MY_VAR", "--has-a-cool-value") 146 147 Expect(ui.Outputs()).To(ContainSubstrings( 148 []string{ 149 "Setting env variable", 150 "MY_VAR", 151 "--has-a-cool-value", 152 }, 153 []string{"OK"}, 154 []string{"TIP"}, 155 )) 156 _, params := appRepo.UpdateArgsForCall(0) 157 Expect(*params.EnvironmentVars).To(Equal(map[string]interface{}{ 158 "MY_VAR": "--has-a-cool-value", 159 "foo": "bar", 160 })) 161 }) 162 163 Context("when setting fails", func() { 164 BeforeEach(func() { 165 appRepo.UpdateReturns(models.Application{}, errors.New("Error updating app.")) 166 }) 167 168 It("tells the user", func() { 169 runCommand("please", "dont", "fail") 170 171 Expect(ui.Outputs()).To(ContainSubstrings( 172 []string{"Setting env variable"}, 173 []string{"FAILED"}, 174 []string{"Error updating app."}, 175 )) 176 }) 177 }) 178 179 It("gives the appropriate tip", func() { 180 runCommand("my-app", "DATABASE_URL", "mysql://new-example.com/my-db") 181 Expect(ui.Outputs()).To(ContainSubstrings( 182 []string{"TIP: Use 'cf restage my-app' to ensure your env variable changes take effect"}, 183 )) 184 }) 185 }) 186 })