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

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