github.com/wanddynosios/cli@v7.1.0+incompatible/integration/v6/experimental/v3_env_command_test.go (about)

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