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