github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/experimental/v3_unset_env_command_test.go (about) 1 package experimental 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/integration/helpers" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 . "github.com/onsi/gomega/gexec" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("v3-unset-env command", func() { 15 var ( 16 orgName string 17 spaceName string 18 appName string 19 envVarName string 20 ) 21 22 BeforeEach(func() { 23 orgName = helpers.NewOrgName() 24 spaceName = helpers.NewSpaceName() 25 appName = helpers.PrefixedRandomName("app") 26 envVarName = "SOME_ENV_VAR" 27 }) 28 29 Describe("help", func() { 30 Context("when --help flag is set", func() { 31 It("displays command usage to output", func() { 32 session := helpers.CF("v3-unset-env", "--help") 33 34 Eventually(session).Should(Say("NAME:")) 35 Eventually(session).Should(Say("v3-unset-env - Remove an env variable from an app")) 36 Eventually(session).Should(Say("USAGE:")) 37 Eventually(session).Should(Say("cf v3-unset-env APP_NAME ENV_VAR_NAME")) 38 Eventually(session).Should(Say("SEE ALSO:")) 39 Eventually(session).Should(Say("v3-apps, v3-env, v3-restart, v3-set-env, v3-stage")) 40 Eventually(session).Should(Exit(0)) 41 }) 42 }) 43 }) 44 45 Context("when the app name is not provided", func() { 46 It("tells the user that the app name is required, prints help text, and exits 1", func() { 47 session := helpers.CF("v3-unset-env") 48 49 Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME` and `ENV_VAR_NAME` were not provided")) 50 Eventually(session).Should(Say("NAME:")) 51 Eventually(session).Should(Exit(1)) 52 }) 53 }) 54 55 Context("when ENV_VAR_NAME is not provided", func() { 56 It("tells the user that ENV_VAR_NAME is required, prints help text, and exits 1", func() { 57 session := helpers.CF("v3-unset-env", appName) 58 59 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ENV_VAR_NAME` was not provided")) 60 Eventually(session).Should(Say("NAME:")) 61 Eventually(session).Should(Exit(1)) 62 }) 63 }) 64 65 It("displays the experimental warning", func() { 66 session := helpers.CF("v3-unset-env", appName, envVarName) 67 Eventually(session).Should(Say("This command is in EXPERIMENTAL stage and may change without notice")) 68 Eventually(session).Should(Exit()) 69 }) 70 71 Context("when the environment is not setup correctly", func() { 72 Context("when the v3 api does not exist", func() { 73 var server *Server 74 75 BeforeEach(func() { 76 server = helpers.StartAndTargetServerWithoutV3API() 77 }) 78 79 AfterEach(func() { 80 server.Close() 81 }) 82 83 It("fails with error message that the minimum version is not met", func() { 84 session := helpers.CF("v3-unset-env", appName, envVarName) 85 Eventually(session).Should(Say("FAILED")) 86 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\.")) 87 Eventually(session).Should(Exit(1)) 88 }) 89 }) 90 91 Context("when the v3 api version is lower than the minimum version", func() { 92 var server *Server 93 94 BeforeEach(func() { 95 server = helpers.StartAndTargetServerWithV3Version("3.0.0") 96 }) 97 98 AfterEach(func() { 99 server.Close() 100 }) 101 102 It("fails with error message that the minimum version is not met", func() { 103 session := helpers.CF("v3-unset-env", appName, envVarName) 104 Eventually(session).Should(Say("FAILED")) 105 Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\.")) 106 Eventually(session).Should(Exit(1)) 107 }) 108 }) 109 110 Context("when no API endpoint is set", func() { 111 BeforeEach(func() { 112 helpers.UnsetAPI() 113 }) 114 115 It("fails with no API endpoint set message", func() { 116 session := helpers.CF("v3-unset-env", appName, envVarName) 117 Eventually(session).Should(Say("FAILED")) 118 Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\.")) 119 Eventually(session).Should(Exit(1)) 120 }) 121 }) 122 123 Context("when not logged in", func() { 124 BeforeEach(func() { 125 helpers.LogoutCF() 126 }) 127 128 It("fails with not logged in message", func() { 129 session := helpers.CF("v3-unset-env", appName, envVarName) 130 Eventually(session).Should(Say("FAILED")) 131 Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\.")) 132 Eventually(session).Should(Exit(1)) 133 }) 134 }) 135 136 Context("when there is no org set", func() { 137 BeforeEach(func() { 138 helpers.LogoutCF() 139 helpers.LoginCF() 140 }) 141 142 It("fails with no org targeted error message", func() { 143 session := helpers.CF("v3-unset-env", appName, envVarName) 144 Eventually(session).Should(Say("FAILED")) 145 Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\.")) 146 Eventually(session).Should(Exit(1)) 147 }) 148 }) 149 150 Context("when there is no space set", func() { 151 BeforeEach(func() { 152 helpers.LogoutCF() 153 helpers.LoginCF() 154 helpers.TargetOrg(ReadOnlyOrg) 155 }) 156 157 It("fails with no space targeted error message", func() { 158 session := helpers.CF("v3-unset-env", appName, envVarName) 159 Eventually(session).Should(Say("FAILED")) 160 Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\.")) 161 Eventually(session).Should(Exit(1)) 162 }) 163 }) 164 }) 165 166 Context("when the environment is set up correctly", func() { 167 var userName string 168 169 BeforeEach(func() { 170 setupCF(orgName, spaceName) 171 userName, _ = helpers.GetCredentials() 172 }) 173 174 Context("when the app does not exist", func() { 175 It("displays app not found and exits 1", func() { 176 invalidAppName := "invalid-app-name" 177 session := helpers.CF("v3-unset-env", invalidAppName, envVarName) 178 179 Eventually(session).Should(Say("Removing env variable %s from app %s in org %s / space %s as %s\\.\\.\\.", envVarName, invalidAppName, orgName, spaceName, userName)) 180 Eventually(session.Err).Should(Say("App %s not found", invalidAppName)) 181 Eventually(session).Should(Say("FAILED")) 182 Eventually(session).Should(Exit(1)) 183 }) 184 }) 185 186 Context("when the app exists", func() { 187 BeforeEach(func() { 188 helpers.WithHelloWorldApp(func(appDir string) { 189 Eventually(helpers.CF("v3-push", appName, "-p", appDir)).Should(Exit(0)) 190 }) 191 }) 192 193 Context("when the environment variable has not been previously set", func() { 194 It("returns a warning indicating variable was not set", func() { 195 session := helpers.CF("v3-unset-env", appName, envVarName) 196 197 Eventually(session).Should(Say("Removing env variable %s from app %s in org %s / space %s as %s\\.\\.\\.", envVarName, appName, orgName, spaceName, userName)) 198 Eventually(session).Should(Say("Env variable %s was not set.", envVarName)) 199 Eventually(session).Should(Say("OK")) 200 Eventually(session).Should(Exit(0)) 201 }) 202 }) 203 204 Context("when the environment variable has been previously set", func() { 205 BeforeEach(func() { 206 Eventually(helpers.CF("v3-set-env", appName, envVarName, "some-value")).Should(Exit(0)) 207 }) 208 209 It("overrides the value of the existing environment variable", func() { 210 session := helpers.CF("v3-unset-env", appName, envVarName) 211 212 Eventually(session).Should(Say("Removing env variable %s from app %s in org %s / space %s as %s\\.\\.\\.", envVarName, appName, orgName, spaceName, userName)) 213 Eventually(session).Should(Say("OK")) 214 Eventually(session).Should(Say("TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\\.", appName)) 215 216 session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName))) 217 Eventually(session).ShouldNot(Say(`"%s"`, envVarName)) 218 Eventually(session).Should(Exit(0)) 219 }) 220 }) 221 }) 222 }) 223 })