github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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  		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  		AfterEach(func() {
   106  			helpers.QuickDeleteOrg(orgName)
   107  		})
   108  
   109  		Context("when the app name is not provided", func() {
   110  			It("tells the user that the app name is required, prints help text, and exits 1", func() {
   111  				session := helpers.CF("app")
   112  
   113  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
   114  				Eventually(session.Out).Should(Say("NAME:"))
   115  				Eventually(session).Should(Exit(1))
   116  			})
   117  		})
   118  
   119  		Context("when the app does not exist", func() {
   120  			Context("when no flags are given", func() {
   121  				It("tells the user that the app is not found and exits 1", func() {
   122  					appName := helpers.PrefixedRandomName("app")
   123  					session := helpers.CF("app", appName)
   124  
   125  					Eventually(session.Out).Should(Say("FAILED"))
   126  					Eventually(session.Err).Should(Say("App %s not found", appName))
   127  					Eventually(session).Should(Exit(1))
   128  				})
   129  			})
   130  
   131  			Context("when the --guid flag is given", func() {
   132  				It("tells the user that the app is not found and exits 1", func() {
   133  					appName := helpers.PrefixedRandomName("app")
   134  					session := helpers.CF("app", "--guid", appName)
   135  
   136  					Eventually(session.Out).Should(Say("FAILED"))
   137  					Eventually(session.Err).Should(Say("App %s not found", appName))
   138  					Eventually(session).Should(Exit(1))
   139  				})
   140  			})
   141  		})
   142  
   143  		Context("when the app does exist", func() {
   144  			Context("when the app is a buildpack app", func() {
   145  				var (
   146  					domainName string
   147  					tcpDomain  helpers.Domain
   148  					appName    string
   149  				)
   150  
   151  				BeforeEach(func() {
   152  					Eventually(helpers.CF("create-isolation-segment", RealIsolationSegment)).Should(Exit(0))
   153  					Eventually(helpers.CF("enable-org-isolation", orgName, RealIsolationSegment)).Should(Exit(0))
   154  					Eventually(helpers.CF("set-space-isolation-segment", spaceName, RealIsolationSegment)).Should(Exit(0))
   155  
   156  					appName = helpers.PrefixedRandomName("app")
   157  					domainName = defaultSharedDomain()
   158  					tcpDomain = helpers.NewDomain(orgName, helpers.DomainName("tcp"))
   159  					tcpDomain.CreateWithRouterGroup("default-tcp")
   160  					helpers.WithHelloWorldApp(func(appDir string) {
   161  						manifestContents := []byte(fmt.Sprintf(`
   162  ---
   163  applications:
   164  - name: %s
   165    memory: 128M
   166    instances: 2
   167    disk_quota: 128M
   168    routes:
   169    - route: %s.%s
   170    - route: %s:0
   171  `, appName, appName, domainName, tcpDomain.Name))
   172  						manifestPath := filepath.Join(appDir, "manifest.yml")
   173  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   174  						Expect(err).ToNot(HaveOccurred())
   175  
   176  						// Create manifest
   177  						Eventually(helpers.CF("push", appName, "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack", "--random-route")).Should(Exit(0))
   178  					})
   179  				})
   180  
   181  				AfterEach(func() {
   182  					Eventually(helpers.CF("delete", appName, "-f", "-r")).Should(Exit(0))
   183  				})
   184  
   185  				Context("when the app is started and has 2 instances", func() {
   186  					It("displays the app information with instances table", func() {
   187  						session := helpers.CF("app", appName)
   188  						Eventually(session).Should(Say("name:\\s+%s", appName))
   189  						Eventually(session).Should(Say("requested state:\\s+started"))
   190  						Eventually(session).Should(Say("instances:\\s+2/2"))
   191  						Eventually(session).Should(Say("isolation segment:\\s+%s", RealIsolationSegment))
   192  						Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   193  						Eventually(session).Should(Say("routes:\\s+[a-z-]+\\.%s, %s:\\d+", domainName, tcpDomain.Name))
   194  						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}"))
   195  						Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   196  						Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   197  						Eventually(session).Should(Say(""))
   198  						Eventually(session).Should(Say("state\\s+since\\s+cpu\\s+memory\\s+disk\\s+details"))
   199  						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"))
   200  						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"))
   201  						Eventually(session).Should(Exit(0))
   202  					})
   203  				})
   204  
   205  				Context("when the app is stopped", func() {
   206  					BeforeEach(func() {
   207  						Eventually(helpers.CF("stop", appName)).Should(Exit(0))
   208  					})
   209  
   210  					It("displays the app information", func() {
   211  						session := helpers.CF("app", appName)
   212  						Eventually(session).Should(Say("name:\\s+%s", appName))
   213  						Eventually(session).Should(Say("requested state:\\s+stopped"))
   214  						Eventually(session).Should(Say("instances:\\s+0/2"))
   215  						Eventually(session).Should(Say("usage:\\s+128M x 2 instances"))
   216  						Eventually(session).Should(Say("routes:\\s+[a-z-]+.%s, %s:\\d+", domainName, tcpDomain.Name))
   217  						Eventually(session).Should(Say("last uploaded:"))
   218  						Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   219  						Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   220  
   221  						Eventually(session).Should(Say("There are no running instances of this app."))
   222  						Eventually(session).Should(Exit(0))
   223  					})
   224  				})
   225  
   226  				Context("when the app has 0 instances", func() {
   227  					BeforeEach(func() {
   228  						Eventually(helpers.CF("scale", appName, "-i", "0")).Should(Exit(0))
   229  					})
   230  
   231  					It("displays the app information", func() {
   232  						session := helpers.CF("app", appName)
   233  						Eventually(session).Should(Say("name:\\s+%s", appName))
   234  						Eventually(session).Should(Say("requested state:\\s+started"))
   235  						Eventually(session).Should(Say("instances:\\s+0/0"))
   236  						Eventually(session).Should(Say("usage:\\s+128M x 0 instances"))
   237  						Eventually(session).Should(Say("routes:\\s+[a-z-]+\\.%s, %s:\\d+", domainName, tcpDomain.Name))
   238  						Eventually(session).Should(Say("last uploaded:"))
   239  						Eventually(session).Should(Say("stack:\\s+cflinuxfs2"))
   240  						Eventually(session).Should(Say("buildpack:\\s+staticfile_buildpack"))
   241  
   242  						Eventually(session).Should(Say("There are no running instances of this app."))
   243  						Eventually(session).Should(Exit(0))
   244  					})
   245  				})
   246  
   247  				Context("when the --guid flag is given", func() {
   248  					var appGUID string
   249  
   250  					BeforeEach(func() {
   251  						session := helpers.CF("curl", fmt.Sprintf("/v2/apps?q=name:%s", appName))
   252  						Eventually(session).Should(Exit(0))
   253  						rawJSON := strings.TrimSpace(string(session.Out.Contents()))
   254  						var AppInfo struct {
   255  							Resources []struct {
   256  								Metadata struct {
   257  									GUID string `json:"guid"`
   258  								} `json:"metadata"`
   259  							} `json:"resources"`
   260  						}
   261  
   262  						err := json.Unmarshal([]byte(rawJSON), &AppInfo)
   263  						Expect(err).NotTo(HaveOccurred())
   264  
   265  						appGUID = AppInfo.Resources[0].Metadata.GUID
   266  					})
   267  
   268  					It("displays the app guid", func() {
   269  						session := helpers.CF("app", "--guid", appName)
   270  						Eventually(session).Should(Say(appGUID))
   271  						Eventually(session).Should(Exit(0))
   272  					})
   273  				})
   274  			})
   275  
   276  			Context("when the app is a Docker app", func() {
   277  				var (
   278  					domainName string
   279  					tcpDomain  helpers.Domain
   280  					appName    string
   281  				)
   282  
   283  				BeforeEach(func() {
   284  					appName = helpers.PrefixedRandomName("app")
   285  					domainName = defaultSharedDomain()
   286  					tcpDomain = helpers.NewDomain(orgName, helpers.DomainName("tcp"))
   287  					tcpDomain.CreateWithRouterGroup("default-tcp")
   288  					Eventually(helpers.CF("push", appName, "-o", DockerImage)).Should(Exit())
   289  				})
   290  
   291  				It("displays the docker image and does not display buildpack", func() {
   292  					session := helpers.CF("app", appName)
   293  					Eventually(session).Should(Say("name:\\s+%s", appName))
   294  					Consistently(session).ShouldNot(Say("buildpack:"))
   295  					Eventually(session).Should(Say("docker image:\\s+%s", DockerImage))
   296  					Consistently(session).ShouldNot(Say("buildpack:"))
   297  					Eventually(session).Should(Exit(0))
   298  				})
   299  			})
   300  		})
   301  	})
   302  })