Skip to content

Commit

Permalink
Merge pull request #9 from Kong/feat/update-to-new-db-and-pdk
Browse files Browse the repository at this point in the history
feat(serverless-functions) update to new db and pdk
  • Loading branch information
kikito authored Oct 17, 2018
2 parents 0bf3b74 + e7cbd68 commit 84089e6
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ env:
before_install:
- git clone https://$GITHUB_TOKEN:@github.com/Kong/kong-ci.git
- source kong-ci/setup_env.sh
- git clone https://github.com/Kong/kong.git kong-ce
- git clone --single-branch -b next https://github.com/Kong/kong.git kong-ce

install:
- luarocks make
Expand Down
40 changes: 17 additions & 23 deletions kong/plugins/post-function/schema.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
local Errors = require "kong.dao.errors"
local typedefs = require "kong.db.schema.typedefs"


local function check_functions(value)
for i, entry in ipairs(value) do
local _, err = loadstring(entry)
if err then
return false, Errors.schema("Error parsing post-function #" .. i .. ": " .. err)
end
local function validate_function(fun)
local _, err = loadstring(fun)
if err then
return false, "Error parsing post-function: " .. err
end

return true
end


return {
no_consumer = true,
api_only = true,

name = "post-function",
fields = {
functions = {
required = true,
type = "array",
}
{ consumer = typedefs.no_consumer },
{ config = {
type = "record",
fields = {
{ functions = {
required = true, type = "array",
elements = { type = "string", custom_validator = validate_function },
}, },
},
},
},
},

self_check = function(schema, config, dao, is_updating)
local _, err = check_functions(config.functions)
if err then
return false, err
end

return true
end,
}
40 changes: 17 additions & 23 deletions kong/plugins/pre-function/schema.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
local Errors = require "kong.dao.errors"
local typedefs = require "kong.db.schema.typedefs"


local function check_functions(value)
for i, entry in ipairs(value) do
local _, err = loadstring(entry)
if err then
return false, Errors.schema("Error parsing pre-function #" .. i .. ": " .. err)
end
local function validate_function(fun)
local _, err = loadstring(fun)
if err then
return false, "Error parsing pre-function: " .. err
end

return true
end


return {
no_consumer = true,
api_only = true,

name = "pre-function",
fields = {
functions = {
required = true,
type = "array",
}
{ consumer = typedefs.no_consumer },
{ config = {
type = "record",
fields = {
{ functions = {
required = true, type = "array",
elements = { type = "string", custom_validator = validate_function },
}, },
},
},
},
},

self_check = function(schema, config, dao, is_updating)
local _, err = check_functions(config.functions)
if err then
return false, err
end

return true
end,
}
116 changes: 61 additions & 55 deletions spec/01-pre-function/01-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,63 +29,69 @@ describe("Plugin: pre-function (access)", function()
local client, admin_client

setup(function()
helpers.dao:run_migrations()

local api1 = assert(helpers.dao.apis:insert {
name = "api-1",
hosts = { "api1.pre-function.com" },
upstream_url = helpers.mock_upstream_url,
})
assert(helpers.dao.plugins:insert {
name = "pre-function",
api_id = api1.id,
config = {
local bp, db = helpers.get_db_utils()

assert(db:truncate())

local service = bp.services:insert {
name = "service-1",
host = helpers.mock_upstream_host,
port = helpers.mock_upstream_port,
}

local route1 = bp.routes:insert {
service = { id = service.id },
hosts = { "one.pre-function.com" },
}

local route2 = bp.routes:insert {
service = { id = service.id },
hosts = { "two.pre-function.com" },
}

local route3 = bp.routes:insert {
service = { id = service.id },
hosts = { "three.pre-function.com" },
}

local route4 = bp.routes:insert {
service = { id = service.id },
hosts = { "four.pre-function.com" },
}

bp.plugins:insert {
name = "pre-function",
route = { id = route1.id },
config = {
functions = { mock_fn_one }
},
})

local api2 = assert(helpers.dao.apis:insert {
name = "api-2",
hosts = { "api2.pre-function.com" },
upstream_url = helpers.mock_upstream_url,
})
assert(helpers.dao.plugins:insert {
name = "pre-function",
api_id = api2.id,
config = {
functions = { mock_fn_two }
},
})

local api3 = assert(helpers.dao.apis:insert {
name = "api-3",
hosts = { "api3.pre-function.com" },
upstream_url = helpers.mock_upstream_url,
})
assert(helpers.dao.plugins:insert {
name = "pre-function",
api_id = api3.id,
config = {
functions = { mock_fn_three }
},
})

local api4 = assert(helpers.dao.apis:insert {
name = "api-4",
hosts = { "api4.pre-function.com" },
upstream_url = helpers.mock_upstream_url,
})
assert(helpers.dao.plugins:insert {
name = "pre-function",
api_id = api4.id,
config = {
functions = { mock_fn_four, mock_fn_five }
}

bp.plugins:insert {
name = "pre-function",
route = { id = route2.id },
config = {
functions = { mock_fn_two }
},
}

bp.plugins:insert {
name = "pre-function",
route = { id = route3.id },
config = {
functions = { mock_fn_three }
},
})
}

bp.plugins:insert {
name = "pre-function",
route = { id = route4.id },
config = {
functions = { mock_fn_four, mock_fn_five }
},
}

assert(helpers.start_kong({
custom_plugins = "pre-function",
nginx_conf = "spec/fixtures/custom_nginx.template",
}))

Expand All @@ -107,7 +113,7 @@ describe("Plugin: pre-function (access)", function()
method = "GET",
path = "/status/200",
headers = {
["Host"] = "api1.pre-function.com"
["Host"] = "one.pre-function.com"
}
})

Expand All @@ -119,7 +125,7 @@ describe("Plugin: pre-function (access)", function()
method = "GET",
path = "/status/200",
headers = {
["Host"] = "api2.pre-function.com"
["Host"] = "two.pre-function.com"
}
})
local body = assert.res_status(404, res)
Expand All @@ -131,7 +137,7 @@ describe("Plugin: pre-function (access)", function()
method = "GET",
path = "/status/200",
headers = {
["Host"] = "api3.pre-function.com"
["Host"] = "three.pre-function.com"
}
})
local body = assert.res_status(406, res)
Expand All @@ -144,7 +150,7 @@ describe("Plugin: pre-function (access)", function()
method = "GET",
path = "/status/200",
headers = {
["Host"] = "api4.pre-function.com"
["Host"] = "four.pre-function.com"
}
})
local body = assert.res_status(400, res)
Expand Down
26 changes: 13 additions & 13 deletions spec/01-pre-function/02-schema_spec.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
local validate_entity = require("kong.dao.schemas_validation").validate_entity
local pre_schema = require "kong.plugins.pre-function.schema"
local v = require("spec.helpers").validate_plugin_config_schema

local mock_fn_one = 'print("hello world!")'
local mock_fn_two = 'local x = 1'
local mock_fn_invalid = 'print('

describe("pre-function schema", function()
it("validates single function", function()
local ok, err = validate_entity({ functions = { mock_fn_one } }, pre_schema)
local ok, err = v({ functions = { mock_fn_one } }, pre_schema)

assert.True(ok)
assert.is_nil(err)
assert.truthy(ok)
assert.falsy(err)
end)

it("validates multiple functions", function()
local ok, err = validate_entity({ functions = { mock_fn_one, mock_fn_two } }, pre_schema)
local ok, err = v({ functions = { mock_fn_one, mock_fn_two } }, pre_schema)

assert.True(ok)
assert.is_nil(err)
assert.truthy(ok)
assert.falsy(err)
end)

describe("errors", function()
it("with an invalid function", function()
local ok, _, err = validate_entity({ functions = { mock_fn_invalid } }, pre_schema)
local ok, err = v({ functions = { mock_fn_invalid } }, pre_schema)

assert.False(ok)
assert.equals("Error parsing pre-function #1: [string \"print(\"]:1: unexpected symbol near '<eof>'", err.message)
assert.falsy(ok)
assert.equals("Error parsing pre-function: [string \"print(\"]:1: unexpected symbol near '<eof>'", err.config.functions)
end)

it("with a valid and invalid function", function()
local ok, _, err = validate_entity({ functions = { mock_fn_one, mock_fn_invalid } }, pre_schema)
local ok, err = v({ functions = { mock_fn_one, mock_fn_invalid } }, pre_schema)

assert.False(ok)
assert.equals("Error parsing pre-function #2: [string \"print(\"]:1: unexpected symbol near '<eof>'", err.message)
assert.falsy(ok)
assert.equals("Error parsing pre-function: [string \"print(\"]:1: unexpected symbol near '<eof>'", err.config.functions)
end)
end)
end)
Loading

0 comments on commit 84089e6

Please sign in to comment.