github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/integration/isolated/start_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"code.cloudfoundry.org/cli/integration/helpers"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gbytes"
    14  	. "github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("start command", func() {
    18  	Describe("help", func() {
    19  		Context("when --help flag is set", func() {
    20  			It("Displays command usage to output", func() {
    21  				session := helpers.CF("start", "--help")
    22  				Eventually(session).Should(Say("NAME:"))
    23  				Eventually(session).Should(Say("start - Start an app"))
    24  				Eventually(session).Should(Say("USAGE:"))
    25  				Eventually(session).Should(Say("cf start APP_NAME"))
    26  				Eventually(session).Should(Say("ALIAS:"))
    27  				Eventually(session).Should(Say("st"))
    28  				Eventually(session).Should(Say("ENVIRONMENT:"))
    29  				Eventually(session).Should(Say("CF_STAGING_TIMEOUT=15\\s+Max wait time for buildpack staging, in minutes"))
    30  				Eventually(session).Should(Say("CF_STARTUP_TIMEOUT=5\\s+Max wait time for app instance startup, in minutes"))
    31  				Eventually(session).Should(Say("SEE ALSO:"))
    32  				Eventually(session).Should(Say("apps, logs, restart, run-task, scale, ssh, stop"))
    33  				Eventually(session).Should(Exit(0))
    34  			})
    35  		})
    36  	})
    37  
    38  	Context("when the environment is not setup correctly", func() {
    39  		Context("when no API endpoint is set", func() {
    40  			BeforeEach(func() {
    41  				helpers.UnsetAPI()
    42  			})
    43  
    44  			It("fails with no API endpoint set message", func() {
    45  				session := helpers.CF("start", "wut")
    46  				Eventually(session.Out).Should(Say("FAILED"))
    47  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    48  				Eventually(session).Should(Exit(1))
    49  			})
    50  		})
    51  
    52  		Context("when not logged in", func() {
    53  			BeforeEach(func() {
    54  				helpers.LogoutCF()
    55  			})
    56  
    57  			It("fails with not logged in message", func() {
    58  				session := helpers.CF("start", "wut")
    59  				Eventually(session.Out).Should(Say("FAILED"))
    60  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    61  				Eventually(session).Should(Exit(1))
    62  			})
    63  		})
    64  
    65  		Context("when there is no org set", func() {
    66  			BeforeEach(func() {
    67  				helpers.LogoutCF()
    68  				helpers.LoginCF()
    69  			})
    70  
    71  			It("fails with no targeted org error message", func() {
    72  				session := helpers.CF("start", "wut")
    73  				Eventually(session.Out).Should(Say("FAILED"))
    74  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    75  				Eventually(session).Should(Exit(1))
    76  			})
    77  		})
    78  
    79  		Context("when there is no space set", func() {
    80  			BeforeEach(func() {
    81  				helpers.LogoutCF()
    82  				helpers.LoginCF()
    83  				helpers.TargetOrg(ReadOnlyOrg)
    84  			})
    85  
    86  			It("fails with no targeted space error message", func() {
    87  				session := helpers.CF("start", "wut")
    88  				Eventually(session.Out).Should(Say("FAILED"))
    89  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
    90  				Eventually(session).Should(Exit(1))
    91  			})
    92  		})
    93  	})
    94  
    95  	Context("when the environment is set up correctly", func() {
    96  		var (
    97  			orgName   string
    98  			spaceName string
    99  		)
   100  
   101  		BeforeEach(func() {
   102  			orgName = helpers.NewOrgName()
   103  			spaceName = helpers.NewSpaceName()
   104  
   105  			setupCF(orgName, spaceName)
   106  		})
   107  
   108  		AfterEach(func() {
   109  			helpers.QuickDeleteOrg(orgName)
   110  		})
   111  
   112  		Context("when the app does not exist", func() {
   113  			It("tells the user that the start is not found and exits 1", func() {
   114  				appName := helpers.PrefixedRandomName("app")
   115  				session := helpers.CF("start", appName)
   116  
   117  				Eventually(session.Out).Should(Say("FAILED"))
   118  				Eventually(session.Err).Should(Say("App %s not found", appName))
   119  				Eventually(session).Should(Exit(1))
   120  			})
   121  		})
   122  
   123  		Context("when the app does exist", func() {
   124  			var (
   125  				domainName string
   126  				appName    string
   127  			)
   128  
   129  			Context("when the app is started", func() {
   130  				BeforeEach(func() {
   131  					appName = helpers.PrefixedRandomName("app")
   132  					domainName = defaultSharedDomain()
   133  					helpers.WithHelloWorldApp(func(appDir string) {
   134  						Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   135  					})
   136  				})
   137  
   138  				It("only displays the app already started message", func() {
   139  					userName, _ := helpers.GetCredentials()
   140  					session := helpers.CF("start", appName)
   141  					Eventually(session).Should(Say("Starting app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   142  					Eventually(session).Should(Say("App %s is already started", appName))
   143  					Eventually(session).Should(Exit(0))
   144  				})
   145  			})
   146  
   147  			Context("when the app is stopped", func() {
   148  				Context("when the app has been staged", func() {
   149  					BeforeEach(func() {
   150  						appName = helpers.PrefixedRandomName("app")
   151  						domainName = defaultSharedDomain()
   152  						helpers.WithHelloWorldApp(func(appDir string) {
   153  							manifestContents := []byte(fmt.Sprintf(`
   154  ---
   155  applications:
   156  - name: %s
   157    memory: 128M
   158    instances: 2
   159    disk_quota: 128M
   160    routes:
   161    - route: %s.%s
   162  `, appName, appName, domainName))
   163  							manifestPath := filepath.Join(appDir, "manifest.yml")
   164  							err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   165  							Expect(err).ToNot(HaveOccurred())
   166  
   167  							Eventually(helpers.CF("push", appName, "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   168  						})
   169  						Eventually(helpers.CF("stop", appName)).Should(Exit(0))
   170  					})
   171  
   172  					It("displays the app information with instances table", func() {
   173  						userName, _ := helpers.GetCredentials()
   174  						session := helpers.CF("start", appName)
   175  						Eventually(session).Should(Say("Starting app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   176  						Eventually(session).Should(Say("Waiting for app to start..."))
   177  
   178  						Eventually(session).Should(Say("name:\\s+%s", appName))
   179  						Eventually(session).Should(Say("requested state:\\s+started"))
   180  						Eventually(session).Should(Say("instances:\\s+2/2"))
   181  						Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   182  						Eventually(session).Should(Say("routes:\\s+%s.%s", appName, domainName))
   183  						Eventually(session).Should(Say("last uploaded:"))
   184  						Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   185  						Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   186  						Eventually(session).Should(Say("start command:"))
   187  
   188  						Eventually(session).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   189  						Eventually(session).Should(Say("#0\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 128M.*of 128M"))
   190  						Eventually(session).Should(Say("#1\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 128M.*of 128M"))
   191  						Eventually(session).Should(Exit(0))
   192  					})
   193  				})
   194  
   195  				Context("when the app has *not* yet been staged", func() {
   196  					Context("when the app does *not* stage properly because the app was not detected by any buildpacks", func() {
   197  						BeforeEach(func() {
   198  							appName = helpers.PrefixedRandomName("app")
   199  							domainName = defaultSharedDomain()
   200  							helpers.WithHelloWorldApp(func(appDir string) {
   201  								err := os.Remove(filepath.Join(appDir, "Staticfile"))
   202  								Expect(err).ToNot(HaveOccurred())
   203  								Eventually(helpers.CF("push", appName, "-p", appDir, "--no-start")).Should(Exit(0))
   204  							})
   205  						})
   206  
   207  						It("fails and displays the staging failure message", func() {
   208  							userName, _ := helpers.GetCredentials()
   209  							session := helpers.CF("start", appName)
   210  							Eventually(session).Should(Say("Starting app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   211  
   212  							// The staticfile_buildback does compile an index.html file. However, it requires a "Staticfile" during buildpack detection.
   213  							Eventually(session.Err).Should(Say("Error staging application: An app was not successfully detected by any available buildpack"))
   214  							Eventually(session.Err).Should(Say(`TIP: Use 'cf buildpacks' to see a list of supported buildpacks.`))
   215  							Eventually(session).Should(Exit(1))
   216  						})
   217  					})
   218  
   219  					Context("when the app stages properly", func() {
   220  						Context("when the app does *not* start properly", func() {
   221  							BeforeEach(func() {
   222  								appName = helpers.PrefixedRandomName("app")
   223  								helpers.WithHelloWorldApp(func(appDir string) {
   224  									Eventually(helpers.CF("push", appName, "-p", appDir, "--no-start", "-b", "staticfile_buildpack", "-c", "gibberish")).Should(Exit(0))
   225  								})
   226  							})
   227  
   228  							It("fails and displays the start failure message", func() {
   229  								userName, _ := helpers.GetCredentials()
   230  								session := helpers.CF("start", appName)
   231  								Eventually(session).Should(Say("Starting app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   232  
   233  								Eventually(session.Err).Should(Say("Start unsuccessful"))
   234  								Eventually(session.Err).Should(Say("TIP: use 'cf logs .* --recent' for more information"))
   235  								Eventually(session).Should(Exit(1))
   236  							})
   237  						})
   238  
   239  						Context("when the app starts properly", func() {
   240  							BeforeEach(func() {
   241  								Eventually(helpers.CF("create-isolation-segment", RealIsolationSegment)).Should(Exit(0))
   242  								Eventually(helpers.CF("enable-org-isolation", orgName, RealIsolationSegment)).Should(Exit(0))
   243  								Eventually(helpers.CF("set-space-isolation-segment", spaceName, RealIsolationSegment)).Should(Exit(0))
   244  								appName = helpers.PrefixedRandomName("app")
   245  								domainName = defaultSharedDomain()
   246  								helpers.WithHelloWorldApp(func(appDir string) {
   247  									manifestContents := []byte(fmt.Sprintf(`
   248  ---
   249  applications:
   250  - name: %s
   251    memory: 128M
   252    instances: 2
   253    disk_quota: 128M
   254    routes:
   255    - route: %s.%s
   256  `, appName, appName, domainName))
   257  									manifestPath := filepath.Join(appDir, "manifest.yml")
   258  									err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   259  									Expect(err).ToNot(HaveOccurred())
   260  
   261  									Eventually(helpers.CF("push", appName, "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   262  								})
   263  								Eventually(helpers.CF("stop", appName)).Should(Exit(0))
   264  							})
   265  
   266  							It("displays the app logs and information with instances table", func() {
   267  								userName, _ := helpers.GetCredentials()
   268  								session := helpers.CF("start", appName)
   269  								Eventually(session).Should(Say("Starting app %s in org %s / space %s as %s...", appName, orgName, spaceName, userName))
   270  
   271  								// Display Staging Logs
   272  								Eventually(session).Should(Say("Uploading droplet..."))
   273  								Eventually(session).Should(Say("Waiting for app to start..."))
   274  
   275  								Eventually(session).Should(Say("name:\\s+%s", appName))
   276  								Eventually(session).Should(Say("requested state:\\s+started"))
   277  								Eventually(session).Should(Say("instances:\\s+2/2"))
   278  								Eventually(session).Should(Say("isolation segment:\\s+%s", RealIsolationSegment))
   279  								Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   280  								Eventually(session).Should(Say("routes:\\s+%s.%s", appName, domainName))
   281  								Eventually(session).Should(Say("last uploaded:"))
   282  								Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   283  								Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   284  								Eventually(session).Should(Say("start command:"))
   285  
   286  								Eventually(session).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   287  
   288  								Eventually(session).Should(Say("#0\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 128M.*of 128M"))
   289  								Eventually(session).Should(Say("#1\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 128M.*of 128M"))
   290  								Eventually(session).Should(Exit(0))
   291  							})
   292  						})
   293  					})
   294  				})
   295  			})
   296  		})
   297  	})
   298  })