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