Skip to content

Commit

Permalink
tests(openresty): Enables testing through resty
Browse files Browse the repository at this point in the history
Now it is possible to run tests as if it was run from within openresty. It will also use openresty DB connectors if applicable.
Usage: sailor test --resty
Known problems: No way to pass flags to Busted
  • Loading branch information
Etiene committed Nov 9, 2015
1 parent 56d31ed commit a5f4cd0
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 27 deletions.
15 changes: 13 additions & 2 deletions sailor
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,22 @@ local function create()
end

local function test()
local resty = false
local flags = ''
for i=2,#arg do
flags = flags..arg[i]..' '
if arg[i] == '--resty' then
resty = true
else
flags = flags..arg[i]..' '
end
end
local ok, code = os.execute('busted --helper=tests/bootstrap.lua '..flags..'tests/unit/* tests/functional/*')
local ok, code
if resty then
ok, code = os.execute('resty tests/bootstrap_resty.lua')
else
ok, code = os.execute('busted --helper=tests/bootstrap.lua '..flags..'tests/unit/* tests/functional/*')
end

if type(ok) == "number" then return ok end -- Lua 5.1 just returns the status code
return ok and 0 or 1 -- don't care about actual value
end
Expand Down
9 changes: 8 additions & 1 deletion src/remy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ local remy = {
MODE_MOD_PLUA = 1,
MODE_NGINX = 2,
MODE_LWAN = 3,
MODE_LIGHTTPD = 4
MODE_LIGHTTPD = 4,
}

forced_mode = nil

local emu = {}

-- The values below will be updated during runtime
Expand Down Expand Up @@ -124,6 +126,7 @@ end
-- Detects the Lua environment
function remy.detect(native_request)
local mode = nil
if forced_mode then return forced_mode end
if package.loaded.cgilua ~= nil then
mode = remy.MODE_CGILUA
elseif package.loaded.ngx ~= nil then
Expand Down Expand Up @@ -178,4 +181,8 @@ function remy.sha1(str)
return sha1(str)
end

function remy.force_mode(mode)
forced_mode = mode
end

return remy
17 changes: 10 additions & 7 deletions src/sailor/db.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ local main_conf = require "conf.conf"
local conf = main_conf.db[main_conf.sailor.environment]
local remy = require "remy"

local db

if remy.detect() == remy.MODE_NGINX and conf.driver == "mysql" then
db = require "sailor.db.resty_mysql"
else
db = require "sailor.db.luasql"
function detect()
local m
if remy.detect() == remy.MODE_NGINX and conf.driver == "mysql" then
m = require "sailor.db.resty_mysql"
else
m = require "sailor.db.luasql"
end
m.detect = detect
return m
end

return db
return detect()
6 changes: 3 additions & 3 deletions src/sailor/db/resty_mysql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ local mysql = require "resty.mysql"
local db = {instance = nil}

local luasql = require("luasql."..conf.driver)

function db.instantiate()
if not db.instance then
local instance, err = mysql:new()
if not instance then
error("Failed to instantiate mysql: ".. err)
error("Failed to instantiate mysql: ".. (err or ''))
end
db.instance = instance
end
end
-- Creates the connection of the instance
function db.connect()
db.instantiate()
conf.host = string.gsub(conf.host, "localhost", "127.0.0.1")

conf.host = string.gsub(conf.host, "localhost", "127.0.0.1")
local ok, err, errno, sqlstate = db.instance:connect{
host = conf.host,
port = 3306,
Expand All @@ -36,6 +35,7 @@ function db.connect()
password = conf.pass,
max_packet_size = 1024 * 1024
}

if not ok then
error("Failed to connect to database: ".. err ..": ".. (errno or '') .." "..(sqlstate or ''))
end
Expand Down
33 changes: 19 additions & 14 deletions src/sailor/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,41 @@

local sailor = require "sailor"
local lfs = require "lfs"
local db = require "sailor.db"
local M = {req = {}, page = nil}

local M = {req = {}}

local page
local body
local function write(_,data) body = body .. data end

-- Prepares for making a request
function M:prepare(write_func, headers_in)
function M:prepare(headers_in)
headers_in = headers_in or {}
M.req = { uri = '', write = write_func, puts = write_func, headers_in = headers_in, headers_out = {} }
page = sailor.init(M.req)
self.req = { uri = '', write = write, puts = write, headers_in = headers_in, headers_out = {} }
self.page = sailor.init(M.req)
return self
end

-- Loads tests fixtures into the database
-- Warning, this will truncate the table, make sure you have configured a test database
-- Returns table with objects created
function load_fixtures(model_name)
local db = require "sailor.db"


local Model = sailor.model(model_name)
local fixtures = require("tests.fixtures."..model_name) or {}
local objects = {}
db.connect()
db.query('truncate table ' .. Model.db.table .. ';') -- Reseting current state
db.close()


for _,v in pairs(fixtures) do -- loading fixtures
local o = Model:new(v)
o:save(false)
table.insert(objects, o)
end
return objects

end

function M.load_fixtures(model_name)
Expand Down Expand Up @@ -74,18 +78,19 @@ end
-- body: string. The body of the response.
-- headers: table. Any headers out that were set.
-- redirected: function. The above redirect function


function M.request(path, data, additional_headers)
local conf = require "conf.conf"
data = data or {}
local body = ''
local function write(_,data) body = body .. data end
body = ''
conf.sailor.friendly_urls = false

M:prepare(write,additional_headers)
page.POST = data.post or {}
page.GET = data.get or {}
page.GET[conf.sailor.route_parameter] = path
local status = sailor.route(page)
M:prepare(additional_headers)
M.page.POST = data.post or {}
M.page.GET = data.get or {}
M.page.GET[conf.sailor.route_parameter] = path
local status = sailor.route(M.page)

return {status = status, body = body, headers = M.req.headers_out, redirected = redirected}
end
Expand Down
61 changes: 61 additions & 0 deletions test/dev-app/tests/bootstrap_resty.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
sailor = require "sailor"
local t = require "sailor.test"
local busted = require 'busted.runner'
local lfs = require 'lfs'

-- load fixtures

t.load_fixtures('user')
t.load_fixtures('category')
t.load_fixtures('post')

-- prepare busted
busted()

local busted_lib = {
describe = describe,
it = it,
setup = setup,
assert = assert,
teardown = teardown,
before_each = before_each,
finally = finally,
pending = pending,
spy = spy,
stub = stub,
mock = mock,
async = async,
done = done
}

local test_dirs = {
"tests/unit",
"tests/functional",
}

-- Get busted vars to pass them ahead when loading each test file

local env = {}
for k,v in pairs(_G) do env[k] = v end
for name,f in pairs(busted_lib) do
env[name] = f
end

-- Looping through test dirs and loading them with environment with busted vars

for _,dir in pairs(test_dirs) do
dir = sailor.path..'/'..dir
for file in lfs.dir(dir) do
if file ~= '.' and file ~= '..' then
local t
if _VERSION == "Lua 5.1" then
t = assert(loadfile(dir..'/'..file))
setfenv(t,env)
else
t = assert(loadfile(dir..'/'..file, env))
end
t()
end
end
end

0 comments on commit a5f4cd0

Please sign in to comment.