github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/integration/isolated/app_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"code.cloudfoundry.org/cli/integration/helpers"
    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("app 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("app", "--help")
    22  				Eventually(session).Should(Say("NAME:"))
    23  				Eventually(session).Should(Say("app - Display health and status for an app"))
    24  				Eventually(session).Should(Say("USAGE:"))
    25  				Eventually(session).Should(Say("cf app APP_NAME"))
    26  				Eventually(session).Should(Say("OPTIONS:"))
    27  				Eventually(session).Should(Say("--guid      Retrieve and display the given app's guid.  All other health and status output for the app is suppressed."))
    28  				Eventually(session).Should(Say("SEE ALSO:"))
    29  				Eventually(session).Should(Say("apps, events, logs, map-route, push, unmap-route"))
    30  				Eventually(session).Should(Exit(0))
    31  			})
    32  		})
    33  	})
    34  
    35  	Context("when the environment is not setup correctly", func() {
    36  		It("fails with the appropriate errors", func() {
    37  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "app", "some-app")
    38  		})
    39  	})
    40  
    41  	Context("when the environment is set up correctly", func() {
    42  		var (
    43  			orgName   string
    44  			spaceName string
    45  		)
    46  
    47  		BeforeEach(func() {
    48  			orgName = helpers.NewOrgName()
    49  			spaceName = helpers.NewSpaceName()
    50  
    51  			setupCF(orgName, spaceName)
    52  		})
    53  
    54  		AfterEach(func() {
    55  			helpers.QuickDeleteOrg(orgName)
    56  		})
    57  
    58  		Context("when the app name is not provided", func() {
    59  			It("tells the user that the app name is required, prints help text, and exits 1", func() {
    60  				session := helpers.CF("app")
    61  
    62  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    63  				Eventually(session).Should(Say("NAME:"))
    64  				Eventually(session).Should(Exit(1))
    65  			})
    66  		})
    67  
    68  		Context("when the app does not exist", func() {
    69  			Context("when no flags are given", func() {
    70  				It("tells the user that the app is not found and exits 1", func() {
    71  					appName := helpers.PrefixedRandomName("app")
    72  					session := helpers.CF("app", appName)
    73  
    74  					Eventually(session).Should(Say("FAILED"))
    75  					Eventually(session.Err).Should(Say("App %s not found", appName))
    76  					Eventually(session).Should(Exit(1))
    77  				})
    78  			})
    79  
    80  			Context("when the --guid flag is given", func() {
    81  				It("tells the user that the app is not found and exits 1", func() {
    82  					appName := helpers.PrefixedRandomName("app")
    83  					session := helpers.CF("app", "--guid", appName)
    84  
    85  					Eventually(session).Should(Say("FAILED"))
    86  					Eventually(session.Err).Should(Say("App %s not found", appName))
    87  					Eventually(session).Should(Exit(1))
    88  				})
    89  			})
    90  		})
    91  
    92  		Context("when the app does exist", func() {
    93  			Context("when the app is a buildpack app", func() {
    94  				var (
    95  					domainName string
    96  					tcpDomain  helpers.Domain
    97  					appName    string
    98  				)
    99  
   100  				BeforeEach(func() {
   101  					Eventually(helpers.CF("create-isolation-segment", RealIsolationSegment)).Should(Exit(0))
   102  					Eventually(helpers.CF("enable-org-isolation", orgName, RealIsolationSegment)).Should(Exit(0))
   103  					Eventually(helpers.CF("set-space-isolation-segment", spaceName, RealIsolationSegment)).Should(Exit(0))
   104  
   105  					appName = helpers.PrefixedRandomName("app")
   106  					domainName = defaultSharedDomain()
   107  					tcpDomain = helpers.NewDomain(orgName, helpers.DomainName("tcp"))
   108  					tcpDomain.CreateWithRouterGroup(helpers.FindOrCreateTCPRouterGroup(GinkgoParallelNode()))
   109  					helpers.WithHelloWorldApp(func(appDir string) {
   110  						manifestContents := []byte(fmt.Sprintf(`
   111  ---
   112  applications:
   113  - name: %s
   114    memory: 128M
   115    instances: 2
   116    disk_quota: 128M
   117    routes:
   118    - route: %s.%s
   119    - route: %s:1024
   120  `, appName, appName, domainName, tcpDomain.Name))
   121  						manifestPath := filepath.Join(appDir, "manifest.yml")
   122  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   123  						Expect(err).ToNot(HaveOccurred())
   124  
   125  						// Create manifest
   126  						Eventually(helpers.CF("push", appName, "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   127  					})
   128  				})
   129  
   130  				Context("when the app is started and has 2 instances", func() {
   131  					It("displays the app information with instances table", func() {
   132  						session := helpers.CF("app", appName)
   133  						Eventually(session).Should(Say("name:\\s+%s", appName))
   134  						Eventually(session).Should(Say("requested state:\\s+started"))
   135  						Eventually(session).Should(Say("instances:\\s+2/2"))
   136  						Eventually(session).Should(Say("isolation segment:\\s+%s", RealIsolationSegment))
   137  						Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   138  						Eventually(session).Should(Say("routes:\\s+[\\w\\d-]+\\.%s, %s:1024", domainName, tcpDomain.Name))
   139  						Eventually(session).Should(Say("last uploaded:\\s+\\w{3} [0-3]\\d \\w{3} [0-2]\\d:[0-5]\\d:[0-5]\\d \\w+ \\d{4}"))
   140  						Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   141  						Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   142  						Eventually(session).Should(Say(""))
   143  						Eventually(session).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   144  						Eventually(session).Should(Say("#0\\s+running\\s+\\d{4}-[01]\\d-[0-3]\\dT[0-2][0-9]:[0-5]\\d:[0-5]\\dZ\\s+\\d+\\.\\d+%.*of 128M.*of 128M"))
   145  						Eventually(session).Should(Say("#1\\s+running\\s+\\d{4}-[01]\\d-[0-3]\\dT[0-2][0-9]:[0-5]\\d:[0-5]\\dZ\\s+\\d+\\.\\d+%.*of 128M.*of 128M"))
   146  						Eventually(session).Should(Exit(0))
   147  					})
   148  				})
   149  
   150  				Context("when the app is stopped", func() {
   151  					BeforeEach(func() {
   152  						Eventually(helpers.CF("stop", appName)).Should(Exit(0))
   153  					})
   154  
   155  					It("displays the app information", func() {
   156  						session := helpers.CF("app", appName)
   157  						Eventually(session).Should(Say("name:\\s+%s", appName))
   158  						Eventually(session).Should(Say("requested state:\\s+stopped"))
   159  						Eventually(session).Should(Say("instances:\\s+0/2"))
   160  						Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   161  						Eventually(session).Should(Say("routes:\\s+[\\w\\d-]+.%s, %s:1024", domainName, tcpDomain.Name))
   162  						Eventually(session).Should(Say("last uploaded:"))
   163  						Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   164  						Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   165  
   166  						Eventually(session).Should(Say("There are no running instances of this app."))
   167  						Eventually(session).Should(Exit(0))
   168  					})
   169  				})
   170  
   171  				Context("when the app has 0 instances", func() {
   172  					BeforeEach(func() {
   173  						Eventually(helpers.CF("scale", appName, "-i", "0")).Should(Exit(0))
   174  					})
   175  
   176  					It("displays the app information", func() {
   177  						session := helpers.CF("app", appName)
   178  						Eventually(session).Should(Say("name:\\s+%s", appName))
   179  						Eventually(session).Should(Say("requested state:\\s+started"))
   180  						Eventually(session).Should(Say("instances:\\s+0/0"))
   181  						Eventually(session).Should(Say("usage:\\s+128M x 0 instances"))
   182  						Eventually(session).Should(Say("routes:\\s+[\\w\\d-]+\\.%s, %s:1024", domainName, tcpDomain.Name))
   183  						Eventually(session).Should(Say("last uploaded:"))
   184  						Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   185  						Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   186  
   187  						Eventually(session).Should(Say("There are no running instances of this app."))
   188  						Eventually(session).Should(Exit(0))
   189  					})
   190  				})
   191  
   192  				Context("when the --guid flag is given", func() {
   193  					var appGUID string
   194  
   195  					BeforeEach(func() {
   196  						session := helpers.CF("curl", fmt.Sprintf("/v2/apps?q=name:%s", appName))
   197  						Eventually(session).Should(Exit(0))
   198  						rawJSON := strings.TrimSpace(string(session.Out.Contents()))
   199  						var AppInfo struct {
   200  							Resources []struct {
   201  								Metadata struct {
   202  									GUID string `json:"guid"`
   203  								} `json:"metadata"`
   204  							} `json:"resources"`
   205  						}
   206  
   207  						err := json.Unmarshal([]byte(rawJSON), &AppInfo)
   208  						Expect(err).NotTo(HaveOccurred())
   209  
   210  						appGUID = AppInfo.Resources[0].Metadata.GUID
   211  					})
   212  
   213  					It("displays the app guid", func() {
   214  						session := helpers.CF("app", "--guid", appName)
   215  						Eventually(session).Should(Say(appGUID))
   216  						Eventually(session).Should(Exit(0))
   217  					})
   218  				})
   219  			})
   220  
   221  			Context("when the app is a Docker app", func() {
   222  				var (
   223  					tcpDomain helpers.Domain
   224  					appName   string
   225  				)
   226  
   227  				BeforeEach(func() {
   228  					appName = helpers.PrefixedRandomName("app")
   229  					tcpDomain = helpers.NewDomain(orgName, helpers.DomainName("tcp"))
   230  					tcpDomain.CreateWithRouterGroup(helpers.FindOrCreateTCPRouterGroup(GinkgoParallelNode()))
   231  					Eventually(helpers.CF("push", appName, "-o", DockerImage)).Should(Exit())
   232  				})
   233  
   234  				It("displays the docker image and does not display buildpack", func() {
   235  					session := helpers.CF("app", appName)
   236  					Eventually(session).Should(Say("name:\\s+%s", appName))
   237  					Consistently(session).ShouldNot(Say("buildpack:"))
   238  					Eventually(session).Should(Say("docker image:\\s+%s", DockerImage))
   239  					Consistently(session).ShouldNot(Say("buildpack:"))
   240  					Eventually(session).Should(Exit(0))
   241  				})
   242  			})
   243  		})
   244  	})
   245  })