Skip to content

Commit

Permalink
next iteration with test code
Browse files Browse the repository at this point in the history
  • Loading branch information
bisakhmondal committed Nov 16, 2021
1 parent 6b0a755 commit 3712d80
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 4 deletions.
5 changes: 2 additions & 3 deletions apisix/plugins/azure-functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ local schema = {
}
},
timeout = {type = "integer", minimum = 1000, default = 3000},
ssl_verify = {type = "boolean", default = false},
ssl_verify = {type = "boolean", default = true},
keepalive = {type = "boolean", default = true},
keepalive_timeout = {type = "integer", minimum = 1000, default = 60000},
keepalive_pool = {type = "integer", minimum = 1, default = 5}
Expand All @@ -61,6 +61,7 @@ function _M.access(conf, ctx)
local headers = core.request.headers(ctx) or {}
local req_body, _ = core.request.get_body()


-- set authorization headers
if conf.authorization then
headers["x-functions-key"] = conf.authorization.apikey or ""
Expand All @@ -85,8 +86,6 @@ function _M.access(conf, ctx)
params.keepalive_pool = conf.keepalive_pool
end

-- TODO: path processing

local httpc = http.new()
httpc:set_timeout(conf.timeout)

Expand Down
3 changes: 3 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,13 @@ _EOC_
$ipv6_listen_conf = "listen \[::1\]:1984;"
}

my $additional_http_config = $block->additional_http_config // "";
my $config = $block->config // '';
$config .= <<_EOC_;
$ipv6_listen_conf
$additional_http_config
listen 1994 ssl http2;
ssl_certificate cert/apisix.crt;
ssl_certificate_key cert/apisix.key;
Expand Down
2 changes: 1 addition & 1 deletion t/admin/plugins.t
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ __DATA__
--- request
GET /apisix/admin/plugins/list
--- response_body_like eval
qr/\["real-ip","client-control","ext-plugin-pre-req","zipkin","request-id","fault-injection","serverless-pre-function","batch-requests","cors","ip-restriction","ua-restriction","referer-restriction","uri-blocker","request-validation","openid-connect","authz-casbin","wolf-rbac","ldap-auth","hmac-auth","basic-auth","jwt-auth","key-auth","consumer-restriction","authz-keycloak","proxy-mirror","proxy-cache","proxy-rewrite","api-breaker","limit-conn","limit-count","limit-req","gzip","server-info","traffic-split","redirect","response-rewrite","grpc-transcode","prometheus","datadog","echo","http-logger","skywalking-logger","sls-logger","tcp-logger","kafka-logger","syslog","udp-logger","example-plugin","serverless-post-function","ext-plugin-post-req"\]/
qr/\["real-ip","client-control","ext-plugin-pre-req","zipkin","request-id","fault-injection","serverless-pre-function","batch-requests","cors","ip-restriction","ua-restriction","referer-restriction","uri-blocker","request-validation","openid-connect","authz-casbin","wolf-rbac","ldap-auth","hmac-auth","basic-auth","jwt-auth","key-auth","consumer-restriction","authz-keycloak","proxy-mirror","proxy-cache","proxy-rewrite","api-breaker","limit-conn","limit-count","limit-req","gzip","server-info","traffic-split","redirect","response-rewrite","grpc-transcode","azure-functions","prometheus","datadog","echo","http-logger","skywalking-logger","sls-logger","tcp-logger","kafka-logger","syslog","udp-logger","example-plugin","serverless-post-function","ext-plugin-post-req"\]/
--- no_error_log
[error]
Expand Down
228 changes: 228 additions & 0 deletions t/plugin/azure-functions.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
#
# 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.
#
use t::APISIX 'no_plan';

repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();

add_block_preprocessor(sub {
my ($block) = @_;

# $block->set_value("stream_conf_enable", 1);

if (!defined $block->additional_http_config) {
my $inside_lua_block = $block->inside_lua_block // "";
chomp($inside_lua_block);
my $test_config = <<_EOC_;
listen 8765;
location /azure-demo {
content_by_lua_block {
$inside_lua_block
}
}
_EOC_
$block->set_value("additional_http_config", $test_config);
}

if (!$block->request) {
$block->set_value("request", "GET /t");
}
if (!$block->no_error_log && !$block->error_log) {
$block->set_value("no_error_log", "[error]\n[alert]");
}
});

run_tests;

__DATA__
=== TEST 1: sanity
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.azure-functions")
local conf = {
function_uri = "http://some-url.com"
}
local ok, err = plugin.check_schema(conf)
if not ok then
ngx.say(err)
end
ngx.say("done")
}
}
--- response_body
done
=== TEST 2: function_uri missing
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.azure-functions")
local ok, err = plugin.check_schema({})
if not ok then
ngx.say(err)
else
ngx.say("done")
end
}
}
--- response_body
property "function_uri" is required
=== TEST 3: create route with azure-function plugin enabled
--- 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,
[[{
"plugins": {
"azure-functions": {
"function_uri": "http://localhost:8765/azure-demo"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1982": 1
},
"type": "roundrobin"
},
"uri": "/azure"
}]],
[[{
"node": {
"value": {
"plugins": {
"azure-functions": {
"keepalive": true,
"timeout": 3000,
"ssl_verify": true,
"keepalive_timeout": 60000,
"keepalive_pool": 5,
"function_uri": "http://localhost:8765/azure-demo"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1982": 1
},
"type": "roundrobin"
},
"uri": "/azure"
},
"key": "/apisix/routes/1"
},
"action": "set"
}]]
)
if code >= 300 then
ngx.status = code
ngx.say("fail")
return
end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 4: Test plugin endpoint
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, _, body = t("/azure", "GET")
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
ngx.print(body)
}
}
--- inside_lua_block
ngx.say("faas invoked")
--- response_body
faas invoked
=== TEST 5: check authz header
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
-- passing an apikey
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"azure-functions": {
"function_uri": "http://localhost:8765/azure-demo",
"authorization": {
"apikey": "test_key"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1982": 1
},
"type": "roundrobin"
},
"uri": "/azure"
}]]
)
if code >= 300 then
ngx.status = code
ngx.say("fail")
return
end
ngx.say(body)
local code, _, body = t("/azure", "GET")
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end
ngx.print(body)
}
}
--- inside_lua_block
local headers = ngx.req.get_headers() or {}
ngx.say("Authz-Header - " .. headers["x-functions-key"] or "")
--- response_body
passed
Authz-Header - test_key

0 comments on commit 3712d80

Please sign in to comment.