membphis commented on a change in pull request #2326:
URL: https://github.com/apache/apisix/pull/2326#discussion_r495532864



##########
File path: t/core/ctx.t
##########
@@ -155,3 +155,264 @@ GET /t?a=aaa
 --- error_code: 500
 --- error_log
 invalid argument, expect string value
+
+
+
+=== TEST 7: add route and get `route_id`
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() ngx.log(ngx.ERR, 
\"route_id: \", ngx.ctx.api_ctx.route_id) end"]

Review comment:
       we can not use `error` level here. it will make the test case confused.

##########
File path: t/core/ctx.t
##########
@@ -155,3 +155,264 @@ GET /t?a=aaa
 --- error_code: 500
 --- error_log
 invalid argument, expect string value
+
+
+
+=== TEST 7: add route and get `route_id`
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() ngx.log(ngx.ERR, 
\"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    },
+                    "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "methods": [
+                                "GET"
+                            ],
+                            "uri": "/hello",
+                            "plugins": {
+                                "serverless-pre-function": {
+                                    "phase": "access",
+                                    "functions" : ["return function() 
ngx.log(ngx.ERR, \"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                                }
+                            },
+                            "upstream": {
+                                "type": "roundrobin",
+                                "nodes": {
+                                    "127.0.0.1:1980": 1
+                                }
+                            }
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: `url` exist and `route_id` is not empty
+--- request
+GET /hello
+--- response_body
+hello world
+--- error_log
+route_id: 1
+
+
+
+=== TEST 9: `url` does not exist and  `route_id` is empty
+--- request
+GET /hello123
+--- error_code: 404
+--- no_error_log
+[error]
+
+
+
+=== TEST 10: create a service and `service_id` is 1
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/services/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "desc": "new_service"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "desc": "new_service"
+                        },
+                        "key": "/apisix/services/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 11: the route object not bind any service object
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() if 
ngx.ctx.api_ctx.service_id then ngx.log(ngx.ERR, \"service_id: \", 
ngx.ctx.api_ctx.service_id) end end"]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    },
+                    "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "methods": [
+                                "GET"
+                            ],
+                            "uri": "/hello",
+                            "plugins": {
+                                "serverless-pre-function": {
+                                    "phase": "access",
+                                    "functions" : ["return function() if 
ngx.ctx.api_ctx.service_id then ngx.log(ngx.ERR, \"service_id: \", 
ngx.ctx.api_ctx.service_id) end end"]
+                                }
+                            },
+                            "upstream": {
+                                "type": "roundrobin",
+                                "nodes": {
+                                    "127.0.0.1:1980": 1
+                                }
+                            }
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 12: service_id is empty
+--- request
+GET /hello
+--- response_body
+hello world
+--- no_error_log
+[error]

Review comment:
       need to check the value

##########
File path: t/core/ctx.t
##########
@@ -155,3 +155,264 @@ GET /t?a=aaa
 --- error_code: 500
 --- error_log
 invalid argument, expect string value
+
+
+
+=== TEST 7: add route and get `route_id`
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() ngx.log(ngx.ERR, 
\"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    },
+                    "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "methods": [
+                                "GET"
+                            ],
+                            "uri": "/hello",
+                            "plugins": {
+                                "serverless-pre-function": {
+                                    "phase": "access",
+                                    "functions" : ["return function() 
ngx.log(ngx.ERR, \"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                                }
+                            },
+                            "upstream": {
+                                "type": "roundrobin",
+                                "nodes": {
+                                    "127.0.0.1:1980": 1
+                                }
+                            }
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: `url` exist and `route_id` is not empty
+--- request
+GET /hello
+--- response_body
+hello world
+--- error_log
+route_id: 1
+
+
+
+=== TEST 9: `url` does not exist and  `route_id` is empty
+--- request
+GET /hello123
+--- error_code: 404
+--- no_error_log
+[error]
+
+
+
+=== TEST 10: create a service and `service_id` is 1
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/services/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "desc": "new_service"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "desc": "new_service"
+                        },
+                        "key": "/apisix/services/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 11: the route object not bind any service object
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() if 
ngx.ctx.api_ctx.service_id then ngx.log(ngx.ERR, \"service_id: \", 
ngx.ctx.api_ctx.service_id) end end"]

Review comment:
       we should print the `service_id` always, and check the output value in 
error log.
   
   `ngx.log(ngx.INFO, "service_id: ", ngx.ctx.api_ctx.var["service_id"] or 
"empty id")`

##########
File path: t/core/ctx.t
##########
@@ -155,3 +155,264 @@ GET /t?a=aaa
 --- error_code: 500
 --- error_log
 invalid argument, expect string value
+
+
+
+=== TEST 7: add route and get `route_id`
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() ngx.log(ngx.ERR, 
\"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    },
+                    "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "methods": [
+                                "GET"
+                            ],
+                            "uri": "/hello",
+                            "plugins": {
+                                "serverless-pre-function": {
+                                    "phase": "access",
+                                    "functions" : ["return function() 
ngx.log(ngx.ERR, \"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                                }
+                            },
+                            "upstream": {
+                                "type": "roundrobin",
+                                "nodes": {
+                                    "127.0.0.1:1980": 1
+                                }
+                            }
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: `url` exist and `route_id` is not empty
+--- request
+GET /hello
+--- response_body
+hello world
+--- error_log
+route_id: 1
+
+
+
+=== TEST 9: `url` does not exist and  `route_id` is empty
+--- request
+GET /hello123
+--- error_code: 404
+--- no_error_log
+[error]
+
+
+
+=== TEST 10: create a service and `service_id` is 1
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/services/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "desc": "new_service"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "desc": "new_service"
+                        },
+                        "key": "/apisix/services/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 11: the route object not bind any service object
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() if 
ngx.ctx.api_ctx.service_id then ngx.log(ngx.ERR, \"service_id: \", 
ngx.ctx.api_ctx.service_id) end end"]

Review comment:
       `ngx.ctx.api_ctx.service_id` -> `ngx.ctx.api_ctx.var["service_id"]` ?

##########
File path: apisix/core/ctx.lua
##########
@@ -89,6 +90,14 @@ do
                 key = re_gsub(key, "-", "_", "jo")
                 val = get_var(key, t._request)
 
+            elseif key == "route_id" then
+                local route_id = ngx.ctx.api_ctx.route_id
+                val = route_id
+
+            elseif key == "service_id" then
+                local service_id = ngx.ctx.api_ctx.service_id

Review comment:
       ditto

##########
File path: apisix/core/ctx.lua
##########
@@ -89,6 +90,14 @@ do
                 key = re_gsub(key, "-", "_", "jo")
                 val = get_var(key, t._request)
 
+            elseif key == "route_id" then
+                local route_id = ngx.ctx.api_ctx.route_id

Review comment:
       the `api_ctx` maybe a `nil` value, we need to check it first.

##########
File path: t/core/ctx.t
##########
@@ -155,3 +155,264 @@ GET /t?a=aaa
 --- error_code: 500
 --- error_log
 invalid argument, expect string value
+
+
+
+=== TEST 7: add route and get `route_id`
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() ngx.log(ngx.ERR, 
\"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    },
+                    "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "methods": [
+                                "GET"
+                            ],
+                            "uri": "/hello",
+                            "plugins": {
+                                "serverless-pre-function": {
+                                    "phase": "access",
+                                    "functions" : ["return function() 
ngx.log(ngx.ERR, \"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                                }
+                            },
+                            "upstream": {
+                                "type": "roundrobin",
+                                "nodes": {
+                                    "127.0.0.1:1980": 1
+                                }
+                            }
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: `url` exist and `route_id` is not empty
+--- request
+GET /hello
+--- response_body
+hello world
+--- error_log
+route_id: 1
+
+
+
+=== TEST 9: `url` does not exist and  `route_id` is empty
+--- request
+GET /hello123
+--- error_code: 404
+--- no_error_log
+[error]
+
+
+
+=== TEST 10: create a service and `service_id` is 1
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/services/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "desc": "new_service"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "desc": "new_service"
+                        },
+                        "key": "/apisix/services/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 11: the route object not bind any service object
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() if 
ngx.ctx.api_ctx.service_id then ngx.log(ngx.ERR, \"service_id: \", 
ngx.ctx.api_ctx.service_id) end end"]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    },
+                    "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "methods": [
+                                "GET"
+                            ],
+                            "uri": "/hello",
+                            "plugins": {
+                                "serverless-pre-function": {
+                                    "phase": "access",
+                                    "functions" : ["return function() if 
ngx.ctx.api_ctx.service_id then ngx.log(ngx.ERR, \"service_id: \", 
ngx.ctx.api_ctx.service_id) end end"]
+                                }
+                            },
+                            "upstream": {
+                                "type": "roundrobin",
+                                "nodes": {
+                                    "127.0.0.1:1980": 1
+                                }
+                            }
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 12: service_id is empty
+--- request
+GET /hello
+--- response_body
+hello world
+--- no_error_log
+[error]
+
+
+
+=== TEST 13: update route and binding service_id
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                     "service_id": 1,
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() ngx.log(ngx.ERR, 
\"service_id: \", ngx.ctx.api_ctx.service_id) end"]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    },
+                    "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "methods": [
+                                "GET"
+                            ],
+                            "uri": "/hello",
+                            "service_id": 1,
+                            "plugins": {
+                                "serverless-pre-function": {
+                                    "phase": "access",
+                                    "functions" : ["return function() 
ngx.log(ngx.ERR, \"service_id: \", ngx.ctx.api_ctx.service_id) end"]
+                                }
+                            },
+                            "upstream": {
+                                "type": "roundrobin",
+                                "nodes": {
+                                    "127.0.0.1:1980": 1
+                                }
+                            }
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 14: service_id is not empty
+--- request
+GET /hello
+--- response_body
+hello world
+--- error_log
+service_id: 1

Review comment:
       
![image](https://user-images.githubusercontent.com/6814606/94357088-54237080-00c8-11eb-90a7-2414b76375c5.png)
   

##########
File path: t/core/ctx.t
##########
@@ -155,3 +155,183 @@ GET /t?a=aaa
 --- error_code: 500
 --- error_log
 invalid argument, expect string value
+
+
+
+=== TEST 7: add route
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "methods": ["GET"],
+                    "plugins": {
+                        "serverless-pre-function": {
+                            "phase": "access",
+                            "functions" : ["return function() ngx.log(ngx.ERR, 
\"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                        }
+                    },
+                    "upstream": {
+                            "type": "roundrobin",
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            }
+                    },
+                    "uri": "/hello"
+                }]],
+                [[{
+                    "node": {
+                        "value": {
+                            "methods": [
+                                "GET"
+                            ],
+                            "uri": "/hello",
+                            "plugins": {
+                                "serverless-pre-function": {
+                                    "phase": "access",
+                                    "functions" : ["return function() 
ngx.log(ngx.ERR, \"route_id: \", ngx.ctx.api_ctx.route_id) end"]
+                                }
+                            },
+                            "upstream": {
+                                "type": "roundrobin",
+                                "nodes": {
+                                    "127.0.0.1:1980": 1
+                                }
+                            }
+                        },
+                        "key": "/apisix/routes/1"
+                    },
+                    "action": "set"
+                }]]
+                )
+
+            ngx.status = code
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 8: route_id
+--- request
+GET /hello
+--- response_body
+hello world
+--- error_log
+route_id: 1

Review comment:
       still not fixed?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to