9. OpenResty
At least the following Lua libraries and Nginx modules can be used with this ngx_lua module:
lua‑resty‑memcached
lua‑resty‑mysql
lua‑resty‑redis
lua‑resty‑dns
lua‑resty‑upload
lua‑resty‑websocket
lua‑resty‑lock
etc ...
9
10. ex.) lua‑resty‑redis
server {
location /test {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ok, err = red:set("dog", "an animal")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
10
11. Test Suite
Nginx version >= 1.4.2
Perl modules:
Test::Nginx: https://github.com/openrety/test‑nginx
Nginx modules:
ngx_devel_kit
ngx_set_misc
ngx_auth_request (this is not needed if you're using Nginx 1.5.4+.
ngx_echo
ngx_memc
ngx_srcache
etc ...
11
24. Test::Nginxとは
Test::Nginx ‑ Data‑driven test scaffold for Nginx C module and Nginx/OpenResty‑based libraries
and applications
OpenRestyの作者が作ったNginxのテストライブラリ(perl)
実際にnginxを起こしてそこにリクエストしてテストする
詳細は後述
24
29. Test::Nginx Runnning Tests
$ prove -v t/foo.t t/bar.t t/baz.t
$ prove -v t/*.t
$ prove -r t/
$ prove -v t/foo.t
t/foo.t ..
ok 1 - TEST 1: hello, world - status code ok
ok 2 - TEST 1: hello, world - response_body - response is expected (req 0)
1..2
ok
All tests successful.
Files=1, Tests=2, 0 wallclock secs (0.01 usr 0.01 sys + 0.07 cusr 0.03 csys = 0.12 CPU
Result: PASS
29
30. Test::Nginx Running Individual Test Blocks
=== TEST 1: hello, world
This is just a simple demonstration of the
echo directive provided by ngx_http_echo_module.
--- config
location = /t {
echo "hello, world!";
}
--- request
GET /t
--- response_body
hello, world!
--- ONLY
30
31. Test::Nginx Skipping Tests
=== TEST 1: test for the future
--- config
location /t {
some_fancy_directive;
}
--- request
GET /t
--- response_body
blah blah blah
--- SKIP
31
32. Test::Nginx Test Running Order
ファイル名順
t/000-sanity.t
t/001-set.t
t/002-content.t
t/003-errors.t
...
t/139-ssl-cert-by.t
Test::Nginx Test Block Running Order
デフォルトはランダム。
use Test::Nginx::Socket 'no_plan'; # ランダムをやめる (名前順?)
no_shuffle(); # 上から順
run_tests();
__DATA__
...
32
33. Test::Nginx Preparing Test ‑test code‑
__DATA__
=== TEST 1:
--- main_config
env MY_ENVIRONMENT;
--- http_config
init_worker_by_lua_block {
print("init")
}
--- config
location = /t {
echo ok;
}
--- request
GET /t
--- response_body
ok
33
35. Test::Nginx Preparing Requests
--- request
GET /t
--- request
GET /t HTTP/1.0
--- request
GET /t
--- more_headers
Foo: bar
Bar: baz
--- pipelined_requests eval
["GET /t", "GET /t"]
--- response_body eval
["okn", "okn"]
35
36. Test::Nginx Checking Responses
=== TEST 1:
--- config
location = /t {
echo "Life is short.";
echo "Moon is bright.";
echo "Sun is shining.";
}
--- request
GET /t
--- response_body
Life is short.
Moon is deem.
Sun is shining.
36
38. Test::Nginx Checking NGINX Error Logs
--- error_log
Hello world from my server
--- error_log eval
[
"This is a dog!",
qr/w+ is a cat?/,
]
--- error_log eval
qr/w+ is a cat?/
--- no_error_log
[error]
38