Skip to content

Commit

Permalink
fix: an error will occur if pass_host is set to node when creatin…
Browse files Browse the repository at this point in the history
…g upstream (#750)

* fix: bug #749

* fix: bug #749

* test: add test cases

* fix: CI

* fix: CI fail
  • Loading branch information
nic-chen authored Nov 9, 2020
1 parent 033942d commit a5ab0b3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
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)

}

0 comments on commit a5ab0b3

Please sign in to comment.