github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/isolated/set_env_command_test.go (about) 1 package isolated 2 3 import ( 4 "fmt" 5 6 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 7 8 "code.cloudfoundry.org/cli/integration/helpers" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/gbytes" 12 . "github.com/onsi/gomega/gexec" 13 ) 14 15 var _ = Describe("set-env command", func() { 16 var ( 17 orgName string 18 spaceName string 19 appName string 20 envVarName string 21 envVarValue string 22 ) 23 24 BeforeEach(func() { 25 orgName = helpers.NewOrgName() 26 spaceName = helpers.NewSpaceName() 27 appName = helpers.PrefixedRandomName("app") 28 envVarName = "SOME_ENV_VAR" 29 envVarValue = "SOME_ENV_VAR_VALUE" 30 }) 31 32 Describe("help", func() { 33 When("--help flag is set", func() { 34 It("appears in cf help -a", func() { 35 session := helpers.CF("help", "-a") 36 Eventually(session).Should(Exit(0)) 37 Expect(session).To(HaveCommandInCategoryWithDescription("set-env", "APPS", "Set an env variable for an app")) 38 }) 39 40 It("displays command usage to output", func() { 41 session := helpers.CF("set-env", "--help") 42 43 Eventually(session).Should(Say("NAME:")) 44 Eventually(session).Should(Say("set-env - Set an env variable for an app")) 45 Eventually(session).Should(Say("USAGE:")) 46 Eventually(session).Should(Say("cf set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE")) 47 Eventually(session).Should(Say("ALIAS:")) 48 Eventually(session).Should(Say("se")) 49 Eventually(session).Should(Say("SEE ALSO:")) 50 Eventually(session).Should(Say("apps, env, restart, set-running-environment-variable-group, set-staging-environment-variable-group, stage, unset-env")) 51 Eventually(session).Should(Exit(0)) 52 }) 53 }) 54 }) 55 56 When("the app name is not provided", func() { 57 It("tells the user that the app name is required, prints help text, and exits 1", func() { 58 session := helpers.CF("set-env") 59 60 Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME`, `ENV_VAR_NAME` and `ENV_VAR_VALUE` were not provided")) 61 Eventually(session).Should(Say("NAME:")) 62 Eventually(session).Should(Exit(1)) 63 }) 64 }) 65 66 When("ENV_VAR_NAME is not provided", func() { 67 It("tells the user that ENV_VAR_NAME is required, prints help text, and exits 1", func() { 68 session := helpers.CF("set-env", appName) 69 70 Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `ENV_VAR_NAME` and `ENV_VAR_VALUE` were not provided")) 71 Eventually(session).Should(Say("NAME:")) 72 Eventually(session).Should(Exit(1)) 73 }) 74 }) 75 76 When("the ENV_VAR_VALUE is not provided", func() { 77 It("tells the user that ENV_VAR_VALUE is required, prints help text, and exits 1", func() { 78 session := helpers.CF("set-env", appName, envVarName) 79 80 Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ENV_VAR_VALUE` was not provided")) 81 Eventually(session).Should(Say("NAME:")) 82 Eventually(session).Should(Exit(1)) 83 }) 84 }) 85 86 When("the environment is not setup correctly", func() { 87 It("fails with the appropriate errors", func() { 88 helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "set-env", appName, envVarName, envVarValue) 89 }) 90 }) 91 92 When("the environment is set up correctly", func() { 93 var userName string 94 95 BeforeEach(func() { 96 helpers.SetupCF(orgName, spaceName) 97 userName, _ = helpers.GetCredentials() 98 }) 99 100 AfterEach(func() { 101 helpers.QuickDeleteOrg(orgName) 102 }) 103 104 When("the app does not exist", func() { 105 It("displays app not found and exits 1", func() { 106 invalidAppName := "invalid-app-name" 107 session := helpers.CF("set-env", invalidAppName, envVarName, envVarValue) 108 109 Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, invalidAppName, orgName, spaceName, userName)) 110 Eventually(session.Err).Should(Say("App '%s' not found", invalidAppName)) 111 Eventually(session).Should(Say("FAILED")) 112 Eventually(session).Should(Exit(1)) 113 }) 114 }) 115 116 When("the app exists", func() { 117 BeforeEach(func() { 118 helpers.WithHelloWorldApp(func(appDir string) { 119 Eventually(helpers.CF("push", appName, "-p", appDir, "--no-start")).Should(Exit(0)) 120 }) 121 }) 122 123 When("the environment variable has not been previously set", func() { 124 It("sets the environment variable value pair", func() { 125 session := helpers.CF("set-env", appName, envVarName, envVarValue) 126 127 Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName)) 128 Eventually(session).Should(Say("OK")) 129 Eventually(session).Should(Say(`TIP: Use 'cf restage %s' to ensure your env variable changes take effect\.`, appName)) 130 Eventually(session).Should(Exit(0)) 131 132 session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName))) 133 Eventually(session).Should(Exit(0)) 134 135 bytes := session.Out.Contents() 136 137 actualEnvVarValue := helpers.GetsDefaultEnvVarValue(bytes) 138 Expect(actualEnvVarValue).To(Equal(envVarValue)) 139 }) 140 141 // This is to prevent the '-' being read in as another flag 142 When("the environment variable value starts with a '-' character", func() { 143 BeforeEach(func() { 144 envVarValue = "-" + envVarValue 145 }) 146 147 It("sets the environment variable value pair", func() { 148 session := helpers.CF("set-env", appName, envVarName, envVarValue) 149 150 Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName)) 151 Eventually(session).Should(Say("OK")) 152 Eventually(session).Should(Say(`TIP: Use 'cf restage %s' to ensure your env variable changes take effect\.`, appName)) 153 Eventually(session).Should(Exit(0)) 154 155 session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName))) 156 Eventually(session).Should(Exit(0)) 157 158 bytes := session.Out.Contents() 159 160 actualEnvVarValue := helpers.GetsDefaultEnvVarValue(bytes) 161 Expect(actualEnvVarValue).To(Equal(envVarValue)) 162 }) 163 }) 164 }) 165 166 When("the environment variable has been previously set", func() { 167 BeforeEach(func() { 168 Eventually(helpers.CF("set-env", appName, envVarName, envVarValue)).Should(Exit(0)) 169 }) 170 171 It("overrides the value of the existing environment variable", func() { 172 someOtherValue := "some-other-value" 173 session := helpers.CF("set-env", appName, envVarName, someOtherValue) 174 175 Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName)) 176 Eventually(session).Should(Say("OK")) 177 Eventually(session).Should(Say(`TIP: Use 'cf restage %s' to ensure your env variable changes take effect\.`, appName)) 178 Eventually(session).Should(Exit(0)) 179 180 session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName))) 181 Eventually(session).Should(Exit(0)) 182 bytes := session.Out.Contents() 183 184 actualEnvVarValue := helpers.GetsDefaultEnvVarValue(bytes) 185 Expect(actualEnvVarValue).To(Equal(someOtherValue)) 186 }) 187 }) 188 189 }) 190 }) 191 })