Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: an error will occur if pass_host is set to node when creating upstream #750

Merged
merged 5 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion api/internal/core/entity/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
)

func NodesFormat(obj interface{}) interface{} {
var nodes []*Node
if value, ok := obj.(map[string]float64); ok {
var nodes []*Node
var strArr []string
for key, val := range value {
node := &Node{}
Expand All @@ -48,5 +48,22 @@ func NodesFormat(obj interface{}) interface{} {
return nodes
}

if nodes, ok := obj.([]*Node); ok {
return nodes
}

if list, ok := obj.([]interface{}); ok {
for _, v := range list {
val := v.(map[string]interface{})
node := &Node{}
node.Host = val["host"].(string)
node.Port = int(val["port"].(float64))
node.Weight = int(val["weight"].(float64))
nodes = append(nodes, node)
}

return nodes
}

return obj
}
59 changes: 52 additions & 7 deletions api/internal/handler/upstream/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ import (
"github.com/apisix/manager-api/internal/core/store"
)

var upstreamHandler *Handler

func TestUpstream(t *testing.T) {
// init
err := storage.InitETCDClient([]string{"127.0.0.1:2379"})
assert.Nil(t, err)
err = store.InitStores()
assert.Nil(t, err)

handler := &Handler{
upstreamHandler = &Handler{
upstreamStore: store.GetStore(store.HubKeyUpstream),
}
assert.NotNil(t, handler)
assert.NotNil(t, upstreamHandler)

//create
ctx := droplet.NewContext()
Expand Down Expand Up @@ -94,7 +96,7 @@ func TestUpstream(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), upstream)
assert.Nil(t, err)
ctx.SetInput(upstream)
_, err = handler.Create(ctx)
_, err = upstreamHandler.Create(ctx)
assert.Nil(t, err)

//sleep
Expand All @@ -104,7 +106,7 @@ func TestUpstream(t *testing.T) {
input := &GetInput{}
input.ID = "1"
ctx.SetInput(input)
ret, err := handler.Get(ctx)
ret, err := upstreamHandler.Get(ctx)
stored := ret.(*entity.Upstream)
assert.Nil(t, err)
assert.Equal(t, stored.ID, upstream.ID)
Expand Down Expand Up @@ -161,7 +163,7 @@ func TestUpstream(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), upstream2)
assert.Nil(t, err)
ctx.SetInput(upstream2)
_, err = handler.Update(ctx)
_, err = upstreamHandler.Update(ctx)
assert.Nil(t, err)

//list
Expand All @@ -170,7 +172,7 @@ func TestUpstream(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err := handler.List(ctx)
retPage, err := upstreamHandler.List(ctx)
assert.Nil(t, err)
dataPage := retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)
Expand All @@ -181,7 +183,50 @@ func TestUpstream(t *testing.T) {
err = json.Unmarshal([]byte(reqBody), inputDel)
assert.Nil(t, err)
ctx.SetInput(inputDel)
_, err = handler.BatchDelete(ctx)
_, err = upstreamHandler.BatchDelete(ctx)
assert.Nil(t, err)

}

func TestUpstream_Pass_Host(t *testing.T) {
//create
ctx := droplet.NewContext()
upstream := &entity.Upstream{}
reqBody := `{
"id": "2",
"nodes": [{
"host": "httpbin.org",
"port": 80,
"weight": 1
}],
"type": "roundrobin",
"pass_host": "node"
}`
err := json.Unmarshal([]byte(reqBody), upstream)
assert.Nil(t, err)
ctx.SetInput(upstream)
_, err = upstreamHandler.Create(ctx)
assert.Nil(t, err)

//sleep
time.Sleep(time.Duration(20) * time.Millisecond)

//get
input := &GetInput{}
input.ID = "2"
ctx.SetInput(input)
ret, err := upstreamHandler.Get(ctx)
stored := ret.(*entity.Upstream)
assert.Nil(t, err)
assert.Equal(t, stored.ID, upstream.ID)

//delete test data
inputDel := &BatchDelete{}
reqBody = `{"ids": "2"}`
err = json.Unmarshal([]byte(reqBody), inputDel)
assert.Nil(t, err)
ctx.SetInput(inputDel)
_, err = upstreamHandler.BatchDelete(ctx)
assert.Nil(t, err)

}