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

     1  package experimental
     2  
     3  import (
     4  	"github.com/liamawhite/cli-with-i18n/integration/helpers"
     5  	. "github.com/onsi/ginkgo"
     6  	. "github.com/onsi/gomega"
     7  	. "github.com/onsi/gomega/gbytes"
     8  	. "github.com/onsi/gomega/gexec"
     9  	. "github.com/onsi/gomega/ghttp"
    10  )
    11  
    12  var _ = Describe("v3-env command", func() {
    13  	var (
    14  		orgName     string
    15  		spaceName   string
    16  		appName     string
    17  		envVarName  string
    18  		envVarValue string
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		orgName = helpers.NewOrgName()
    23  		spaceName = helpers.NewSpaceName()
    24  		appName = helpers.PrefixedRandomName("app")
    25  		envVarName = "SOME_ENV_VAR"
    26  		envVarValue = "SOME_ENV_VAR_VALUE"
    27  	})
    28  
    29  	Describe("help", func() {
    30  		Context("when --help flag is set", func() {
    31  			It("displays command usage to output", func() {
    32  				session := helpers.CF("v3-env", "--help")
    33  
    34  				Eventually(session.Out).Should(Say("NAME:"))
    35  				Eventually(session.Out).Should(Say("v3-env - Show all env variables for an app"))
    36  				Eventually(session.Out).Should(Say("USAGE:"))
    37  				Eventually(session.Out).Should(Say("cf v3-env APP_NAME"))
    38  				Eventually(session.Out).Should(Say("SEE ALSO:"))
    39  				Eventually(session.Out).Should(Say("running-environment-variable-group, staging-environment-variable-group, v3-app, v3-apps, v3-set-env, v3-unset-env"))
    40  				Eventually(session).Should(Exit(0))
    41  			})
    42  		})
    43  	})
    44  
    45  	Context("when the app name is not provided", func() {
    46  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    47  			session := helpers.CF("v3-env")
    48  
    49  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    50  			Eventually(session.Out).Should(Say("NAME:"))
    51  			Eventually(session).Should(Exit(1))
    52  		})
    53  	})
    54  
    55  	Context("when the environment is not setup correctly", func() {
    56  		Context("when the v3 api does not exist", func() {
    57  			var server *Server
    58  
    59  			BeforeEach(func() {
    60  				server = helpers.StartAndTargetServerWithoutV3API()
    61  			})
    62  
    63  			AfterEach(func() {
    64  				server.Close()
    65  			})
    66  
    67  			It("fails with error message that the minimum version is not met", func() {
    68  				session := helpers.CF("v3-env", appName, envVarName, envVarValue)
    69  				Eventually(session).Should(Say("FAILED"))
    70  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
    71  				Eventually(session).Should(Exit(1))
    72  			})
    73  		})
    74  
    75  		Context("when the v3 api version is lower than the minimum version", func() {
    76  			var server *Server
    77  
    78  			BeforeEach(func() {
    79  				server = helpers.StartAndTargetServerWithV3Version("3.0.0")
    80  			})
    81  
    82  			AfterEach(func() {
    83  				server.Close()
    84  			})
    85  
    86  			It("fails with error message that the minimum version is not met", func() {
    87  				session := helpers.CF("v3-env", appName, envVarName, envVarValue)
    88  				Eventually(session).Should(Say("FAILED"))
    89  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
    90  				Eventually(session).Should(Exit(1))
    91  			})
    92  		})
    93  
    94  		Context("when no API endpoint is set", func() {
    95  			BeforeEach(func() {
    96  				helpers.UnsetAPI()
    97  			})
    98  
    99  			It("fails with no API endpoint set message", func() {
   100  				session := helpers.CF("v3-env", appName, envVarName, envVarValue)
   101  				Eventually(session).Should(Say("FAILED"))
   102  				Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
   103  				Eventually(session).Should(Exit(1))
   104  			})
   105  		})
   106  
   107  		Context("when not logged in", func() {
   108  			BeforeEach(func() {
   109  				helpers.LogoutCF()
   110  			})
   111  
   112  			It("fails with not logged in message", func() {
   113  				session := helpers.CF("v3-env", appName, envVarName, envVarValue)
   114  				Eventually(session).Should(Say("FAILED"))
   115  				Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
   116  				Eventually(session).Should(Exit(1))
   117  			})
   118  		})
   119  
   120  		Context("when there is no org set", func() {
   121  			BeforeEach(func() {
   122  				helpers.LogoutCF()
   123  				helpers.LoginCF()
   124  			})
   125  
   126  			It("fails with no org targeted error message", func() {
   127  				session := helpers.CF("v3-env", appName, envVarName, envVarValue)
   128  				Eventually(session.Out).Should(Say("FAILED"))
   129  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
   130  				Eventually(session).Should(Exit(1))
   131  			})
   132  		})
   133  
   134  		Context("when there is no space set", func() {
   135  			BeforeEach(func() {
   136  				helpers.LogoutCF()
   137  				helpers.LoginCF()
   138  				helpers.TargetOrg(ReadOnlyOrg)
   139  			})
   140  
   141  			It("fails with no space targeted error message", func() {
   142  				session := helpers.CF("v3-env", appName, envVarName, envVarValue)
   143  				Eventually(session.Out).Should(Say("FAILED"))
   144  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
   145  				Eventually(session).Should(Exit(1))
   146  			})
   147  		})
   148  	})
   149  
   150  	Context("when the environment is set up correctly", func() {
   151  		var userName string
   152  
   153  		BeforeEach(func() {
   154  			setupCF(orgName, spaceName)
   155  			userName, _ = helpers.GetCredentials()
   156  		})
   157  
   158  		AfterEach(func() {
   159  			helpers.QuickDeleteOrg(orgName)
   160  		})
   161  
   162  		Context("when the app does not exist", func() {
   163  			It("displays app not found and exits 1", func() {
   164  				invalidAppName := "invalid-app-name"
   165  				session := helpers.CF("v3-env", invalidAppName)
   166  
   167  				Eventually(session.Out).Should(Say("Getting env variables for app %s in org %s / space %s as %s\\.\\.\\.", invalidAppName, orgName, spaceName, userName))
   168  				Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   169  				Eventually(session.Out).Should(Say("FAILED"))
   170  				Eventually(session).Should(Exit(1))
   171  			})
   172  		})
   173  
   174  		Context("when the app exists", func() {
   175  			var (
   176  				userProvidedServiceName string
   177  			)
   178  			BeforeEach(func() {
   179  				userProvidedServiceName = helpers.PrefixedRandomName("service")
   180  				helpers.WithHelloWorldApp(func(appDir string) {
   181  					Eventually(helpers.CF("v3-push", appName, "-p", appDir)).Should(Exit(0))
   182  				})
   183  
   184  				Eventually(helpers.CF("v3-set-env", appName, "user-provided-env-name", "user-provided-env-value")).Should(Exit(0))
   185  				Eventually(helpers.CF("create-user-provided-service", userProvidedServiceName)).Should(Exit(0))
   186  				Eventually(helpers.CF("bind-service", appName, userProvidedServiceName)).Should(Exit(0))
   187  				Eventually(helpers.CF("set-running-environment-variable-group", `{"running-env-name": "running-env-value"}`)).Should(Exit(0))
   188  				Eventually(helpers.CF("set-staging-environment-variable-group", `{"staging-env-name": "staging-env-value"}`)).Should(Exit(0))
   189  			})
   190  
   191  			AfterEach(func() {
   192  				Eventually(helpers.CF("unbind-service", appName, userProvidedServiceName)).Should(Exit(0))
   193  				Eventually(helpers.CF("delete-service", userProvidedServiceName)).Should(Exit(0))
   194  				Eventually(helpers.CF("set-running-environment-variable-group", `{}`)).Should(Exit(0))
   195  				Eventually(helpers.CF("set-staging-environment-variable-group", `{}`)).Should(Exit(0))
   196  			})
   197  
   198  			It("displays the environment variables", func() {
   199  				By("displaying env variables when they are set")
   200  				session := helpers.CF("v3-env", appName)
   201  				Eventually(session.Out).Should(Say("Getting env variables for app %s in org %s / space %s as %s\\.\\.\\.", appName, orgName, spaceName, userName))
   202  				Eventually(session.Out).Should(Say("System-Provided:"))
   203  				Eventually(session.Out).Should(Say("VCAP_SERVICES"))
   204  				Eventually(session.Out).Should(Say("VCAP_APPLICATION"))
   205  
   206  				Eventually(session.Out).Should(Say("User-Provided:"))
   207  				Eventually(session.Out).Should(Say(`user-provided-env-name: "user-provided-env-value"`))
   208  
   209  				Eventually(session.Out).Should(Say("Running Environment Variable Groups:"))
   210  				Eventually(session.Out).Should(Say(`running-env-name: "running-env-value"`))
   211  
   212  				Eventually(session.Out).Should(Say("Staging Environment Variable Groups:"))
   213  				Eventually(session.Out).Should(Say(`staging-env-name: "staging-env-value"`))
   214  				Eventually(session).Should(Exit(0))
   215  
   216  				By("displaying help messages when they are not set")
   217  				Eventually(helpers.CF("v3-unset-env", appName, "user-provided-env-name")).Should(Exit(0))
   218  				Eventually(helpers.CF("unbind-service", appName, userProvidedServiceName)).Should(Exit(0))
   219  				Eventually(helpers.CF("set-running-environment-variable-group", `{}`)).Should(Exit(0))
   220  				Eventually(helpers.CF("set-staging-environment-variable-group", `{}`)).Should(Exit(0))
   221  
   222  				session = helpers.CF("v3-env", appName)
   223  				Eventually(session.Out).Should(Say("Getting env variables for app %s in org %s / space %s as %s\\.\\.\\.", appName, orgName, spaceName, userName))
   224  				Eventually(session.Out).Should(Say("System-Provided:"))
   225  				Eventually(session.Out).ShouldNot(Say("VCAP_SERVICES"))
   226  				Eventually(session.Out).Should(Say("VCAP_APPLICATION"))
   227  
   228  				Eventually(session.Out).Should(Say("No user-provided env variables have been set"))
   229  
   230  				Eventually(session.Out).Should(Say("No running env variables have been set"))
   231  
   232  				Eventually(session.Out).Should(Say("No staging env variables have been set"))
   233  				Eventually(session).Should(Exit(0))
   234  			})
   235  		})
   236  	})
   237  })