github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/integration/v7/isolated/set_health_check_command_test.go (about)

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