github.com/sleungcy/cli@v7.1.0+incompatible/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(Say(`"%s": "%s"`, envVarName, envVarValue))
   134  					Eventually(session).Should(Exit(0))
   135  				})
   136  
   137  				// This is to prevent the '-' being read in as another flag
   138  				When("the environment variable value starts with a '-' character", func() {
   139  					BeforeEach(func() {
   140  						envVarValue = "-" + envVarValue
   141  					})
   142  
   143  					It("sets the environment variable value pair", func() {
   144  						session := helpers.CF("set-env", appName, envVarName, envVarValue)
   145  
   146  						Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   147  						Eventually(session).Should(Say("OK"))
   148  						Eventually(session).Should(Say(`TIP: Use 'cf restage %s' to ensure your env variable changes take effect\.`, appName))
   149  						Eventually(session).Should(Exit(0))
   150  
   151  						session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   152  						Eventually(session).Should(Say(`"%s": "%s"`, envVarName, envVarValue))
   153  						Eventually(session).Should(Exit(0))
   154  					})
   155  				})
   156  			})
   157  
   158  			When("the environment variable has been previously set", func() {
   159  				BeforeEach(func() {
   160  					Eventually(helpers.CF("set-env", appName, envVarName, envVarValue)).Should(Exit(0))
   161  				})
   162  
   163  				It("overrides the value of the existing environment variable", func() {
   164  					someOtherValue := "some-other-value"
   165  					session := helpers.CF("set-env", appName, envVarName, someOtherValue)
   166  
   167  					Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   168  					Eventually(session).Should(Say("OK"))
   169  					Eventually(session).Should(Say(`TIP: Use 'cf restage %s' to ensure your env variable changes take effect\.`, appName))
   170  					Eventually(session).Should(Exit(0))
   171  
   172  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   173  					Eventually(session).Should(Say(`"%s": "%s"`, envVarName, someOtherValue))
   174  					Eventually(session).Should(Exit(0))
   175  				})
   176  			})
   177  
   178  		})
   179  	})
   180  })