-
Notifications
You must be signed in to change notification settings - Fork 2.8k
test: register a custom variable in a custom Lua file and source it with extra_lua_path
#10086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||||||||||
| -- | ||||||||||||||||||||||||
| -- Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||||||||||||||||
| -- contributor license agreements. See the NOTICE file distributed with | ||||||||||||||||||||||||
| -- this work for additional information regarding copyright ownership. | ||||||||||||||||||||||||
| -- The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||||||||||||||||||||
| -- (the "License"); you may not use this file except in compliance with | ||||||||||||||||||||||||
| -- the License. You may obtain a copy of the License at | ||||||||||||||||||||||||
| -- | ||||||||||||||||||||||||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||
| -- | ||||||||||||||||||||||||
| -- Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||
| -- See the License for the specific language governing permissions and | ||||||||||||||||||||||||
| -- limitations under the License. | ||||||||||||||||||||||||
| -- | ||||||||||||||||||||||||
| local apisix = require "apisix" | ||||||||||||||||||||||||
| local core = require "apisix.core" | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| local register_custom_var = function() | ||||||||||||||||||||||||
| core.ctx.register_var("a6_route_labels", function(ctx) | ||||||||||||||||||||||||
| local route = ctx.matched_route and ctx.matched_route.value | ||||||||||||||||||||||||
| if route and route.labels then | ||||||||||||||||||||||||
| return route.labels | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||
| end) | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| local old_http_init = apisix.http_init | ||||||||||||||||||||||||
| apisix.http_init = function(...) | ||||||||||||||||||||||||
| register_custom_var() | ||||||||||||||||||||||||
| ngx.log(ngx.EMERG, "my hook works in http") | ||||||||||||||||||||||||
| old_http_init(...) | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| local old_stream_init = apisix.stream_init | ||||||||||||||||||||||||
| apisix.stream_init = function(...) | ||||||||||||||||||||||||
| register_custom_var() | ||||||||||||||||||||||||
| ngx.log(ngx.EMERG, "my hook works in stream") | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
please use |
||||||||||||||||||||||||
| old_stream_init(...) | ||||||||||||||||||||||||
| end | ||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To show that the variable was really registered, should we try to access and log this in another phase and then grep and assert in the tests?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean something like this? local old_http_access_phase = apisix.http_access_phase
apisix.http_access_phase = function(...)
ngx.log(ngx.EMERG, ctx.a6_route_labels)
old_http_access_phase(...)
endMy understanding is that the custom variable The apisix/t/plugin/http-logger-log-format.t Lines 431 to 441 in 89c1858
Please advise.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes part of the suggestion was to PUT that route (just like in the test you mentioned) and then send request to that route, after which to make assertion. But I don't have a strong opinion here. If others think that showing how it's registered is good enough then I think its fine. cc: @monkeyDluffy6017 @leslie-tsang What do you think?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've already add test cases for this feature, and it doesn't seem necessary to do it again in the demo.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I understand this doesn't cover the entire use case. The initial motivation of this was users having difficulties understanding how to register a variable with a custom Lua file that gets sourced (i.e. |
||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please standardize indentation style.
4space will be please