Skip to content

Commit 300ae74

Browse files
committed
new
1 parent bb0ad55 commit 300ae74

4 files changed

Lines changed: 70 additions & 86 deletions

File tree

cmd/routes.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io/ioutil"
78
"os"
89

@@ -51,14 +52,14 @@ func (d *DryccCmd) RoutesList(appID string, results int) error {
5152
d.Println("Could not find any route")
5253
} else {
5354
table := tablewriter.NewWriter(d.WOut)
54-
table.SetHeader([]string{"Name", "Type", "Kind", "Gateway"})
55+
table.SetHeader([]string{"Name", "Type", "Kind", "Service Port", "Gateway", "Listener Port"})
5556
for _, route := range routes {
5657
if len(route.ParentRefs) > 0 {
5758
for _, gateway := range route.ParentRefs {
58-
table.Append([]string{route.Name, route.Type, route.Kind, gateway.Name})
59+
table.Append([]string{route.Name, route.Type, route.Kind, fmt.Sprint(route.Port), gateway.Name, fmt.Sprint(gateway.Port)})
5960
}
6061
} else {
61-
table.Append([]string{route.Name, route.Type, route.Kind, ""})
62+
table.Append([]string{route.Name, route.Type, route.Kind, fmt.Sprint(route.Port), "", ""})
6263
}
6364
}
6465
table.SetAutoMergeCellsByColumnIndex([]int{0})

cmd/routes_test.go

Lines changed: 63 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package cmd
33
import (
44
"bytes"
55
"fmt"
6+
"io/ioutil"
67
"net/http"
8+
"os"
79
"testing"
810

911
"github.com/drycc/controller-sdk-go/api"
@@ -58,12 +60,13 @@ func TestRoutesList(t *testing.T) {
5860
"owner": "test",
5961
"updated": "2023-04-19T00:00:00UTC",
6062
"name": "example-go",
61-
"procfile_type": "web",
62-
"kind": "HTTPRoute",
63-
"parent_refs": [
63+
"procfile_type": "web",
64+
"kind": "HTTPRoute",
65+
"port": 80,
66+
"parent_refs": [
6467
{
6568
"name": "example-go",
66-
"sectionName": "example-go-80-http"
69+
"port": 80
6770
}
6871
]
6972
}
@@ -75,78 +78,14 @@ func TestRoutesList(t *testing.T) {
7578
assert.NoError(t, err)
7679

7780
assert.Equal(t, b.String(), `=== foo Routes
78-
+------------+------+-----------+------------+
79-
| NAME | TYPE | KIND | GATEWAY |
80-
+------------+------+-----------+------------+
81-
| example-go | web | HTTPRoute | example-go |
82-
+------------+------+-----------+------------+
81+
+------------+------+-----------+--------------+------------+---------------+
82+
| NAME | TYPE | KIND | SERVICE PORT | GATEWAY | LISTENER PORT |
83+
+------------+------+-----------+--------------+------------+---------------+
84+
| example-go | web | HTTPRoute | 80 | example-go | 80 |
85+
+------------+------+-----------+--------------+------------+---------------+
8386
`, "output")
8487
}
8588

86-
func TestRouteGet(t *testing.T) {
87-
t.Parallel()
88-
cf, server, err := testutil.NewTestServerAndClient()
89-
if err != nil {
90-
t.Fatal(err)
91-
}
92-
defer server.Close()
93-
var b bytes.Buffer
94-
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
95-
96-
server.Mux.HandleFunc("/v2/apps/foo/routes/example-go/", func(w http.ResponseWriter, r *http.Request) {
97-
testutil.SetHeaders(w)
98-
fmt.Fprintf(w, `[
99-
{
100-
"backendRefs": [
101-
{
102-
"kind": "Service",
103-
"name": "py3django3",
104-
"port": 80
105-
}
106-
]
107-
}
108-
]`)
109-
})
110-
111-
err = cmdr.RoutesGet("foo", "example-go")
112-
assert.NoError(t, err)
113-
114-
assert.Equal(t, b.String(), `[
115-
{
116-
"backendRefs": [
117-
{
118-
"kind": "Service",
119-
"name": "py3django3",
120-
"port": 80
121-
}
122-
]
123-
}
124-
]
125-
`, "output")
126-
}
127-
128-
func TestRouteSet(t *testing.T) {
129-
t.Parallel()
130-
cf, server, err := testutil.NewTestServerAndClient()
131-
if err != nil {
132-
t.Fatal(err)
133-
}
134-
defer server.Close()
135-
var b bytes.Buffer
136-
cmdr := DryccCmd{WOut: &b, ConfigFile: cf}
137-
138-
server.Mux.HandleFunc("/v2/apps/foo/routes/example-go/", func(w http.ResponseWriter, r *http.Request) {
139-
testutil.SetHeaders(w)
140-
w.WriteHeader(http.StatusNoContent)
141-
})
142-
rules := `"[{\"backendRefs\": [{\"kind\": \"Service\",\"name\": \"py3django3\",\"port\": 80}]}]"`
143-
144-
err = cmdr.RoutesSet("foo", "example-go", rules)
145-
assert.NoError(t, err)
146-
147-
assert.Equal(t, testutil.StripProgress(b.String()), "Applying rules... done\n", "output")
148-
}
149-
15089
func TestRoutesAttach(t *testing.T) {
15190
t.Parallel()
15291
cf, server, err := testutil.NewTestServerAndClient()
@@ -194,7 +133,7 @@ func TestRoutesDetach(t *testing.T) {
194133
assert.Equal(t, testutil.StripProgress(b.String()), "Detaching route example-go to gateway example-go... done\n", "output")
195134
}
196135

197-
func TestRoutesGet(t *testing.T) {
136+
func TestRouteGet(t *testing.T) {
198137
t.Parallel()
199138
cf, server, err := testutil.NewTestServerAndClient()
200139
if err != nil {
@@ -206,15 +145,34 @@ func TestRoutesGet(t *testing.T) {
206145

207146
server.Mux.HandleFunc("/v2/apps/foo/routes/example-go/rules/", func(w http.ResponseWriter, r *http.Request) {
208147
testutil.SetHeaders(w)
209-
w.WriteHeader(http.StatusOK)
210-
// TODO real rule
211-
w.Write([]byte(""))
148+
fmt.Fprintf(w, `[
149+
{
150+
"backendRefs": [
151+
{
152+
"kind": "Service",
153+
"name": "py3django3",
154+
"port": 80
155+
}
156+
]
157+
}
158+
]`)
212159
})
213160

214161
err = cmdr.RoutesGet("foo", "example-go")
215162
assert.NoError(t, err)
216163

217-
assert.Equal(t, testutil.StripProgress(b.String()), "\n", "output")
164+
assert.Equal(t, b.String(), `[
165+
{
166+
"backendRefs": [
167+
{
168+
"kind": "Service",
169+
"name": "py3django3",
170+
"port": 80
171+
}
172+
]
173+
}
174+
]
175+
`, "output")
218176
}
219177

220178
func TestRoutesSet(t *testing.T) {
@@ -232,8 +190,33 @@ func TestRoutesSet(t *testing.T) {
232190
w.WriteHeader(http.StatusNoContent)
233191
w.Write([]byte(""))
234192
})
235-
236-
err = cmdr.RoutesSet("foo", "example-go", "")
193+
ruleFile, err := ioutil.TempFile("", "rules.json")
194+
rules := `
195+
[
196+
{
197+
"backendRefs": [
198+
{
199+
"kind": "Service",
200+
"name": "example-go",
201+
"port": 1234
202+
}
203+
],
204+
"matches": [
205+
{
206+
"path": {
207+
"type": "PathPrefix",
208+
"value": "/get"
209+
}
210+
}
211+
]
212+
}
213+
]`
214+
assert.NoError(t, err)
215+
defer os.Remove(ruleFile.Name())
216+
_, err = ruleFile.Write([]byte(rules))
217+
assert.NoError(t, err)
218+
ruleFile.Close()
219+
err = cmdr.RoutesSet("foo", "example-go", ruleFile.Name())
237220
assert.NoError(t, err)
238221

239222
assert.Equal(t, testutil.StripProgress(b.String()), "Applying rules... done\n", "output")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ require (
2626
golang.org/x/text v0.7.0 // indirect
2727
)
2828

29-
replace github.com/drycc/controller-sdk-go => github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230516092539-830b42c9e810
29+
replace github.com/drycc/controller-sdk-go => github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230517053503-b97a90682782

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWm
1515
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
1616
github.com/goware/urlx v0.3.2 h1:gdoo4kBHlkqZNaf6XlQ12LGtQOmpKJrR04Rc3RnpJEo=
1717
github.com/goware/urlx v0.3.2/go.mod h1:h8uwbJy68o+tQXCGZNa9D73WN8n0r9OBae5bUnLcgjw=
18-
github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230516092539-830b42c9e810 h1:/agIwe7pL7gFCvL08kZnWL4syejqO5/rw0D6ZjGbyIg=
19-
github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230516092539-830b42c9e810/go.mod h1:q6gbIO0f+xUM8mejPgbbUT8pdAYTIb5vugUsoTcbLw0=
18+
github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230517053503-b97a90682782 h1:eSLQNFi9Ws4mAqb1ZzkSB3UyTiF/xE44CZjYiI5NjXs=
19+
github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230517053503-b97a90682782/go.mod h1:q6gbIO0f+xUM8mejPgbbUT8pdAYTIb5vugUsoTcbLw0=
2020
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
2121
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
2222
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=

0 commit comments

Comments
 (0)