github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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/api/cloudcontroller/ccversion"
     7  	"code.cloudfoundry.org/cli/integration/helpers"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  	. "github.com/onsi/gomega/ghttp"
    13  )
    14  
    15  var _ = Describe("v3-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("displays command usage to output", func() {
    35  				session := helpers.CF("v3-set-env", "--help")
    36  
    37  				Eventually(session).Should(Say("NAME:"))
    38  				Eventually(session).Should(Say("v3-set-env - Set an env variable for an app"))
    39  				Eventually(session).Should(Say("USAGE:"))
    40  				Eventually(session).Should(Say("cf v3-set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE"))
    41  				Eventually(session).Should(Say("SEE ALSO:"))
    42  				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"))
    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("v3-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("v3-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("v3-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  	It("displays the experimental warning", func() {
    79  		session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
    80  		Eventually(session.Err).Should(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    81  		Eventually(session).Should(Exit())
    82  	})
    83  
    84  	When("the environment is not setup correctly", func() {
    85  		When("the v3 api version is lower than the minimum version", func() {
    86  			var server *Server
    87  
    88  			BeforeEach(func() {
    89  				server = helpers.StartAndTargetServerWithAPIVersions(helpers.DefaultV2Version, ccversion.MinV3ClientVersion)
    90  			})
    91  
    92  			AfterEach(func() {
    93  				server.Close()
    94  			})
    95  
    96  			It("fails with error message that the minimum version is not met", func() {
    97  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
    98  				Eventually(session).Should(Say("FAILED"))
    99  				Eventually(session.Err).Should(Say(`This command requires CF API version 3\.27\.0 or higher\.`))
   100  				Eventually(session).Should(Exit(1))
   101  			})
   102  		})
   103  
   104  		When("no API endpoint is set", func() {
   105  			BeforeEach(func() {
   106  				helpers.UnsetAPI()
   107  			})
   108  
   109  			It("fails with no API endpoint set message", func() {
   110  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   111  				Eventually(session).Should(Say("FAILED"))
   112  				Eventually(session.Err).Should(Say(`No API endpoint set\. Use 'cf login' or 'cf api' to target an endpoint\.`))
   113  				Eventually(session).Should(Exit(1))
   114  			})
   115  		})
   116  
   117  		When("not logged in", func() {
   118  			BeforeEach(func() {
   119  				helpers.LogoutCF()
   120  			})
   121  
   122  			It("fails with not logged in message", func() {
   123  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   124  				Eventually(session).Should(Say("FAILED"))
   125  				Eventually(session.Err).Should(Say(`Not logged in\. Use 'cf login' to log in\.`))
   126  				Eventually(session).Should(Exit(1))
   127  			})
   128  		})
   129  
   130  		When("there is no org set", func() {
   131  			BeforeEach(func() {
   132  				helpers.LogoutCF()
   133  				helpers.LoginCF()
   134  			})
   135  
   136  			It("fails with no org targeted error message", func() {
   137  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   138  				Eventually(session).Should(Say("FAILED"))
   139  				Eventually(session.Err).Should(Say(`No org targeted, use 'cf target -o ORG' to target an org\.`))
   140  				Eventually(session).Should(Exit(1))
   141  			})
   142  		})
   143  
   144  		When("there is no space set", func() {
   145  			BeforeEach(func() {
   146  				helpers.LogoutCF()
   147  				helpers.LoginCF()
   148  				helpers.TargetOrg(ReadOnlyOrg)
   149  			})
   150  
   151  			It("fails with no space targeted error message", func() {
   152  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   153  				Eventually(session).Should(Say("FAILED"))
   154  				Eventually(session.Err).Should(Say(`No space targeted, use 'cf target -s SPACE' to target a space\.`))
   155  				Eventually(session).Should(Exit(1))
   156  			})
   157  		})
   158  	})
   159  
   160  	When("the environment is set up correctly", func() {
   161  		var userName string
   162  
   163  		BeforeEach(func() {
   164  			helpers.SetupCF(orgName, spaceName)
   165  			userName, _ = helpers.GetCredentials()
   166  		})
   167  
   168  		AfterEach(func() {
   169  			helpers.QuickDeleteOrg(orgName)
   170  		})
   171  
   172  		When("the app does not exist", func() {
   173  			It("displays app not found and exits 1", func() {
   174  				invalidAppName := "invalid-app-name"
   175  				session := helpers.CF("v3-set-env", invalidAppName, envVarName, envVarValue)
   176  
   177  				Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, invalidAppName, orgName, spaceName, userName))
   178  				Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   179  				Eventually(session).Should(Say("FAILED"))
   180  				Eventually(session).Should(Exit(1))
   181  			})
   182  		})
   183  
   184  		When("the app exists", func() {
   185  			BeforeEach(func() {
   186  				helpers.WithHelloWorldApp(func(appDir string) {
   187  					Eventually(helpers.CF("v3-push", appName, "-p", appDir)).Should(Exit(0))
   188  				})
   189  			})
   190  
   191  			When("the environment variable has not been previously set", func() {
   192  				It("sets the environment variable value pair", func() {
   193  					session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   194  
   195  					Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   196  					Eventually(session).Should(Say("OK"))
   197  					Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   198  					Eventually(session).Should(Exit(0))
   199  
   200  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   201  					Eventually(session).Should(Say(`"%s": "%s"`, envVarName, envVarValue))
   202  					Eventually(session).Should(Exit(0))
   203  				})
   204  
   205  				// This is to prevent the '-' being read in as another flag
   206  				When("the environment variable value starts with a '-' character", func() {
   207  					BeforeEach(func() {
   208  						envVarValue = "-" + envVarValue
   209  					})
   210  
   211  					It("sets the environment variable value pair", func() {
   212  						session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   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, envVarValue))
   221  						Eventually(session).Should(Exit(0))
   222  					})
   223  				})
   224  			})
   225  
   226  			When("the environment variable has been previously set", func() {
   227  				BeforeEach(func() {
   228  					Eventually(helpers.CF("v3-set-env", appName, envVarName, envVarValue)).Should(Exit(0))
   229  				})
   230  
   231  				It("overrides the value of the existing environment variable", func() {
   232  					someOtherValue := "some-other-value"
   233  					session := helpers.CF("v3-set-env", appName, envVarName, someOtherValue)
   234  
   235  					Eventually(session).Should(Say(`Setting env variable %s for app %s in org %s / space %s as %s\.\.\.`, envVarName, appName, orgName, spaceName, userName))
   236  					Eventually(session).Should(Say("OK"))
   237  					Eventually(session).Should(Say(`TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\.`, appName))
   238  					Eventually(session).Should(Exit(0))
   239  
   240  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   241  					Eventually(session).Should(Say(`"%s": "%s"`, envVarName, someOtherValue))
   242  					Eventually(session).Should(Exit(0))
   243  				})
   244  			})
   245  
   246  		})
   247  	})
   248  })