Skip to content

Commit 6d6a81f

Browse files
committed
new
1 parent ad5ede6 commit 6d6a81f

4 files changed

Lines changed: 77 additions & 11 deletions

File tree

cmd/routes.go

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

@@ -139,19 +138,22 @@ func (d *DryccCmd) RoutesSet(appID string, name string, ruleFile string) error {
139138
}
140139

141140
var rules interface{}
142-
var contents string
141+
var contents []byte
143142
if _, err := os.Stat(ruleFile); err == nil {
144-
contents, err := ioutil.ReadFile(ruleFile)
143+
contents, err = ioutil.ReadFile(ruleFile)
145144
if err != nil {
146145
return err
147146
}
148-
if err := json.Unmarshal([]byte(contents), &rules); err != nil {
149-
fmt.Println(err)
147+
if err := json.Unmarshal(contents, &rules); err != nil {
148+
return err
150149
}
151150
}
152151

153-
d.Print("Applying rules ... ")
154-
err = routes.ApplyRule(s.Client, appID, name, contents)
152+
d.Print("Applying rules... ")
153+
quit := progress(d.WOut)
154+
err = routes.SetRule(s.Client, appID, name, string(contents))
155+
quit <- true
156+
<-quit
155157
if d.checkAPICompatibility(s.Client, err) != nil {
156158
return err
157159
}

cmd/routes_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,70 @@ func TestRoutesList(t *testing.T) {
8383
`, "output")
8484
}
8585

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+
86150
func TestRoutesAttach(t *testing.T) {
87151
t.Parallel()
88152
cf, server, err := testutil.NewTestServerAndClient()
@@ -172,7 +236,7 @@ func TestRoutesSet(t *testing.T) {
172236
err = cmdr.RoutesSet("foo", "example-go", "")
173237
assert.NoError(t, err)
174238

175-
assert.Equal(t, testutil.StripProgress(b.String()), "Applying rules ... done\n", "output")
239+
assert.Equal(t, testutil.StripProgress(b.String()), "Applying rules... done\n", "output")
176240
}
177241

178242
func TestRoutesRemove(t *testing.T) {

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-20230509075640-ffe8cfa2734e
29+
replace github.com/drycc/controller-sdk-go => github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230510015731-e76cc1c241d5

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-20230509075640-ffe8cfa2734e h1:YnI0PMoEuM0sEtVRMgFqkj2PfnQVpO2+Hm1OZfoNa/o=
19-
github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230509075640-ffe8cfa2734e/go.mod h1:q6gbIO0f+xUM8mejPgbbUT8pdAYTIb5vugUsoTcbLw0=
18+
github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230510015731-e76cc1c241d5 h1:ABcJ0u/yPS0mapA6OVPIMCN5cyS5PqMer+ChexMw2Gg=
19+
github.com/jianxiaoguo/controller-sdk-go v0.0.0-20230510015731-e76cc1c241d5/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)