github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+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  func FindOrCreateTCPRouterGroup(node int) string {
    20  	routerGroupName := fmt.Sprintf("INTEGRATION-TCP-NODE-%d", node)
    21  
    22  	session := CF("curl", fmt.Sprintf("/routing/v1/router_groups?name=%s", routerGroupName))
    23  	Eventually(session).Should(Exit(0))
    24  	doesNotExist := regexp.MustCompile("ResourceNotFoundError")
    25  	if doesNotExist.Match(session.Out.Contents()) {
    26  		jsonBody := fmt.Sprintf(`{"name": "%s", "type": "tcp", "reservable_ports": "%d-%d"}`, routerGroupName, MinTestPort, MaxTestPort)
    27  		session := CF("curl", "-d", jsonBody, "-X", "POST", "/routing/v1/router_groups")
    28  		Eventually(session).Should(Say(`"name":\s*"%s"`, routerGroupName))
    29  		Eventually(session).Should(Say(`"type":\s*"tcp"`))
    30  		Eventually(session).Should(Exit(0))
    31  	}
    32  
    33  	return routerGroupName
    34  }
    35  
    36  type Route struct {
    37  	Domain string
    38  	Host   string
    39  	Path   string
    40  	Port   int
    41  	Space  string
    42  }
    43  
    44  func NewRoute(space string, domain string, hostname string, path string) Route {
    45  	return Route{
    46  		Space:  space,
    47  		Domain: domain,
    48  		Host:   hostname,
    49  		Path:   path,
    50  	}
    51  }
    52  
    53  func NewTCPRoute(space string, domain string, port int) Route {
    54  	return Route{
    55  		Space:  space,
    56  		Domain: domain,
    57  		Port:   port,
    58  	}
    59  }
    60  
    61  func (r Route) Create() {
    62  	if r.Port != 0 {
    63  		Eventually(CF("create-route", r.Space, r.Domain, "--port", fmt.Sprint(r.Port))).Should(Exit(0))
    64  	} else {
    65  		Eventually(CF("create-route", r.Space, r.Domain, "--hostname", r.Host, "--path", r.Path)).Should(Exit(0))
    66  	}
    67  }
    68  
    69  func (r Route) Delete() {
    70  	if r.Port != 0 {
    71  		Eventually(CF("delete-route", r.Domain, "--port", fmt.Sprint(r.Port))).Should(Exit(0))
    72  	} else {
    73  		Eventually(CF("delete-route", r.Domain, "--hostname", r.Host, "--path", r.Path, "-f")).Should(Exit(0))
    74  	}
    75  }
    76  
    77  func (r Route) String() string {
    78  	routeString := r.Domain
    79  
    80  	if r.Port != 0 {
    81  		routeString = fmt.Sprintf("%s:%d", routeString, r.Port)
    82  	}
    83  
    84  	if r.Host != "" {
    85  		routeString = fmt.Sprintf("%s.%s", r.Host, routeString)
    86  	}
    87  
    88  	if r.Path != "" {
    89  		routeString = path.Join(routeString, r.Path)
    90  	}
    91  
    92  	return routeString
    93  }