Skip to content

Commit 0b904a5

Browse files
committed
merged pk-test as a subtree to pk-test
2 parents 91246b1 + 02cb3fb commit 0b904a5

11 files changed

+1125
-0
lines changed

pk-test/.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
*~
2+
.*~
3+
\#*\#
4+
.\#*\#
5+
6+
.DS_Store
7+
._.DS_Store
8+
.project
9+
.settings
10+
.directory
11+
12+
*.kdevelop.*
13+
*.kdevelop
14+
*.kdevses
15+
*.kdev4
16+
/Doxyfile
17+
/tags
18+
19+
nohup.out
20+
*.log
21+
.idea
22+
*.iml
23+
24+
*.kate_swp
25+
tmp/*
26+
*.kate_swps

pk-test/bin/pk-test

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
ROOT="${BASH_SOURCE[0]}";
6+
if([ -h "${ROOT}" ]) then
7+
while([ -h "${ROOT}" ]) do ROOT=$(readlink "${ROOT}"); done
8+
fi
9+
ROOT=$(cd $(dirname "${ROOT}")/../ && pwd)
10+
11+
NAME="pk-test"
12+
MODULE="pk-test/run"
13+
14+
if [ "$1" = "--rockless" ]; then
15+
shift
16+
ROCK_DIR="$(dirname $0)/../"
17+
else
18+
ROCK_DIR="$(luarocks show --rock-dir ${NAME})"
19+
fi
20+
PRIVATE_MODULES_PATH="${ROCK_DIR}/?.lua"
21+
22+
WORKDIR="$(pwd)"
23+
while [ ! -d "${WORKDIR}/test/cases" -a ! "${WORKDIR}" = "/" ]; do
24+
WORKDIR="$(dirname $WORKDIR)"
25+
done
26+
if [ "${WORKDIR}" = "/" ]; then
27+
echo "${NAME}: test/cases not found" >&2
28+
exit 1
29+
fi
30+
31+
TEST_CASES_PATH="${WORKDIR}/test/cases"
32+
PRIVATE_MODULES_PATH="${PRIVATE_MODULES_PATH};${WORKDIR}/?.lua"
33+
34+
exec pk-call-lua-module \
35+
"${PRIVATE_MODULES_PATH}" \
36+
"${MODULE}" \
37+
"run" \
38+
"--root=${ROOT}" \
39+
"--test-cases-path=${TEST_CASES_PATH}" \
40+
"$@"

pk-test/docs/env.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Переменные окружения
2+
====================
3+
4+
PK_TEST_BASE_PORT -- базовый порт для тестирования "слушающих" сервисов
5+
(каждый новый тест получит следующий порт, начиная с base)
6+
По умолчанию 50000
7+
PK_TEST_DB_HOST -- хост с тестовым MySQL (по умолчанию localhost)
8+
PK_TEST_DB_PORT -- порт MySQL (по умолчанию 3306)
9+
PK_TEST_DB_NAME -- имя тестовой базы MySQL (база должна существовать,
10+
по умолчанию "pk-test")
11+
PK_TEST_DB_USER -- имя пользователя MySQL для тестов (по умолчанию root)
12+
PK_TEST_DB_PASSWORD пароль тестового пользователя MySQL (по умолчанию 12345)
13+
14+
Примечание: большая часть переменных непосредственно кодом pk-test никак не
15+
распознается, но является policy для тестового окружения, и должно
16+
распознаваться самими тестами.

pk-test/pk-test/http_client.lua

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
--------------------------------------------------------------------------------
2+
-- http_client.lua: test-only client-side utilities for http
3+
-- This file is a part of pk-test library
4+
-- Copyright (c) Alexander Gladysh <[email protected]>
5+
-- Copyright (c) Dmitry Potapov <[email protected]>
6+
-- See file `COPYRIGHT` for the license
7+
--------------------------------------------------------------------------------
8+
9+
local socket = require 'socket'
10+
require 'socket.http'
11+
require 'socket.url'
12+
13+
--------------------------------------------------------------------------------
14+
15+
local arguments,
16+
optional_arguments,
17+
method_arguments
18+
= import 'lua-nucleo/args.lua'
19+
{
20+
'arguments',
21+
'optional_arguments',
22+
'method_arguments'
23+
}
24+
25+
local ensure,
26+
ensure_equals,
27+
ensure_tequals,
28+
ensure_tdeepequals,
29+
ensure_strequals,
30+
ensure_fails_with_substring,
31+
ensure_returns,
32+
ensure_error
33+
= import 'lua-nucleo/ensure.lua'
34+
{
35+
'ensure',
36+
'ensure_equals',
37+
'ensure_tequals',
38+
'ensure_tdeepequals',
39+
'ensure_strequals',
40+
'ensure_fails_with_substring',
41+
'ensure_returns',
42+
'ensure_error'
43+
}
44+
45+
local make_concatter = import 'lua-nucleo/string.lua' { 'make_concatter' }
46+
47+
local make_loggers
48+
= import 'pk-core/log.lua'
49+
{
50+
'make_loggers'
51+
}
52+
53+
--------------------------------------------------------------------------------
54+
55+
local log, dbg, spam, log_error = make_loggers("http_client", "HCL")
56+
57+
--------------------------------------------------------------------------------
58+
59+
-- Note this function intentionally does not prepend "?" to the query
60+
local build_simple_http_query = function(param)
61+
arguments(
62+
"table", param
63+
)
64+
65+
local cat, concat = make_concatter()
66+
67+
local first = true
68+
for k, v in pairs(param) do
69+
if not first then
70+
cat "&"
71+
else
72+
first = false
73+
end
74+
75+
cat (socket.url.escape(assert(tostring(k)))) "=" (socket.url.escape(assert(tostring(v))))
76+
end
77+
78+
return concat()
79+
end
80+
81+
local check_post_request = function(url, param, expected_checker)
82+
arguments(
83+
"string", url,
84+
"table", param,
85+
"function", expected_checker
86+
)
87+
88+
expected_checker(
89+
ensure(
90+
"do post query",
91+
socket.http.request(url, build_simple_http_query(param))
92+
)
93+
)
94+
end
95+
96+
local check_post_request_simple = function(url, param, expected)
97+
arguments(
98+
"string", url,
99+
"table", param,
100+
"string", expected
101+
)
102+
103+
check_post_request(
104+
url,
105+
param,
106+
function(response)
107+
ensure_strequals(
108+
"request matches expected",
109+
response,
110+
expected
111+
)
112+
end
113+
)
114+
end
115+
116+
--------------------------------------------------------------------------------
117+
118+
return
119+
{
120+
build_simple_http_query = build_simple_http_query;
121+
check_post_request = check_post_request;
122+
check_post_request_simple = check_post_request_simple;
123+
}

pk-test/pk-test/http_server.lua

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
--------------------------------------------------------------------------------
2+
-- http_server.lua: test-only xavante http server
3+
-- This file is a part of pk-test library
4+
-- Copyright (c) Alexander Gladysh <[email protected]>
5+
-- Copyright (c) Dmitry Potapov <[email protected]>
6+
-- See file `COPYRIGHT` for the license
7+
--------------------------------------------------------------------------------
8+
9+
local xavante = require 'xavante'
10+
11+
--------------------------------------------------------------------------------
12+
13+
local arguments
14+
= import 'lua-nucleo/args.lua'
15+
{
16+
'arguments'
17+
}
18+
19+
local empty_table
20+
= import 'lua-nucleo/table.lua'
21+
{
22+
'empty_table'
23+
}
24+
25+
--------------------------------------------------------------------------------
26+
27+
-- TODO: Support non-static pages.
28+
-- TODO: Support WSAPI pages via standard WSAPI-Xavante interface.
29+
local make_http_tcp_server_loop
30+
do
31+
local make_url_responder = function(url, headers, body)
32+
arguments(
33+
"string", url,
34+
"table", headers,
35+
"string", body
36+
)
37+
38+
return function(req, res)
39+
res.headers = headers -- TODO: tclone?
40+
res:send_headers()
41+
res:send_data(body)
42+
end
43+
end
44+
45+
make_http_tcp_server_loop = function(url_list)
46+
return function(host, port)
47+
-- WARNING: Entire work with Xavante MUST happen inside this function.
48+
-- Xavante is not designed to be run in a several instances
49+
-- from the same Lua state. This function is a TCP server
50+
-- loop, usually being run from a fork, so the above limitation
51+
-- is bearable.
52+
53+
declare 'cookies' -- Xavante wants to access this global
54+
55+
local rules = { }
56+
for url, url_info in pairs(url_list) do
57+
rules[#rules + 1] =
58+
{
59+
match = url;
60+
with = make_url_responder(
61+
url,
62+
url_info.headers or empty_table,
63+
url_info[1]
64+
);
65+
}
66+
end
67+
68+
xavante.HTTP
69+
{
70+
server = { host = host; port = port };
71+
72+
defaultHost =
73+
{
74+
rules = rules;
75+
};
76+
}
77+
78+
xavante.start()
79+
end
80+
end
81+
end
82+
83+
--------------------------------------------------------------------------------
84+
85+
return
86+
{
87+
make_http_tcp_server_loop = make_http_tcp_server_loop;
88+
}

pk-test/pk-test/log.lua

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--------------------------------------------------------------------------------
2+
-- log.lua
3+
-- This file is a part of pk-test library
4+
-- Copyright (c) Alexander Gladysh <[email protected]>
5+
-- Copyright (c) Dmitry Potapov <[email protected]>
6+
-- See file `COPYRIGHT` for the license
7+
--------------------------------------------------------------------------------
8+
9+
local posix = require "posix"
10+
11+
--------------------------------------------------------------------------------
12+
13+
local LOG_LEVEL,
14+
wrap_file_sink,
15+
make_common_logging_config
16+
= import 'lua-nucleo/log.lua'
17+
{
18+
'LOG_LEVEL',
19+
'wrap_file_sink',
20+
'make_common_logging_config'
21+
}
22+
23+
local create_common_logging_system,
24+
get_current_logsystem_date_microsecond
25+
= import 'pk-core/log.lua'
26+
{
27+
'create_common_logging_system',
28+
'get_current_logsystem_date_microsecond'
29+
}
30+
31+
--------------------------------------------------------------------------------
32+
33+
local init_test_logging_system,
34+
update_test_logging_system_pid
35+
do
36+
local LOG_LEVEL_CONFIG =
37+
{
38+
[LOG_LEVEL.ERROR] = true;
39+
[LOG_LEVEL.LOG] = true;
40+
[LOG_LEVEL.DEBUG] = true;
41+
[LOG_LEVEL.SPAM] = true;
42+
}
43+
44+
local LOG_MODULE_CONFIG =
45+
{
46+
-- Empty; everything is enabled by default.
47+
}
48+
49+
local logging_system_id = "{TTTTT} "
50+
51+
local get_logging_system_id = function()
52+
return logging_system_id
53+
end
54+
55+
update_test_logging_system_pid = function()
56+
logging_system_id = "{"..("%05d"):format(posix.getpid("pid")).."} "
57+
end
58+
59+
init_test_logging_system = function()
60+
local logging_config = make_common_logging_config(
61+
LOG_LEVEL_CONFIG,
62+
LOG_MODULE_CONFIG
63+
)
64+
create_common_logging_system(
65+
get_logging_system_id,
66+
wrap_file_sink(io.stdout),
67+
logging_config,
68+
get_current_logsystem_date_microsecond
69+
)
70+
end
71+
end
72+
73+
--------------------------------------------------------------------------------
74+
75+
return
76+
{
77+
init_test_logging_system = init_test_logging_system;
78+
update_test_logging_system_pid = update_test_logging_system_pid;
79+
}

0 commit comments

Comments
 (0)