github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/isolated/restage_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("restage 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("restage", "--help")
    22  
    23  				Eventually(session).Should(Say("NAME:"))
    24  				Eventually(session).Should(Say("restage - Recreate the app's executable artifact using the latest pushed app files and the latest environment \\(variables, service bindings, buildpack, stack, etc\\.\\)"))
    25  				Eventually(session).Should(Say("USAGE:"))
    26  				Eventually(session).Should(Say("cf restage APP_NAME"))
    27  				Eventually(session).Should(Say("ALIAS:"))
    28  				Eventually(session).Should(Say("rg"))
    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("restart"))
    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, "restage", "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  			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("restage", 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 does *not* stage properly because the app was not detected by any buildpacks", func() {
    80  				BeforeEach(func() {
    81  					appName = helpers.PrefixedRandomName("app")
    82  					domainName = defaultSharedDomain()
    83  					helpers.WithHelloWorldApp(func(appDir string) {
    84  						err := os.Remove(filepath.Join(appDir, "Staticfile"))
    85  						Expect(err).ToNot(HaveOccurred())
    86  						Eventually(helpers.CF("push", appName, "-p", appDir)).Should(Exit(1))
    87  					})
    88  				})
    89  
    90  				It("fails and displays the staging failure message", func() {
    91  					userName, _ := helpers.GetCredentials()
    92  					session := helpers.CF("restage", appName)
    93  					Eventually(session).Should(Say("Restaging app %s in org %s / space %s as %s\\.\\.\\.", appName, orgName, spaceName, userName))
    94  
    95  					// The staticfile_buildback does compile an index.html file. However, it requires a "Staticfile" during buildpack detection.
    96  					Eventually(session.Err).Should(Say("Error staging application: An app was not successfully detected by any available buildpack"))
    97  					Eventually(session.Err).Should(Say(`TIP: Use 'cf buildpacks' to see a list of supported buildpacks.`))
    98  					Eventually(session).Should(Exit(1))
    99  				})
   100  			})
   101  
   102  			Context("when the app does *not* start properly", func() {
   103  				BeforeEach(func() {
   104  					appName = helpers.PrefixedRandomName("app")
   105  					helpers.WithHelloWorldApp(func(appDir string) {
   106  						Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "-c", "gibberish")).Should(Exit(1))
   107  					})
   108  				})
   109  
   110  				It("fails and displays the start failure message", func() {
   111  					userName, _ := helpers.GetCredentials()
   112  					session := helpers.CF("restage", appName)
   113  					Eventually(session).Should(Say("Restaging app %s in org %s / space %s as %s\\.\\.\\.", appName, orgName, spaceName, userName))
   114  
   115  					Eventually(session.Err).Should(Say("Start unsuccessful"))
   116  					Eventually(session.Err).Should(Say("TIP: use 'cf logs .* --recent' for more information"))
   117  					Eventually(session).Should(Exit(1))
   118  				})
   119  			})
   120  
   121  			Context("when the app stages and starts properly", func() {
   122  				BeforeEach(func() {
   123  					Eventually(helpers.CF("create-isolation-segment", RealIsolationSegment)).Should(Exit(0))
   124  					Eventually(helpers.CF("enable-org-isolation", orgName, RealIsolationSegment)).Should(Exit(0))
   125  					Eventually(helpers.CF("set-space-isolation-segment", spaceName, RealIsolationSegment)).Should(Exit(0))
   126  					appName = helpers.PrefixedRandomName("app")
   127  					domainName = defaultSharedDomain()
   128  					helpers.WithHelloWorldApp(func(appDir string) {
   129  						manifestContents := []byte(fmt.Sprintf(`
   130  ---
   131  applications:
   132  - name: %s
   133    memory: 128M
   134    instances: 2
   135    disk_quota: 128M
   136    routes:
   137    - route: %s.%s
   138  `, appName, appName, domainName))
   139  						manifestPath := filepath.Join(appDir, "manifest.yml")
   140  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   141  						Expect(err).ToNot(HaveOccurred())
   142  
   143  						Eventually(helpers.CF("push", appName, "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   144  					})
   145  
   146  				})
   147  
   148  				It("displays the staging and app logs and information with instances table", func() {
   149  					userName, _ := helpers.GetCredentials()
   150  					session := helpers.CF("restage", appName, "-v")
   151  					Eventually(session).Should(Say("Restaging app %s in org %s / space %s as %s\\.\\.\\.", appName, orgName, spaceName, userName))
   152  
   153  					// Display Staging Logs
   154  					Eventually(session).Should(Say("Staging app and tracing logs\\.\\.\\."))
   155  					Eventually(session).Should(Say("Uploading droplet\\.\\.\\."))
   156  					Eventually(session).Should(Say("Waiting for app to start\\.\\.\\."))
   157  
   158  					Eventually(session).Should(Say("name:\\s+%s", appName))
   159  					Eventually(session).Should(Say("requested state:\\s+started"))
   160  					Eventually(session).Should(Say("instances:\\s+2/2"))
   161  					Eventually(session).Should(Say("isolation segment:\\s+%s", RealIsolationSegment))
   162  					Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   163  					Eventually(session).Should(Say("routes:\\s+%s.%s", appName, domainName))
   164  					Eventually(session).Should(Say("last uploaded:"))
   165  					Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   166  					Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   167  					Eventually(session).Should(Say("start command:"))
   168  
   169  					Eventually(session).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   170  
   171  					Eventually(session).Should(Say("#0\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 128M.*of 128M"))
   172  					Eventually(session).Should(Say("#1\\s+(running|starting)\\s+.*\\d+\\.\\d+%.*of 128M.*of 128M"))
   173  					Eventually(session).Should(Exit(0))
   174  				})
   175  			})
   176  		})
   177  	})
   178  })