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