github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/integration/v7/isolated/create_route_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/integration/helpers"
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	. "github.com/onsi/gomega/gbytes"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("create-route command", func() {
    14  	Context("Help", func() {
    15  		It("displays the help information", func() {
    16  			session := helpers.CF("create-route", "--help")
    17  			Eventually(session).Should(Say(`NAME:`))
    18  			Eventually(session).Should(Say(`create-route - Create a url route in a space for later use\n`))
    19  			Eventually(session).Should(Say(`\n`))
    20  
    21  			Eventually(session).Should(Say(`USAGE:`))
    22  			Eventually(session).Should(Say(`Create an HTTP route:`))
    23  			Eventually(session).Should(Say(`cf create-route SPACE DOMAIN \[--hostname HOSTNAME\] \[--path PATH\]\n`))
    24  			Eventually(session).Should(Say(`\n`))
    25  
    26  			Eventually(session).Should(Say(`Create a TCP route:`))
    27  			Eventually(session).Should(Say(`cf create-route SPACE DOMAIN \(--port PORT \| --random-port\)\n`))
    28  			Eventually(session).Should(Say(`\n`))
    29  
    30  			Eventually(session).Should(Say(`EXAMPLES:`))
    31  			Eventually(session).Should(Say(`cf create-route my-space example.com\s+# example.com`))
    32  			Eventually(session).Should(Say(`cf create-route my-space example.com --hostname myapp\s+# myapp.example.com`))
    33  			Eventually(session).Should(Say(`cf create-route my-space example.com --hostname myapp --path foo\s+# myapp.example.com/foo`))
    34  			Eventually(session).Should(Say(`cf create-route my-space example.com --port 5000\s+# example.com:5000\n`))
    35  			Eventually(session).Should(Say(`\n`))
    36  
    37  			Eventually(session).Should(Say(`OPTIONS:`))
    38  			Eventually(session).Should(Say(`--hostname, -n\s+Hostname for the HTTP route \(required for shared domains\)`))
    39  			Eventually(session).Should(Say(`--path\s+Path for the HTTP route`))
    40  			Eventually(session).Should(Say(`--port\s+Port for the TCP route`))
    41  			Eventually(session).Should(Say(`--random-port\s+Create a random port for the TCP route\n`))
    42  			Eventually(session).Should(Say(`\n`))
    43  
    44  			Eventually(session).Should(Say(`SEE ALSO:`))
    45  			Eventually(session).Should(Say(`check-route, domains, map-route`))
    46  
    47  			Eventually(session).Should(Exit(0))
    48  		})
    49  	})
    50  
    51  	Context("Flag Errors", func() {
    52  		When("--hostname and --port are provided", func() {
    53  			It("fails with a message about being unable to mix --port with the HTTP route options", func() {
    54  				session := helpers.CF("create-route", "some-space", "some-domain", "--hostname", "some-host", "--port", "1122")
    55  				Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --hostname, --port`))
    56  				Eventually(session).Should(Exit(1))
    57  			})
    58  		})
    59  
    60  		When("--hostname and --random-port are provided", func() {
    61  			It("fails with a message about being unable to mix --random-port with any other options", func() {
    62  				session := helpers.CF("create-route", "some-space", "some-domain", "--hostname", "some-host", "--random-port")
    63  				Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --hostname, --random-port`))
    64  				Eventually(session).Should(Exit(1))
    65  			})
    66  		})
    67  
    68  		When("--path and --port are provided", func() {
    69  			It("fails with a message about being unable to mix --port with the HTTP route options", func() {
    70  				session := helpers.CF("create-route", "some-space", "some-domain", "--path", "/some-path", "--port", "1111")
    71  				Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --path, --port`))
    72  				Eventually(session).Should(Exit(1))
    73  			})
    74  		})
    75  
    76  		When("--path and --random-port are provided", func() {
    77  			It("fails with a message about being unable to mix --random-port with any other options", func() {
    78  				session := helpers.CF("create-route", "some-space", "some-domain", "--path", "/some-path", "--random-port")
    79  				Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --path, --random-port`))
    80  				Eventually(session).Should(Exit(1))
    81  			})
    82  		})
    83  
    84  		When("both --port and --random-port are provided", func() {
    85  			It("fails with a message about being unable to mix --random-port with any other options", func() {
    86  				session := helpers.CF("create-route", "some-space", "some-domain", "--port", "1121", "--random-port")
    87  				Eventually(session.Err).Should(Say(`Incorrect Usage: The following arguments cannot be used together: --port, --random-port`))
    88  				Eventually(session).Should(Exit(1))
    89  			})
    90  		})
    91  
    92  		When("the provided port is not valid / parseable", func() {
    93  			It("fails with an appropriate error", func() {
    94  				session := helpers.CF("create-route", "some-space", "some-domain", "--port", "ABC")
    95  				Eventually(session.Err).Should(Say(`Incorrect Usage: invalid argument for flag '--port' \(expected int > 0\)`))
    96  				Eventually(session).Should(Exit(1))
    97  			})
    98  		})
    99  	})
   100  
   101  	When("the environment is not setup correctly", func() {
   102  		It("fails with the appropriate errors", func() {
   103  			helpers.CheckEnvironmentTargetedCorrectly(true, false, ReadOnlyOrg, "create-route", "some-space", "some-domain")
   104  		})
   105  	})
   106  
   107  	When("the environment is set up correctly", func() {
   108  		var (
   109  			orgName   string
   110  			spaceName string
   111  		)
   112  
   113  		BeforeEach(func() {
   114  			orgName = helpers.NewOrgName()
   115  			spaceName = helpers.NewSpaceName()
   116  
   117  			helpers.SetupCF(orgName, spaceName)
   118  		})
   119  
   120  		AfterEach(func() {
   121  			helpers.QuickDeleteOrg(orgName)
   122  		})
   123  
   124  		When("the space does not exist", func() {
   125  			It("displays 'space not found' and exits 1", func() {
   126  				badSpaceName := fmt.Sprintf("%s-1", spaceName)
   127  				session := helpers.CF("create-route", badSpaceName, "some-domain")
   128  				Eventually(session).Should(Say(`FAILED`))
   129  				Eventually(session.Err).Should(Say(`Space '%s' not found\.`, badSpaceName))
   130  				Eventually(session).Should(Exit(1))
   131  			})
   132  		})
   133  
   134  		When("the space is not specified", func() {
   135  			It("displays error and exits 1", func() {
   136  				session := helpers.CF("create-route")
   137  				Eventually(session.Err).Should(Say("Incorrect Usage: the required arguments `SPACE` and `DOMAIN` were not provided\n"))
   138  				Eventually(session.Err).Should(Say("\n"))
   139  				Eventually(session).Should(Say("NAME:\n"))
   140  				Eventually(session).Should(Exit(1))
   141  			})
   142  		})
   143  
   144  		When("the domain does not exist", func() {
   145  			It("displays error and exits 1", func() {
   146  				session := helpers.CF("create-route", spaceName, "some-domain")
   147  				Eventually(session).Should(Say(`FAILED`))
   148  				Eventually(session.Err).Should(Say(`Domain some-domain not found`))
   149  				Eventually(session).Should(Exit(1))
   150  			})
   151  		})
   152  
   153  		When("the domain is not specified", func() {
   154  			It("displays error and exits 1", func() {
   155  				session := helpers.CF("create-route", spaceName)
   156  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `DOMAIN` was not provided\n"))
   157  				Eventually(session.Err).Should(Say("\n"))
   158  				Eventually(session).Should(Say("NAME:\n"))
   159  				Eventually(session).Should(Exit(1))
   160  			})
   161  		})
   162  
   163  		When("the space and domain exist", func() {
   164  			var (
   165  				userName   string
   166  				domainName string
   167  			)
   168  
   169  			BeforeEach(func() {
   170  				domainName = helpers.NewDomainName()
   171  				userName, _ = helpers.GetCredentials()
   172  			})
   173  
   174  			When("the route already exists", func() {
   175  				var domain helpers.Domain
   176  
   177  				BeforeEach(func() {
   178  					domain = helpers.NewDomain(orgName, domainName)
   179  					domain.Create()
   180  					Eventually(helpers.CF("create-route", spaceName, domainName)).Should(Exit(0))
   181  				})
   182  
   183  				AfterEach(func() {
   184  					domain.Delete()
   185  				})
   186  
   187  				It("warns the user that it has already been created and runs to completion without failing", func() {
   188  					session := helpers.CF("create-route", spaceName, domainName)
   189  					Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   190  					Eventually(session.Err).Should(Say(`Route %s already exists\.`, domainName))
   191  					Eventually(session).Should(Say(`OK`))
   192  					Eventually(session).Should(Exit(0))
   193  				})
   194  			})
   195  
   196  			When("the route already exists in a different space", func() {
   197  				var domain helpers.Domain
   198  
   199  				BeforeEach(func() {
   200  					domain = helpers.NewDomain(orgName, domainName)
   201  					domain.Create()
   202  					differentSpaceName := helpers.NewSpaceName()
   203  					helpers.CreateSpace(differentSpaceName)
   204  					Eventually(helpers.CF("create-route", differentSpaceName, domainName)).Should(Exit(0))
   205  				})
   206  
   207  				AfterEach(func() {
   208  					domain.Delete()
   209  				})
   210  
   211  				It("warns the user that the route is already in use and then fails", func() {
   212  					session := helpers.CF("create-route", spaceName, domainName)
   213  					Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   214  					Eventually(session.Err).Should(Say("The app cannot be mapped to route %s because the route exists in a different space.", domainName))
   215  					Eventually(session).Should(Say(`FAILED`))
   216  					Eventually(session).Should(Exit(1))
   217  				})
   218  			})
   219  
   220  			When("the route does not already exist", func() {
   221  				When("the domain is private", func() {
   222  					var domain helpers.Domain
   223  
   224  					BeforeEach(func() {
   225  						domain = helpers.NewDomain(orgName, domainName)
   226  						domain.Create()
   227  						Eventually(helpers.CF("create-route", spaceName, domainName)).Should(Exit(0))
   228  					})
   229  
   230  					AfterEach(func() {
   231  						domain.Delete()
   232  					})
   233  
   234  					When("no flags are used", func() {
   235  						It("creates the route", func() {
   236  							session := helpers.CF("create-route", spaceName, domainName)
   237  							Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   238  							Eventually(session).Should(Exit(0))
   239  						})
   240  					})
   241  
   242  					When("the path is provided but the hostname is not", func() {
   243  						var path string
   244  
   245  						BeforeEach(func() {
   246  							path = helpers.PrefixedRandomName("path")
   247  						})
   248  
   249  						It("creates the route", func() {
   250  							session := helpers.CF("create-route", spaceName, domainName, "--path", path)
   251  							Eventually(session).Should(Say(`Creating route %s/%s for org %s / space %s as %s\.\.\.`, domainName, path, orgName, spaceName, userName))
   252  							Eventually(session).Should(Exit(0))
   253  						})
   254  					})
   255  				})
   256  
   257  				When("the domain is a shared HTTP domain", func() {
   258  					var domain helpers.Domain
   259  
   260  					BeforeEach(func() {
   261  						domain = helpers.NewDomain(orgName, domainName)
   262  						domain.CreateShared()
   263  					})
   264  
   265  					AfterEach(func() {
   266  						domain.DeleteShared()
   267  					})
   268  
   269  					When("no flags are used", func() {
   270  						When("the domain already has some routes", func() {
   271  							var hostName string
   272  
   273  							BeforeEach(func() {
   274  								hostName = helpers.PrefixedRandomName("my-host")
   275  								Eventually(helpers.CF("create-route", spaceName, domainName, "--hostname", hostName)).Should(Exit(0))
   276  							})
   277  
   278  							It("fails with error message informing users to provide a port or random-port", func() {
   279  								session := helpers.CF("create-route", spaceName, domainName)
   280  								Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   281  								Eventually(session).Should(Say(`FAILED`))
   282  								Eventually(session.Err).Should(Say(`The route is invalid: host is required for shared-domains`))
   283  								Eventually(session).Should(Exit(1))
   284  							})
   285  						})
   286  
   287  						It("fails with an error message and exits 1", func() {
   288  							session := helpers.CF("create-route", spaceName, domainName)
   289  							Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   290  							Eventually(session.Err).Should(Say(`The route is invalid: host is required for shared-domains`))
   291  							Eventually(session).Should(Exit(1))
   292  						})
   293  					})
   294  
   295  					When("TCP flag options are provided", func() {
   296  						It("fails with an error message and exits 1", func() {
   297  							port := "90230"
   298  							session := helpers.CF("create-route", spaceName, domainName, "--port", port)
   299  							Eventually(session).Should(Say(`Creating route %s:%s for org %s / space %s as %s\.\.\.`, domainName, port, orgName, spaceName, userName))
   300  							Eventually(session.Err).Should(Say(`Port not allowed in HTTP domain %s`, domainName))
   301  							Eventually(session).Should(Exit(1))
   302  						})
   303  
   304  						It("fails with an error message and exits 1", func() {
   305  							session := helpers.CF("create-route", spaceName, domainName, "--random-port")
   306  							Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   307  							Eventually(session.Err).Should(Say(`Port not allowed in HTTP domain %s`, domainName))
   308  							Eventually(session).Should(Exit(1))
   309  						})
   310  					})
   311  
   312  					When("the hostname is provided", func() {
   313  						var hostName string
   314  
   315  						BeforeEach(func() {
   316  							hostName = helpers.PrefixedRandomName("my-host")
   317  						})
   318  
   319  						When("no path is provided", func() {
   320  							It("creates the route", func() {
   321  								session := helpers.CF("create-route", spaceName, domainName, "--hostname", hostName)
   322  								Eventually(session).Should(Say(`Creating route %s.%s for org %s / space %s as %s\.\.\.`, hostName, domainName, orgName, spaceName, userName))
   323  								Eventually(session).Should(Exit(0))
   324  							})
   325  						})
   326  
   327  						When("a path is provided", func() {
   328  							It("creates the route", func() {
   329  								path := fmt.Sprintf("/%s", helpers.PrefixedRandomName("path"))
   330  								session := helpers.CF("create-route", spaceName, domainName, "--hostname", hostName, "--path", path)
   331  								Eventually(session).Should(Say(`Creating route %s.%s%s for org %s / space %s as %s\.\.\.`, hostName, domainName, path, orgName, spaceName, userName))
   332  								Eventually(session).Should(Exit(0))
   333  							})
   334  						})
   335  					})
   336  
   337  					When("the hostname is not provided", func() {
   338  						var path string
   339  
   340  						BeforeEach(func() {
   341  							path = helpers.PrefixedRandomName("path")
   342  						})
   343  
   344  						When("the path is provided", func() {
   345  							It("fails with an error message and exits 1", func() {
   346  								session := helpers.CF("create-route", spaceName, domainName, "--path", path)
   347  								Eventually(session).Should(Say(`Creating route %s/%s for org %s / space %s as %s\.\.\.`, domainName, path, orgName, spaceName, userName))
   348  								Eventually(session.Err).Should(Say(`The route is invalid: host is required for shared-domains`))
   349  								Eventually(session).Should(Exit(1))
   350  							})
   351  						})
   352  					})
   353  				})
   354  			})
   355  		})
   356  	})
   357  })