github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/integration/v7/isolated/set_health_check_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"regexp"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  var _ = Describe("set-health-check command", func() {
    16  	var (
    17  		orgName   string
    18  		spaceName string
    19  		appName   string
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		orgName = helpers.NewOrgName()
    24  		spaceName = helpers.NewSpaceName()
    25  		appName = helpers.PrefixedRandomName("app")
    26  	})
    27  
    28  	Describe("help", func() {
    29  		When("--help flag is set", func() {
    30  			It("appears in cf help -a", func() {
    31  				session := helpers.CF("help", "-a")
    32  				Eventually(session).Should(Exit(0))
    33  				Expect(session).To(HaveCommandInCategoryWithDescription("set-health-check", "APPS", "Change type of health check performed on an app's process"))
    34  			})
    35  
    36  			It("Displays command usage to output", func() {
    37  				session := helpers.CF("set-health-check", "--help")
    38  
    39  				Eventually(session).Should(Say("NAME:"))
    40  				Eventually(session).Should(Say("set-health-check - Change type of health check performed on an app's process"))
    41  				Eventually(session).Should(Say("USAGE:"))
    42  				Eventually(session).Should(Say(`cf set-health-check APP_NAME \(process \| port \| http \[--endpoint PATH\]\) \[--process PROCESS\] \[--invocation-timeout INVOCATION_TIMEOUT\]`))
    43  
    44  				Eventually(session).Should(Say("EXAMPLES:"))
    45  				Eventually(session).Should(Say("cf set-health-check worker-app process --process worker"))
    46  				Eventually(session).Should(Say("cf set-health-check my-web-app http --endpoint /foo"))
    47  				Eventually(session).Should(Say("cf set-health-check my-web-app http --invocation-timeout 10"))
    48  
    49  				Eventually(session).Should(Say("OPTIONS:"))
    50  				Eventually(session).Should(Say(`--endpoint\s+Path on the app \(Default: /\)`))
    51  				Eventually(session).Should(Say(`--invocation-timeout\s+Time \(in seconds\) that controls individual health check invocations`))
    52  				Eventually(session).Should(Say(`--process\s+App process to update \(Default: web\)`))
    53  
    54  				Eventually(session).Should(Exit(0))
    55  			})
    56  		})
    57  	})
    58  
    59  	When("the app name is not provided", func() {
    60  		It("tells the user that the app name is required, prints help text, and exits 1", func() {
    61  			session := helpers.CF("set-health-check")
    62  
    63  			Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `APP_NAME` and `HEALTH_CHECK_TYPE` were not provided"))
    64  			Eventually(session).Should(Say("NAME:"))
    65  			Eventually(session).Should(Exit(1))
    66  		})
    67  	})
    68  
    69  	When("the health check type is not provided", func() {
    70  		It("tells the user that health check type is required, prints help text, and exits 1", func() {
    71  			session := helpers.CF("set-health-check", appName)
    72  
    73  			Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `HEALTH_CHECK_TYPE` was not provided"))
    74  			Eventually(session).Should(Say("NAME:"))
    75  			Eventually(session).Should(Exit(1))
    76  		})
    77  	})
    78  
    79  	When("the environment is not setup correctly", func() {
    80  		It("fails with the appropriate errors", func() {
    81  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "set-health-check", appName, "port")
    82  		})
    83  	})
    84  
    85  	When("the environment is set up correctly", func() {
    86  		var userName string
    87  
    88  		BeforeEach(func() {
    89  			helpers.SetupCF(orgName, spaceName)
    90  			userName, _ = helpers.GetCredentials()
    91  		})
    92  
    93  		AfterEach(func() {
    94  			helpers.QuickDeleteOrg(orgName)
    95  		})
    96  
    97  		When("the app exists", func() {
    98  			BeforeEach(func() {
    99  				helpers.WithProcfileApp(func(appDir string) {
   100  					Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName, "--no-start")).Should(Exit(0))
   101  				})
   102  			})
   103  
   104  			When("the process type is set", func() {
   105  				It("displays the health check types for each process", func() {
   106  					packageGUID := helpers.GetFirstAppPackageGuid(appName)
   107  					stageSession := helpers.CF("stage", appName, "--package-guid", packageGUID)
   108  					Eventually(stageSession).Should(Exit(0))
   109  
   110  					regex := regexp.MustCompile(`droplet guid:\s+(.+)`)
   111  					matches := regex.FindStringSubmatch(string(stageSession.Out.Contents()))
   112  					Expect(matches).To(HaveLen(2))
   113  
   114  					dropletGUID := matches[1]
   115  					setDropletSession := helpers.CF("set-droplet", appName, "--droplet-guid", dropletGUID)
   116  					Eventually(setDropletSession).Should(Exit(0))
   117  
   118  					session := helpers.CF("set-health-check", appName, "http", "--endpoint", "/healthcheck", "--process", "console")
   119  					Eventually(session).Should(Say(`Updating health check type for app %s process console in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   120  					Eventually(session).Should(Exit(0))
   121  
   122  					session = helpers.CF("get-health-check", appName)
   123  					Eventually(session).Should(Say(`process\s+health check\s+endpoint \(for http\)\s+invocation timeout`))
   124  					Eventually(session).Should(Say(`web\s+port\s+1`))
   125  					Eventually(session).Should(Say(`console\s+http\s+/healthcheck\s+1`))
   126  
   127  					Eventually(session).Should(Exit(0))
   128  				})
   129  			})
   130  
   131  			When("the invocation timeout is set", func() {
   132  				It("displays the health check types for each process", func() {
   133  					session := helpers.CF("set-health-check", appName, "http", "--endpoint", "/healthcheck", "--invocation-timeout", "2")
   134  					Eventually(session).Should(Say(`Updating health check type for app %s process web in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   135  					Eventually(session).Should(Exit(0))
   136  
   137  					session = helpers.CF("get-health-check", appName)
   138  					Eventually(session).Should(Say(`process\s+health check\s+endpoint \(for http\)\s+invocation timeout`))
   139  					Eventually(session).Should(Say(`web\s+http\s+/healthcheck\s+2`))
   140  
   141  					Eventually(session).Should(Exit(0))
   142  				})
   143  
   144  			})
   145  
   146  			When("process type and invocation timeout are not set", func() {
   147  				It("displays the health check types for each process", func() {
   148  					session := helpers.CF("set-health-check", appName, "http", "--endpoint", "/healthcheck")
   149  					Eventually(session).Should(Say(`Updating health check type for app %s process web in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   150  					Eventually(session).Should(Exit(0))
   151  
   152  					session = helpers.CF("get-health-check", appName)
   153  					Eventually(session).Should(Say(`process\s+health check\s+endpoint \(for http\)\s+invocation timeout`))
   154  					Eventually(session).Should(Say(`web\s+http\s+/healthcheck\s+1`))
   155  
   156  					Eventually(session).Should(Exit(0))
   157  				})
   158  			})
   159  
   160  			When("no http health check endpoint is given", func() {
   161  				BeforeEach(func() {
   162  					Eventually(helpers.CF("set-health-check", appName, "http")).Should(Exit(0))
   163  				})
   164  
   165  				It("sets the http health check endpoint to /", func() {
   166  					session := helpers.CF("get-health-check", appName)
   167  					Eventually(session).Should(Say(`web\s+http\s+/`))
   168  					Eventually(session).Should(Exit(0))
   169  				})
   170  			})
   171  
   172  			When("the process type does not exist", func() {
   173  				BeforeEach(func() {
   174  					helpers.WithProcfileApp(func(appDir string) {
   175  						Eventually(helpers.CustomCF(helpers.CFEnv{WorkingDirectory: appDir}, "push", appName)).Should(Exit(0))
   176  					})
   177  				})
   178  
   179  				It("returns a process not found error", func() {
   180  					session := helpers.CF("set-health-check", appName, "http", "--endpoint", "/healthcheck", "--process", "nonexistent-type")
   181  					Eventually(session).Should(Say(`Updating health check type for app %s process nonexistent-type in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   182  					Eventually(session.Err).Should(Say("Process nonexistent-type not found"))
   183  					Eventually(session).Should(Say("FAILED"))
   184  					Eventually(session).Should(Exit(1))
   185  				})
   186  			})
   187  
   188  			When("health check type is not 'http' and endpoint is set", func() {
   189  				It("returns an error", func() {
   190  					session := helpers.CF("set-health-check", appName, "port", "--endpoint", "oh-no", "--process", "console")
   191  					Eventually(session.Out).Should(Say(`Updating health check type for app %s process console in org %s / space %s as %s\.\.\.`, appName, orgName, spaceName, userName))
   192  					Eventually(session.Err).Should(Say("Health check type must be 'http' to set a health check HTTP endpoint."))
   193  					Eventually(session.Out).Should(Say("FAILED"))
   194  					Eventually(session).Should(Exit(1))
   195  				})
   196  			})
   197  
   198  			When("an invalid http health check endpoint is given", func() {
   199  				It("outputs an error and exits 1", func() {
   200  					session := helpers.CF("set-health-check", appName, "http", "--endpoint", "invalid")
   201  					Eventually(session.Err).Should(Say("Health check endpoint must be a valid URI path"))
   202  					Eventually(session).Should(Exit(1))
   203  				})
   204  			})
   205  
   206  			When("the invocation timeout is less than one", func() {
   207  				It("returns an error", func() {
   208  					session := helpers.CF("set-health-check", appName, "port", "--invocation-timeout", "0", "--process", "console")
   209  					Eventually(session.Err).Should(Say("Value must be greater than or equal to 1."))
   210  					Eventually(session).Should(Exit(1))
   211  				})
   212  			})
   213  
   214  			When("None is passed in", func() {
   215  				It("returns an error", func() {
   216  					session := helpers.CF("set-health-check", appName, "none")
   217  					Eventually(session.Err).Should(Say(`Incorrect Usage: HEALTH_CHECK_TYPE must be "port", "process", or "http"`))
   218  					Eventually(session).Should(Exit(1))
   219  				})
   220  			})
   221  		})
   222  
   223  		When("the app does not exist", func() {
   224  			It("displays app not found and exits 1", func() {
   225  				invalidAppName := "invalid-app-name"
   226  				session := helpers.CF("set-health-check", invalidAppName, "port")
   227  
   228  				Eventually(session.Out).Should(Say(`Updating health check type for app %s process web in org %s / space %s as %s\.\.\.`, invalidAppName, orgName, spaceName, userName))
   229  				Eventually(session.Err).Should(Say("App '%s' not found", invalidAppName))
   230  				Eventually(session.Out).Should(Say("FAILED"))
   231  
   232  				Eventually(session).Should(Exit(1))
   233  			})
   234  		})
   235  	})
   236  })