Skip to content

Commit

Permalink
test(aws-lambda) mock AWS backend
Browse files Browse the repository at this point in the history
  • Loading branch information
p0pr0ck5 committed May 9, 2017
1 parent 491cbb4 commit b2511de
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 25 deletions.
5 changes: 3 additions & 2 deletions kong/plugins/aws-lambda/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ local ngx_req_get_body_data = ngx.req.get_body_data

local CONTENT_TYPE = "content-type"

local AWS_PORT = 443

local AWSLambdaHandler = BasePlugin:extend()

function AWSLambdaHandler:new()
Expand Down Expand Up @@ -49,7 +51,6 @@ function AWSLambdaHandler:access(conf)
local host = string.format("lambda.%s.amazonaws.com", conf.aws_region)
local path = string.format("/2015-03-31/functions/%s/invocations",
conf.function_name)

local opts = {
region = conf.aws_region,
service = "lambda",
Expand All @@ -75,7 +76,7 @@ function AWSLambdaHandler:access(conf)

-- Trigger request
local client = http.new()
client:connect(host, 443)
client:connect(host, conf.port or AWS_PORT)
client:set_timeout(conf.timeout)
local ok, err = client:ssl_handshake()
if not ok then
Expand Down
3 changes: 2 additions & 1 deletion kong/plugins/aws-lambda/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ return {
invocation_type = {type = "string", required = true, default = "RequestResponse",
enum = {"RequestResponse", "Event", "DryRun"}},
log_type = {type = "string", required = true, default = "Tail",
enum = {"Tail", "None"}}
enum = {"Tail", "None"}},
port = { type = "number", default = 443 },
}
}
62 changes: 40 additions & 22 deletions spec/03-plugins/23-aws-lambda/01-access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
local helpers = require "spec.helpers"

pending("Plugin: AWS Lambda (access)", function()
-- pending due to AWS account issues and
-- waiting for a mock solution as suggested by
-- https://github.com/Mashape/kong/pull/2287
describe("Plugin: AWS Lambda (access)", function()
local client, api_client

setup(function()
Expand All @@ -25,12 +22,19 @@ pending("Plugin: AWS Lambda (access)", function()
upstream_url = "http://httpbin.org"
})

local api4 = assert(helpers.dao.apis:insert {
name = "lambda4.com",
hosts = { "lambda4.com" },
upstream_url = "http://httpbin.org"
})

assert(helpers.dao.plugins:insert {
name = "aws-lambda",
api_id = api1.id,
config = {
aws_key = "AKIAIDPNYYGMJOXN26SQ",
aws_secret = "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",
port = 10001,
aws_key = "mock-key",
aws_secret = "mock-secret",
aws_region = "us-east-1",
function_name = "kongLambdaTest"
}
Expand All @@ -40,8 +44,9 @@ pending("Plugin: AWS Lambda (access)", function()
name = "aws-lambda",
api_id = api2.id,
config = {
aws_key = "AKIAIDPNYYGMJOXN26SQ",
aws_secret = "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",
port = 10001,
aws_key = "mock-key",
aws_secret = "mock-secret",
aws_region = "us-east-1",
function_name = "kongLambdaTest",
invocation_type = "Event"
Expand All @@ -52,28 +57,31 @@ pending("Plugin: AWS Lambda (access)", function()
name = "aws-lambda",
api_id = api3.id,
config = {
aws_key = "AKIAIDPNYYGMJOXN26SQ",
aws_secret = "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",
port = 10001,
aws_key = "mock-key",
aws_secret = "mock-secret",
aws_region = "us-east-1",
function_name = "kongLambdaTest",
invocation_type = "DryRun"
}
})

assert(helpers.start_kong())

-- Improve test reliability here: warm up AWS lambda because the first
-- invocation will take the most time
client = helpers.proxy_client()
client:set_timeout(2 * 60 * 1000) -- 2 minute timeout for the warmup
assert(client:send {
method = "GET",
path = "/get?key1=some_value1&key2=some_value2&key3=some_value3",
headers = {
["Host"] = "lambda.com"
assert(helpers.dao.plugins:insert {
name = "aws-lambda",
api_id = api4.id,
config = {
port = 10001,
aws_key = "mock-key",
aws_secret = "mock-secret",
aws_region = "us-east-1",
function_name = "kongLambdaTest",
timeout = 100,
}
})
client:close()

assert(helpers.start_kong{
nginx_conf = "spec/fixtures/custom_nginx.template",
})
end)

before_each(function()
Expand Down Expand Up @@ -189,5 +197,15 @@ pending("Plugin: AWS Lambda (access)", function()
assert.res_status(204, res)
assert.is_string(res.headers["x-amzn-RequestId"])
end)
it("errors on connection timeout", function()
local res = assert(client:send {
method = "GET",
path = "/get?key1=some_value1&key2=some_value2&key3=some_value3",
headers = {
["Host"] = "lambda4.com",
}
})
assert.res_status(500, res)
end)

end)
36 changes: 36 additions & 0 deletions spec/fixtures/custom_nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,41 @@ http {
ngx.say(name)
}
}

location ~ "/2015-03-31/functions/(?:[^/])*/invocations" {
content_by_lua_block {
local function say(res, status)
ngx.header["x-amzn-RequestId"] = "foo"
ngx.status = status

if type(res) == 'string' then
ngx.header["Content-Length"] = #res + 1
ngx.say(res)

else
ngx.req.discard_body()
ngx.header['Content-Length'] = 0
end

ngx.exit(0)
end

ngx.sleep(.2) -- mock some network latency

local invocation_type = ngx.var.http_x_amz_invocation_type
if invocation_type == 'Event' then
say(nil, 202)

elseif invocation_type == 'DryRun' then
say(nil, 204)
end

local qargs = ngx.req.get_uri_args()
ngx.req.read_body()
local args = require("cjson").decode(ngx.req.get_body_data())

say(string.format("%q", qargs.key1 or args.key1), 200)
}
}
}
}
1 change: 1 addition & 0 deletions spec/fixtures/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
127.0.0.1 lambda.us-east-1.amazonaws.com localhost
2 changes: 2 additions & 0 deletions spec/kong_tests.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ cassandra_keyspace = kong_tests
cassandra_timeout = 10000
anonymous_reports = off

dns_hostsfile = spec/fixtures/hosts

lua_package_path = ?/init.lua;./kong/?.lua

nginx_worker_processes = 1
Expand Down

0 comments on commit b2511de

Please sign in to comment.