github.com/loafoe/cli@v7.1.0+incompatible/integration/helpers/route.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "path" 6 "regexp" 7 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gbytes" 10 . "github.com/onsi/gomega/gexec" 11 ) 12 13 // MinTestPort should be defined by the CF router group for integration tests. 14 const MinTestPort = 1024 15 16 // MaxTestPort should be defined by the CF router group for integration tests. 17 const MaxTestPort = 1034 18 19 // FindOrCreateTCPRouterGroup uses the routing API to find a router group with name 20 // INTEGRATION-TCP-NODE-<node>, or create one if it does not exist. Returns the name of 21 // the router group. 22 func FindOrCreateTCPRouterGroup(node int) string { 23 routerGroupName := fmt.Sprintf("INTEGRATION-TCP-NODE-%d", node) 24 25 session := CF("curl", fmt.Sprintf("/routing/v1/router_groups?name=%s", routerGroupName)) 26 Eventually(session).Should(Exit(0)) 27 doesNotExist := regexp.MustCompile("ResourceNotFoundError") 28 if doesNotExist.Match(session.Out.Contents()) { 29 jsonBody := fmt.Sprintf(`{"name": "%s", "type": "tcp", "reservable_ports": "%d-%d"}`, routerGroupName, MinTestPort, MaxTestPort) 30 session := CF("curl", "-d", jsonBody, "-X", "POST", "/routing/v1/router_groups") 31 Eventually(session).Should(Say(`"name":\s*"%s"`, routerGroupName)) 32 Eventually(session).Should(Say(`"type":\s*"tcp"`)) 33 Eventually(session).Should(Exit(0)) 34 } 35 36 return routerGroupName 37 } 38 39 // Route represents a route. 40 type Route struct { 41 Domain string 42 Host string 43 Path string 44 Port int 45 Space string 46 } 47 48 // NewRoute constructs a route with given space, domain, hostname, and path. 49 func NewRoute(space string, domain string, hostname string, path string) Route { 50 return Route{ 51 Space: space, 52 Domain: domain, 53 Host: hostname, 54 Path: path, 55 } 56 } 57 58 // NewTCPRoute constructs a TCP route with given space, domain, and port. 59 func NewTCPRoute(space string, domain string, port int) Route { 60 return Route{ 61 Space: space, 62 Domain: domain, 63 Port: port, 64 } 65 } 66 67 // Create creates a route using the 'cf create-route' command. 68 func (r Route) Create() { 69 if r.Port != 0 { 70 Eventually(CF("create-route", r.Space, r.Domain, "--port", fmt.Sprint(r.Port))).Should(Exit(0)) 71 } else { 72 Eventually(CF("create-route", r.Space, r.Domain, "--hostname", r.Host, "--path", r.Path)).Should(Exit(0)) 73 } 74 } 75 76 // Create creates a route using the 'cf create-route' command. 77 func (r Route) V7Create() { 78 if r.Port != 0 { 79 Eventually(CF("create-route", r.Domain, "--port", fmt.Sprint(r.Port))).Should(Exit(0)) 80 } else { 81 Eventually(CF("create-route", r.Domain, "--hostname", r.Host, "--path", r.Path)).Should(Exit(0)) 82 } 83 } 84 85 // Delete deletes a route using the 'cf delete-route' command. 86 func (r Route) Delete() { 87 if r.Port != 0 { 88 Eventually(CF("delete-route", r.Domain, "--port", fmt.Sprint(r.Port))).Should(Exit(0)) 89 } else { 90 Eventually(CF("delete-route", r.Domain, "--hostname", r.Host, "--path", r.Path, "-f")).Should(Exit(0)) 91 } 92 } 93 94 // String stringifies a route (e.g. "host.domain.com:port/path") 95 func (r Route) String() string { 96 routeString := r.Domain 97 98 if r.Port != 0 { 99 routeString = fmt.Sprintf("%s:%d", routeString, r.Port) 100 } 101 102 if r.Host != "" { 103 routeString = fmt.Sprintf("%s.%s", r.Host, routeString) 104 } 105 106 if r.Path != "" { 107 routeString = path.Join(routeString, r.Path) 108 } 109 110 return routeString 111 }