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 d6dcaa4 feat: add create_time/update_time for global_rule (#1218)
d6dcaa4 is described below
commit d6dcaa4066acf864b2d9303e570486b5dabb9265
Author: Joey <[email protected]>
AuthorDate: Mon Jan 11 20:35:03 2021 +0800
feat: add create_time/update_time for global_rule (#1218)
* feat: support create_time/update_time for global_rule
Signed-off-by: imjoey <[email protected]>
* Update global_rule schema with new create_time/update_time
Signed-off-by: imjoey <[email protected]>
* Fix backend Unit test failure
Signed-off-by: imjoey <[email protected]>
* fix: use BEJSON to format schema.json
* fix style and code conventions
Signed-off-by: imjoey <[email protected]>
Co-authored-by: liuxiran <[email protected]>
---
api/conf/schema.json | 6 ++
api/internal/core/entity/entity.go | 2 +-
.../handler/global_rule/global_rule_test.go | 18 ++--
api/test/e2e/global_rule_test.go | 113 +++++++++++++++++++++
api/test/e2e/go.sum | 1 +
5 files changed, 130 insertions(+), 10 deletions(-)
diff --git a/api/conf/schema.json b/api/conf/schema.json
index 9ee9603..8801ad9 100644
--- a/api/conf/schema.json
+++ b/api/conf/schema.json
@@ -54,6 +54,9 @@
"global_rule": {
"additionalProperties": false,
"properties": {
+ "create_time": {
+ "type": "integer"
+ },
"id": {
"anyOf": [{
"maxLength": 64,
@@ -67,6 +70,9 @@
},
"plugins": {
"type": "object"
+ },
+ "update_time": {
+ "type": "integer"
}
},
"required": ["plugins"],
diff --git a/api/internal/core/entity/entity.go
b/api/internal/core/entity/entity.go
index c8c2ff1..e7b82f0 100644
--- a/api/internal/core/entity/entity.go
+++ b/api/internal/core/entity/entity.go
@@ -237,7 +237,7 @@ type Script struct {
// swagger:model GlobalPlugins
type GlobalPlugins struct {
- ID interface{} `json:"id"`
+ BaseInfo
Plugins map[string]interface{} `json:"plugins"`
}
diff --git a/api/internal/handler/global_rule/global_rule_test.go
b/api/internal/handler/global_rule/global_rule_test.go
index 9311b77..92a5434 100644
--- a/api/internal/handler/global_rule/global_rule_test.go
+++ b/api/internal/handler/global_rule/global_rule_test.go
@@ -142,15 +142,15 @@ func TestHandler_List(t *testing.T) {
PageNumber: 1,
},
giveData: []*entity.GlobalPlugins{
- {ID: "global-rules-1"},
- {ID: "global-rules-2"},
- {ID: "global-rules-3"},
+ {BaseInfo: entity.BaseInfo{ID:
"global-rules-1"}},
+ {BaseInfo: entity.BaseInfo{ID:
"global-rules-2"}},
+ {BaseInfo: entity.BaseInfo{ID:
"global-rules-3"}},
},
wantRet: &store.ListOutput{
Rows: []interface{}{
- &entity.GlobalPlugins{ID:
"global-rules-1"},
- &entity.GlobalPlugins{ID:
"global-rules-2"},
- &entity.GlobalPlugins{ID:
"global-rules-3"},
+ &entity.GlobalPlugins{BaseInfo:
entity.BaseInfo{ID: "global-rules-1"}},
+ &entity.GlobalPlugins{BaseInfo:
entity.BaseInfo{ID: "global-rules-2"}},
+ &entity.GlobalPlugins{BaseInfo:
entity.BaseInfo{ID: "global-rules-3"}},
},
TotalSize: 3,
},
@@ -229,7 +229,7 @@ func TestHandler_Set(t *testing.T) {
},
giveCtx: context.WithValue(context.Background(),
"test", "value"),
wantInput: &entity.GlobalPlugins{
- ID: "name",
+ BaseInfo: entity.BaseInfo{ID: "name"},
Plugins: map[string]interface{}{
"jwt-auth": map[string]interface{}{},
},
@@ -245,8 +245,8 @@ func TestHandler_Set(t *testing.T) {
},
giveErr: fmt.Errorf("create failed"),
wantInput: &entity.GlobalPlugins{
- ID: "name",
- Plugins: map[string]interface{}(nil),
+ BaseInfo: entity.BaseInfo{ID: "name"},
+ Plugins: map[string]interface{}(nil),
},
wantErr: fmt.Errorf("create failed"),
wantRet: &data.SpecCodeResponse{
diff --git a/api/test/e2e/global_rule_test.go b/api/test/e2e/global_rule_test.go
index d9bab48..f94a5eb 100644
--- a/api/test/e2e/global_rule_test.go
+++ b/api/test/e2e/global_rule_test.go
@@ -17,8 +17,13 @@
package e2e
import (
+ "io/ioutil"
"net/http"
"testing"
+ "time"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/tidwall/gjson"
)
func TestGlobalRule(t *testing.T) {
@@ -282,3 +287,111 @@ func TestGlobalRule(t *testing.T) {
testCaseCheck(tc, t)
}
}
+
+func TestGlobalRule_with_createtime_updatetime(t *testing.T) {
+ tests := []HttpTestCase{
+ {
+ Desc: "create global rule",
+ Object: ManagerApiExpect(t),
+ Path: "/apisix/admin/global_rules/1",
+ Method: http.MethodPut,
+ Body: `{
+ "plugins": {
+ "response-rewrite": {
+ "headers": {
+ "X-VERSION":"1.0"
+ }
+ },
+ "uri-blocker": {
+ "block_rules":
["select.+(from|limit)", "(?:(union(.*?)select))"]
+ }
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ Sleep: sleepTime,
+ },
+ }
+
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ basepath := "http://127.0.0.1:9000/apisix/admin/global_rules/1"
+ time.Sleep(time.Duration(100) * time.Millisecond)
+
+ // get the global_rule, save createtime and updatetime
+ request, _ := http.NewRequest("GET", basepath, nil)
+ request.Header.Add("Authorization", token)
+ resp, err := http.DefaultClient.Do(request)
+ assert.Nil(t, err)
+ defer resp.Body.Close()
+
+ respBody, _ := ioutil.ReadAll(resp.Body)
+ createtime := gjson.Get(string(respBody), "data.create_time")
+ updatetime := gjson.Get(string(respBody), "data.update_time")
+ assert.True(t, createtime.Int() >= time.Now().Unix()-1 &&
createtime.Int() <= time.Now().Unix()+1)
+ assert.True(t, updatetime.Int() >= time.Now().Unix()-1 &&
updatetime.Int() <= time.Now().Unix()+1)
+
+ // wait 1 second so the update_time should be different
+ time.Sleep(time.Duration(1) * time.Second)
+
+ tests = []HttpTestCase{
+ {
+ Desc: "update the global rule",
+ Object: ManagerApiExpect(t),
+ Path: "/apisix/admin/global_rules/1",
+ Method: http.MethodPut,
+ Body: `{
+ "plugins": {
+ "response-rewrite": {
+ "headers": {
+ "X-VERSION":"1.1"
+ }
+ },
+ "uri-blocker": {
+ "block_rules":
["select.+(from|limit)", "(?:(union(.*?)select))"]
+ }
+ }
+ }`,
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ }
+
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+
+ // get the global rule
+ time.Sleep(time.Duration(1) * time.Second)
+ request, _ = http.NewRequest("GET", basepath, nil)
+ request.Header.Add("Authorization", token)
+ resp, err = http.DefaultClient.Do(request)
+ assert.Nil(t, err)
+ defer resp.Body.Close()
+
+ respBody, _ = ioutil.ReadAll(resp.Body)
+ createtime2 := gjson.Get(string(respBody), "data.create_time")
+ updatetime2 := gjson.Get(string(respBody), "data.update_time")
+
+ // verify the global and compare result
+ assert.Equal(t, "1.1", gjson.Get(string(respBody),
"data.plugins.response-rewrite.headers.X-VERSION").String())
+ assert.Equal(t, createtime.String(), createtime2.String())
+ assert.NotEqual(t, updatetime.String(), updatetime2.String())
+
+ tests = []HttpTestCase{
+ {
+ Desc: "delete the global rule",
+ Object: ManagerApiExpect(t),
+ Method: http.MethodDelete,
+ Path: "/apisix/admin/global_rules/1",
+ Headers: map[string]string{"Authorization": token},
+ ExpectStatus: http.StatusOK,
+ },
+ }
+
+ for _, tc := range tests {
+ testCaseCheck(tc, t)
+ }
+}
diff --git a/api/test/e2e/go.sum b/api/test/e2e/go.sum
index 69e0f2f..7e5ce36 100644
--- a/api/test/e2e/go.sum
+++ b/api/test/e2e/go.sum
@@ -65,6 +65,7 @@ github.com/savsgio/gotils v0.0.0-20200117113501-90175b0fbe3f
h1:PgA+Olipyj258EIE
github.com/savsgio/gotils v0.0.0-20200117113501-90175b0fbe3f/go.mod
h1:lHhJedqxCoHN+zMtwGNTXWmF0u9Jt363FYRhV6g0CdY=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod
h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
+github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod
h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod
h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0
h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=