github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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  		Context("when the app does not exist", func() {
   119  			It("tells the user that the app is not found and exits 1", func() {
   120  				appName := helpers.PrefixedRandomName("app")
   121  				session := helpers.CF("set-health-check", appName, "port")
   122  
   123  				Eventually(session).Should(Say("FAILED"))
   124  				Eventually(session.Err).Should(Say("App %s not found", appName))
   125  				Eventually(session).Should(Exit(1))
   126  			})
   127  		})
   128  
   129  		Context("when the app exists", func() {
   130  			var appName string
   131  
   132  			BeforeEach(func() {
   133  				appName = helpers.PrefixedRandomName("app")
   134  				helpers.WithHelloWorldApp(func(appDir string) {
   135  					Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack", "--no-start")).Should(Exit(0))
   136  				})
   137  			})
   138  
   139  			DescribeTable("Updates health-check-type and exits 0",
   140  				func(settingType string) {
   141  					session := helpers.CF("set-health-check", appName, settingType)
   142  
   143  					username, _ := helpers.GetCredentials()
   144  					Eventually(session).Should(Say("Updating health check type for app %s in org %s / space %s as %s", appName, orgName, spaceName, username))
   145  					Eventually(session).Should(Say("OK"))
   146  					Eventually(session).Should(Exit(0))
   147  
   148  					getSession := helpers.CF("get-health-check", appName)
   149  					Eventually(getSession).Should(Say("health check type:\\s+%s", settingType))
   150  					Eventually(getSession).Should(Exit(0))
   151  				},
   152  				Entry("when setting the health-check-type to 'none'", "none"),
   153  				Entry("when setting the health-check-type to 'process'", "process"),
   154  				Entry("when setting the health-check-type to 'port'", "port"),
   155  				Entry("when setting the health-check-type to 'http'", "http"),
   156  			)
   157  
   158  			Context("when no http health check endpoint is given", func() {
   159  				BeforeEach(func() {
   160  					Eventually(helpers.CF("set-health-check", appName, "http")).Should(Exit(0))
   161  				})
   162  
   163  				It("sets the http health check endpoint to /", func() {
   164  					session := helpers.CF("get-health-check", appName)
   165  					Eventually(session.Out).Should(Say("endpoint \\(for http type\\):\\s+/"))
   166  					Eventually(session).Should(Exit(0))
   167  				})
   168  			})
   169  
   170  			Context("when a valid http health check endpoint is given", func() {
   171  				BeforeEach(func() {
   172  					Eventually(helpers.CF("set-health-check", appName, "http", "--endpoint", "/foo")).Should(Exit(0))
   173  				})
   174  
   175  				It("sets the http health check endpoint to the given endpoint", func() {
   176  					session := helpers.CF("get-health-check", appName)
   177  					Eventually(session.Out).Should(Say("endpoint \\(for http type\\):\\s+/foo"))
   178  					Eventually(session).Should(Exit(0))
   179  				})
   180  			})
   181  
   182  			Context("when an invalid http health check endpoint is given", func() {
   183  				It("outputs an error and exits 1", func() {
   184  					session := helpers.CF("set-health-check", appName, "http", "--endpoint", "invalid")
   185  					Eventually(session.Err).Should(Say("The app is invalid: health_check_http_endpoint HTTP health check endpoint is not a valid URI path: invalid"))
   186  					Eventually(session).Should(Exit(1))
   187  				})
   188  			})
   189  
   190  			Context("when an endpoint is given with a non-http health check type", func() {
   191  				It("outputs an error and exits 1", func() {
   192  					session := helpers.CF("set-health-check", appName, "port", "--endpoint", "/foo")
   193  					Eventually(session.Err).Should(Say("Health check type must be 'http' to set a health check HTTP endpoint."))
   194  					Eventually(session).Should(Exit(1))
   195  				})
   196  			})
   197  
   198  			Context("when the app is started", func() {
   199  				BeforeEach(func() {
   200  					appName = helpers.PrefixedRandomName("app")
   201  					helpers.WithHelloWorldApp(func(appDir string) {
   202  						Eventually(helpers.CF("push", appName, "-p", appDir, "-b", "staticfile_buildpack")).Should(Exit(0))
   203  					})
   204  				})
   205  
   206  				It("displays tip to restart the app", func() {
   207  					session := helpers.CF("set-health-check", appName, "port")
   208  					Eventually(session).Should(Say("TIP: An app restart is required for the change to take affect."))
   209  					Eventually(session).Should(Exit(0))
   210  				})
   211  			})
   212  		})
   213  	})
   214  })