github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/v7/isolated/set_env_command_test.go (about)

     1  package isolated
     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  )
    12  
    13  var _ = Describe("set-env command", func() {
    14  	var (
    15  		orgName     string
    16  		spaceName   string
    17  		appName     string
    18  		envVarName  string
    19  		envVarValue 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  		envVarValue = "SOME_ENV_VAR_VALUE"
    28  	})
    29  
    30  	Describe("help", func() {
    31  		When("--help flag is set", func() {
    32  			It("displays command usage to output", func() {
    33  				session := helpers.CF("set-env", "--help")
    34  
    35  				Eventually(session).Should(Say("NAME:"))
    36  				Eventually(session).Should(Say("set-env - Set an env variable for an app"))
    37  				Eventually(session).Should(Say("USAGE:"))
    38  				Eventually(session).Should(Say("cf set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE"))
    39  				Eventually(session).Should(Say("ALIAS:"))
    40  				Eventually(session).Should(Say("se"))
    41  				Eventually(session).Should(Say("SEE ALSO:"))
    42  				Eventually(session).Should(Say("env, set-running-environment-variable-group, set-staging-environment-variable-group, unset-env, v3-apps, v3-restart, v3-stage"))
    43  				Eventually(session).Should(Exit(0))
    44  			})
    45  		})
    46  	})
    47  
    48  	When("the app name is not provided", func() {
    49  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    50  			session := helpers.CF("set-env")
    51  
    52  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME`, `ENV_VAR_NAME` and `ENV_VAR_VALUE` were not provided"))
    53  			Eventually(session).Should(Say("NAME:"))
    54  			Eventually(session).Should(Exit(1))
    55  		})
    56  	})
    57  
    58  	When("ENV_VAR_NAME is not provided", func() {
    59  		It("tells the user that ENV_VAR_NAME is required, prints help text, and exits 1", func() {
    60  			session := helpers.CF("set-env", appName)
    61  
    62  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `ENV_VAR_NAME` and `ENV_VAR_VALUE` were not provided"))
    63  			Eventually(session).Should(Say("NAME:"))
    64  			Eventually(session).Should(Exit(1))
    65  		})
    66  	})
    67  
    68  	When("the ENV_VAR_VALUE is not provided", func() {
    69  		It("tells the user that ENV_VAR_VALUE is required, prints help text, and exits 1", func() {
    70  			session := helpers.CF("set-env", appName, envVarName)
    71  
    72  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ENV_VAR_VALUE` was not provided"))
    73  			Eventually(session).Should(Say("NAME:"))
    74  			Eventually(session).Should(Exit(1))
    75  		})
    76  	})
    77  
    78  	When("the environment is not setup correctly", func() {
    79  		It("fails with the appropriate errors", func() {
    80  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "set-env", appName, envVarName, envVarValue)
    81  		})
    82  	})
    83  
    84  	When("the environment is set up correctly", func() {
    85  		var userName string
    86  
    87  		BeforeEach(func() {
    88  			helpers.SetupCF(orgName, spaceName)
    89  			userName, _ = helpers.GetCredentials()
    90  		})
    91  
    92  		AfterEach(func() {
    93  			helpers.QuickDeleteOrg(orgName)
    94  		})
    95  
    96  		When("the app does not exist", func() {
    97  			It("displays app not found and exits 1", func() {
    98  				invalidAppName := "invalid-app-name"
    99  				session := helpers.CF("set-env", invalidAppName, envVarName, envVarValue)
   100  
   101  				Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, invalidAppName, orgName, spaceName, userName))
   102  				Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   103  				Eventually(session).Should(Say("FAILED"))
   104  				Eventually(session).Should(Exit(1))
   105  			})
   106  		})
   107  
   108  		When("the app exists", func() {
   109  			BeforeEach(func() {
   110  				helpers.WithHelloWorldApp(func(appDir string) {
   111  					Eventually(helpers.CF("push", appName, "-p", appDir)).Should(Exit(0))
   112  				})
   113  			})
   114  
   115  			When("the environment variable has not been previously set", func() {
   116  				It("sets the environment variable value pair", func() {
   117  					session := helpers.CF("set-env", appName, envVarName, envVarValue)
   118  
   119  					Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   120  					Eventually(session).Should(Say("OK"))
   121  					Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   122  					Eventually(session).Should(Exit(0))
   123  
   124  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   125  					Eventually(session).Should(Say(`"%s": "%s"`, envVarName, envVarValue))
   126  					Eventually(session).Should(Exit(0))
   127  				})
   128  
   129  				// This is to prevent the '-' being read in as another flag
   130  				When("the environment variable value starts with a '-' character", func() {
   131  					BeforeEach(func() {
   132  						envVarValue = "-" + envVarValue
   133  					})
   134  
   135  					It("sets the environment variable value pair", func() {
   136  						session := helpers.CF("set-env", appName, envVarName, envVarValue)
   137  
   138  						Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   139  						Eventually(session).Should(Say("OK"))
   140  						Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   141  						Eventually(session).Should(Exit(0))
   142  
   143  						session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   144  						Eventually(session).Should(Say(`"%s": "%s"`, envVarName, envVarValue))
   145  						Eventually(session).Should(Exit(0))
   146  					})
   147  				})
   148  			})
   149  
   150  			When("the environment variable has been previously set", func() {
   151  				BeforeEach(func() {
   152  					Eventually(helpers.CF("set-env", appName, envVarName, envVarValue)).Should(Exit(0))
   153  				})
   154  
   155  				It("overrides the value of the existing environment variable", func() {
   156  					someOtherValue := "some-other-value"
   157  					session := helpers.CF("set-env", appName, envVarName, someOtherValue)
   158  
   159  					Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   160  					Eventually(session).Should(Say("OK"))
   161  					Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   162  					Eventually(session).Should(Exit(0))
   163  
   164  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   165  					Eventually(session).Should(Say(`"%s": "%s"`, envVarName, someOtherValue))
   166  					Eventually(session).Should(Exit(0))
   167  				})
   168  			})
   169  
   170  		})
   171  	})
   172  })