github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/global/env_command_test.go (about)

     1  package global
     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("env command", func() {
    14  	var (
    15  		orgName   string
    16  		spaceName string
    17  		appName   string
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		orgName = helpers.NewOrgName()
    22  		spaceName = helpers.NewSpaceName()
    23  		appName = helpers.PrefixedRandomName("app")
    24  	})
    25  
    26  	Describe("help", func() {
    27  		When("--help flag is set", func() {
    28  			It("displays command usage to output", func() {
    29  				session := helpers.CF("env", "--help")
    30  
    31  				Eventually(session).Should(Say("NAME:"))
    32  				Eventually(session).Should(Say("env - Show all env variables for an app"))
    33  				Eventually(session).Should(Say("USAGE:"))
    34  				Eventually(session).Should(Say("cf env APP_NAME"))
    35  				Eventually(session).Should(Say("SEE ALSO:"))
    36  				Eventually(session).Should(Say("app, apps, running-environment-variable-group, set-env, staging-environment-variable-group, unset-env"))
    37  				Eventually(session).Should(Exit(0))
    38  			})
    39  		})
    40  	})
    41  
    42  	When("the app name is not provided", func() {
    43  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    44  			session := helpers.CF("env")
    45  
    46  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    47  			Eventually(session).Should(Say("NAME:"))
    48  			Eventually(session).Should(Exit(1))
    49  		})
    50  	})
    51  
    52  	When("the environment is not setup correctly", func() {
    53  		It("fails with the appropriate errors", func() {
    54  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "env", appName)
    55  		})
    56  	})
    57  
    58  	When("the environment is set up correctly", func() {
    59  		var userName string
    60  
    61  		BeforeEach(func() {
    62  			helpers.SetupCF(orgName, spaceName)
    63  			userName, _ = helpers.GetCredentials()
    64  		})
    65  
    66  		AfterEach(func() {
    67  			helpers.QuickDeleteOrg(orgName)
    68  		})
    69  
    70  		When("the app does not exist", func() {
    71  			It("displays app not found and exits 1", func() {
    72  				invalidAppName := "invalid-app-name"
    73  				session := helpers.CF("env", invalidAppName)
    74  
    75  				Eventually(session).Should(Say(`Getting env variables for app %s in org %s / space %s as %s\.\.\.`, invalidAppName, orgName, spaceName, userName))
    76  
    77  				Eventually(session.Err).Should(Say("App '%s' not found", invalidAppName))
    78  				Eventually(session).Should(Say("FAILED"))
    79  				Eventually(session).Should(Exit(1))
    80  			})
    81  		})
    82  
    83  		When("the app exists", func() {
    84  			var (
    85  				userProvidedServiceName string
    86  			)
    87  			BeforeEach(func() {
    88  				userProvidedServiceName = helpers.PrefixedRandomName("service")
    89  				helpers.WithHelloWorldApp(func(appDir string) {
    90  					Eventually(helpers.CF("push", appName, "-p", appDir)).Should(Exit(0))
    91  				})
    92  
    93  				Eventually(helpers.CF("set-env", appName, "user-provided-env-name", "user-provided-env-value")).Should(Exit(0))
    94  				Eventually(helpers.CF("set-env", appName, "an-out-of-order-name", "some-env-value")).Should(Exit(0))
    95  				Eventually(helpers.CF("set-env", appName, "Capital-env-var", "some-other-env-value")).Should(Exit(0))
    96  				Eventually(helpers.CF("create-user-provided-service", userProvidedServiceName)).Should(Exit(0))
    97  				Eventually(helpers.CF("bind-service", appName, userProvidedServiceName)).Should(Exit(0))
    98  				Eventually(helpers.CF("set-running-environment-variable-group", `{"running-env-name": "running-env-value", "Xstring": "X"}`)).Should(Exit(0))
    99  				Eventually(helpers.CF("set-staging-environment-variable-group", `{"staging-env-name": "staging-env-value", "Ystring": "Y"}`)).Should(Exit(0))
   100  			})
   101  
   102  			AfterEach(func() {
   103  				Eventually(helpers.CF("unbind-service", appName, userProvidedServiceName)).Should(Exit(0))
   104  				Eventually(helpers.CF("delete-service", userProvidedServiceName)).Should(Exit(0))
   105  				Eventually(helpers.CF("set-running-environment-variable-group", `{}`)).Should(Exit(0))
   106  				Eventually(helpers.CF("set-staging-environment-variable-group", `{}`)).Should(Exit(0))
   107  			})
   108  
   109  			It("displays the environment variables", func() {
   110  				By("displaying env variables when they are set")
   111  				session := helpers.CF("env", appName)
   112  
   113  				Eventually(session).Should(Say(fmt.Sprintf(`Getting env variables for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName)))
   114  
   115  				Eventually(session).Should(Say("System-Provided:"))
   116  				Eventually(session).Should(Say(`VCAP_SERVICES: {\s*\n`))
   117  				Eventually(session).Should(Say("VCAP_APPLICATION"))
   118  
   119  				Eventually(session).Should(Say("User-Provided:"))
   120  				Eventually(session).Should(Say(`Capital-env-var:\s+some-other-env-value`))
   121  				Eventually(session).Should(Say(`an-out-of-order-name:\s+some-env-value`))
   122  				Eventually(session).Should(Say(`user-provided-env-name:\s+user-provided-env-value`))
   123  
   124  				Eventually(session).Should(Say("Running Environment Variable Groups:"))
   125  				Eventually(session).Should(Say(`Xstring:\s+X`))
   126  				Eventually(session).Should(Say(`running-env-name:\s+running-env-value`))
   127  
   128  				Eventually(session).Should(Say("Staging Environment Variable Groups:"))
   129  				Eventually(session).Should(Say(`Ystring:\s+Y`))
   130  				Eventually(session).Should(Say(`staging-env-name:\s+staging-env-value`))
   131  
   132  				Eventually(session).Should(Exit(0))
   133  
   134  				By("displaying help messages when they are not set")
   135  				Eventually(helpers.CF("unset-env", appName, "user-provided-env-name")).Should(Exit(0))
   136  				Eventually(helpers.CF("unset-env", appName, "an-out-of-order-name")).Should(Exit(0))
   137  				Eventually(helpers.CF("unset-env", appName, "Capital-env-var")).Should(Exit(0))
   138  				Eventually(helpers.CF("unbind-service", appName, userProvidedServiceName)).Should(Exit(0))
   139  				Eventually(helpers.CF("set-running-environment-variable-group", `{}`)).Should(Exit(0))
   140  				Eventually(helpers.CF("set-staging-environment-variable-group", `{}`)).Should(Exit(0))
   141  
   142  				session = helpers.CF("env", appName)
   143  				Eventually(session).Should(Say(`Getting env variables for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   144  
   145  				Eventually(session).Should(Say("System-Provided:"))
   146  				Eventually(session).Should(Say("VCAP_SERVICES: {}"))
   147  				Eventually(session).Should(Say("VCAP_APPLICATION"))
   148  
   149  				Eventually(session).Should(Say("No user-provided env variables have been set"))
   150  
   151  				Eventually(session).Should(Say("No running env variables have been set"))
   152  
   153  				Eventually(session).Should(Say("No staging env variables have been set"))
   154  				Eventually(session).Should(Exit(0))
   155  			})
   156  		})
   157  	})
   158  })