github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/isolated/map_route_command_test.go (about)

     1  package isolated
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
     7  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("map-route command", func() {
    17  	Context("help", func() {
    18  		It("appears in cf help -a", func() {
    19  			session := helpers.CF("help", "-a")
    20  			Eventually(session).Should(Exit(0))
    21  			Expect(session).To(HaveCommandInCategoryWithDescription("map-route", "ROUTES", "Map a route to an app"))
    22  		})
    23  
    24  		It("displays command usage to output", func() {
    25  			session := helpers.CF("map-route", "--help")
    26  			Eventually(session).Should(Say(`NAME:`))
    27  			Eventually(session).Should(Say(`map-route - Map a route to an app\n`))
    28  			Eventually(session).Should(Say(`\n`))
    29  
    30  			Eventually(session).Should(Say(`USAGE:`))
    31  			Eventually(session).Should(Say(`Map an HTTP route:\n`))
    32  			Eventually(session).Should(Say(`cf map-route APP_NAME DOMAIN \[--hostname HOSTNAME\] \[--path PATH\] \[--app-protocol PROTOCOL\]\n`))
    33  			Eventually(session).Should(Say(`Map a TCP route:\n`))
    34  			Eventually(session).Should(Say(`cf map-route APP_NAME DOMAIN \[--port PORT]\n`))
    35  			Eventually(session).Should(Say(`\n`))
    36  
    37  			Eventually(session).Should(Say(`EXAMPLES:`))
    38  			Eventually(session).Should(Say(`cf map-route my-app example.com                                                # example.com`))
    39  			Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost                              # myhost.example.com`))
    40  			Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost --path foo                   # myhost.example.com/foo`))
    41  			Eventually(session).Should(Say(`cf map-route my-app example.com --hostname myhost --app-protocol http2 # myhost.example.com`))
    42  			Eventually(session).Should(Say(`cf map-route my-app example.com --port 5000                                    # example.com:5000`))
    43  			Eventually(session).Should(Say(`\n`))
    44  
    45  			Eventually(session).Should(Say(`OPTIONS:`))
    46  			Eventually(session).Should(Say(`--hostname, -n\s+Hostname for the HTTP route \(required for shared domains\)`))
    47  			Eventually(session).Should(Say(`--path\s+Path for the HTTP route`))
    48  			Eventually(session).Should(Say(`--port\s+Port for the TCP route \(default: random port\)`))
    49  			Eventually(session).Should(Say(`--app-protocol\s+\[Beta flag, subject to change\] Protocol for the route destination \(default: http1\). Only applied to HTTP routes`))
    50  
    51  			Eventually(session).Should(Say(`\n`))
    52  
    53  			Eventually(session).Should(Say(`SEE ALSO:`))
    54  			Eventually(session).Should(Say(`create-route, routes, unmap-route`))
    55  
    56  			Eventually(session).Should(Exit(0))
    57  		})
    58  	})
    59  
    60  	When("the environment is not setup correctly", func() {
    61  		It("fails with the appropriate errors", func() {
    62  			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "map-route", "app-name", "domain-name")
    63  		})
    64  	})
    65  
    66  	When("the environment is set up correctly", func() {
    67  		var (
    68  			orgName    string
    69  			spaceName  string
    70  			domainName string
    71  			hostName   string
    72  			path       string
    73  			userName   string
    74  			appName    string
    75  		)
    76  
    77  		BeforeEach(func() {
    78  			appName = helpers.NewAppName()
    79  			hostName = helpers.NewHostName()
    80  			path = helpers.NewPath()
    81  			orgName = helpers.NewOrgName()
    82  			spaceName = helpers.NewSpaceName()
    83  			helpers.SetupCF(orgName, spaceName)
    84  			userName, _ = helpers.GetCredentials()
    85  
    86  			helpers.WithHelloWorldApp(func(dir string) {
    87  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, "push", appName)
    88  				Eventually(session).Should(Exit(0))
    89  			})
    90  		})
    91  
    92  		When("the http route already exists", func() {
    93  			var (
    94  				route helpers.Route
    95  			)
    96  
    97  			BeforeEach(func() {
    98  				domainName = helpers.DefaultSharedDomain()
    99  				route = helpers.NewRoute(spaceName, domainName, hostName, path)
   100  				route.V7Create()
   101  			})
   102  
   103  			AfterEach(func() {
   104  				route.Delete()
   105  			})
   106  			When("route is already mapped to app", func() {
   107  				BeforeEach(func() {
   108  					session := helpers.CF("map-route", appName, domainName, "--hostname", route.Host, "--path", route.Path)
   109  					Eventually(session).Should(Exit(0))
   110  				})
   111  				It("exits 0 with helpful message saying that the route is already mapped to the app", func() {
   112  					session := helpers.CF("map-route", appName, domainName, "--hostname", route.Host, "--path", route.Path)
   113  
   114  					Eventually(session).Should(Say(`Mapping route %s.%s%s to app %s in org %s / space %s as %s\.\.\.`, hostName, domainName, path, appName, orgName, spaceName, userName))
   115  					Eventually(session).Should(Say(`App '%s' is already mapped to route '%s.%s%s'\. Nothing has been updated\.`, appName, hostName, domainName, path))
   116  					Eventually(session).Should(Say(`OK`))
   117  					Eventually(session).Should(Exit(0))
   118  
   119  				})
   120  			})
   121  			When("route is not yet mapped to the app", func() {
   122  				It("maps the route to an app", func() {
   123  					session := helpers.CF("map-route", appName, domainName, "--hostname", route.Host, "--path", route.Path)
   124  
   125  					Eventually(session).Should(Say(`Mapping route %s.%s%s to app %s in org %s / space %s as %s\.\.\.`, hostName, domainName, path, appName, orgName, spaceName, userName))
   126  					Eventually(session).Should(Say(`OK`))
   127  					Eventually(session).Should(Exit(0))
   128  				})
   129  
   130  				When("destination protocol is provided", func() {
   131  					BeforeEach(func() {
   132  						helpers.SkipIfVersionLessThan(ccversion.MinVersionHTTP2RoutingV3)
   133  					})
   134  
   135  					It("maps the route to an app", func() {
   136  						session := helpers.CF("map-route", appName, domainName, "--hostname", route.Host, "--app-protocol", "http2")
   137  
   138  						Eventually(session).Should(Say(`Mapping route %s.%s to app %s with protocol http2 in org %s / space %s as %s\.\.\.`, hostName, domainName, appName, orgName, spaceName, userName))
   139  						Eventually(session).Should(Say(`OK`))
   140  						Eventually(session).Should(Exit(0))
   141  					})
   142  				})
   143  			})
   144  		})
   145  
   146  		When("the tcp route already exists", func() {
   147  			var (
   148  				domain      helpers.Domain
   149  				routerGroup helpers.RouterGroup
   150  				route       helpers.Route
   151  			)
   152  
   153  			BeforeEach(func() {
   154  				domainName = helpers.NewDomainName()
   155  				domain = helpers.NewDomain("", domainName)
   156  				routerGroup = helpers.NewRouterGroup(
   157  					helpers.NewRouterGroupName(),
   158  					"1024-2048",
   159  				)
   160  
   161  				routerGroup.Create()
   162  				domain.CreateWithRouterGroup(routerGroup.Name)
   163  				route = helpers.NewTCPRoute(spaceName, domainName, 1082)
   164  			})
   165  
   166  			AfterEach(func() {
   167  				domain.DeleteShared()
   168  				routerGroup.Delete()
   169  			})
   170  
   171  			When("route is already mapped to app", func() {
   172  				BeforeEach(func() {
   173  					session := helpers.CF("map-route", appName, domainName, "--port", fmt.Sprintf("%d", route.Port))
   174  					Eventually(session).Should(Exit(0))
   175  				})
   176  				It("exits 0 with helpful message saying that the route is already mapped to the app", func() {
   177  					session := helpers.CF("map-route", appName, domainName, "--port", fmt.Sprintf("%d", route.Port))
   178  
   179  					Eventually(session).Should(Say(`Mapping route %s:%d to app %s in org %s / space %s as %s\.\.\.`, domainName, route.Port, appName, orgName, spaceName, userName))
   180  					Eventually(session).Should(Say(`App '%s' is already mapped to route '%s:%d'\. Nothing has been updated\.`, appName, domainName, route.Port))
   181  					Eventually(session).Should(Say(`OK`))
   182  					Eventually(session).Should(Exit(0))
   183  				})
   184  			})
   185  
   186  			When("route is not yet mapped to the app", func() {
   187  				It("maps the route to an app", func() {
   188  					session := helpers.CF("map-route", appName, domainName, "--port", fmt.Sprintf("%d", route.Port))
   189  
   190  					Eventually(session).Should(Say(`Mapping route %s:%d to app %s in org %s / space %s as %s\.\.\.`, domainName, route.Port, appName, orgName, spaceName, userName))
   191  					Eventually(session).Should(Say(`OK`))
   192  					Eventually(session).Should(Exit(0))
   193  				})
   194  			})
   195  
   196  			When("port is not specified", func() {
   197  				It("creates a new route with a random port and maps it to the app", func() {
   198  					session := helpers.CF("map-route", appName, domainName)
   199  
   200  					Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   201  					Eventually(session).Should(Say(`OK`))
   202  					Eventually(session).Should(Say(`Mapping route %s:[0-9]+ to app %s in org %s / space %s as %s\.\.\.`, domainName, appName, orgName, spaceName, userName))
   203  					Eventually(session).Should(Say(`OK`))
   204  					Eventually(session).Should(Exit(0))
   205  				})
   206  			})
   207  		})
   208  
   209  		When("the route does *not* exist", func() {
   210  			When("it is an HTTP domain", func() {
   211  				BeforeEach(func() {
   212  					domainName = helpers.DefaultSharedDomain()
   213  				})
   214  
   215  				It("creates the route and maps it to an app", func() {
   216  					session := helpers.CF("map-route", appName, domainName, "--hostname", hostName, "--path", path)
   217  					Eventually(session).Should(Say(`Creating route %s.%s%s for org %s / space %s as %s\.\.\.`, hostName, domainName, path, orgName, spaceName, userName))
   218  					Eventually(session).Should(Say(`OK`))
   219  					Eventually(session).Should(Say(`Mapping route %s.%s%s to app %s in org %s / space %s as %s\.\.\.`, hostName, domainName, path, appName, orgName, spaceName, userName))
   220  					Eventually(session).Should(Say(`OK`))
   221  					Eventually(session).Should(Exit(0))
   222  				})
   223  
   224  				When("destination protocol is provided", func() {
   225  					BeforeEach(func() {
   226  						helpers.SkipIfVersionLessThan(ccversion.MinVersionHTTP2RoutingV3)
   227  					})
   228  
   229  					It("maps the route to an app", func() {
   230  						session := helpers.CF("map-route", appName, domainName, "--hostname", hostName, "--app-protocol", "http2")
   231  						Eventually(session).Should(Say(`Creating route %s.%s for org %s / space %s as %s\.\.\.`, hostName, domainName, orgName, spaceName, userName))
   232  						Eventually(session).Should(Say(`OK`))
   233  						Eventually(session).Should(Say(`Mapping route %s.%s to app %s with protocol http2 in org %s / space %s as %s\.\.\.`, hostName, domainName, appName, orgName, spaceName, userName))
   234  						Eventually(session).Should(Say(`OK`))
   235  						Eventually(session).Should(Exit(0))
   236  					})
   237  				})
   238  			})
   239  
   240  			When("it is an TCP domain", func() {
   241  				var (
   242  					domain      helpers.Domain
   243  					routerGroup helpers.RouterGroup
   244  				)
   245  
   246  				BeforeEach(func() {
   247  					domainName = helpers.NewDomainName()
   248  					domain = helpers.NewDomain("", domainName)
   249  					routerGroup = helpers.NewRouterGroup(
   250  						helpers.NewRouterGroupName(),
   251  						"1024-2048",
   252  					)
   253  
   254  					routerGroup.Create()
   255  					domain.CreateWithRouterGroup(routerGroup.Name)
   256  				})
   257  
   258  				AfterEach(func() {
   259  					domain.DeleteShared()
   260  					routerGroup.Delete()
   261  				})
   262  
   263  				When("a port is provided", func() {
   264  					It("creates the route and maps it to an app", func() {
   265  						session := helpers.CF("map-route", appName, domainName, "--port", "1052")
   266  						Eventually(session).Should(Say(`Creating route %s:%s for org %s / space %s as %s\.\.\.`, domainName, "1052", orgName, spaceName, userName))
   267  						Eventually(session).Should(Say(`OK`))
   268  						Eventually(session).Should(Say(`Mapping route %s:%s to app %s in org %s / space %s as %s\.\.\.`, domainName, "1052", appName, orgName, spaceName, userName))
   269  						Eventually(session).Should(Say(`OK`))
   270  						Eventually(session).Should(Exit(0))
   271  					})
   272  				})
   273  
   274  				When("a port is not provided", func() {
   275  					It("creates the route and maps it to an app", func() {
   276  						session := helpers.CF("map-route", appName, domainName)
   277  						Eventually(session).Should(Say(`Creating route %s for org %s / space %s as %s\.\.\.`, domainName, orgName, spaceName, userName))
   278  						Eventually(session).Should(Say(`OK`))
   279  						Eventually(session).Should(Say(`Mapping route %s:[0-9]+ to app %s in org %s / space %s as %s\.\.\.`, domainName, appName, orgName, spaceName, userName))
   280  						Eventually(session).Should(Say(`OK`))
   281  						Eventually(session).Should(Exit(0))
   282  					})
   283  				})
   284  			})
   285  		})
   286  	})
   287  })