xwill

To be double-cool

04 Oct 2024

Nginx本地test

介绍

步骤

  • 切换root用户或者sudo 执行相关命令
  • nginx 启停更,或者读写文件需要。避免权限错误。所以后续操作统一用root级用户角色
  • 本地安装好nginx
1
2
3
4
$ which nginx
/usr/local/bin/nginx
$ nginx -v
nginx version: openresty/1.19.3.1
  • 安装 test::Nginx
1
2
3
通过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用户组

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// 针对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
1
2
3
4
$ cd your-codedir/nginx/conf/lua_dir

$ sudo mkdir t
$ touch test.sh
  • 新建自定义shell脚本
1
2
3
4
5
$ cat test.sh
#! /usr/bin/env bash
export PATH=/usr/local/bin/nginx:$PATH

exec prove "$@"
  • 尝试运行下ut
1
$ ./test.sh test/your_testfile.t
  • 如果出错,很多内容,不好分辨
  • 可以尝试启动测试nginx,排除环境问题。如果nginx可以正常启动,那么就是测试文件 test/qtest_scheduler.t的问题,按照提示修正
1
$ nginx -p your-codedir/nginx/conf/lua/t/servroot/ -c your-codedir/nginx/conf/lua/t/servroot/conf/nginx.conf