github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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 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  		Context("when no API endpoint is set", func() {
    37  			BeforeEach(func() {
    38  				helpers.UnsetAPI()
    39  			})
    40  
    41  			It("fails with no API endpoint set message", func() {
    42  				session := helpers.CF("app", "wut")
    43  				Eventually(session.Out).Should(Say("FAILED"))
    44  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    45  				Eventually(session).Should(Exit(1))
    46  			})
    47  		})
    48  
    49  		Context("when not logged in", func() {
    50  			BeforeEach(func() {
    51  				helpers.LogoutCF()
    52  			})
    53  
    54  			It("fails with not logged in message", func() {
    55  				session := helpers.CF("app", "wut")
    56  				Eventually(session.Out).Should(Say("FAILED"))
    57  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    58  				Eventually(session).Should(Exit(1))
    59  			})
    60  		})
    61  
    62  		Context("when there is no org set", func() {
    63  			BeforeEach(func() {
    64  				helpers.LogoutCF()
    65  				helpers.LoginCF()
    66  			})
    67  
    68  			It("fails with no targeted org error message", func() {
    69  				session := helpers.CF("app", "wut")
    70  				Eventually(session.Out).Should(Say("FAILED"))
    71  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    72  				Eventually(session).Should(Exit(1))
    73  			})
    74  		})
    75  
    76  		Context("when there is no space set", func() {
    77  			BeforeEach(func() {
    78  				helpers.LogoutCF()
    79  				helpers.LoginCF()
    80  				helpers.TargetOrg(ReadOnlyOrg)
    81  			})
    82  
    83  			It("fails with no targeted space error message", func() {
    84  				session := helpers.CF("app", "wut")
    85  				Eventually(session.Out).Should(Say("FAILED"))
    86  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
    87  				Eventually(session).Should(Exit(1))
    88  			})
    89  		})
    90  	})
    91  
    92  	Context("when the environment is set up correctly", func() {
    93  		var (
    94  			orgName   string
    95  			spaceName string
    96  		)
    97  
    98  		BeforeEach(func() {
    99  			orgName = helpers.NewOrgName()
   100  			spaceName = helpers.NewSpaceName()
   101  
   102  			setupCF(orgName, spaceName)
   103  		})
   104  
   105  		Context("when the app name is not provided", func() {
   106  			It("tells the user that the app name is required, prints help text, and exits 1", func() {
   107  				session := helpers.CF("app")
   108  
   109  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
   110  				Eventually(session.Out).Should(Say("NAME:"))
   111  				Eventually(session).Should(Exit(1))
   112  			})
   113  		})
   114  
   115  		Context("when the app does not exist", func() {
   116  			Context("when no flags are given", func() {
   117  				It("tells the user that the app is not found and exits 1", func() {
   118  					appName := helpers.PrefixedRandomName("app")
   119  					session := helpers.CF("app", appName)
   120  
   121  					Eventually(session.Out).Should(Say("FAILED"))
   122  					Eventually(session.Err).Should(Say("App %s not found", appName))
   123  					Eventually(session).Should(Exit(1))
   124  				})
   125  			})
   126  
   127  			Context("when the --guid flag is given", func() {
   128  				It("tells the user that the app is not found and exits 1", func() {
   129  					appName := helpers.PrefixedRandomName("app")
   130  					session := helpers.CF("app", "--guid", appName)
   131  
   132  					Eventually(session.Out).Should(Say("FAILED"))
   133  					Eventually(session.Err).Should(Say("App %s not found", appName))
   134  					Eventually(session).Should(Exit(1))
   135  				})
   136  			})
   137  		})
   138  
   139  		Context("when the app does exist", func() {
   140  			var (
   141  				domainName string
   142  				tcpDomain  helpers.Domain
   143  				appName    string
   144  			)
   145  
   146  			BeforeEach(func() {
   147  				Eventually(helpers.CF("create-isolation-segment", RealIsolationSegment)).Should(Exit(0))
   148  				Eventually(helpers.CF("enable-org-isolation", orgName, RealIsolationSegment)).Should(Exit(0))
   149  				Eventually(helpers.CF("set-space-isolation-segment", spaceName, RealIsolationSegment)).Should(Exit(0))
   150  
   151  				appName = helpers.PrefixedRandomName("app")
   152  				domainName = defaultSharedDomain()
   153  				tcpDomain = helpers.NewDomain(orgName, helpers.DomainName("tcp"))
   154  				tcpDomain.CreateWithRouterGroup("default-tcp")
   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    - route: %s:0
   166  `, appName, appName, domainName, tcpDomain.Name))
   167  					manifestPath := filepath.Join(appDir, "manifest.yml")
   168  					err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   169  					Expect(err).ToNot(HaveOccurred())
   170  
   171  					// Create manifest
   172  					Eventually(helpers.CF("push", appName, "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack", "--random-route")).Should(Exit(0))
   173  				})
   174  			})
   175  
   176  			AfterEach(func() {
   177  				Eventually(helpers.CF("delete", appName, "-f", "-r")).Should(Exit(0))
   178  			})
   179  
   180  			Context("when the app is started and has 2 instances", func() {
   181  				It("displays the app information with instances table", func() {
   182  					session := helpers.CF("app", appName)
   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("isolation segment:\\s+%s", RealIsolationSegment))
   187  					Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   188  					Eventually(session).Should(Say("routes:\\s+[a-z-]+\\.%s, %s:\\d+", domainName, tcpDomain.Name))
   189  					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}"))
   190  					Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   191  					Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   192  					Eventually(session).Should(Say(""))
   193  					Eventually(session).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   194  					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"))
   195  					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"))
   196  					Eventually(session).Should(Exit(0))
   197  				})
   198  			})
   199  
   200  			Context("when the app is stopped", func() {
   201  				BeforeEach(func() {
   202  					Eventually(helpers.CF("stop", appName)).Should(Exit(0))
   203  				})
   204  
   205  				It("displays the app information", func() {
   206  					session := helpers.CF("app", appName)
   207  					Eventually(session).Should(Say("name:\\s+%s", appName))
   208  					Eventually(session).Should(Say("requested state:\\s+stopped"))
   209  					Eventually(session).Should(Say("instances:\\s+0/2"))
   210  					Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   211  					Eventually(session).Should(Say("routes:\\s+[a-z-]+.%s, %s:\\d+", domainName, tcpDomain.Name))
   212  					Eventually(session).Should(Say("last uploaded:"))
   213  					Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   214  					Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   215  
   216  					Eventually(session).Should(Say("There are no running instances of this app."))
   217  					Eventually(session).Should(Exit(0))
   218  				})
   219  			})
   220  
   221  			Context("when the app has 0 instances", func() {
   222  				BeforeEach(func() {
   223  					Eventually(helpers.CF("scale", appName, "-i", "0")).Should(Exit(0))
   224  				})
   225  
   226  				It("displays the app information", func() {
   227  					session := helpers.CF("app", appName)
   228  					Eventually(session).Should(Say("name:\\s+%s", appName))
   229  					Eventually(session).Should(Say("requested state:\\s+started"))
   230  					Eventually(session).Should(Say("instances:\\s+0/0"))
   231  					Eventually(session).Should(Say("usage:\\s+128M x 0 instances"))
   232  					Eventually(session).Should(Say("routes:\\s+[a-z-]+\\.%s, %s:\\d+", domainName, tcpDomain.Name))
   233  					Eventually(session).Should(Say("last uploaded:"))
   234  					Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   235  					Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   236  
   237  					Eventually(session).Should(Say("There are no running instances of this app."))
   238  					Eventually(session).Should(Exit(0))
   239  				})
   240  			})
   241  
   242  			Context("when the --guid flag is given", func() {
   243  				var appGUID string
   244  
   245  				BeforeEach(func() {
   246  					session := helpers.CF("curl", fmt.Sprintf("/v2/apps?q=name:%s", appName))
   247  					Eventually(session).Should(Exit(0))
   248  					rawJSON := strings.TrimSpace(string(session.Out.Contents()))
   249  					var AppInfo struct {
   250  						Resources []struct {
   251  							Metadata struct {
   252  								GUID string `json:"guid"`
   253  							} `json:"metadata"`
   254  						} `json:"resources"`
   255  					}
   256  
   257  					err := json.Unmarshal([]byte(rawJSON), &AppInfo)
   258  					Expect(err).NotTo(HaveOccurred())
   259  
   260  					appGUID = AppInfo.Resources[0].Metadata.GUID
   261  				})
   262  
   263  				It("displays the app guid", func() {
   264  					session := helpers.CF("app", "--guid", appName)
   265  					Eventually(session).Should(Say(appGUID))
   266  					Eventually(session).Should(Exit(0))
   267  				})
   268  			})
   269  		})
   270  	})
   271  })