github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/integration/isolated/set_health_check_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/integration/helpers"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/ginkgo/extensions/table"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("set-health-check command", func() {
    14  	Describe("help", func() {
    15  		Context("when --help flag is set", func() {
    16  			It("Displays command usage to output", func() {
    17  				session := helpers.CF("set-health-check", "--help")
    18  				Eventually(session).Should(Say("NAME:"))
    19  				Eventually(session).Should(Say("set-health-check - Change type of health check performed on an app"))
    20  				Eventually(session).Should(Say("USAGE:"))
    21  				Eventually(session).Should(Say("cf set-health-check APP_NAME \\(process \\| port \\| http \\[--endpoint PATH\\]\\)"))
    22  				Eventually(session).Should(Say("TIP: 'none' has been deprecated but is accepted for 'process'."))
    23  				Eventually(session).Should(Say("EXAMPLES:"))
    24  				Eventually(session).Should(Say("   cf set-health-check worker-app process"))
    25  				Eventually(session).Should(Say("   cf set-health-check my-web-app http --endpoint /foo"))
    26  				Eventually(session).Should(Say("OPTIONS:"))
    27  				Eventually(session).Should(Say("   --endpoint      Path on the app \\(Default: /\\)"))
    28  				Eventually(session).Should(Exit(0))
    29  			})
    30  		})
    31  	})
    32  
    33  	Context("when the environment is not setup correctly", func() {
    34  		Context("when no API endpoint is set", func() {
    35  			BeforeEach(func() {
    36  				helpers.UnsetAPI()
    37  			})
    38  
    39  			It("fails with no API endpoint set message", func() {
    40  				session := helpers.CF("set-health-check", "some-app", "port")
    41  				Eventually(session).Should(Say("FAILED"))
    42  				Eventually(session.Err).Should(Say("No API endpoint set. Use 'cf login' or 'cf api' to target an endpoint."))
    43  				Eventually(session).Should(Exit(1))
    44  			})
    45  		})
    46  
    47  		Context("when not logged in", func() {
    48  			BeforeEach(func() {
    49  				helpers.LogoutCF()
    50  			})
    51  
    52  			It("fails with not logged in message", func() {
    53  				session := helpers.CF("set-health-check", "some-app", "port")
    54  				Eventually(session).Should(Say("FAILED"))
    55  				Eventually(session.Err).Should(Say("Not logged in. Use 'cf login' to log in."))
    56  				Eventually(session).Should(Exit(1))
    57  			})
    58  		})
    59  
    60  		Context("when there is no org and space set", func() {
    61  			BeforeEach(func() {
    62  				helpers.LogoutCF()
    63  				helpers.LoginCF()
    64  			})
    65  
    66  			It("fails with no targeted org error message", func() {
    67  				session := helpers.CF("set-health-check", "some-app", "port")
    68  				Eventually(session).Should(Say("FAILED"))
    69  				Eventually(session.Err).Should(Say("No org targeted, use 'cf target -o ORG' to target an org."))
    70  				Eventually(session).Should(Exit(1))
    71  			})
    72  		})
    73  
    74  		Context("when there is no space set", func() {
    75  			BeforeEach(func() {
    76  				helpers.LogoutCF()
    77  				helpers.LoginCF()
    78  				helpers.TargetOrg(ReadOnlyOrg)
    79  			})
    80  
    81  			It("fails with no targeted space error message", func() {
    82  				session := helpers.CF("set-health-check", "some-app", "port")
    83  				Eventually(session).Should(Say("FAILED"))
    84  				Eventually(session.Err).Should(Say("No space targeted, use 'cf target -s SPACE' to target a space."))
    85  				Eventually(session).Should(Exit(1))
    86  			})
    87  		})
    88  	})
    89  
    90  	Context("when the input is invalid", func() {
    91  		DescribeTable("fails with incorrect usage method",
    92  			func(args ...string) {
    93  				cmd := append([]string{"set-health-check"}, args...)
    94  				session := helpers.CF(cmd...)
    95  				Eventually(session.Err).Should(Say("Incorrect Usage:"))
    96  				Eventually(session).Should(Say("NAME:"))
    97  				Eventually(session).Should(Exit(1))
    98  			},
    99  			Entry("when app-name and health-check-type are not passed in"),
   100  			Entry("when health-check-type is not passed in", "some-app"),
   101  			Entry("when health-check-type is invalid", "some-app", "wut"),
   102  		)
   103  	})
   104  
   105  	Context("when the environment is set up correctly", func() {
   106  		var (
   107  			orgName   string
   108  			spaceName string
   109  		)
   110  
   111  		BeforeEach(func() {
   112  			orgName = helpers.NewOrgName()
   113  			spaceName = helpers.NewSpaceName()
   114  
   115  			setupCF(orgName, spaceName)
   116  		})
   117  
   118  		AfterEach(func() {
   119  			helpers.QuickDeleteOrg(orgName)
   120  		})
   121  
   122  		Context("when the app does not exist", 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("set-health-check", appName, "port")
   126  
   127  				Eventually(session).Should(Say("FAILED"))
   128  				Eventually(session.Err).Should(Say("App %s not found", appName))
   129  				Eventually(session).Should(Exit(1))
   130  			})
   131  		})
   132  
   133  		Context("when the app exists", func() {
   134  			var appName string
   135  
   136  			BeforeEach(func() {
   137  				appName = helpers.PrefixedRandomName("app")
   138  				helpers.WithHelloWorldApp(func(appDir string) {
   139  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   140  				})
   141  			})
   142  
   143  			DescribeTable("Updates health-check-type and exits 0",
   144  				func(settingType string) {
   145  					session := helpers.CF("set-health-check", appName, settingType)
   146  
   147  					username, _ := helpers.GetCredentials()
   148  					Eventually(session).Should(Say("Updating health check type for app %s in org %s / space %s as %s", appName, orgName, spaceName, username))
   149  					Eventually(session).Should(Say("OK"))
   150  					Eventually(session).Should(Exit(0))
   151  
   152  					getSession := helpers.CF("get-health-check", appName)
   153  					Eventually(getSession).Should(Say("health check type:\\s+%s", settingType))
   154  					Eventually(getSession).Should(Exit(0))
   155  				},
   156  				Entry("when setting the health-check-type to 'none'", "none"),
   157  				Entry("when setting the health-check-type to 'process'", "process"),
   158  				Entry("when setting the health-check-type to 'port'", "port"),
   159  				Entry("when setting the health-check-type to 'http'", "http"),
   160  			)
   161  
   162  			Context("when no http health check endpoint is given", func() {
   163  				BeforeEach(func() {
   164  					Eventually(helpers.CF("set-health-check", appName, "http")).Should(Exit(0))
   165  				})
   166  
   167  				It("sets the http health check endpoint to /", func() {
   168  					session := helpers.CF("get-health-check", appName)
   169  					Eventually(session.Out).Should(Say("endpoint \\(for http type\\):\\s+/"))
   170  					Eventually(session).Should(Exit(0))
   171  				})
   172  			})
   173  
   174  			Context("when a valid http health check endpoint is given", func() {
   175  				BeforeEach(func() {
   176  					Eventually(helpers.CF("set-health-check", appName, "http", "--endpoint", "/foo")).Should(Exit(0))
   177  				})
   178  
   179  				It("sets the http health check endpoint to the given endpoint", func() {
   180  					session := helpers.CF("get-health-check", appName)
   181  					Eventually(session.Out).Should(Say("endpoint \\(for http type\\):\\s+/foo"))
   182  					Eventually(session).Should(Exit(0))
   183  				})
   184  			})
   185  
   186  			Context("when an invalid http health check endpoint is given", func() {
   187  				It("outputs an error and exits 1", func() {
   188  					session := helpers.CF("set-health-check", appName, "http", "--endpoint", "invalid")
   189  					Eventually(session.Err).Should(Say("The app is invalid: health_check_http_endpoint HTTP health check endpoint is not a valid URI path: invalid"))
   190  					Eventually(session).Should(Exit(1))
   191  				})
   192  			})
   193  
   194  			Context("when an endpoint is given with a non-http health check type", func() {
   195  				It("outputs an error and exits 1", func() {
   196  					session := helpers.CF("set-health-check", appName, "port", "--endpoint", "/foo")
   197  					Eventually(session.Err).Should(Say("Health check type must be 'http' to set a health check HTTP endpoint\\."))
   198  					Eventually(session).Should(Exit(1))
   199  				})
   200  			})
   201  
   202  			Context("when the app is started", func() {
   203  				BeforeEach(func() {
   204  					appName = helpers.PrefixedRandomName("app")
   205  					helpers.WithHelloWorldApp(func(appDir string) {
   206  						Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   207  					})
   208  				})
   209  
   210  				It("displays tip to restart the app", func() {
   211  					session := helpers.CF("set-health-check", appName, "port")
   212  					Eventually(session).Should(Say("TIP: An app restart is required for the change to take affect\\."))
   213  					Eventually(session).Should(Exit(0))
   214  				})
   215  			})
   216  		})
   217  	})
   218  })