github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+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  }
    94  
    95  type Domain struct {
    96  	Org  string
    97  	Name string
    98  }
    99  
   100  func NewDomain(org string, name string) Domain {
   101  	return Domain{
   102  		Org:  org,
   103  		Name: name,
   104  	}
   105  }
   106  
   107  // globally cached
   108  var foundDefaultDomain string
   109  
   110  func DefaultSharedDomain() string {
   111  	if foundDefaultDomain == "" {
   112  		session := CF("domains")
   113  		Eventually(session).Should(Exit(0))
   114  
   115  		regex, err := regexp.Compile(`(.+?)\s+shared`)
   116  		Expect(err).ToNot(HaveOccurred())
   117  
   118  		matches := regex.FindStringSubmatch(string(session.Out.Contents()))
   119  		Expect(matches).To(HaveLen(2))
   120  
   121  		foundDefaultDomain = matches[1]
   122  	}
   123  	return foundDefaultDomain
   124  }
   125  
   126  func (d Domain) Create() {
   127  	Eventually(CF("create-domain", d.Org, d.Name)).Should(Exit(0))
   128  	Eventually(CF("domains")).Should(And(Exit(0), Say(d.Name)))
   129  }
   130  
   131  func (d Domain) CreateShared() {
   132  	Eventually(CF("create-shared-domain", d.Name)).Should(Exit(0))
   133  }
   134  
   135  func (d Domain) CreateWithRouterGroup(routerGroup string) {
   136  	Eventually(CF("create-shared-domain", d.Name, "--router-group", routerGroup)).Should(Exit(0))
   137  }
   138  
   139  func (d Domain) Share() {
   140  	Eventually(CF("share-private-domain", d.Org, d.Name)).Should(Exit(0))
   141  }
   142  
   143  func (d Domain) Delete() {
   144  	Eventually(CF("delete-domain", d.Name, "-f")).Should(Exit(0))
   145  }
   146  
   147  func (d Domain) DeleteShared() {
   148  	Eventually(CF("delete-shared-domain", d.Name, "-f")).Should(Exit(0))
   149  }