nginxtips
Nginx本地test
介绍
- https://github.com/iresty/programming-openresty-zh/blob/master/testing/README.adoc
- https://openresty.gitbooks.io/programming-openresty/content/
步骤
- 切换root用户或者sudo 执行相关命令
- nginx 启停更,或者读写文件需要。避免权限错误。所以后续操作统一用root级用户角色
- 本地安装好nginx
$ which nginx
/usr/local/bin/nginx
$ nginx -v
nginx version: openresty/1.19.3.1
- 安装 test::Nginx
通过cpan安装(默认已经安装了perl,cpan)
$ cpan -v
$ cpan Test::Nginx
- luacov 安装(不是必须的)
- 下载源码:https://github.com/lunarmodules/luacov/tree/v0.15.0/src
- cp luacov 到对应的lua_package_path 下即可。 比如 net-cache/nginx/share_lua 下
- chash动态库 mac 编译(也不是必须的,用到的话,linux和mac不同)
- 如果本地没有chash的C代码,可以参考 https://github.com/openresty/lua-resty-balancer/tree/master
- 下载.chash.c, chash.h, Makefile 到本地
- make all 即可,生成libchash.dylib
- mv libchash.dylib 到 nginx/for_test_lib 下
- 增加root用户组
// 针对nginx: [emerg] getgrnam("root") failed in nginx.conf 错误
// 原因是mac下root用户不是在root用户组,nginx.conf 默认user group相同
// 新建root用户组,并指定id 为2024
$ sudo dscl . -create /Groups/root PrimaryGroupID 2024
// 添加root用户到root用户组中
$ sudo dscl . -append /Groups/root GroupMembership root
- 新建测试目录t
$ cd your-codedir/nginx/conf/lua_dir
$ sudo mkdir t
$ touch test.sh
- 新建自定义shell脚本
$ cat test.sh
#! /usr/bin/env bash
export PATH=/usr/local/bin/nginx:$PATH
exec prove "$@"
- 尝试运行下ut
$ ./test.sh test/your_testfile.t
- 如果出错,很多内容,不好分辨
- 可以尝试启动测试nginx,排除环境问题。如果nginx可以正常启动,那么就是测试文件 test/qtest_scheduler.t的问题,按照提示修正
$ nginx -p your-codedir/nginx/conf/lua/t/servroot/ -c your-codedir/nginx/conf/lua/t/servroot/conf/nginx.conf