github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/integration/isolated/push_command_with_health_check_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/ginkgo/extensions/table"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/gbytes"
    14  	. "github.com/onsi/gomega/gexec"
    15  )
    16  
    17  var _ = Describe("Push with health check", func() {
    18  	Context("help", func() {
    19  		Context("when displaying help in the refactor", func() {
    20  			It("displays command usage to output", func() {
    21  				session := helpers.CF("push", "--help")
    22  				Eventually(session.Out).Should(Say("--health-check-type, -u\\s+Application health check type \\(Default: 'port', 'none' accepted for 'process', 'http' implies endpoint '/'\\)"))
    23  				Eventually(session).Should(Exit(0))
    24  			})
    25  
    26  			It("displays health check timeout (-t) flag description", func() {
    27  				session := helpers.CF("push", "--help")
    28  				Eventually(session.Out).Should(Say("-t\\s+Time \\(in seconds\\) allowed to elapse between starting up an app and the first healthy response from the app"))
    29  				Eventually(session).Should(Exit(0))
    30  			})
    31  		})
    32  	})
    33  
    34  	Context("when the environment is set up correctly", func() {
    35  		var (
    36  			appName   string
    37  			orgName   string
    38  			spaceName string
    39  		)
    40  
    41  		BeforeEach(func() {
    42  			orgName = helpers.NewOrgName()
    43  			spaceName = helpers.PrefixedRandomName("SPACE")
    44  
    45  			setupCF(orgName, spaceName)
    46  
    47  			appName = helpers.PrefixedRandomName("app")
    48  		})
    49  
    50  		Context("when displaying help in the old code", func() {
    51  			It("displays command usage to output", func() {
    52  				session := helpers.CF("push")
    53  				Eventually(session.Out).Should(Say("--health-check-type, -u\\s+Application health check type \\(Default: 'port', 'none' accepted for 'process', 'http' implies endpoint '/'\\)"))
    54  				Eventually(session).Should(Exit(1))
    55  			})
    56  
    57  			It("displays health check timeout (-t) flag description", func() {
    58  				session := helpers.CF("push")
    59  				Eventually(session.Out).Should(Say("-t\\s+Time \\(in seconds\\) allowed to elapse between starting up an app and the first healthy response from the app"))
    60  				Eventually(session).Should(Exit(1))
    61  			})
    62  		})
    63  
    64  		Context("when pushing app without a manifest", func() {
    65  			Context("when the app doesn't already exist", func() {
    66  				DescribeTable("displays the correct health check type",
    67  					func(healthCheckType string, endpoint string) {
    68  						helpers.WithHelloWorldApp(func(appDir string) {
    69  							Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "-u", healthCheckType)).Should(Exit(0))
    70  						})
    71  
    72  						session := helpers.CF("get-health-check", appName)
    73  						Eventually(session.Out).Should(Say("Health check type:\\s+%s", healthCheckType))
    74  						Eventually(session.Out).Should(Say("Endpoint \\(for http type\\):\\s+%s\n", endpoint))
    75  						Eventually(session).Should(Exit(0))
    76  					},
    77  
    78  					Entry("when the health check type is none", "none", ""),
    79  					Entry("when the health check type is process", "process", ""),
    80  					Entry("when the health check type is port", "port", ""),
    81  					Entry("when the health check type is http", "http", "/"),
    82  				)
    83  			})
    84  
    85  			Context("when the app already exists", func() {
    86  				BeforeEach(func() {
    87  					helpers.WithHelloWorldApp(func(appDir string) {
    88  						Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "-u", "port")).Should(Exit(0))
    89  					})
    90  				})
    91  
    92  				Context("when the app does not already have a health-check-http-endpoint' configured", func() {
    93  					Context("when setting the health check type to 'http'", func() {
    94  						BeforeEach(func() {
    95  							helpers.WithHelloWorldApp(func(appDir string) {
    96  								Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "-u", "http")).Should(Exit(0))
    97  							})
    98  						})
    99  
   100  						It("sets the endpoint to /", func() {
   101  							session := helpers.CF("get-health-check", appName)
   102  							Eventually(session.Out).Should(Say("Endpoint \\(for http type\\):\\s+\\/\n"))
   103  							Eventually(session).Should(Exit(0))
   104  						})
   105  					})
   106  				})
   107  
   108  				Context("when the app already has a health check 'http' endpoint set", func() {
   109  					BeforeEach(func() {
   110  						Eventually(helpers.CF("set-health-check", appName, "http", "--endpoint", "/some-endpoint")).Should(Exit(0))
   111  
   112  						appGUID := helpers.AppGUID(appName)
   113  						session := helpers.CF("curl", fmt.Sprintf("/v2/apps/%s", appGUID))
   114  						Eventually(session.Out).Should(Say(`"health_check_http_endpoint": "/some-endpoint"`))
   115  						Eventually(session).Should(Exit(0))
   116  					})
   117  
   118  					Context("when the health check type to 'http'", func() {
   119  						BeforeEach(func() {
   120  							helpers.WithHelloWorldApp(func(appDir string) {
   121  								Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "-u", "http")).Should(Exit(0))
   122  							})
   123  						})
   124  
   125  						It("preserves the existing endpoint", func() {
   126  							session := helpers.CF("get-health-check", appName)
   127  							Eventually(session.Out).Should(Say("Endpoint \\(for http type\\):\\s+\\/some-endpoint\n"))
   128  							Eventually(session).Should(Exit(0))
   129  						})
   130  					})
   131  
   132  					Context("when updating the health check type to something other than 'http'", func() {
   133  						BeforeEach(func() {
   134  							helpers.WithHelloWorldApp(func(appDir string) {
   135  								Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "-u", "port")).Should(Exit(0))
   136  							})
   137  						})
   138  
   139  						It("preserves the existing endpoint", func() {
   140  							appGUID := helpers.AppGUID(appName)
   141  							session := helpers.CF("curl", fmt.Sprintf("/v2/apps/%s", appGUID))
   142  							Eventually(session.Out).Should(Say(`"health_check_http_endpoint": "/some-endpoint"`))
   143  							Eventually(session).Should(Exit(0))
   144  						})
   145  					})
   146  				})
   147  			})
   148  		})
   149  
   150  		Context("when pushing with manifest", func() {
   151  			DescribeTable("displays the correct health check type",
   152  				func(healthCheckType string, endpoint string) {
   153  					helpers.WithHelloWorldApp(func(appDir string) {
   154  						manifestContents := []byte(fmt.Sprintf(`
   155  ---
   156  applications:
   157  - name: %s
   158    memory: 128M
   159    health-check-type: %s
   160  `, appName, healthCheckType))
   161  						manifestPath := filepath.Join(appDir, "manifest.yml")
   162  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   163  						Expect(err).ToNot(HaveOccurred())
   164  
   165  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   166  					})
   167  
   168  					session := helpers.CF("get-health-check", appName)
   169  					Eventually(session.Out).Should(Say("Health check type:\\s+%s", healthCheckType))
   170  					Eventually(session.Out).Should(Say("Endpoint \\(for http type\\):\\s+%s\n", endpoint))
   171  					Eventually(session).Should(Exit(0))
   172  				},
   173  
   174  				Entry("when the health check type is none", "none", ""),
   175  				Entry("when the health check type is process", "process", ""),
   176  				Entry("when the health check type is port", "port", ""),
   177  				Entry("when the health check type is http", "http", "/"),
   178  			)
   179  
   180  			Context("when the health check type is not 'http' but an endpoint is provided", func() {
   181  				It("displays an error", func() {
   182  					helpers.WithHelloWorldApp(func(appDir string) {
   183  						manifestContents := []byte(fmt.Sprintf(`
   184  ---
   185  applications:
   186  - name: %s
   187    memory: 128M
   188    health-check-type: port
   189    health-check-http-endpoint: /some-endpoint
   190  `, appName))
   191  						manifestPath := filepath.Join(appDir, "manifest.yml")
   192  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   193  						Expect(err).ToNot(HaveOccurred())
   194  
   195  						session := helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")
   196  						Eventually(session.Out).Should(Say("Health check type must be 'http' to set a health check HTTP endpoint."))
   197  						Eventually(session).Should(Exit(1))
   198  					})
   199  				})
   200  			})
   201  
   202  			Context("when the health check type is http and an endpoint is provided", func() {
   203  				It("sets the health check type and endpoint", func() {
   204  					helpers.WithHelloWorldApp(func(appDir string) {
   205  						manifestContents := []byte(fmt.Sprintf(`
   206  ---
   207  applications:
   208  - name: %s
   209    memory: 128M
   210    health-check-type: http
   211    health-check-http-endpoint: /some-endpoint
   212  `, appName))
   213  						manifestPath := filepath.Join(appDir, "manifest.yml")
   214  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   215  						Expect(err).ToNot(HaveOccurred())
   216  
   217  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   218  					})
   219  
   220  					session := helpers.CF("get-health-check", appName)
   221  					Eventually(session.Out).Should(Say("Health check type:\\s+http"))
   222  					Eventually(session.Out).Should(Say("Endpoint \\(for http type\\):\\s+/some-endpoint\n"))
   223  					Eventually(session).Should(Exit(0))
   224  				})
   225  			})
   226  
   227  			Context("when the app already exists", func() {
   228  				It("updates the health check type and endpoint", func() {
   229  					helpers.WithHelloWorldApp(func(appDir string) {
   230  						manifestContents := []byte(fmt.Sprintf(`
   231  ---
   232  applications:
   233  - name: %s
   234    memory: 128M
   235    health-check-type: http
   236    health-check-http-endpoint: /some-endpoint
   237  `, appName))
   238  						manifestPath := filepath.Join(appDir, "manifest.yml")
   239  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   240  						Expect(err).ToNot(HaveOccurred())
   241  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   242  					})
   243  
   244  					helpers.WithHelloWorldApp(func(appDir string) {
   245  						manifestContents := []byte(fmt.Sprintf(`
   246  ---
   247  applications:
   248  - name: %s
   249    memory: 128M
   250    health-check-type: http
   251    health-check-http-endpoint: /new-endpoint
   252  `, appName))
   253  						manifestPath := filepath.Join(appDir, "manifest.yml")
   254  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   255  						Expect(err).ToNot(HaveOccurred())
   256  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   257  					})
   258  
   259  					session := helpers.CF("get-health-check", appName)
   260  					Eventually(session.Out).Should(Say("Health check type:\\s+http"))
   261  					Eventually(session.Out).Should(Say("Endpoint \\(for http type\\):\\s+/new-endpoint\n"))
   262  					Eventually(session).Should(Exit(0))
   263  				})
   264  
   265  				It("uses the existing endpoint if one isn't provided", func() {
   266  					helpers.WithHelloWorldApp(func(appDir string) {
   267  						manifestContents := []byte(fmt.Sprintf(`
   268  ---
   269  applications:
   270  - name: %s
   271    memory: 128M
   272    health-check-type: http
   273    health-check-http-endpoint: /some-endpoint
   274  `, appName))
   275  						manifestPath := filepath.Join(appDir, "manifest.yml")
   276  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   277  						Expect(err).ToNot(HaveOccurred())
   278  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   279  					})
   280  
   281  					helpers.WithHelloWorldApp(func(appDir string) {
   282  						manifestContents := []byte(fmt.Sprintf(`
   283  ---
   284  applications:
   285  - name: %s
   286    memory: 128M
   287    health-check-type: http
   288  `, appName))
   289  						manifestPath := filepath.Join(appDir, "manifest.yml")
   290  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   291  						Expect(err).ToNot(HaveOccurred())
   292  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   293  					})
   294  
   295  					session := helpers.CF("get-health-check", appName)
   296  					Eventually(session.Out).Should(Say("Health check type:\\s+http"))
   297  					Eventually(session.Out).Should(Say("Endpoint \\(for http type\\):\\s+/some-endpoint\n"))
   298  					Eventually(session).Should(Exit(0))
   299  				})
   300  			})
   301  
   302  			Context("when also pushing app with -u option", func() {
   303  				Context("when the -u option is 'port'", func() {
   304  					It("overrides the health check type in the manifest", func() {
   305  						helpers.WithHelloWorldApp(func(appDir string) {
   306  							manifestContents := []byte(fmt.Sprintf(`
   307  ---
   308  applications:
   309  - name: %s
   310    memory: 128M
   311    health-check-type: port
   312  `, appName))
   313  							manifestPath := filepath.Join(appDir, "manifest.yml")
   314  							err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   315  							Expect(err).ToNot(HaveOccurred())
   316  
   317  							Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack", "-u", "http")).Should(Exit(0))
   318  						})
   319  
   320  						session := helpers.CF("get-health-check", appName)
   321  						Eventually(session.Out).Should(Say("Health check type:\\s+http"))
   322  						Eventually(session).Should(Exit(0))
   323  					})
   324  				})
   325  
   326  				Context("when the -u option is 'http'", func() {
   327  					It("uses the endpoint in the manifest", func() {
   328  						helpers.WithHelloWorldApp(func(appDir string) {
   329  							manifestContents := []byte(fmt.Sprintf(`
   330  ---
   331  applications:
   332  - name: %s
   333    memory: 128M
   334    health-check-type: http
   335    health-check-http-endpoint: /some-endpoint
   336  `, appName))
   337  							manifestPath := filepath.Join(appDir, "manifest.yml")
   338  							err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   339  							Expect(err).ToNot(HaveOccurred())
   340  
   341  							Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack", "-u", "http")).Should(Exit(0))
   342  						})
   343  
   344  						session := helpers.CF("get-health-check", appName)
   345  						Eventually(session.Out).Should(Say("Health check type:\\s+http"))
   346  						Eventually(session.Out).Should(Say("Endpoint \\(for http type\\):\\s+/some-endpoint\n"))
   347  						Eventually(session).Should(Exit(0))
   348  					})
   349  				})
   350  			})
   351  		})
   352  	})
   353  })