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