Skip to content
This repository was archived by the owner on Aug 21, 2025. It is now read-only.

Commit 2f2bc5c

Browse files
committed
feat(cfd) add route mapping functionality & integration test
1 parent d92e49e commit 2f2bc5c

4 files changed

Lines changed: 105 additions & 12 deletions

File tree

runner-manager/cfd/cloudgov/cf_client.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func castApp(app *resource.App) *App {
7373
if app == nil || app.GUID == "" {
7474
return nil
7575
}
76-
return &(App{Name: app.Name, GUID: app.GUID, State: app.State})
76+
return &(App{Name: app.Name, GUID: app.GUID, State: app.State, SpaceGUID: app.Relationships.Space.Data.GUID})
7777
}
7878

7979
func castApps(apps []*resource.App) []*App {
@@ -109,3 +109,25 @@ func (cf *CFClientAPI) sshCode() (string, error) {
109109
ctx := context.Background()
110110
return cf.conn().SSHCode(ctx)
111111
}
112+
113+
func (cf *CFClientAPI) mapRoute(
114+
ctx context.Context,
115+
app *App,
116+
domain string, space string, host string, path string, port int,
117+
) error {
118+
opts := resource.NewRouteCreateWithHost(domain, space, host, path, port)
119+
120+
route, err := cf.conn().Routes.Create(ctx, opts)
121+
if err != nil {
122+
return err
123+
}
124+
125+
_, err = cf.conn().Routes.InsertDestinations(
126+
ctx,
127+
route.GUID,
128+
[]*resource.RouteDestinationInsertOrReplace{{
129+
App: resource.RouteDestinationApp{GUID: &app.GUID},
130+
}},
131+
)
132+
return err
133+
}

runner-manager/cfd/cloudgov/cloudgov.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package cloudgov
22

3-
// Stuff we'll need to implement, for ref
4-
//
5-
// mapRoute()
6-
//
7-
// addNetworkPolicy()
8-
// removeNetworkPolicy()
3+
import "context"
4+
95
type ClientAPI interface {
106
connect(url string, creds *Creds) error
117

@@ -15,6 +11,8 @@ type ClientAPI interface {
1511
appsList() (apps []*App, err error)
1612

1713
sshCode() (string, error)
14+
mapRoute(ctx context.Context, app *App, domain string, space string, host string, path string, port int) error
15+
addNetworkPolicy(app *App, dest string, space string, port string) error
1816
}
1917

2018
type CredsGetter interface {
@@ -42,7 +40,10 @@ func (e CloudGovClientError) Error() string {
4240
}
4341

4442
// TODO: we should pull this out of VCAP_APPLICATION
45-
const apiRootURLDefault = "https://api.fr-stage.cloud.gov"
43+
const (
44+
apiRootURLDefault = "https://api.fr-stage.cloud.gov"
45+
internalDomainGUID = "8a5d6a8c-cfc1-4fc4-afc9-aa563ff9df5e"
46+
)
4647

4748
func New(i ClientAPI, o *Opts) (*Client, error) {
4849
if o == nil {
@@ -78,9 +79,10 @@ func (c *Client) Connect() (*Client, error) {
7879
}
7980

8081
type App struct {
81-
Name string
82-
GUID string
83-
State string
82+
Name string
83+
GUID string
84+
State string
85+
SpaceGUID string
8486
}
8587

8688
func (c *Client) AppGet(id string) (*App, error) {
@@ -133,3 +135,7 @@ func (c *Client) ServicesPush(manifests []*AppManifest) ([]*App, error) {
133135
func (c *Client) SSHCode() (string, error) {
134136
return c.sshCode()
135137
}
138+
139+
func (c *Client) MapServiceRoute(app *App) error {
140+
return c.mapRoute(context.Background(), app, internalDomainGUID, app.SpaceGUID, app.Name, "", 0)
141+
}

runner-manager/cfd/cloudgov/cloudgov_integration_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
package cloudgov_test
44

55
import (
6+
"encoding/json"
67
"errors"
8+
"fmt"
9+
"os/exec"
710
"regexp"
811
"testing"
912

@@ -112,3 +115,65 @@ func Test_SSHCode(t *testing.T) {
112115
t.Errorf("wanted string matching /%v/, got %v", re, got)
113116
}
114117
}
118+
119+
func cleanupRoute(t testing.TB, app *cg.App) error {
120+
t.Helper()
121+
122+
delRouteCmd := exec.Command(
123+
"cf", "delete-route", "-f", "apps.internal",
124+
fmt.Sprintf("-n%s", app.Name),
125+
)
126+
127+
out, err := delRouteCmd.CombinedOutput()
128+
if err != nil {
129+
t.Log(string(out))
130+
if exErr, ok := err.(*exec.ExitError); ok {
131+
t.Log(exErr.Error())
132+
t.Fatal(string(exErr.Stderr))
133+
} else {
134+
t.Fatal(err)
135+
}
136+
}
137+
138+
return err
139+
}
140+
141+
func TestClient_MapServiceRoute(t *testing.T) {
142+
setup(t)
143+
144+
apps, err := cgClient.AppsList()
145+
if err != nil {
146+
t.Fatal(err)
147+
}
148+
app := apps[0]
149+
150+
err = cgClient.MapServiceRoute(app)
151+
defer cleanupRoute(t, app)
152+
if err != nil {
153+
t.Fatal(err)
154+
}
155+
156+
ckRouteCmd := exec.Command("cf", "curl", fmt.Sprintf("/v3/apps/%s/routes", app.GUID))
157+
out, err := ckRouteCmd.CombinedOutput()
158+
if err != nil {
159+
t.Log(out)
160+
t.Fatal(err)
161+
}
162+
163+
var routeOut map[string][]map[string]string
164+
if err := json.Unmarshal(out, &routeOut); err != nil {
165+
t.Log("partial unmarshalling error expected…")
166+
t.Log(err)
167+
}
168+
169+
wantUrl := fmt.Sprintf("%s.apps.internal", app.Name)
170+
171+
for _, m := range routeOut["resources"] {
172+
if m["host"] == app.Name && m["url"] == wantUrl {
173+
return
174+
}
175+
}
176+
177+
t.Logf("%#v", routeOut["resources"])
178+
t.Fatalf("could not find route with %s host and correct url", app.Name)
179+
}

runner-manager/cfd/sh/integration_setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ if [[ -z "$skip_create" ]]; then
7070
fi
7171

7272
# create a teeny app we can use to test client.AppGet
73-
cf push -k 8M -m 128M -o busybox -u process -c /bin/sh "$app_name"
73+
cf push --no-route -k 8M -m 128M -o busybox -u process -c /bin/sh "$app_name"
7474

7575
out_arr=(
7676
# get the credentials from key and output

0 commit comments

Comments
 (0)