github.com/sleungcy/cli@v7.1.0+incompatible/integration/v7/isolated/start_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  
    10  	"regexp"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	. "github.com/onsi/gomega/gbytes"
    15  	. "github.com/onsi/gomega/gexec"
    16  )
    17  
    18  const (
    19  	PushCommandName = "push"
    20  )
    21  
    22  var _ = Describe("start command", func() {
    23  	var (
    24  		orgName   string
    25  		spaceName string
    26  		appName   string
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		orgName = helpers.NewOrgName()
    31  		spaceName = helpers.NewSpaceName()
    32  		appName = helpers.PrefixedRandomName("app")
    33  	})
    34  
    35  	Describe("help", func() {
    36  		When("--help flag is set", func() {
    37  			It("appears in cf help -a", func() {
    38  				session := helpers.CF("help", "-a")
    39  				Eventually(session).Should(Exit(0))
    40  				Expect(session).To(HaveCommandInCategoryWithDescription("start", "APPS", "Start an app"))
    41  			})
    42  
    43  			It("Displays command usage to output", func() {
    44  				session := helpers.CF("start", "--help")
    45  
    46  				Eventually(session).Should(Say("NAME:"))
    47  				Eventually(session).Should(Say("start - Start an app"))
    48  				Eventually(session).Should(Say("USAGE:"))
    49  				Eventually(session).Should(Say("cf start APP_NAME"))
    50  				Eventually(session).Should(Say("If the app's most recent package is unstaged, starting the app will stage and run that package."))
    51  				Eventually(session).Should(Say("Otherwise, the app's current droplet will be run."))
    52  				Eventually(session).Should(Say("ALIAS:"))
    53  				Eventually(session).Should(Say("st"))
    54  				Eventually(session).Should(Say("ENVIRONMENT:"))
    55  				Eventually(session).Should(Say(`CF_STAGING_TIMEOUT=15\s+Max wait time for staging, in minutes`))
    56  				Eventually(session).Should(Say(`CF_STARTUP_TIMEOUT=5\s+Max wait time for app instance startup, in minutes`))
    57  				Eventually(session).Should(Say("SEE ALSO:"))
    58  				Eventually(session).Should(Say("apps, logs, restart, run-task, scale, ssh, stop"))
    59  
    60  				Eventually(session).Should(Exit(0))
    61  			})
    62  		})
    63  	})
    64  
    65  	When("the app name is not provided", func() {
    66  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    67  			session := helpers.CF("start")
    68  
    69  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    70  			Eventually(session).Should(Say("NAME:"))
    71  			Eventually(session).Should(Exit(1))
    72  		})
    73  	})
    74  
    75  	When("the environment is not setup correctly", func() {
    76  		It("fails with the appropriate errors", func() {
    77  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "start", appName)
    78  		})
    79  	})
    80  
    81  	When("the environment is set up correctly", func() {
    82  		BeforeEach(func() {
    83  			helpers.SetupCF(orgName, spaceName)
    84  			Eventually(helpers.CF("create-app", appName)).Should(Exit(0))
    85  		})
    86  
    87  		AfterEach(func() {
    88  			helpers.QuickDeleteOrg(orgName)
    89  		})
    90  
    91  		When("the app exists", func() {
    92  			When("the app does not need to be staged", func() {
    93  				BeforeEach(func() {
    94  					var packageGUID string
    95  
    96  					mapRouteSession := helpers.CF("map-route", appName, helpers.DefaultSharedDomain(), "-n", appName)
    97  					Eventually(mapRouteSession).Should(Exit(0))
    98  
    99  					helpers.WithHelloWorldApp(func(dir string) {
   100  						pkgSession := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, "create-package", appName)
   101  						Eventually(pkgSession).Should(Exit(0))
   102  						regex := regexp.MustCompile(`Package with guid '(.+)' has been created.`)
   103  						matches := regex.FindStringSubmatch(string(pkgSession.Out.Contents()))
   104  						Expect(matches).To(HaveLen(2))
   105  
   106  						packageGUID = matches[1]
   107  					})
   108  
   109  					stageSession := helpers.CF("stage-package", appName, "--package-guid", packageGUID)
   110  					Eventually(stageSession).Should(Exit(0))
   111  
   112  					regex := regexp.MustCompile(`droplet guid:\s+(.+)`)
   113  					matches := regex.FindStringSubmatch(string(stageSession.Out.Contents()))
   114  					Expect(matches).To(HaveLen(2))
   115  
   116  					dropletGUID := matches[1]
   117  					setDropletSession := helpers.CF("set-droplet", appName, dropletGUID)
   118  					Eventually(setDropletSession).Should(Exit(0))
   119  				})
   120  
   121  				It("starts the app", func() {
   122  					userName, _ := helpers.GetCredentials()
   123  
   124  					session := helpers.CF("start", appName)
   125  					Eventually(session).Should(Say(`Starting app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   126  					Eventually(session).Should(Say(`Waiting for app to start\.\.\.`))
   127  					Eventually(session).Should(Say(`name:\s+%s`, appName))
   128  					Eventually(session).Should(Say(`requested state:\s+started`))
   129  					Eventually(session).Should(Say(`routes:\s+%s.%s`, appName, helpers.DefaultSharedDomain()))
   130  					Eventually(session).Should(Say(`type:\s+web`))
   131  					Eventually(session).Should(Say(`instances:\s+1/1`))
   132  					Eventually(session).Should(Say(`memory usage:\s+32M`))
   133  					Eventually(session).Should(Say(`\s+state\s+since\s+cpu\s+memory\s+disk\s+details`))
   134  					Eventually(session).Should(Say(`#0\s+(starting|running)\s+\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z`))
   135  
   136  					Eventually(session).Should(Exit(0))
   137  				})
   138  
   139  				When("the app is already started", func() {
   140  					BeforeEach(func() {
   141  						Eventually(helpers.CF("start", appName)).Should(Exit(0))
   142  					})
   143  
   144  					It("displays app already started and exits 0", func() {
   145  						session := helpers.CF("start", appName)
   146  
   147  						Eventually(session).Should(Say(`App '%s' is already started\.`, appName))
   148  						Eventually(session).Should(Say("OK"))
   149  
   150  						Eventually(session).Should(Exit(0))
   151  					})
   152  				})
   153  			})
   154  
   155  			When("the app needs to be staged", func() {
   156  				var packageGUID = ""
   157  				BeforeEach(func() {
   158  					helpers.WithHelloWorldApp(func(dir string) {
   159  						session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName)
   160  						Eventually(session).Should(Say(`\s+name:\s+%s`, appName))
   161  						Eventually(session).Should(Say(`requested state:\s+started`))
   162  						Eventually(session).Should(Exit(0))
   163  					})
   164  
   165  					session := helpers.CF("stop", appName)
   166  					Eventually(session).Should(Say("OK"))
   167  
   168  					helpers.WithBananaPantsApp(func(dir string) {
   169  						pkgSession := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, "create-package", appName)
   170  						Eventually(pkgSession).Should(Exit(0))
   171  						regex := regexp.MustCompile(`Package with guid '(.+)' has been created.`)
   172  						matches := regex.FindStringSubmatch(string(pkgSession.Out.Contents()))
   173  						Expect(matches).To(HaveLen(2))
   174  
   175  						packageGUID = matches[1]
   176  					})
   177  				})
   178  
   179  				It("stages and starts the app", func() {
   180  					session := helpers.CF("start", appName)
   181  
   182  					Eventually(session).Should(Say(`Staging app and tracing logs`))
   183  					helpers.ConfirmStagingLogs(session)
   184  
   185  					Eventually(session).Should(Say(`Waiting for app to start\.\.\.`))
   186  					Eventually(session).Should(Say(`name:\s+%s`, appName))
   187  					Eventually(session).Should(Say(`requested state:\s+started`))
   188  					Eventually(session).Should(Say(`type:\s+web`))
   189  					Eventually(session).Should(Say(`instances:\s+1/1`))
   190  					Eventually(session).Should(Say(`memory usage:\s+32M`))
   191  					Eventually(session).Should(Say(`\s+state\s+since\s+cpu\s+memory\s+disk\s+details`))
   192  					Eventually(session).Should(Say(`#0\s+(starting|running)\s+\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z`))
   193  
   194  					Eventually(session).Should(Exit(0))
   195  					Expect(session.Err).ToNot(Say(`timeout connecting to log server, no log will be shown`))
   196  
   197  					Expect(helpers.GetPackageFirstDroplet(packageGUID)).To(Equal(helpers.GetAppDroplet(helpers.AppGUID(appName))))
   198  				})
   199  			})
   200  			When("the package does not exist", func() {
   201  				var appGUID string
   202  				var postBody string
   203  				BeforeEach(func() {
   204  					session := helpers.CF("app", appName, "--guid")
   205  					Eventually(session).Should(Exit(0))
   206  					appGUID = strings.TrimSuffix(string(session.Out.Contents()), "\n")
   207  
   208  					postBody = fmt.Sprintf(`{
   209  						"type": "bits",
   210  						"relationships": {
   211  							"app": {
   212  								"data": {
   213  									"guid": "%s"
   214  								}
   215  							}
   216  						}
   217  					}`, appGUID)
   218  					session = helpers.CF("curl", "-X", "POST", "/v3/packages/", "-d", postBody)
   219  					Eventually(session).Should(Exit(0))
   220  
   221  				})
   222  
   223  				It("gives a luxurious error message", func() {
   224  					session := helpers.CF("start", appName)
   225  					Eventually(session.Err).Should(Say(`Cannot stage package unless its state is 'READY'.`))
   226  					Eventually(session).Should(Say("FAILED"))
   227  
   228  					Eventually(session).Should(Exit(1))
   229  				})
   230  			})
   231  
   232  			When("the app cannot be started or staged", func() {
   233  				It("gives an error", func() {
   234  					session := helpers.CF("start", appName)
   235  
   236  					Eventually(session.Err).Should(Say(`App cannot start without a package to stage or a droplet to run.`))
   237  					Eventually(session).Should(Say("FAILED"))
   238  
   239  					Eventually(session).Should(Exit(1))
   240  				})
   241  			})
   242  		})
   243  
   244  		When("the app does not exist", func() {
   245  			It("displays app not found and exits 1", func() {
   246  				invalidAppName := "invalid-app-name"
   247  				session := helpers.CF("start", invalidAppName)
   248  
   249  				Eventually(session.Err).Should(Say(`App '%s' not found\.`, invalidAppName))
   250  				Eventually(session).Should(Say("FAILED"))
   251  
   252  				Eventually(session).Should(Exit(1))
   253  			})
   254  		})
   255  	})
   256  })