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