Skip to content

Commit

Permalink
wip: fix tests with new config
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed May 27, 2016
1 parent 4353d92 commit fbb40ad
Show file tree
Hide file tree
Showing 34 changed files with 368 additions and 867 deletions.
10 changes: 10 additions & 0 deletions bin/busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env resty

package.path = "?/init.lua;"..package.path

if ngx ~= nil then
ngx.exit = function()end
end

-- Busted command-line runner
require 'busted.runner'({ standalone = false })
2 changes: 2 additions & 0 deletions kong/api/app.lua → kong/api/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ local function parse_params(fn)
return responses.send_HTTP_BAD_REQUEST("Cannot parse JSON body")
end
end
local inspect = require "inspect"
print(inspect(self.params))
self.params = api_helpers.normalize_nested_params(self.params)
return fn(self, ...)
end)
Expand Down
2 changes: 1 addition & 1 deletion kong/cmd/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local function execute(args)
prefix = args.prefix
}))

local dao = DAOFactory(conf, conf.plugins)
local dao = DAOFactory(conf)
assert(dao:run_migrations())
assert(nginx_conf_compiler.prepare_prefix(conf, conf.prefix))
assert(serf_signals.start(conf, conf.prefix, dao))
Expand Down
3 changes: 2 additions & 1 deletion kong/cmd/utils/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ local _LEVELS = {
verbose = 2,
info = 3,
warn = 4,
error = 5
error = 5,
quiet = 6
}

local r_levels = {}
Expand Down
1 change: 1 addition & 0 deletions kong/cmd/utils/nginx_conf_compiler.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local NGINX_VARS = {
plugins = true,
cluster_listen = true,
cluster_listen_rpc = true,
database = true,
pg_host = true,
pg_port = true,
Expand Down
4 changes: 2 additions & 2 deletions kong/dao/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ local function load_daos(self, schemas, constraints, events_handler)
end
end

function Factory:new(kong_config, plugins, events_handler)
function Factory:new(kong_config, events_handler)
self.db_type = kong_config.database
self.daos = {}
self.kong_config = kong_config
self.plugin_names = plugins or {}
self.plugin_names = kong_config.plugins or {}

local schemas = {}
local DB = require("kong.dao."..self.db_type.."_db")
Expand Down
2 changes: 1 addition & 1 deletion kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function Kong.init(config)
local events = Events()

-- instanciate long-lived DAO
local dao = DAOFactory(config, config.plugins, events)
local dao = DAOFactory(config, events)
assert(dao:run_migrations()) -- migrating in case embedded in custom nginx

-- populate singletons
Expand Down
2 changes: 1 addition & 1 deletion kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ server {
ngx.exit(204)
end
require('apis').serve('kong.api.app')
require('lapis').serve('kong.api')
}
}
Expand Down
73 changes: 67 additions & 6 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,77 @@ local BIN_PATH = "bin/kong"
local TEST_CONF_PATH = "spec/kong_tests.conf"

local conf_loader = require "kong.conf_loader"
local DAOFactory = require "kong.dao.factory"
local pl_stringx = require "pl.stringx"
local pl_utils = require "pl.utils"
local pl_path = require "pl.path"
local pl_file = require "pl.file"
local pl_dir = require "pl.dir"
local http = require "resty.http"
local log = require "kong.cmd.utils.log"

log.set_lvl(log.levels.quiet) -- disable stdout logs in tests

---------------
-- Conf and DAO
---------------
local conf = assert(conf_loader(TEST_CONF_PATH))

--local DAOFactory = require "kong.dao.factory"
--local dao = DAOFactory(conf, conf.plugins)
local dao = DAOFactory(conf)
-- make sure migrations are up-to-date
--assert(dao:run_migrations())

--------------------
-- Custom properties
--------------------
local admin_port = string.match(conf.admin_listen, ":([%d]+)$")
local proxy_port = string.match(conf.proxy_listen, ":([%d]+)$")

-----------------
-- Custom helpers
-----------------
local resty_http_proxy_mt = {}

function resty_http_proxy_mt:send(opts)
local cjson = require "cjson"
local utils = require "kong.tools.utils"

opts = opts or {}
local body
local headers = opts.headers or {}
local content_type = headers["Content-Type"]
local t_body_table = type(opts.body) == "table"
if string.find(content_type, "application/json") and t_body_table then
body = cjson.encode(opts.body)
elseif string.find(content_type, "www-form-urlencoded", nil, true) and t_body_table then
body = utils.encode_args(opts.body, true) -- true: not % encoded
end

if body then
opts.body = body
end

return self:request(opts)
end

function resty_http_proxy_mt:__index(k)
local f = rawget(resty_http_proxy_mt, k)
if f then
return f
end

return self.client[k]
end

local function http_client(host, port, timeout)
timeout = timeout or 500
local client = assert(http.new())
assert(client:connect(host, port))
client:set_timeout(timeout)
return setmetatable({
client = client
}, resty_http_proxy_mt)
end

--------------------
-- Custom assertions
--------------------
Expand All @@ -40,7 +96,8 @@ local function res_status(state, args)
table.insert(args, 1, expected)
return false
end
return true, {res:read_body()}
local body = pl_stringx.strip(res:read_body())
return true, {body}
end
say:set("assertion.res_status.negative", [[
Invalid response status code.
Expand Down Expand Up @@ -80,16 +137,20 @@ return {
execute = pl_utils.executeex,

-- Kong testing properties
dao = dao,
bin_path = BIN_PATH,
test_conf = conf,
test_conf_path = TEST_CONF_PATH,
proxy_port = proxy_port,
admin_port = admin_port,

-- Kong testing helpers
kong_exec = kong_exec,
http_client = http_client,
prepare_prefix = function(prefix)
prefix = prefix or conf.prefix
pl_dir.makepath(prefix)
kong_exec("stop", prefix)
return pl_dir.makepath(prefix)
--kong_exec("stop", prefix)
end,
clean_prefix = function(prefix)
prefix = prefix or conf.prefix
Expand Down
12 changes: 6 additions & 6 deletions spec/integration/01-dao/01-factory_spec.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
local utils = require "spec.spec_helpers"
local helpers = require "spec.integration.01-dao.helpers"
local Factory = require "kong.dao.factory"

utils.for_each_dao(function(db_type, default_options)
describe("Model Factory with DB: #"..db_type, function()
helpers.for_each_dao(function(kong_conf)
describe("Model Factory with DB: #"..kong_conf.database, function()
it("should be instanciable", function()
local factory
assert.has_no_errors(function()
factory = Factory(db_type, default_options)
factory = Factory(kong_conf)
end)

assert.True(factory:is(Factory))
assert.is_table(factory.daos)
assert.equal(db_type, factory.db_type)
assert.equal(kong_conf.database, factory.db_type)
end)
it("should have shorthands to access the underlying daos", function()
local factory = Factory(db_type, default_options)
local factory = Factory(kong_conf)
assert.equal(factory.daos.apis, factory.apis)
assert.equal(factory.daos.consumers, factory.consumers)
assert.equal(factory.daos.plugins, factory.plugins)
Expand Down
41 changes: 23 additions & 18 deletions spec/integration/01-dao/02-migrations_spec.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
local helpers = require "spec.integration.01-dao.helpers"
local utils = require "kong.tools.utils"
local helpers = require "spec.spec_helpers"

local Factory = require "kong.dao.factory"

helpers.for_each_dao(function(db_type, default_opts, TYPES)
describe("Model migrations with DB: #"..db_type, function()
helpers.for_each_dao(function(kong_config)
describe("Model migrations with DB: #"..kong_config.database, function()
local factory
setup(function()
local f = Factory(db_type, default_opts)
local f = Factory(kong_config)
f:drop_schema()
end)
before_each(function()
factory = Factory(db_type, default_opts)
factory = Factory(kong_config)
end)

describe("current_migrations()", function()
Expand All @@ -20,20 +20,20 @@ helpers.for_each_dao(function(db_type, default_opts, TYPES)
assert.falsy(err)
assert.same({}, cur_migrations)
end)
it("should return errors", function()
local invalid_opts = utils.shallow_copy(default_opts)
if db_type == TYPES.CASSANDRA then
invalid_opts.keyspace = "_inexistent_"
elseif db_type == TYPES.POSTGRES then
invalid_opts.database = "_inexistent_"
pending("should return errors", function()
local invalid_conf = utils.shallow_copy(kong_config)
if invalid_conf.database == "cassandra" then
invalid_conf.cassandra_keyspace = "_inexistent_"
elseif invalid_conf.database == "postgres" then
invalid_conf.pg_database = "_inexistent_"
end

local xfactory = Factory(db_type, invalid_opts)
local xfactory = Factory(invalid_conf)

local cur_migrations, err = xfactory:current_migrations()
if db_type == TYPES.CASSANDRA then
if kong_config.database == "cassandra" then
assert.same({}, cur_migrations)
elseif db_type == TYPES.POSTGRES then
elseif kong_config.database == "postgres" then
assert.truthy(err)
assert.falsy(cur_migrations)
assert.True(err.db)
Expand Down Expand Up @@ -66,12 +66,17 @@ helpers.for_each_dao(function(db_type, default_opts, TYPES)
-- Integration behavior.
-- Must run in order.
describe("[INTEGRATION]", function()
local n_ids = 0
local flatten_migrations = {}
setup(function()
factory:drop_schema()
for identifier, migs in pairs(factory:migrations_modules()) do
n_ids = n_ids + 1
for _, mig in ipairs(migs) do
flatten_migrations[#flatten_migrations + 1] = {identifier = identifier, name = mig.name}
flatten_migrations[#flatten_migrations + 1] = {
identifier = identifier,
name = mig.name
}
end
end
end)
Expand All @@ -86,12 +91,12 @@ helpers.for_each_dao(function(db_type, default_opts, TYPES)
assert.falsy(err)
assert.True(ok)

assert.spy(on_migration).was_called(1)
assert.spy(on_migration).was_called(n_ids)
assert.spy(on_success).was_called(#flatten_migrations)

for _, mig in ipairs(flatten_migrations) do
assert.spy(on_migration).was_called_with(mig.identifier)
assert.spy(on_success).was_called_with(mig.identifier, mig.name)
assert.spy(on_migration).was_called_with(mig.identifier, factory:infos())
assert.spy(on_success).was_called_with(mig.identifier, mig.name, factory:infos())
end
end)
it("should return the migrations recorded as executed", function()
Expand Down
8 changes: 4 additions & 4 deletions spec/integration/01-dao/03-crud_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ say:set("assertion.raw_table.positive", "Expected %s\nto be a raw table")
say:set("assertion.raw_table.negative", "Expected %s\nto not be a raw_table")
assert:register("assertion", "raw_table", raw_table, "assertion.raw_table.positive", "assertion.raw_table.negative")

local utils = require "spec.spec_helpers"
local helpers = require "spec.integration.01-dao.helpers"
local Factory = require "kong.dao.factory"

local api_tbl = {
Expand All @@ -30,11 +30,11 @@ local api_tbl = {
upstream_url = "https://mockbin.com"
}

utils.for_each_dao(function(db_type, default_options, TYPES)
describe("Model (CRUD) with DB: #"..db_type, function()
helpers.for_each_dao(function(kong_config)
describe("Model (CRUD) with DB: #"..kong_config.database, function()
local factory, apis
setup(function()
factory = Factory(db_type, default_options)
factory = Factory(kong_config)
apis = factory.apis
assert(factory:run_migrations())
end)
Expand Down
10 changes: 5 additions & 5 deletions spec/integration/01-dao/04-constraints_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local utils = require "kong.tools.utils"
local helpers = require "spec.spec_helpers"
local helpers = require "spec.integration.01-dao.helpers"
local Factory = require "kong.dao.factory"
local utils = require "kong.tools.utils"

local api_tbl = {
name = "mockbin",
Expand All @@ -14,12 +14,12 @@ local plugin_tbl = {
name = "key-auth"
}

helpers.for_each_dao(function(db_type, default_options, TYPES)
describe("Model (Constraints) with DB: #"..db_type, function()
helpers.for_each_dao(function(kong_config)
describe("Model (Constraints) with DB: #"..kong_config.database, function()
local plugin_fixture, api_fixture
local factory, apis, plugins
setup(function()
factory = Factory(db_type, default_options)
factory = Factory(kong_config)
apis = factory.apis
plugins = factory.plugins
assert(factory:run_migrations())
Expand Down
8 changes: 4 additions & 4 deletions spec/integration/01-dao/05-use_cases_spec.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local helpers = require "spec.spec_helpers"
local helpers = require "spec.integration.01-dao.helpers"
local Factory = require "kong.dao.factory"

helpers.for_each_dao(function(db_type, default_options, TYPES)
describe("Real use-cases with DB: #"..db_type, function()
helpers.for_each_dao(function(kong_config)
describe("Real use-cases with DB: #"..kong_config.database, function()
local factory
setup(function()
factory = Factory(db_type, default_options)
factory = Factory(kong_config)
assert(factory:run_migrations())

factory:truncate_tables()
Expand Down
Loading

0 comments on commit fbb40ad

Please sign in to comment.