From 16f9655ab93889a1d913999d9e34bb7f698a7535 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Thu, 5 Nov 2020 21:00:28 +0800 Subject: [PATCH 1/5] fix: route bug, dirty data exists after updating --- api/internal/handler/route/route.go | 8 ++ api/internal/handler/route/route_test.go | 143 +++++++++++++++++++++++ 2 files changed, 151 insertions(+) diff --git a/api/internal/handler/route/route.go b/api/internal/handler/route/route.go index e9d1d80bd6..b7b1850fbf 100644 --- a/api/internal/handler/route/route.go +++ b/api/internal/handler/route/route.go @@ -304,6 +304,14 @@ func (h *Handler) Update(c droplet.Context) (interface{}, error) { return handler.SpecCodeResponse(err), err } } + } else { + //remove exists script + script, _ := h.scriptStore.Get(input.Route.ID) + if script != nil { + if err := h.scriptStore.BatchDelete(c.Context(), strings.Split(input.Route.ID, ",")); err != nil { + log.Warnf("try to delete script %s again", input.Route.ID) + } + } } if err := h.routeStore.Update(c.Context(), &input.Route, true); err != nil { diff --git a/api/internal/handler/route/route_test.go b/api/internal/handler/route/route_test.go index f858be8246..d70fff3a80 100644 --- a/api/internal/handler/route/route_test.go +++ b/api/internal/handler/route/route_test.go @@ -986,3 +986,146 @@ func TestRoute(t *testing.T) { assert.Nil(t, err) } + +func Test_Route_With_Script(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{ + routeStore: store.GetStore(store.HubKeyRoute), + svcStore: store.GetStore(store.HubKeyService), + upstreamStore: store.GetStore(store.HubKeyUpstream), + scriptStore: store.GetStore(store.HubKeyScript), + } + assert.NotNil(t, handler) + + //create Note: depends on lib `dag-to-lua` if script exists + ctx := droplet.NewContext() + route := &entity.Route{} + reqBody := `{ + "id": "1", + "uri": "/index.html", + "upstream": { + "type": "roundrobin", + "nodes": [{ + "host": "www.a.com", + "port": 80, + "weight": 1 + }] + }, + "script":{ + "rule":{ + "root":"451106f8-560c-43a4-acf2-2a6ed0ea57b8", + "451106f8-560c-43a4-acf2-2a6ed0ea57b8":[ + [ + "code == 403", + "b93d622c-92ef-48b4-b6bb-57e1ce893ee3" + ], + [ + "", + "988ef5c2-c896-4606-a666-3d4cbe24a731" + ] + ] + }, + "conf":{ + "451106f8-560c-43a4-acf2-2a6ed0ea57b8":{ + "name":"uri-blocker", + "conf":{ + "block_rules":[ + "root.exe", + "root.m+" + ], + "rejected_code":403 + } + }, + "988ef5c2-c896-4606-a666-3d4cbe24a731":{ + "name":"kafka-logger", + "conf":{ + "batch_max_size":1000, + "broker_list":{ + }, + "buffer_duration":60, + "inactive_timeout":5, + "include_req_body":false, + "kafka_topic":"1", + "key":"2", + "max_retry_count":0, + "name":"kafka logger", + "retry_delay":1, + "timeout":3 + } + }, + "b93d622c-92ef-48b4-b6bb-57e1ce893ee3":{ + "name":"fault-injection", + "conf":{ + "abort":{ + "body":"200", + "http_status":300 + }, + "delay":{ + "duration":500 + } + } + } + }, + "chart":{ + } + } + }` + err = json.Unmarshal([]byte(reqBody), route) + assert.Nil(t, err) + ctx.SetInput(route) + _, err = handler.Create(ctx) + assert.Nil(t, err) + + //sleep + time.Sleep(time.Duration(20) * time.Millisecond) + + //get + input := &GetInput{} + input.ID = "1" + ctx.SetInput(input) + ret, err := handler.Get(ctx) + stored := ret.(*entity.Route) + assert.Nil(t, err) + assert.Equal(t, stored.ID, route.ID) + assert.NotNil(t, stored.Script) + + //update + route2 := &UpdateInput{} + route2.ID = "1" + reqBody = `{ + "id": "1", + "uri": "/index.html", + "upstream": { + "type": "roundrobin", + "nodes": [{ + "host": "www.a.com", + "port": 80, + "weight": 1 + }] + } + }` + + err = json.Unmarshal([]byte(reqBody), route2) + assert.Nil(t, err) + ctx.SetInput(route2) + _, err = handler.Update(ctx) + assert.Nil(t, err) + + //sleep + time.Sleep(time.Duration(100) * time.Millisecond) + + //get, script should be nil + input = &GetInput{} + input.ID = "1" + ctx.SetInput(input) + ret, err = handler.Get(ctx) + stored = ret.(*entity.Route) + assert.Nil(t, err) + assert.Equal(t, stored.ID, route.ID) + assert.Nil(t, stored.Script) +} From f72026efb0a251aa472e07367a5a2349ee2a7bca Mon Sep 17 00:00:00 2001 From: nic-chen Date: Thu, 5 Nov 2020 21:05:42 +0800 Subject: [PATCH 2/5] fix mod for env --- api/internal/route.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/internal/route.go b/api/internal/route.go index 0304605341..df409ccb43 100644 --- a/api/internal/route.go +++ b/api/internal/route.go @@ -38,10 +38,10 @@ import ( ) func SetUpRouter() *gin.Engine { - if conf.ENV != conf.EnvLOCAL && conf.ENV != conf.EnvBETA { - gin.SetMode(gin.DebugMode) - } else { + if conf.ENV == conf.EnvLOCAL && conf.ENV == conf.EnvDEV { gin.SetMode(gin.ReleaseMode) + } else { + gin.SetMode(gin.DebugMode) } r := gin.New() store := cookie.NewStore([]byte("secret")) From 93d33a1c0893b8261f08ffd67d71ebc2905f8ecb Mon Sep 17 00:00:00 2001 From: nic-chen Date: Thu, 5 Nov 2020 21:12:11 +0800 Subject: [PATCH 3/5] fix mod for env --- api/internal/route.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/internal/route.go b/api/internal/route.go index df409ccb43..04a721f654 100644 --- a/api/internal/route.go +++ b/api/internal/route.go @@ -38,10 +38,10 @@ import ( ) func SetUpRouter() *gin.Engine { - if conf.ENV == conf.EnvLOCAL && conf.ENV == conf.EnvDEV { - gin.SetMode(gin.ReleaseMode) - } else { + if conf.ENV == conf.EnvLOCAL || conf.ENV == conf.EnvDEV { gin.SetMode(gin.DebugMode) + } else { + gin.SetMode(gin.ReleaseMode) } r := gin.New() store := cookie.NewStore([]byte("secret")) From 1d4672ee22e4e110cfe1996661320c9aba5dd334 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Thu, 5 Nov 2020 22:40:55 +0800 Subject: [PATCH 4/5] fix error log --- api/internal/handler/route/route.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/internal/handler/route/route.go b/api/internal/handler/route/route.go index b7b1850fbf..d3d80af544 100644 --- a/api/internal/handler/route/route.go +++ b/api/internal/handler/route/route.go @@ -309,7 +309,7 @@ func (h *Handler) Update(c droplet.Context) (interface{}, error) { script, _ := h.scriptStore.Get(input.Route.ID) if script != nil { if err := h.scriptStore.BatchDelete(c.Context(), strings.Split(input.Route.ID, ",")); err != nil { - log.Warnf("try to delete script %s again", input.Route.ID) + log.Warnf("script %s failed", input.Route.ID) } } } From 8f76782fbe76f158c1d4c8cc063295064fe6f8b3 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Fri, 6 Nov 2020 07:54:05 +0800 Subject: [PATCH 5/5] fix error log --- api/internal/handler/route/route.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/internal/handler/route/route.go b/api/internal/handler/route/route.go index d3d80af544..1d912d52ab 100644 --- a/api/internal/handler/route/route.go +++ b/api/internal/handler/route/route.go @@ -309,7 +309,7 @@ func (h *Handler) Update(c droplet.Context) (interface{}, error) { script, _ := h.scriptStore.Get(input.Route.ID) if script != nil { if err := h.scriptStore.BatchDelete(c.Context(), strings.Split(input.Route.ID, ",")); err != nil { - log.Warnf("script %s failed", input.Route.ID) + log.Warnf("delete script %s failed", input.Route.ID) } } }