github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+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  
    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("app command", func() {
    19  	BeforeEach(func() {
    20  		helpers.RunIfExperimental("target command refactor is still experimental")
    21  	})
    22  
    23  	Describe("help", func() {
    24  		Context("when --help flag is set", func() {
    25  			It("Displays command usage to output", func() {
    26  				session := helpers.CF("app", "--help")
    27  				Eventually(session).Should(Say("NAME:"))
    28  				Eventually(session).Should(Say("app - Display health and status for app"))
    29  				Eventually(session).Should(Say("USAGE:"))
    30  				Eventually(session).Should(Say("cf app APP_NAME"))
    31  				Eventually(session).Should(Say("OPTIONS:"))
    32  				Eventually(session).Should(Say("--guid      Retrieve and display the given app's guid.  All other health and status output for the app is suppressed."))
    33  				Eventually(session).Should(Say("SEE ALSO:"))
    34  				Eventually(session).Should(Say("apps, events, logs, map-route, push, unmap-route"))
    35  				Eventually(session).Should(Exit(0))
    36  			})
    37  		})
    38  	})
    39  
    40  	Context("when the environment is not setup correctly", func() {
    41  		Context("when no API endpoint is set", func() {
    42  			BeforeEach(func() {
    43  				helpers.UnsetAPI()
    44  			})
    45  
    46  			It("fails with no API endpoint set message", func() {
    47  				session := helpers.CF("app", "wut")
    48  				Eventually(session.Out).Should(Say("FAILED"))
    49  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    50  				Eventually(session).Should(Exit(1))
    51  			})
    52  		})
    53  
    54  		Context("when not logged in", func() {
    55  			BeforeEach(func() {
    56  				helpers.LogoutCF()
    57  			})
    58  
    59  			It("fails with not logged in message", func() {
    60  				session := helpers.CF("app", "wut")
    61  				Eventually(session.Out).Should(Say("FAILED"))
    62  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    63  				Eventually(session).Should(Exit(1))
    64  			})
    65  		})
    66  
    67  		Context("when there is no org set", func() {
    68  			BeforeEach(func() {
    69  				helpers.LogoutCF()
    70  				helpers.LoginCF()
    71  			})
    72  
    73  			It("fails with no targeted org error message", func() {
    74  				session := helpers.CF("app", "wut")
    75  				Eventually(session.Out).Should(Say("FAILED"))
    76  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    77  				Eventually(session).Should(Exit(1))
    78  			})
    79  		})
    80  
    81  		Context("when there is no space set", func() {
    82  			BeforeEach(func() {
    83  				helpers.LogoutCF()
    84  				helpers.LoginCF()
    85  				helpers.TargetOrg(ReadOnlyOrg)
    86  			})
    87  
    88  			It("fails with no targeted space error message", func() {
    89  				session := helpers.CF("app", "wut")
    90  				Eventually(session.Out).Should(Say("FAILED"))
    91  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
    92  				Eventually(session).Should(Exit(1))
    93  			})
    94  		})
    95  	})
    96  
    97  	Context("when the environment is set up correctly", func() {
    98  		var (
    99  			orgName   string
   100  			spaceName string
   101  		)
   102  
   103  		BeforeEach(func() {
   104  			orgName = helpers.NewOrgName()
   105  			spaceName = helpers.PrefixedRandomName("SPACE")
   106  
   107  			setupCF(orgName, spaceName)
   108  		})
   109  
   110  		Context("when the app does not exist", func() {
   111  			Context("when no flags are given", func() {
   112  				It("tells the user that the app is not found and exits 1", func() {
   113  					appName := helpers.PrefixedRandomName("app")
   114  					session := helpers.CF("app", appName)
   115  
   116  					Eventually(session.Out).Should(Say("FAILED"))
   117  					Eventually(session.Err).Should(Say("App %s not found", appName))
   118  					Eventually(session).Should(Exit(1))
   119  				})
   120  			})
   121  
   122  			Context("when the --guid flag is given", func() {
   123  				It("tells the user that the app is not found and exits 1", func() {
   124  					appName := helpers.PrefixedRandomName("app")
   125  					session := helpers.CF("app", "--guid", appName)
   126  
   127  					Eventually(session.Out).Should(Say("FAILED"))
   128  					Eventually(session.Err).Should(Say("App %s not found", appName))
   129  					Eventually(session).Should(Exit(1))
   130  				})
   131  			})
   132  		})
   133  
   134  		Context("when the app does exist", func() {
   135  			var (
   136  				domainName string
   137  				tcpDomain  helpers.Domain
   138  				appName    string
   139  			)
   140  			BeforeEach(func() {
   141  				appName = helpers.PrefixedRandomName("app")
   142  				domainName = defaultSharedDomain()
   143  				tcpDomain = helpers.NewDomain(orgName, helpers.DomainName("tcp"))
   144  				tcpDomain.CreateWithRouterGroup("default-tcp")
   145  				helpers.WithHelloWorldApp(func(appDir string) {
   146  					manifestContents := []byte(fmt.Sprintf(`
   147  ---
   148  applications:
   149  - name: %s
   150    memory: 128M
   151    instances: 2
   152    disk_quota: 128M
   153    routes:
   154    - route: %s.%s
   155    - route: %s:1111
   156  `, appName, appName, domainName, tcpDomain.Name))
   157  					manifestPath := filepath.Join(appDir, "manifest.yml")
   158  					err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   159  					Expect(err).ToNot(HaveOccurred())
   160  
   161  					// Create manifest
   162  					Eventually(helpers.CF("push", appName, "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   163  				})
   164  			})
   165  
   166  			AfterEach(func() {
   167  				Eventually(helpers.CF("delete", appName, "-f", "-r")).Should(Exit(0))
   168  			})
   169  
   170  			Context("when the app is started and has 2 instances", func() {
   171  				It("displays the app information with instances table", func() {
   172  					session := helpers.CF("app", appName)
   173  					Eventually(session).Should(Say("Name:              %s", appName))
   174  					Eventually(session).Should(Say("Requested state:   started"))
   175  					Eventually(session).Should(Say("Instances:         2/2"))
   176  					Eventually(session).Should(Say("Usage:             128M x 2 instances"))
   177  					Eventually(session).Should(Say("Routes:            %s.%s, %s:1111", appName, domainName, tcpDomain.Name))
   178  					Eventually(session).Should(Say("Last uploaded:"))
   179  					Eventually(session).Should(Say("Stack:             cflinuxfs2"))
   180  					Eventually(session).Should(Say("Buildpack:         staticfile_buildpack"))
   181  
   182  					Eventually(session).Should(Say("State\\s+Since\\s+CPU\\s+Memory\\s+Disk\\s+Details"))
   183  					Eventually(session).Should(Say("#0\\s+running\\s+.*\\d+\\.\\d+%.*of 128M.*of 128M"))
   184  					Eventually(session).Should(Say("#1\\s+running\\s+.*\\d+\\.\\d+%.*of 128M.*of 128M"))
   185  					Eventually(session).Should(Exit(0))
   186  				})
   187  			})
   188  
   189  			Context("when the app is stopped", func() {
   190  				BeforeEach(func() {
   191  					Eventually(helpers.CF("stop", appName)).Should(Exit(0))
   192  				})
   193  
   194  				It("displays the app information", func() {
   195  					session := helpers.CF("app", appName)
   196  					Eventually(session).Should(Say("Name:              %s", appName))
   197  					Eventually(session).Should(Say("Requested state:   stopped"))
   198  					Eventually(session).Should(Say("Instances:         0/2"))
   199  					Eventually(session).Should(Say("Usage:             128M x 2 instances"))
   200  					Eventually(session).Should(Say("Routes:            %s.%s, %s:1111", appName, domainName, tcpDomain.Name))
   201  					Eventually(session).Should(Say("Last uploaded:"))
   202  					Eventually(session).Should(Say("Stack:             cflinuxfs2"))
   203  					Eventually(session).Should(Say("Buildpack:         staticfile_buildpack"))
   204  
   205  					Eventually(session).Should(Say("There are no running instances of this app."))
   206  					Eventually(session).Should(Exit(0))
   207  				})
   208  			})
   209  
   210  			Context("when the app has 0 instances", func() {
   211  				BeforeEach(func() {
   212  					Eventually(helpers.CF("scale", appName, "-i", "0")).Should(Exit(0))
   213  				})
   214  
   215  				It("displays the app information", func() {
   216  					session := helpers.CF("app", appName)
   217  					Eventually(session).Should(Say("Name:              %s", appName))
   218  					Eventually(session).Should(Say("Requested state:   started"))
   219  					Eventually(session).Should(Say("Instances:         0/0"))
   220  					Eventually(session).Should(Say("Usage:             128M x 0 instances"))
   221  					Eventually(session).Should(Say("Routes:            %s.%s, %s:1111", appName, domainName, tcpDomain.Name))
   222  					Eventually(session).Should(Say("Last uploaded:"))
   223  					Eventually(session).Should(Say("Stack:             cflinuxfs2"))
   224  					Eventually(session).Should(Say("Buildpack:         staticfile_buildpack"))
   225  
   226  					Eventually(session).Should(Say("There are no running instances of this app."))
   227  					Eventually(session).Should(Exit(0))
   228  				})
   229  
   230  			})
   231  
   232  			Context("when the --guid flag is given", func() {
   233  				var appGUID string
   234  
   235  				BeforeEach(func() {
   236  					session := helpers.CF("curl", fmt.Sprintf("/v2/apps?q=name:%s", appName))
   237  					Eventually(session).Should(Exit(0))
   238  					rawJSON := strings.TrimSpace(string(session.Out.Contents()))
   239  					var AppInfo struct {
   240  						Resources []struct {
   241  							Metadata struct {
   242  								GUID string `json:"guid"`
   243  							} `json:"metadata"`
   244  						} `json:"resources"`
   245  					}
   246  
   247  					err := json.Unmarshal([]byte(rawJSON), &AppInfo)
   248  					Expect(err).NotTo(HaveOccurred())
   249  
   250  					appGUID = AppInfo.Resources[0].Metadata.GUID
   251  				})
   252  
   253  				It("displays the app guid", func() {
   254  					session := helpers.CF("app", "--guid", appName)
   255  					Eventually(session).Should(Say(appGUID))
   256  					Eventually(session).Should(Exit(0))
   257  				})
   258  			})
   259  		})
   260  	})
   261  })