github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/integration/experimental/v3_set_env_command_test.go (about)

     1  package experimental
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/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.Out).Should(Say("NAME:"))
    37  				Eventually(session.Out).Should(Say("v3-set-env - Set an env variable for an app"))
    38  				Eventually(session.Out).Should(Say("USAGE:"))
    39  				Eventually(session.Out).Should(Say("cf v3-set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE"))
    40  				Eventually(session.Out).Should(Say("SEE ALSO:"))
    41  				Eventually(session.Out).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.Out).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.Out).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.Out).Should(Say("NAME:"))
    73  			Eventually(session).Should(Exit(1))
    74  		})
    75  	})
    76  
    77  	Context("when the environment is not setup correctly", func() {
    78  		Context("when the v3 api does not exist", func() {
    79  			var server *Server
    80  
    81  			BeforeEach(func() {
    82  				server = helpers.StartAndTargetServerWithoutV3API()
    83  			})
    84  
    85  			AfterEach(func() {
    86  				server.Close()
    87  			})
    88  
    89  			It("fails with error message that the minimum version is not met", func() {
    90  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
    91  				Eventually(session).Should(Say("FAILED"))
    92  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
    93  				Eventually(session).Should(Exit(1))
    94  			})
    95  		})
    96  
    97  		Context("when the v3 api version is lower than the minimum version", func() {
    98  			var server *Server
    99  
   100  			BeforeEach(func() {
   101  				server = helpers.StartAndTargetServerWithV3Version("3.0.0")
   102  			})
   103  
   104  			AfterEach(func() {
   105  				server.Close()
   106  			})
   107  
   108  			It("fails with error message that the minimum version is not met", func() {
   109  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   110  				Eventually(session).Should(Say("FAILED"))
   111  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
   112  				Eventually(session).Should(Exit(1))
   113  			})
   114  		})
   115  
   116  		Context("when no API endpoint is set", func() {
   117  			BeforeEach(func() {
   118  				helpers.UnsetAPI()
   119  			})
   120  
   121  			It("fails with no API endpoint set message", func() {
   122  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   123  				Eventually(session).Should(Say("FAILED"))
   124  				Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
   125  				Eventually(session).Should(Exit(1))
   126  			})
   127  		})
   128  
   129  		Context("when not logged in", func() {
   130  			BeforeEach(func() {
   131  				helpers.LogoutCF()
   132  			})
   133  
   134  			It("fails with not logged in message", func() {
   135  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   136  				Eventually(session).Should(Say("FAILED"))
   137  				Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
   138  				Eventually(session).Should(Exit(1))
   139  			})
   140  		})
   141  
   142  		Context("when there is no org set", func() {
   143  			BeforeEach(func() {
   144  				helpers.LogoutCF()
   145  				helpers.LoginCF()
   146  			})
   147  
   148  			It("fails with no org targeted error message", func() {
   149  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   150  				Eventually(session.Out).Should(Say("FAILED"))
   151  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
   152  				Eventually(session).Should(Exit(1))
   153  			})
   154  		})
   155  
   156  		Context("when there is no space set", func() {
   157  			BeforeEach(func() {
   158  				helpers.LogoutCF()
   159  				helpers.LoginCF()
   160  				helpers.TargetOrg(ReadOnlyOrg)
   161  			})
   162  
   163  			It("fails with no space targeted error message", func() {
   164  				session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   165  				Eventually(session.Out).Should(Say("FAILED"))
   166  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
   167  				Eventually(session).Should(Exit(1))
   168  			})
   169  		})
   170  	})
   171  
   172  	Context("when the environment is set up correctly", func() {
   173  		var userName string
   174  
   175  		BeforeEach(func() {
   176  			setupCF(orgName, spaceName)
   177  			userName, _ = helpers.GetCredentials()
   178  		})
   179  
   180  		AfterEach(func() {
   181  			helpers.QuickDeleteOrg(orgName)
   182  		})
   183  
   184  		Context("when the app does not exist", func() {
   185  			It("displays app not found and exits 1", func() {
   186  				invalidAppName := "invalid-app-name"
   187  				session := helpers.CF("v3-set-env", invalidAppName, envVarName, envVarValue)
   188  
   189  				Eventually(session.Out).Should(Say("Setting env variable %s for app %s in org %s / space %s as %s\\.\\.\\.", envVarName, invalidAppName, orgName, spaceName, userName))
   190  				Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   191  				Eventually(session.Out).Should(Say("FAILED"))
   192  				Eventually(session).Should(Exit(1))
   193  			})
   194  		})
   195  
   196  		Context("when the app exists", func() {
   197  			BeforeEach(func() {
   198  				helpers.WithHelloWorldApp(func(appDir string) {
   199  					Eventually(helpers.CF("v3-push", appName, "-p", appDir)).Should(Exit(0))
   200  				})
   201  			})
   202  
   203  			Context("when the environment variable has not been previously set", func() {
   204  				It("sets the environment variable value pair", func() {
   205  					session := helpers.CF("v3-set-env", appName, envVarName, envVarValue)
   206  
   207  					Eventually(session.Out).Should(Say("Setting env variable %s for app %s in org %s / space %s as %s\\.\\.\\.", envVarName, appName, orgName, spaceName, userName))
   208  					Eventually(session.Out).Should(Say("OK"))
   209  					Eventually(session.Out).Should(Say("TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\\.", appName))
   210  					Eventually(session).Should(Exit(0))
   211  
   212  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   213  					Eventually(session.Out).Should(Say(`"%s": "%s"`, envVarName, envVarValue))
   214  					Eventually(session).Should(Exit(0))
   215  				})
   216  			})
   217  
   218  			Context("when the environment variable has been previously set", func() {
   219  				BeforeEach(func() {
   220  					Eventually(helpers.CF("v3-set-env", appName, envVarName, envVarValue)).Should(Exit(0))
   221  				})
   222  
   223  				It("overrides the value of the existing environment variable", func() {
   224  					someOtherValue := "some-other-value"
   225  					session := helpers.CF("v3-set-env", appName, envVarName, someOtherValue)
   226  
   227  					Eventually(session.Out).Should(Say("Setting env variable %s for app %s in org %s / space %s as %s\\.\\.\\.", envVarName, appName, orgName, spaceName, userName))
   228  					Eventually(session.Out).Should(Say("OK"))
   229  					Eventually(session.Out).Should(Say("TIP: Use 'cf v3-stage %s' to ensure your env variable changes take effect\\.", appName))
   230  					Eventually(session).Should(Exit(0))
   231  
   232  					session = helpers.CF("curl", fmt.Sprintf("v3/apps/%s/environment_variables", helpers.AppGUID(appName)))
   233  					Eventually(session.Out).Should(Say(`"%s": "%s"`, envVarName, someOtherValue))
   234  					Eventually(session).Should(Exit(0))
   235  				})
   236  			})
   237  		})
   238  	})
   239  })