[tests] Add simple lua test

personal/stbuehler/wip
Stefan Bühler 13 years ago
parent 8ffcd3e246
commit e87fc4007a

@ -20,6 +20,7 @@ Each test class can provide Prepare, Run and Cleanup handlers (CurlRequest alrea
A test intance has the following attributes:
* config: vhost config (error/access log and vhost handling gets added)
* plain_config: gets added before all vhost configs (define global actions here)
* name: unique test name, has a sane default
* vhost: the vhost name; must be unique if a config is provided;
GroupTest will set the vhost of subtests to the vhost of the GroupTest
@ -72,11 +73,12 @@ def load_test_file(name):
return module
def vhostname(testname):
return testname[1:-1].replace('/', '.')
return testname[1:-1].replace('/', '.').lower()
# basic interface
class TestBase(object):
config = None
plain_config = None
name = None
vhost = None
runnable = True
@ -96,18 +98,22 @@ class TestBase(object):
def _prepare(self):
self.Prepare()
if None != self.plain_config:
self.tests.append_config(("\n# %s \n" % (self.name)) + self.plain_config)
if None != self.config:
errorlog = self.PrepareFile("log/error.log-%s" % self.vhost, "")
accesslog = self.PrepareFile("log/access.log-%s" % self.vhost, "")
config = """
# %s
var.vhosts = var.vhosts + [ "%s" : ${
log = [ "*": "file:%s" ];
accesslog = "%s";
%s
}
];
""" % (self.vhost, errorlog, accesslog, self.config)
self.tests.append_config(config)
""" % (self.name, self.vhost, errorlog, accesslog, self.config)
self.tests.append_vhosts_config(config)
def _run(self):
failed = False
@ -212,6 +218,7 @@ class Tests(object):
self.tests = [] # all tests (we always prepare/cleanup all tests)
self.tests_dict = { }
self.config = None
self.vhosts_config = None
self.testname_len = 60
self.prepared_dirs = { }
@ -248,6 +255,11 @@ class Tests(object):
raise BaseException("Not prepared for adding config")
self.config += config
def append_vhosts_config(self, config):
if None == self.vhosts_config:
raise BaseException("Not prepared for adding config")
self.vhosts_config += config
def LoadTests(self):
files = os.listdir(Env.sourcedir)
files = filter(lambda x: x[-3:] == '.py', files)
@ -302,10 +314,14 @@ var.vhosts = [ "default": ${{
}} ];
""".format(Env = Env, errorlog = errorlog, accesslog = accesslog)
self.vhosts_config = ""
for t in self.tests:
print >> Env.log, "[Start] Preparing test '%s'" % (t.name)
t._prepare()
self.config += self.vhosts_config
self.config += """
vhost.map var.vhosts;
"""
@ -340,7 +356,7 @@ allow-listen {{ ip "127.0.0.1:{Env.port}"; }}
COLOR_BLUE = Env.color and "\033[1;34m" or ""
COLOR_GREEN = Env.color and "\033[1;32m" or ""
COLOR_YELLOW = Env.color and "\033[1;33m" or ""
COLOR_RED = Env.color and "\033[1;38m" or ""
COLOR_RED = Env.color and "\033[1;31m" or ""
COLOR_CYAN = Env.color and "\033[1;36m" or ""
PASS = COLOR_GREEN + "[PASS]" + COLOR_RESET
@ -369,6 +385,9 @@ allow-listen {{ ip "127.0.0.1:{Env.port}"; }}
self.stat_fail += 1
failed = True
i += 1
Env.log.flush()
sys.stdout.flush()
sys.stderr.flush()
self.failed = failed
print >> sys.stdout, ("%i out of %i tests passed (%.2f%%), %i tests failed, %i todo items, %i todo items are ready" %
(self.stat_pass, testcount, (100.0 * self.stat_pass)/testcount, self.stat_fail, self.stat_todo, self.stat_done))

@ -10,10 +10,43 @@ TEST_TXT="""Hi!
0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
"""
class Test(CurlRequest):
LUA_SHOW_ENV_INFO="""
function show_env_info(vr)
if vr:handle_direct() then
vr.resp.status = 200
vr.resp.headers["Content-Type"] = "text/plain"
vr.out:add(vr.env["INFO"])
end
end
actions = show_env_info
"""
class TestSimpleRequest(CurlRequest):
URL = "/test.txt"
EXPECT_RESPONSE_BODY = TEST_TXT
EXPECT_RESPONSE_CODE = 200
class TestSimpleInfo(CurlRequest):
URL = "/?a_simple_query"
EXPECT_RESPONSE_BODY = "a_simple_query"
EXPECT_RESPONSE_CODE = 200
config = """
env.set "INFO" => "%{req.query}";
show_env_info;
"""
class Test(GroupTest):
group = [TestSimpleRequest,TestSimpleInfo]
def Prepare(self):
self.PrepareFile("www/default/test.txt", TEST_TXT)
show_env_info_lua = self.PrepareFile("lua/show_env_info.lua", LUA_SHOW_ENV_INFO)
self.plain_config = """
show_env_info {{
lua.handler "{show_env_info_lua}";
}}
""".format(show_env_info_lua = show_env_info_lua)

Loading…
Cancel
Save