github.com/sleungcy/cli@v7.1.0+incompatible/integration/v7/isolated/create_route_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     7  	"code.cloudfoundry.org/cli/integration/helpers"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gbytes"
    11  	. "github.com/onsi/gomega/gexec"
    12  )
    13  
    14  var _ = Describe("create-route command", func() {
    15  	Context("Help", func() {
    16  		It("appears in cf help -a", func() {
    17  			session := helpers.CF("help", "-a")
    18  			Eventually(session).Should(Exit(0))
    19  			Expect(session).To(HaveCommandInCategoryWithDescription("create-route", "ROUTES", "Create a route for later use"))
    20  		})
    21  
    22  		It("displays the help information", func() {
    23  			session := helpers.CF("create-route", "--help")
    24  			Eventually(session).Should(Say(`NAME:`))
    25  			Eventually(session).Should(Say(`create-route - Create a route for later use\n`))
    26  			Eventually(session).Should(Say(`\n`))
    27  
    28  			Eventually(session).Should(Say(`USAGE:`))
    29  			Eventually(session).Should(Say(`Create an HTTP route:\n`))
    30  			Eventually(session).Should(Say(`cf create-route DOMAIN \[--hostname HOSTNAME\] \[--path PATH\]\n`))
    31  			Eventually(session).Should(Say(`Create a TCP route:\n`))
    32  			Eventually(session).Should(Say(`cf create-route DOMAIN \[--port PORT\]\n`))
    33  			Eventually(session).Should(Say(`\n`))
    34  
    35  			Eventually(session).Should(Say(`EXAMPLES:`))
    36  			Eventually(session).Should(Say(`cf create-route example.com\s+# example.com`))
    37  			Eventually(session).Should(Say(`cf create-route example.com --hostname myapp\s+# myapp.example.com`))
    38  			Eventually(session).Should(Say(`cf create-route example.com --hostname myapp --path foo\s+# myapp.example.com/foo`))
    39  			Eventually(session).Should(Say(`cf create-route example.com --port 5000\s+# example.com:5000`))
    40  			Eventually(session).Should(Say(`\n`))
    41  
    42  			Eventually(session).Should(Say(`OPTIONS:`))
    43  			Eventually(session).Should(Say(`--hostname, -n\s+Hostname for the HTTP route \(required for shared domains\)`))
    44  			Eventually(session).Should(Say(`--path\s+Path for the HTTP route`))
    45  			Eventually(session).Should(Say(`--port\s+Port for the TCP route \(default: random port\)`))
    46  			Eventually(session).Should(Say(`\n`))
    47  
    48  			Eventually(session).Should(Say(`SEE ALSO:`))
    49  			Eventually(session).Should(Say(`check-route, domains, map-route, routes, unmap-route`))
    50  
    51  			Eventually(session).Should(Exit(0))
    52  		})
    53  	})
    54  
    55  	When("the environment is not setup correctly", func() {
    56  		It("fails with the appropriate errors", func() {
    57  			helpers.CheckEnvironmentTargetedCorrectly(true, false, ReadOnlyOrg, "create-route", "some-domain")
    58  		})
    59  	})
    60  
    61  	When("the environment is set up correctly", func() {
    62  		var (
    63  			orgName   string
    64  			spaceName string
    65  		)
    66  
    67  		BeforeEach(func() {
    68  			orgName = helpers.NewOrgName()
    69  			spaceName = helpers.NewSpaceName()
    70  
    71  			helpers.SetupCF(orgName, spaceName)
    72  		})
    73  
    74  		AfterEach(func() {
    75  			helpers.QuickDeleteOrg(orgName)
    76  		})
    77  
    78  		When("the space and domain exist", func() {
    79  			var (
    80  				userName   string
    81  				domainName string
    82  			)
    83  
    84  			BeforeEach(func() {
    85  				domainName = helpers.NewDomainName()
    86  				userName, _ = helpers.GetCredentials()
    87  			})
    88  
    89  			When("the route already exists", func() {
    90  				var (
    91  					domain   helpers.Domain
    92  					hostname string
    93  				)
    94  
    95  				BeforeEach(func() {
    96  					domain = helpers.NewDomain(orgName, domainName)
    97  					hostname = "key-lime-pie"
    98  					domain.CreatePrivate()
    99  					Eventually(helpers.CF("create-route", domainName, "--hostname", hostname)).Should(Exit(0))
   100  				})
   101  
   102  				AfterEach(func() {
   103  					domain.Delete()
   104  				})
   105  
   106  				It("warns the user that it has already been created and runs to completion without failing", func() {
   107  					session := helpers.CF("create-route", domainName, "--hostname", hostname)
   108  					Eventually(session).Should(Say(`Creating route %s\.%s for org %s / space %s as %s\.\.\.`, hostname, domainName, orgName, spaceName, userName))
   109  					Eventually(session.Err).Should(Say(`Route already exists with host '%s' for domain '%s'\.`, hostname, domainName))
   110  					Eventually(session).Should(Say(`OK`))
   111  					Eventually(session).Should(Exit(0))
   112  				})
   113  			})
   114  
   115  			When("the route does not already exist", func() {
   116  				When("the domain is private", func() {
   117  					var domain helpers.Domain
   118  
   119  					BeforeEach(func() {
   120  						domain = helpers.NewDomain(orgName, domainName)
   121  						domain.Create()
   122  					})
   123  
   124  					AfterEach(func() {
   125  						domain.Delete()
   126  					})
   127  
   128  					When("no flags are used", func() {
   129  						It("creates the route", func() {
   130  							session := helpers.CF("create-route", domainName)
   131  							Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   132  							Eventually(session).Should(Say(`Route %s has been created\.`, domainName))
   133  							Eventually(session).Should(Exit(0))
   134  						})
   135  					})
   136  
   137  					When("passing in a hostname", func() {
   138  						It("creates the route with the hostname", func() {
   139  							hostname := "tiramisu"
   140  							session := helpers.CF("create-route", domainName, "-n", hostname)
   141  							Eventually(session).Should(Say(`Creating route %s\.%s for org %s / space %s as %s\.\.\.`, hostname, domainName, orgName, spaceName, userName))
   142  							Eventually(session).Should(Say(`Route %s\.%s has been created\.`, hostname, domainName))
   143  							Eventually(session).Should(Exit(0))
   144  						})
   145  					})
   146  
   147  					When("passing in hostname and path with a leading '/'", func() {
   148  						It("creates the route with hostname and path", func() {
   149  							hostname := "tiramisu"
   150  							pathString := "/recipes"
   151  							session := helpers.CF("create-route", domainName, "-n", hostname, "--path", pathString)
   152  							Eventually(session).Should(Say(`Creating route %s\.%s%s for org %s / space %s as %s\.\.\.`, hostname, domainName, pathString, orgName, spaceName, userName))
   153  							Eventually(session).Should(Say(`Route %s\.%s%s has been created\.`, hostname, domainName, pathString))
   154  							Eventually(session).Should(Exit(0))
   155  						})
   156  					})
   157  
   158  					When("passing in hostname and path without a leading '/'", func() {
   159  						It("creates the route with hostname and path", func() {
   160  							hostname := "tiramisu"
   161  							pathString := "more-recipes"
   162  							session := helpers.CF("create-route", domainName, "-n", hostname, "--path", pathString)
   163  							Eventually(session).Should(Say(`Creating route %s\.%s\/%s for org %s / space %s as %s\.\.\.`, hostname, domainName, pathString, orgName, spaceName, userName))
   164  							Eventually(session).Should(Say(`Route %s\.%s\/%s has been created\.`, hostname, domainName, pathString))
   165  							Eventually(session).Should(Exit(0))
   166  						})
   167  					})
   168  				})
   169  
   170  				When("the domain is a shared HTTP domain", func() {
   171  					var domain helpers.Domain
   172  
   173  					BeforeEach(func() {
   174  						domain = helpers.NewDomain("", domainName)
   175  						domain.CreateShared()
   176  					})
   177  
   178  					AfterEach(func() {
   179  						domain.DeleteShared()
   180  					})
   181  
   182  					When("no flags are used", func() {
   183  						It("errors indicating that hostname is missing", func() {
   184  							session := helpers.CF("create-route", domainName)
   185  							Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   186  							Eventually(session.Out).Should(Say(`FAILED`))
   187  							Eventually(session.Err).Should(Say(`Missing host. Routes in shared domains must have a host defined.`))
   188  							Eventually(session).Should(Exit(1))
   189  						})
   190  					})
   191  
   192  					When("passing in a hostname", func() {
   193  						It("creates the route with the hostname", func() {
   194  							hostname := "tiramisu"
   195  							session := helpers.CF("create-route", domainName, "-n", hostname)
   196  							Eventually(session).Should(Say(`Creating route %s\.%s for org %s / space %s as %s\.\.\.`, hostname, domainName, orgName, spaceName, userName))
   197  							Eventually(session).Should(Say(`Route %s\.%s has been created\.`, hostname, domainName))
   198  							Eventually(session).Should(Exit(0))
   199  						})
   200  					})
   201  
   202  					When("passing in a hostname and path", func() {
   203  						It("creates the route with the hostname", func() {
   204  							hostname := "tiramisu"
   205  							path := "lion"
   206  							session := helpers.CF("create-route", domainName, "-n", hostname, "--path", path)
   207  							Eventually(session).Should(Say(`Creating route %s\.%s\/%s for org %s / space %s as %s\.\.\.`, hostname, domainName, path, orgName, spaceName, userName))
   208  							Eventually(session).Should(Say(`Route %s\.%s\/%s has been created\.`, hostname, domainName, path))
   209  							Eventually(session).Should(Exit(0))
   210  						})
   211  					})
   212  				})
   213  
   214  				When("the domain is a shared TCP domain", func() {
   215  					var (
   216  						domain      helpers.Domain
   217  						routerGroup helpers.RouterGroup
   218  					)
   219  
   220  					BeforeEach(func() {
   221  						domain = helpers.NewDomain("", domainName)
   222  						routerGroup = helpers.NewRouterGroup(
   223  							helpers.NewRouterGroupName(),
   224  							"1024-2048",
   225  						)
   226  
   227  						routerGroup.Create()
   228  						domain.CreateWithRouterGroup(routerGroup.Name)
   229  					})
   230  
   231  					AfterEach(func() {
   232  						domain.DeleteShared()
   233  						routerGroup.Delete()
   234  					})
   235  
   236  					When("passing in a port", func() {
   237  						It("creates the route with the port", func() {
   238  							port := 1029
   239  							session := helpers.CF("create-route", domainName, "--port", fmt.Sprintf("%d", port))
   240  							Eventually(session).Should(Say(`Creating route %s:%d for org %s / space %s as %s\.\.\.`, domainName, port, orgName, spaceName, userName))
   241  							Eventually(session).Should(Say(`Route %s:%d has been created\.`, domainName, port))
   242  							Eventually(session).Should(Exit(0))
   243  						})
   244  					})
   245  				})
   246  			})
   247  		})
   248  
   249  		When("the domain does not exist", func() {
   250  			It("displays error and exits 1", func() {
   251  				session := helpers.CF("create-route", "some-domain")
   252  				Eventually(session).Should(Say(`FAILED`))
   253  				Eventually(session.Err).Should(Say(`Domain 'some-domain' not found.`))
   254  				Eventually(session).Should(Exit(1))
   255  			})
   256  		})
   257  
   258  		When("the domain is not specified", func() {
   259  			It("displays error and exits 1", func() {
   260  				session := helpers.CF("create-route")
   261  				Eventually(session.Err).Should(Say("Incorrect Usage: the required argument `DOMAIN` was not provided\n"))
   262  				Eventually(session.Err).Should(Say("\n"))
   263  				Eventually(session).Should(Say("NAME:\n"))
   264  				Eventually(session).Should(Exit(1))
   265  			})
   266  		})
   267  	})
   268  })