github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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  		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).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).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).Should(Say("health check type:\\s+%s", healthCheckType))
    74  						Eventually(session).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).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  						session := helpers.CF("get-health-check", appName)
   113  						Eventually(session).Should(Say("endpoint \\(for http type\\):\\s+/some-endpoint"))
   114  						Eventually(session).Should(Exit(0))
   115  					})
   116  
   117  					Context("when the health check type to 'http'", func() {
   118  						BeforeEach(func() {
   119  							helpers.WithHelloWorldApp(func(appDir string) {
   120  								Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "-u", "http")).Should(Exit(0))
   121  							})
   122  						})
   123  
   124  						It("preserves the existing endpoint", func() {
   125  							session := helpers.CF("get-health-check", appName)
   126  							Eventually(session).Should(Say("endpoint \\(for http type\\):\\s+\\/some-endpoint\n"))
   127  							Eventually(session).Should(Exit(0))
   128  						})
   129  					})
   130  
   131  					Context("when updating the health check type to something other than 'http'", func() {
   132  						BeforeEach(func() {
   133  							helpers.WithHelloWorldApp(func(appDir string) {
   134  								Eventually(helpers.CF("push", appName, "--no-start", "-p", appDir, "-b", "staticfile_buildpack", "-u", "port")).Should(Exit(0))
   135  							})
   136  						})
   137  
   138  						It("preserves the existing endpoint", func() {
   139  							session := helpers.CF("get-health-check", appName, "-v")
   140  							Eventually(session).Should(Say(`"health_check_http_endpoint": "/some-endpoint"`))
   141  							Eventually(session).Should(Exit(0))
   142  						})
   143  					})
   144  				})
   145  			})
   146  		})
   147  
   148  		Context("when pushing with manifest", func() {
   149  			DescribeTable("displays the correct health check type",
   150  				func(healthCheckType string, endpoint string) {
   151  					helpers.WithHelloWorldApp(func(appDir string) {
   152  						manifestContents := []byte(fmt.Sprintf(`
   153  ---
   154  applications:
   155  - name: %s
   156    memory: 128M
   157    health-check-type: %s
   158  `, appName, healthCheckType))
   159  						manifestPath := filepath.Join(appDir, "manifest.yml")
   160  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   161  						Expect(err).ToNot(HaveOccurred())
   162  
   163  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   164  					})
   165  
   166  					session := helpers.CF("get-health-check", appName)
   167  					Eventually(session).Should(Say("health check type:\\s+%s", healthCheckType))
   168  					Eventually(session).Should(Say("endpoint \\(for http type\\):\\s+%s\n", endpoint))
   169  					Eventually(session).Should(Exit(0))
   170  				},
   171  
   172  				Entry("when the health check type is none", "none", ""),
   173  				Entry("when the health check type is process", "process", ""),
   174  				Entry("when the health check type is port", "port", ""),
   175  				Entry("when the health check type is http", "http", "/"),
   176  			)
   177  
   178  			Context("when the health check type is not 'http' but an endpoint is provided", func() {
   179  				It("displays an error", func() {
   180  					helpers.WithHelloWorldApp(func(appDir string) {
   181  						manifestContents := []byte(fmt.Sprintf(`
   182  ---
   183  applications:
   184  - name: %s
   185    memory: 128M
   186    health-check-type: port
   187    health-check-http-endpoint: /some-endpoint
   188  `, appName))
   189  						manifestPath := filepath.Join(appDir, "manifest.yml")
   190  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   191  						Expect(err).ToNot(HaveOccurred())
   192  
   193  						session := helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")
   194  						Eventually(session).Should(Say("Health check type must be 'http' to set a health check HTTP endpoint."))
   195  						Eventually(session).Should(Exit(1))
   196  					})
   197  				})
   198  			})
   199  
   200  			Context("when the health check type is http and an endpoint is provided", func() {
   201  				It("sets the health check type and endpoint", func() {
   202  					helpers.WithHelloWorldApp(func(appDir string) {
   203  						manifestContents := []byte(fmt.Sprintf(`
   204  ---
   205  applications:
   206  - name: %s
   207    memory: 128M
   208    health-check-type: http
   209    health-check-http-endpoint: /some-endpoint
   210  `, appName))
   211  						manifestPath := filepath.Join(appDir, "manifest.yml")
   212  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   213  						Expect(err).ToNot(HaveOccurred())
   214  
   215  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   216  					})
   217  
   218  					session := helpers.CF("get-health-check", appName)
   219  					Eventually(session).Should(Say("health check type:\\s+http"))
   220  					Eventually(session).Should(Say("endpoint \\(for http type\\):\\s+/some-endpoint\n"))
   221  					Eventually(session).Should(Exit(0))
   222  				})
   223  			})
   224  
   225  			Context("when the app already exists", func() {
   226  				It("updates the health check type and endpoint", func() {
   227  					helpers.WithHelloWorldApp(func(appDir string) {
   228  						manifestContents := []byte(fmt.Sprintf(`
   229  ---
   230  applications:
   231  - name: %s
   232    memory: 128M
   233    health-check-type: http
   234    health-check-http-endpoint: /some-endpoint
   235  `, appName))
   236  						manifestPath := filepath.Join(appDir, "manifest.yml")
   237  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   238  						Expect(err).ToNot(HaveOccurred())
   239  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   240  					})
   241  
   242  					helpers.WithHelloWorldApp(func(appDir string) {
   243  						manifestContents := []byte(fmt.Sprintf(`
   244  ---
   245  applications:
   246  - name: %s
   247    memory: 128M
   248    health-check-type: http
   249    health-check-http-endpoint: /new-endpoint
   250  `, appName))
   251  						manifestPath := filepath.Join(appDir, "manifest.yml")
   252  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   253  						Expect(err).ToNot(HaveOccurred())
   254  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   255  					})
   256  
   257  					session := helpers.CF("get-health-check", appName)
   258  					Eventually(session).Should(Say("health check type:\\s+http"))
   259  					Eventually(session).Should(Say("endpoint \\(for http type\\):\\s+/new-endpoint\n"))
   260  					Eventually(session).Should(Exit(0))
   261  				})
   262  
   263  				It("uses the existing endpoint if one isn't provided", func() {
   264  					helpers.WithHelloWorldApp(func(appDir string) {
   265  						manifestContents := []byte(fmt.Sprintf(`
   266  ---
   267  applications:
   268  - name: %s
   269    memory: 128M
   270    health-check-type: http
   271    health-check-http-endpoint: /some-endpoint
   272  `, appName))
   273  						manifestPath := filepath.Join(appDir, "manifest.yml")
   274  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   275  						Expect(err).ToNot(HaveOccurred())
   276  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   277  					})
   278  
   279  					helpers.WithHelloWorldApp(func(appDir string) {
   280  						manifestContents := []byte(fmt.Sprintf(`
   281  ---
   282  applications:
   283  - name: %s
   284    memory: 128M
   285    health-check-type: http
   286  `, appName))
   287  						manifestPath := filepath.Join(appDir, "manifest.yml")
   288  						err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   289  						Expect(err).ToNot(HaveOccurred())
   290  						Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack")).Should(Exit(0))
   291  					})
   292  
   293  					session := helpers.CF("get-health-check", appName)
   294  					Eventually(session).Should(Say("health check type:\\s+http"))
   295  					Eventually(session).Should(Say("endpoint \\(for http type\\):\\s+/some-endpoint\n"))
   296  					Eventually(session).Should(Exit(0))
   297  				})
   298  			})
   299  
   300  			Context("when also pushing app with -u option", func() {
   301  				Context("when the -u option is 'port'", func() {
   302  					It("overrides the health check type in the manifest", func() {
   303  						helpers.WithHelloWorldApp(func(appDir string) {
   304  							manifestContents := []byte(fmt.Sprintf(`
   305  ---
   306  applications:
   307  - name: %s
   308    memory: 128M
   309    health-check-type: http
   310  `, appName))
   311  							manifestPath := filepath.Join(appDir, "manifest.yml")
   312  							err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   313  							Expect(err).ToNot(HaveOccurred())
   314  
   315  							Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack", "-u", "port")).Should(Exit(0))
   316  						})
   317  
   318  						session := helpers.CF("get-health-check", appName)
   319  						Eventually(session).Should(Say("health check type:\\s+port"))
   320  						Eventually(session).Should(Exit(0))
   321  					})
   322  				})
   323  
   324  				Context("when the -u option is 'http'", func() {
   325  					It("uses the endpoint in the manifest", func() {
   326  						helpers.WithHelloWorldApp(func(appDir string) {
   327  							manifestContents := []byte(fmt.Sprintf(`
   328  ---
   329  applications:
   330  - name: %s
   331    memory: 128M
   332    health-check-type: port
   333  `, appName))
   334  							manifestPath := filepath.Join(appDir, "manifest.yml")
   335  							err := ioutil.WriteFile(manifestPath, manifestContents, 0666)
   336  							Expect(err).ToNot(HaveOccurred())
   337  
   338  							Eventually(helpers.CF("push", "--no-start", "-p", appDir, "-f", manifestPath, "-b", "staticfile_buildpack", "-u", "http")).Should(Exit(0))
   339  						})
   340  
   341  						session := helpers.CF("get-health-check", appName)
   342  						Eventually(session).Should(Say("health check type:\\s+http"))
   343  						Eventually(session).Should(Say("(?m)endpoint \\(for http type\\):\\s+/$"))
   344  						Eventually(session).Should(Exit(0))
   345  					})
   346  				})
   347  			})
   348  		})
   349  	})
   350  })