github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/set_env_test.go (about) 1 package application_test 2 3 import ( 4 testApplication "github.com/cloudfoundry/cli/cf/api/applications/fakes" 5 "github.com/cloudfoundry/cli/cf/configuration/core_config" 6 "github.com/cloudfoundry/cli/cf/models" 7 testcmd "github.com/cloudfoundry/cli/testhelpers/commands" 8 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 9 testreq "github.com/cloudfoundry/cli/testhelpers/requirements" 10 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 11 12 . "github.com/cloudfoundry/cli/cf/commands/application" 13 . "github.com/cloudfoundry/cli/testhelpers/matchers" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("set-env command", func() { 19 var ( 20 ui *testterm.FakeUI 21 configRepo core_config.ReadWriter 22 app models.Application 23 appRepo *testApplication.FakeApplicationRepository 24 requirementsFactory *testreq.FakeReqFactory 25 ) 26 27 BeforeEach(func() { 28 ui = &testterm.FakeUI{} 29 app = models.Application{} 30 app.Name = "my-app" 31 app.Guid = "my-app-guid" 32 appRepo = &testApplication.FakeApplicationRepository{} 33 requirementsFactory = &testreq.FakeReqFactory{} 34 configRepo = testconfig.NewRepositoryWithDefaults() 35 }) 36 37 runCommand := func(args ...string) bool { 38 return testcmd.RunCommand(NewSetEnv(ui, configRepo, appRepo), args, requirementsFactory) 39 } 40 41 Describe("requirements", func() { 42 It("fails when login is not successful", func() { 43 requirementsFactory.Application = app 44 requirementsFactory.TargetedSpaceSuccess = true 45 46 Expect(runCommand("hey", "gabba", "gabba")).To(BeFalse()) 47 }) 48 49 It("fails when a space is not targeted", func() { 50 requirementsFactory.Application = app 51 requirementsFactory.LoginSuccess = true 52 53 Expect(runCommand("hey", "gabba", "gabba")).To(BeFalse()) 54 }) 55 56 It("fails with usage when not provided with exactly three args", func() { 57 requirementsFactory.Application = app 58 requirementsFactory.LoginSuccess = true 59 requirementsFactory.TargetedSpaceSuccess = true 60 61 runCommand("zomg", "too", "many", "args") 62 Expect(ui.FailedWithUsage).To(BeTrue()) 63 }) 64 }) 65 66 Context("when logged in, a space is targeted and given enough args", func() { 67 BeforeEach(func() { 68 app.EnvironmentVars = map[string]interface{}{"foo": "bar"} 69 requirementsFactory.Application = app 70 requirementsFactory.LoginSuccess = true 71 requirementsFactory.TargetedSpaceSuccess = true 72 }) 73 74 Context("when it is new", func() { 75 It("is created", func() { 76 runCommand("my-app", "DATABASE_URL", "mysql://new-example.com/my-db") 77 78 Expect(ui.Outputs).To(ContainSubstrings( 79 []string{ 80 "Setting env variable", 81 "DATABASE_URL", 82 "mysql://new-example.com/my-db", 83 "my-app", 84 "my-org", 85 "my-space", 86 "my-user", 87 }, 88 []string{"OK"}, 89 []string{"TIP"}, 90 )) 91 92 Expect(requirementsFactory.ApplicationName).To(Equal("my-app")) 93 Expect(appRepo.UpdateAppGuid).To(Equal(app.Guid)) 94 Expect(*appRepo.UpdateParams.EnvironmentVars).To(Equal(map[string]interface{}{ 95 "DATABASE_URL": "mysql://new-example.com/my-db", 96 "foo": "bar", 97 })) 98 }) 99 }) 100 101 Context("when it already exists", func() { 102 BeforeEach(func() { 103 app.EnvironmentVars["DATABASE_URL"] = "mysql://old-example.com/my-db" 104 }) 105 106 It("is updated", func() { 107 runCommand("my-app", "DATABASE_URL", "mysql://new-example.com/my-db") 108 109 Expect(ui.Outputs).To(ContainSubstrings( 110 []string{ 111 "Setting env variable", 112 "DATABASE_URL", 113 "mysql://new-example.com/my-db", 114 "my-app", 115 "my-org", 116 "my-space", 117 "my-user", 118 }, 119 []string{"OK"}, 120 []string{"TIP"}, 121 )) 122 }) 123 }) 124 125 It("allows the variable value to begin with a hyphen", func() { 126 runCommand("my-app", "MY_VAR", "--has-a-cool-value") 127 128 Expect(ui.Outputs).To(ContainSubstrings( 129 []string{ 130 "Setting env variable", 131 "MY_VAR", 132 "--has-a-cool-value", 133 }, 134 []string{"OK"}, 135 []string{"TIP"}, 136 )) 137 Expect(*appRepo.UpdateParams.EnvironmentVars).To(Equal(map[string]interface{}{ 138 "MY_VAR": "--has-a-cool-value", 139 "foo": "bar", 140 })) 141 }) 142 143 Context("when setting fails", func() { 144 BeforeEach(func() { 145 appRepo.UpdateErr = true 146 }) 147 148 It("tells the user", func() { 149 runCommand("please", "dont", "fail") 150 151 Expect(ui.Outputs).To(ContainSubstrings( 152 []string{"Setting env variable"}, 153 []string{"FAILED"}, 154 []string{"Error updating app."}, 155 )) 156 }) 157 }) 158 }) 159 })