Kong的自动化测试是busted(https://github.com/Olivine-Labs/busted)做的。Kong的Makefile里记录了所有的一切:
DEV_ROCKS = busted luacheck lua-llthreads2
BUSTED_ARGS ?= -v
TEST_CMD ?= bin/busted $(BUSTED_ARGS)
OPENSSL_DIR ?= /usr/local/opt/openssl
.PHONY: install dev lint test test-integration test-plugins test-all
install:
@luarocks make OPENSSL_DIR=$(OPENSSL_DIR)
dev: install
@for rock in $(DEV_ROCKS) ; do \
if ! luarocks list | grep $$rock > /dev/null ; then \
echo $$rock not found, installing via luarocks... ; \
luarocks install $$rock ; \
else \
echo $$rock already installed, skipping ; \
fi \
done;
test:
@$(TEST_CMD) spec/01-unit
test-integration:
@$(TEST_CMD) spec/02-integration
test-plugins:
@$(TEST_CMD) spec/03-plugins
test-all:
@$(TEST_CMD) spec/
执行make dev 命令,Kong会借助luarocks自动安装busted luacheck lua-llthreads2这三个组件,make test-*会调用busted执行测试用例。
通常,make 会把其要执行的命令行在命令执行前输出到屏幕上。当我们用“@”字符在 命令行前,那么,这个命令将不被 make 显示出来,最具代表性的例子是,我们用这个功能 来像屏幕显示一些信息。如: @echo 正在编译 XXX 模块...... 当 make 执行时,会输出“正在编译 XXX 模块......”字串,但不会输出命令,如果没 有“@”,那么,make 将输出: echo 正在编译 XXX 模块...... 正在编译 XXX 模块......
以上摘自《跟我一起写 Makefile》作者:陈皓
busted不能在 OpenResty 上下文中运行测试,可是Kong做到了,怎么回事?秘密出在 TEST_CMD ?= bin/busted $(BUSTED_ARGS) 上面,我们看看bin/busted究竟是个什么东西:
#!/usr/bin/env resty
require("kong.core.globalpatches")({cli = true})
-- force LuaSocket usage to resolve `/etc/hosts` until
-- supported by resty-cli.
-- See https://github.com/Mashape/kong/issues/1523
for _, namespace in ipairs({"cassandra", "pgmoon-mashape"}) do
local socket = require(namespace .. ".socket")
socket.force_luasocket(ngx.get_phase(), true)
end
package.path = "?/init.lua;"..package.path
if ngx ~= nil then
ngx.exit = function()end
end
-- Busted command-line runner
require 'busted.runner'({ standalone = false })
参考https://segmentfault.com/a/1190000007178147,再看看bin/busted的第一行,用resty启动脚本,而不是用lua启动脚本,最后一行就是启动busted。
luarocks是什么时候安装的?Kong的安装文件里自动安装了luarocks。(https://github.com/Mashape/kong-distributions/blob/master/.build-package-script.sh#L191~L199)
# Install LuaRocks cd $TMP wget http://luarocks.org/releases/luarocks-$LUAROCKS_VERSION.tar.gz tar xzf luarocks-$LUAROCKS_VERSION.tar.gz cd luarocks-$LUAROCKS_VERSION ./configure --with-lua-include=/usr/local/include/luajit-2.1 --lua-suffix=jit --lua-version=5.1 --with-lua=/usr/local make build make install DESTDIR=$OUT cd $OUT