github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/integration/experimental/v3_app_command_test.go (about)

     1  package experimental
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/liamawhite/cli-with-i18n/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("v3-app command", func() {
    17  	var (
    18  		orgName   string
    19  		spaceName string
    20  		appName   string
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		orgName = helpers.NewOrgName()
    25  		spaceName = helpers.NewSpaceName()
    26  		appName = helpers.PrefixedRandomName("app")
    27  	})
    28  
    29  	Describe("help", func() {
    30  		Context("when --help flag is set", func() {
    31  			It("Displays command usage to output", func() {
    32  				session := helpers.CF("v3-app", "--help")
    33  
    34  				Eventually(session.Out).Should(Say("NAME:"))
    35  				Eventually(session.Out).Should(Say("v3-app - Display health and status for an app"))
    36  				Eventually(session.Out).Should(Say("USAGE:"))
    37  				Eventually(session.Out).Should(Say("cf v3-app APP_NAME [--guid]"))
    38  				Eventually(session).Should(Say("OPTIONS:"))
    39  				Eventually(session).Should(Say("--guid\\s+Retrieve and display the given app's guid.  All other health and status output for the app is suppressed."))
    40  
    41  				Eventually(session).Should(Exit(0))
    42  			})
    43  		})
    44  	})
    45  
    46  	Context("when the app name is not provided", func() {
    47  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    48  			session := helpers.CF("v3-app")
    49  
    50  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `APP_NAME` was not provided"))
    51  			Eventually(session.Out).Should(Say("NAME:"))
    52  			Eventually(session).Should(Exit(1))
    53  		})
    54  	})
    55  
    56  	Context("when the environment is not setup correctly", func() {
    57  		Context("when no API endpoint is set", func() {
    58  			BeforeEach(func() {
    59  				helpers.UnsetAPI()
    60  			})
    61  
    62  			It("fails with no API endpoint set message", func() {
    63  				session := helpers.CF("v3-app", appName)
    64  				Eventually(session).Should(Say("FAILED"))
    65  				Eventually(session.Err).Should(Say("No API endpoint set\\. Use 'cf login' or 'cf api' to target an endpoint\\."))
    66  				Eventually(session).Should(Exit(1))
    67  			})
    68  		})
    69  
    70  		Context("when the v3 api does not exist", func() {
    71  			var server *Server
    72  
    73  			BeforeEach(func() {
    74  				server = helpers.StartAndTargetServerWithoutV3API()
    75  			})
    76  
    77  			AfterEach(func() {
    78  				server.Close()
    79  			})
    80  
    81  			It("fails with error message that the minimum version is not met", func() {
    82  				session := helpers.CF("v3-app", appName)
    83  				Eventually(session).Should(Say("FAILED"))
    84  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
    85  				Eventually(session).Should(Exit(1))
    86  			})
    87  		})
    88  
    89  		Context("when the v3 api version is lower than the minimum version", func() {
    90  			var server *Server
    91  
    92  			BeforeEach(func() {
    93  				server = helpers.StartAndTargetServerWithV3Version("3.0.0")
    94  			})
    95  
    96  			AfterEach(func() {
    97  				server.Close()
    98  			})
    99  
   100  			It("fails with error message that the minimum version is not met", func() {
   101  				session := helpers.CF("v3-app", appName)
   102  				Eventually(session).Should(Say("FAILED"))
   103  				Eventually(session.Err).Should(Say("This command requires CF API version 3\\.27\\.0 or higher\\."))
   104  				Eventually(session).Should(Exit(1))
   105  			})
   106  		})
   107  
   108  		Context("when not logged in", func() {
   109  			BeforeEach(func() {
   110  				helpers.LogoutCF()
   111  			})
   112  
   113  			It("fails with not logged in message", func() {
   114  				session := helpers.CF("v3-app", appName)
   115  				Eventually(session).Should(Say("FAILED"))
   116  				Eventually(session.Err).Should(Say("Not logged in\\. Use 'cf login' to log in\\."))
   117  				Eventually(session).Should(Exit(1))
   118  			})
   119  		})
   120  
   121  		Context("when there is no org set", func() {
   122  			BeforeEach(func() {
   123  				helpers.LogoutCF()
   124  				helpers.LoginCF()
   125  			})
   126  
   127  			It("fails with no org targeted error message", func() {
   128  				session := helpers.CF("v3-app", appName)
   129  				Eventually(session.Out).Should(Say("FAILED"))
   130  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org\\."))
   131  				Eventually(session).Should(Exit(1))
   132  			})
   133  		})
   134  
   135  		Context("when there is no space set", func() {
   136  			BeforeEach(func() {
   137  				helpers.LogoutCF()
   138  				helpers.LoginCF()
   139  				helpers.TargetOrg(ReadOnlyOrg)
   140  			})
   141  
   142  			It("fails with no space targeted error message", func() {
   143  				session := helpers.CF("v3-app", appName)
   144  				Eventually(session.Out).Should(Say("FAILED"))
   145  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space\\."))
   146  				Eventually(session).Should(Exit(1))
   147  			})
   148  		})
   149  	})
   150  
   151  	Context("when the environment is set up correctly", func() {
   152  		BeforeEach(func() {
   153  			setupCF(orgName, spaceName)
   154  		})
   155  
   156  		AfterEach(func() {
   157  			helpers.QuickDeleteOrg(orgName)
   158  		})
   159  
   160  		Context("when the app exists", func() {
   161  			var domainName string
   162  
   163  			BeforeEach(func() {
   164  				helpers.WithHelloWorldApp(func(appDir string) {
   165  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "v3-push", appName)).Should(Exit(0))
   166  				})
   167  
   168  				domainName = defaultSharedDomain()
   169  			})
   170  
   171  			It("displays the app summary", func() {
   172  				userName, _ := helpers.GetCredentials()
   173  
   174  				session := helpers.CF("v3-app", appName)
   175  				Eventually(session.Out).Should(Say("Showing health and status for app %s in org %s / space %s as %s\\.\\.\\.", appName, orgName, spaceName, userName))
   176  
   177  				Eventually(session.Out).Should(Say("name:\\s+%s", appName))
   178  				Eventually(session.Out).Should(Say("requested state:\\s+started"))
   179  				Eventually(session.Out).Should(Say("processes:\\s+web:1/1"))
   180  				Eventually(session.Out).Should(Say("memory usage:\\s+\\d+[KMG] x 1"))
   181  				Eventually(session.Out).Should(Say("routes:\\s+%s\\.%s", appName, domainName))
   182  				Eventually(session.Out).Should(Say("stack:\\s+cflinuxfs2"))
   183  				Eventually(session.Out).Should(Say("buildpacks:\\s+staticfile"))
   184  				Eventually(session.Out).Should(Say("web:1/1"))
   185  				Eventually(session.Out).Should(Say("#0\\s+running\\s+\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} [AP]M"))
   186  
   187  				Eventually(session).Should(Exit(0))
   188  			})
   189  
   190  			Context("when the app is stopped", func() {
   191  				BeforeEach(func() {
   192  					Eventually(helpers.CF("stop", appName)).Should(Exit(0))
   193  				})
   194  
   195  				It("displays that there are no running instances of the app", func() {
   196  					userName, _ := helpers.GetCredentials()
   197  
   198  					session := helpers.CF("v3-app", appName)
   199  
   200  					Eventually(session.Out).Should(Say(`Showing health and status for app %s in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   201  					Consistently(session.Out).ShouldNot(Say(`state\s+since\s+cpu\s+memory\s+disk`))
   202  					Eventually(session.Out).Should(Say("There are no running instances of this app"))
   203  					Eventually(session).Should(Exit(0))
   204  				})
   205  			})
   206  
   207  			Context("when the --guid flag is given", func() {
   208  				var appGUID string
   209  
   210  				BeforeEach(func() {
   211  					session := helpers.CF("curl", fmt.Sprintf("/v3/apps?names=%s", appName))
   212  					Eventually(session).Should(Exit(0))
   213  					rawJSON := strings.TrimSpace(string(session.Out.Contents()))
   214  					var AppInfo struct {
   215  						Resources []struct {
   216  							GUID string `json:"guid"`
   217  						} `json:"resources"`
   218  					}
   219  
   220  					err := json.Unmarshal([]byte(rawJSON), &AppInfo)
   221  					Expect(err).NotTo(HaveOccurred())
   222  
   223  					appGUID = AppInfo.Resources[0].GUID
   224  				})
   225  
   226  				It("displays the app guid", func() {
   227  					session := helpers.CF("v3-app", "--guid", appName)
   228  					Eventually(session).Should(Say(appGUID))
   229  					Eventually(session).Should(Exit(0))
   230  				})
   231  			})
   232  		})
   233  
   234  		Context("when the app is a docker app", func() {
   235  			var domainName string
   236  
   237  			BeforeEach(func() {
   238  				Eventually(helpers.CF("v3-push", appName, "-o", PublicDockerImage)).Should(Exit(0))
   239  				domainName = defaultSharedDomain()
   240  			})
   241  
   242  			It("displays the app summary", func() {
   243  				userName, _ := helpers.GetCredentials()
   244  
   245  				session := helpers.CF("v3-app", appName)
   246  				Eventually(session.Out).Should(Say("Showing health and status for app %s in org %s / space %s as %s\\.\\.\\.", appName, orgName, spaceName, userName))
   247  
   248  				Eventually(session.Out).Should(Say("name:\\s+%s", appName))
   249  				Eventually(session.Out).Should(Say("requested state:\\s+started"))
   250  				Eventually(session.Out).Should(Say("processes:\\s+web:1/1"))
   251  				Eventually(session.Out).Should(Say("memory usage:\\s+\\d+[KMG] x 1"))
   252  				Eventually(session.Out).Should(Say("routes:\\s+%s\\.%s", appName, domainName))
   253  				Eventually(session.Out).Should(Say("stack:\\s+"))
   254  				Eventually(session.Out).Should(Say("docker image:\\s+cloudfoundry/diego-docker-app-custom"))
   255  				Eventually(session.Out).Should(Say("web:1/1"))
   256  				Eventually(session.Out).Should(Say("#0\\s+running\\s+\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} [AP]M"))
   257  
   258  				Eventually(session).Should(Exit(0))
   259  			})
   260  		})
   261  
   262  		Context("when the app does not exist", func() {
   263  			It("displays app not found and exits 1", func() {
   264  				invalidAppName := "invalid-app-name"
   265  				session := helpers.CF("v3-app", invalidAppName)
   266  				userName, _ := helpers.GetCredentials()
   267  
   268  				Eventually(session.Out).Should(Say("Showing health and status for app %s in org %s / space %s as %s\\.\\.\\.", invalidAppName, orgName, spaceName, userName))
   269  				Eventually(session.Err).Should(Say("App %s not found", invalidAppName))
   270  				Eventually(session.Out).Should(Say("FAILED"))
   271  
   272  				Eventually(session).Should(Exit(1))
   273  			})
   274  
   275  			Context("when the --guid flag is given", func() {
   276  				It("tells the user that the app is not found and exits 1", func() {
   277  					appName := helpers.PrefixedRandomName("invalid-app")
   278  					session := helpers.CF("v3-app", "--guid", appName)
   279  
   280  					Eventually(session.Out).Should(Say("FAILED"))
   281  					Eventually(session.Err).Should(Say("App %s not found", appName))
   282  					Eventually(session).Should(Exit(1))
   283  				})
   284  			})
   285  		})
   286  	})
   287  })