Nginx本地test

介绍

步骤

  • 切换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
// 针对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