github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v6/experimental/v3_set_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  )
    12  
    13  var _ = Describe("v3-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("v3-set-env", "--help")
    34  
    35  				Eventually(session).Should(Say("NAME:"))
    36  				Eventually(session).Should(Say("v3-set-env - Set an env variable for an app"))
    37  				Eventually(session).Should(Say("USAGE:"))
    38  				Eventually(session).Should(Say("cf v3-set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE"))
    39  				Eventually(session).Should(Say("SEE ALSO:"))
    40  				Eventually(session).Should(Say("set-running-environment-variable-group, set-staging-environment-variable-group, v3-apps, v3-env, v3-restart, v3-stage, v3-unset-env"))
    41  				Eventually(session).Should(Exit(0))
    42  			})
    43  		})
    44  	})
    45  
    46  	When("the app name is not provided", func() {
    47  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    48  			session := helpers.CF("v3-set-env")
    49  
    50  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME`, `ENV_VAR_NAME` and `ENV_VAR_VALUE` were not provided"))
    51  			Eventually(session).Should(Say("NAME:"))
    52  			Eventually(session).Should(Exit(1))
    53  		})
    54  	})
    55  
    56  	When("ENV_VAR_NAME is not provided", func() {
    57  		It("tells the user that ENV_VAR_NAME is required, prints help text, and exits 1", func() {
    58  			session := helpers.CF("v3-set-env", appName)
    59  
    60  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `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("the ENV_VAR_VALUE is not provided", func() {
    67  		It("tells the user that ENV_VAR_VALUE is required, prints help text, and exits 1", func() {
    68  			session := helpers.CF("v3-set-env", appName, envVarName)
    69  
    70  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `ENV_VAR_VALUE` was not provided"))
    71  			Eventually(session).Should(Say("NAME:"))
    72  			Eventually(session).Should(Exit(1))
    73  		})
    74  	})
    75  
    76  	It("displays the experimental warning", func() {
    77  		session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
    78  		Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    79  		Eventually(session).Should(Exit())
    80  	})
    81  
    82  	When("the environment is not setup correctly", func() {
    83  		When("no API endpoint is set", func() {
    84  			BeforeEach(func() {
    85  				helpers.UnsetAPI()
    86  			})
    87  
    88  			It("fails with no API endpoint set message", func() {
    89  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
    90  				Eventually(session).Should(Say("FAILED"))
    91  				Eventually(session.Err).Should(Say(`No API endpoint set\. Use 'cf login' or 'cf api' to target an endpoint\.`))
    92  				Eventually(session).Should(Exit(1))
    93  			})
    94  		})
    95  
    96  		When("not logged in", func() {
    97  			BeforeEach(func() {
    98  				helpers.LogoutCF()
    99  			})
   100  
   101  			It("fails with not logged in message", func() {
   102  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   103  				Eventually(session).Should(Say("FAILED"))
   104  				Eventually(session.Err).Should(Say(`Not logged in\. Use 'cf login' or 'cf login --sso' to log in\.`))
   105  				Eventually(session).Should(Exit(1))
   106  			})
   107  		})
   108  
   109  		When("there is no org set", func() {
   110  			BeforeEach(func() {
   111  				helpers.LogoutCF()
   112  				helpers.LoginCF()
   113  			})
   114  
   115  			It("fails with no org targeted error message", func() {
   116  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   117  				Eventually(session).Should(Say("FAILED"))
   118  				Eventually(session.Err).Should(Say(`No org targeted, use 'cf target -o ORG' to target an org\.`))
   119  				Eventually(session).Should(Exit(1))
   120  			})
   121  		})
   122  
   123  		When("there is no space set", func() {
   124  			BeforeEach(func() {
   125  				helpers.LogoutCF()
   126  				helpers.LoginCF()
   127  				helpers.TargetOrg(ReadOnlyOrg)
   128  			})
   129  
   130  			It("fails with no space targeted error message", func() {
   131  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   132  				Eventually(session).Should(Say("FAILED"))
   133  				Eventually(session.Err).Should(Say(`No space targeted, use 'cf target -s SPACE' to target a space\.`))
   134  				Eventually(session).Should(Exit(1))
   135  			})
   136  		})
   137  	})
   138  
   139  	When("the environment is set up correctly", func() {
   140  		var userName string
   141  
   142  		BeforeEach(func() {
   143  			helpers.SetupCF(orgName, spaceName)
   144  			userName, _ = helpers.GetCredentials()
   145  		})
   146  
   147  		AfterEach(func() {
   148  			helpers.QuickDeleteOrg(orgName)
   149  		})
   150  
   151  		When("the app does not exist", func() {
   152  			It("displays app not found and exits 1", func() {
   153  				invalidAppName := "invalid-app-name"
   154  				session := helpers.CF("v3-set-env", invalidAppName, envVarName, envVarValue)
   155  
   156  				Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, invalidAppName, orgName, spaceName, userName))
   157  				Eventually(session.Err).Should(Say("App '%s' not found", invalidAppName))
   158  				Eventually(session).Should(Say("FAILED"))
   159  				Eventually(session).Should(Exit(1))
   160  			})
   161  		})
   162  
   163  		When("the app exists", func() {
   164  			BeforeEach(func() {
   165  				helpers.WithHelloWorldApp(func(appDir string) {
   166  					Eventually(helpers.CF("v3-push", appName, "-p", appDir)).Should(Exit(0))
   167  				})
   168  			})
   169  
   170  			When("the environment variable has not been previously set", func() {
   171  				It("sets the environment variable value pair", func() {
   172  					session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   173  
   174  					Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   175  					Eventually(session).Should(Say("OK"))
   176  					Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   177  					Eventually(session).Should(Exit(0))
   178  
   179  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   180  					Eventually(session).Should(Say(`"%s": "%s"`, envVarName, envVarValue))
   181  					Eventually(session).Should(Exit(0))
   182  				})
   183  
   184  				// This is to prevent the '-' being read in as another flag
   185  				When("the environment variable value starts with a '-' character", func() {
   186  					BeforeEach(func() {
   187  						envVarValue = "-" + envVarValue
   188  					})
   189  
   190  					It("sets the environment variable value pair", func() {
   191  						session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   192  
   193  						Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   194  						Eventually(session).Should(Say("OK"))
   195  						Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   196  						Eventually(session).Should(Exit(0))
   197  
   198  						session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   199  						Eventually(session).Should(Say(`"%s": "%s"`, envVarName, envVarValue))
   200  						Eventually(session).Should(Exit(0))
   201  					})
   202  				})
   203  			})
   204  
   205  			When("the environment variable has been previously set", func() {
   206  				BeforeEach(func() {
   207  					Eventually(helpers.CF("v3-set-env", appName, envVarName, envVarValue)).Should(Exit(0))
   208  				})
   209  
   210  				It("overrides the value of the existing environment variable", func() {
   211  					someOtherValue := "some-other-value"
   212  					session := helpers.CF("v3-set-env", appName, envVarName, someOtherValue)
   213  
   214  					Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   215  					Eventually(session).Should(Say("OK"))
   216  					Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   217  					Eventually(session).Should(Exit(0))
   218  
   219  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   220  					Eventually(session).Should(Say(`"%s": "%s"`, envVarName, someOtherValue))
   221  					Eventually(session).Should(Exit(0))
   222  				})
   223  			})
   224  
   225  		})
   226  	})
   227  })