From 3712d808dedfc2b2013099f098b2d1d0c8765c66 Mon Sep 17 00:00:00 2001 From: Bisakh Mondal Date: Tue, 16 Nov 2021 20:05:50 +0530 Subject: [PATCH] next iteration with test code --- apisix/plugins/azure-functions.lua | 5 +- t/APISIX.pm | 3 + t/admin/plugins.t | 2 +- t/plugin/azure-functions.t | 228 +++++++++++++++++++++++++++++ 4 files changed, 234 insertions(+), 4 deletions(-) create mode 100644 t/plugin/azure-functions.t diff --git a/apisix/plugins/azure-functions.lua b/apisix/plugins/azure-functions.lua index 1448556d269f..c9ff9fc16d42 100644 --- a/apisix/plugins/azure-functions.lua +++ b/apisix/plugins/azure-functions.lua @@ -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} @@ -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 "" @@ -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) diff --git a/t/APISIX.pm b/t/APISIX.pm index 43dbf92eacab..4913e285f513 100644 --- a/t/APISIX.pm +++ b/t/APISIX.pm @@ -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; diff --git a/t/admin/plugins.t b/t/admin/plugins.t index 8e69ef8a294b..592317b934f1 100644 --- a/t/admin/plugins.t +++ b/t/admin/plugins.t @@ -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] diff --git a/t/plugin/azure-functions.t b/t/plugin/azure-functions.t new file mode 100644 index 000000000000..c2f85e189e78 --- /dev/null +++ b/t/plugin/azure-functions.t @@ -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