This is an automated email from the ASF dual-hosted git repository.

juzhiyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 8f8d173  change: upstream and route support `chash` and `checks` (#430)
8f8d173 is described below

commit 8f8d173377d9400fe1bb5faaafbfa89b7c652d53
Author: nic-chen <[email protected]>
AuthorDate: Mon Aug 31 16:28:29 2020 +0800

    change: upstream and route support `chash` and `checks` (#430)
---
 api/service/route.go         | 11 ++++--
 api/service/upstream_test.go | 92 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+), 4 deletions(-)

diff --git a/api/service/route.go b/api/service/route.go
index 23bf786..88cc16b 100644
--- a/api/service/route.go
+++ b/api/service/route.go
@@ -341,10 +341,13 @@ func (r Redirect) MarshalJSON() ([]byte, error) {
 }
 
 type Upstream struct {
-       UType           string           `json:"type"`
-       Nodes           map[string]int64 `json:"nodes"`
-       Timeout         UpstreamTimeout  `json:"timeout"`
-       EnableWebsocket bool             `json:"enable_websocket"`
+       UType           string                 `json:"type"`
+       Nodes           map[string]int64       `json:"nodes"`
+       Timeout         UpstreamTimeout        `json:"timeout"`
+       EnableWebsocket bool                   `json:"enable_websocket"`
+       Checks          map[string]interface{} `json:"checks,omitempty"`
+       HashOn          string                 `json:"hash_on,omitempty"`
+       Key             string                 `json:"key,omitempty"`
 }
 
 type UpstreamTimeout struct {
diff --git a/api/service/upstream_test.go b/api/service/upstream_test.go
new file mode 100644
index 0000000..f216de6
--- /dev/null
+++ b/api/service/upstream_test.go
@@ -0,0 +1,92 @@
+package service
+
+import (
+       "testing"
+
+       uuid "github.com/satori/go.uuid"
+       "github.com/stretchr/testify/assert"
+)
+
+// parse from params to RouteRequest must be error
+func TestUpstreamRequest_Parse(t *testing.T) {
+       a := assert.New(t)
+       param := []byte(`{
+               "name": "upstream-test",
+               "description": "test upstream",
+               "type": "roundrobin",
+               "nodes": {
+                       "127.0.0.1:8080":100,
+                       "127.0.0.1:8081":200
+               },
+               "timeout":{
+                       "connect":15,
+                       "send":15,
+                       "read":15
+               },
+               "enable_websocket": true,
+               "hash_on": "header",
+               "key": "server_addr",
+               "checks": {
+                       "active": {
+                               "timeout": 5,
+                               "http_path": "/status",
+                               "host": "foo.com",
+                               "healthy": {
+                                       "interval": 2,
+                                       "successes": 1
+                               },
+                               "unhealthy": {
+                                       "interval": 1,
+                                       "http_failures": 2
+                               },
+                               "req_headers": ["User-Agent: curl/7.29.0"]
+                       },
+                       "passive": {
+                               "healthy": {
+                                       "http_statuses": [200, 201],
+                                       "successes": 3
+                               },
+                               "unhealthy": {
+                                       "http_statuses": [500],
+                                       "http_failures": 3,
+                                       "tcp_failures": 3
+                               }
+                       }
+               }       
+       }`)
+
+       ur := &UpstreamRequest{}
+       err := ur.Parse(param)
+       a.Nil(err)
+       a.Equal("header", ur.HashOn)
+       a.Equal("server_addr", ur.Key)
+
+       u4 := uuid.NewV4()
+       uuid := u4.String()
+       ur.Id = uuid
+
+       aur, err := ur.Parse2Apisix()
+       a.Nil(err)
+
+       res := aur.toJson()
+       a.NotNil(res)
+
+       //create a upstream
+       apisixResp, err := aur.Create()
+       a.Nil(err)
+       rur, err := apisixResp.Parse2Request()
+       a.Nil(err)
+       a.Equal(ur.Key, rur.Key)
+
+       aur.Id = rur.Id
+       //get the upstream just created
+       created, err := aur.FindById()
+       a.Nil(err)
+       createdFormat, err := created.Parse2Request()
+       a.Nil(err)
+       a.Equal(createdFormat.HashOn, rur.HashOn)
+
+       //delete test data
+       _, err = aur.Delete()
+       a.Nil(err)
+}

Reply via email to