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