diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5c97926..13039c2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,11 +22,11 @@ jobs: id: cache-rocks with: path: .rocks/ - key: cache-rocks-${{ matrix.runs-on }}-01 + key: cache-rocks-${{ matrix.tarantool }}-03 - run: echo $PWD/.rocks/bin >> $GITHUB_PATH - - run: tarantoolctl rocks make + - run: ./deps.sh - name: Build module run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index 9457d19..146bd87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ if(NOT CMAKE_BUILD_TYPE) endif() set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) +find_package(LuaTest) + # Find Tarantool and Lua dependecies set(TARANTOOL_FIND_REQUIRED ON) find_package(Tarantool) @@ -21,7 +23,7 @@ enable_testing() set (LUA_PATH "LUA_PATH=${PROJECT_SOURCE_DIR}/?.lua\\;${PROJECT_SOURCE_DIR}/?/init.lua\\;\\;") set (LUA_SOURCE_DIR "LUA_SOURCE_DIR=${PROJECT_SOURCE_DIR}") -add_test(http ${CMAKE_SOURCE_DIR}/test/http.test.lua) +add_test(http ${LUATEST}) set_tests_properties(http PROPERTIES ENVIRONMENT "${LUA_PATH};${LUA_SOURCE_DIR}") diff --git a/cmake/FindLuaTest.cmake b/cmake/FindLuaTest.cmake new file mode 100644 index 0000000..04b039e --- /dev/null +++ b/cmake/FindLuaTest.cmake @@ -0,0 +1,12 @@ +find_program(LUATEST luatest + HINTS .rocks/ + PATH_SUFFIXES bin + DOC "Lua testing framework" +) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(LuaTest + REQUIRED_VARS LUATEST +) + +mark_as_advanced(LUATEST) diff --git a/deps.sh b/deps.sh new file mode 100755 index 0000000..0c4f125 --- /dev/null +++ b/deps.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# Call this script to install test dependencies. + +set -e + +# Test dependencies: +tarantoolctl rocks install luatest 0.5.5 + +tarantoolctl rocks make diff --git a/test/helpers.lua b/test/helpers.lua new file mode 100644 index 0000000..f55f42f --- /dev/null +++ b/test/helpers.lua @@ -0,0 +1,94 @@ +local fio = require('fio') +local http_server = require('http.server') +local http_client = require('http.client') + +local helpers = table.copy(require('luatest').helpers) + +helpers.base_port = 12345 +helpers.base_host = '127.0.0.1' +helpers.base_uri = ('http://%s:%s'):format(helpers.base_host, helpers.base_port) + +helpers.cfgserv = function() + local path = os.getenv('LUA_SOURCE_DIR') or './' + path = fio.pathjoin(path, 'test') + + local httpd = http_server.new(helpers.base_host, helpers.base_port, { + app_dir = path, + log_requests = false, + log_errors = false + }) + :route({path = '/abc/:cde/:def', name = 'test'}, function() end) + :route({path = '/abc'}, function() end) + :route({path = '/ctxaction'}, 'module.controller#action') + :route({path = '/absentaction'}, 'module.controller#absent') + :route({path = '/absent'}, 'module.absent#action') + :route({path = '/abc/:cde'}, function() end) + :route({path = '/abc_:cde_def'}, function() end) + :route({path = '/abc-:cde-def'}, function() end) + :route({path = '/aba*def'}, function() end) + :route({path = '/abb*def/cde', name = 'star'}, function() end) + :route({path = '/banners/:token'}) + :helper('helper_title', function(self, a) return 'Hello, ' .. a end) + :route({path = '/helper', file = 'helper.html.el'}) + :route({path = '/test', file = 'test.html.el' }, + function(cx) return cx:render({ title = 'title: 123' }) end) + + return httpd +end + +local log_queue = {} + +helpers.clear_log_queue = function() + log_queue = {} +end + +helpers.custom_logger = { + debug = function() end, + verbose = function() + table.insert(log_queue, { + log_lvl = 'verbose', + }) + end, + info = function(...) + table.insert(log_queue, { + log_lvl = 'info', + msg = string.format(...) + }) + end, + warn = function(...) + table.insert(log_queue, { + log_lvl = 'warn', + msg = string.format(...) + }) + end, + error = function(...) + table.insert(log_queue, { + log_lvl = 'error', + msg = string.format(...) + }) + end +} + +helpers.find_msg_in_log_queue = function(msg, strict) + for _, log in ipairs(log_queue) do + if not strict then + if log.msg:match(msg) then + return log + end + else + if log.msg == msg then + return log + end + end + end +end + +helpers.teardown = function(httpd) + httpd:stop() + helpers.retrying({}, function() + local r = http_client.request('GET', helpers.base_uri) + return r == nil + end) +end + +return helpers diff --git a/test/http.test.lua b/test/http.test.lua deleted file mode 100755 index 396cbaf..0000000 --- a/test/http.test.lua +++ /dev/null @@ -1,524 +0,0 @@ -#!/usr/bin/env tarantool - -local tap = require('tap') -local fio = require('fio') -local http_lib = require('http.lib') -local http_client = require('http.client') -local http_server = require('http.server') -local json = require('json') -local yaml = require 'yaml' -local urilib = require('uri') - -local test = tap.test("http") -test:plan(8) - -test:test("split_uri", function(test) - test:plan(65) - local function check(uri, rhs) - local lhs = urilib.parse(uri) - local extra = { lhs = lhs, rhs = rhs } - if lhs.query == '' then - lhs.query = nil - end - test:is(lhs.scheme, rhs.scheme, uri.." scheme", extra) - test:is(lhs.host, rhs.host, uri.." host", extra) - test:is(lhs.service, rhs.service, uri.." service", extra) - test:is(lhs.path, rhs.path, uri.." path", extra) - test:is(lhs.query, rhs.query, uri.." query", extra) - end - check('http://abc', { scheme = 'http', host = 'abc'}) - check('http://abc/', { scheme = 'http', host = 'abc', path ='/'}) - check('http://abc?', { scheme = 'http', host = 'abc'}) - check('http://abc/?', { scheme = 'http', host = 'abc', path ='/'}) - check('http://abc/?', { scheme = 'http', host = 'abc', path ='/'}) - check('http://abc:123', { scheme = 'http', host = 'abc', service = '123' }) - check('http://abc:123?', { scheme = 'http', host = 'abc', service = '123'}) - check('http://abc:123?query', { scheme = 'http', host = 'abc', - service = '123', query = 'query'}) - check('http://domain.subdomain.com:service?query', { scheme = 'http', - host = 'domain.subdomain.com', service = 'service', query = 'query'}) - check('google.com', { host = 'google.com'}) - check('google.com?query', { host = 'google.com', query = 'query'}) - check('google.com/abc?query', { host = 'google.com', path = '/abc', - query = 'query'}) - check('https://google.com:443/abc?query', { scheme = 'https', - host = 'google.com', service = '443', path = '/abc', query = 'query'}) -end) - -test:test("template", function(test) - test:plan(5) - test:is(http_lib.template("<% for i = 1, cnt do %> <%= abc %> <% end %>", - {abc = '1 <3>&" ', cnt = 3}), - ' 1 <3>&" 1 <3>&" 1 <3>&" ', - "tmpl1") - test:is(http_lib.template("<% for i = 1, cnt do %> <%= ab %> <% end %>", - {abc = '1 <3>&" ', cnt = 3}), - ' nil nil nil ', "tmpl2") - local r, msg = pcall(http_lib.template, "<% ab() %>", {ab = '1'}) - test:ok(r == false and msg:match("call local 'ab'") ~= nil, "bad template") - - -- gh-18: rendered tempate is truncated - local template = [[ - -
-<%= i %> | -<%= v %> | -