APISIX单元测试准备工作与问题解决方案

最近电脑更新,换了M1的,一切工作重新开始,意味着APISIX插件开发之路中的绊脚石——单测环境准备,也要重新准备。

由于升级了芯片和最新的macOS系统(12.2.1),导致过程并没有想象中的顺利,之前没遇到过的坑,一个个的暴露出来了,这里做一个分享和BackUp。

还有想吐槽一下APISIX官方的文档,在关于单元测试和插件编写方面的内容,输出不是特别的多,而且散落各处,着实给开发人员带来了麻烦。

以下的操作都是基于APISIX源码下操作(https://github.com/apache/apisix),写该文时版本为V2.12.1。

正常安装流程

  1. 安装各种环境依赖
    # install OpenResty, etcd and some compilation tools
    brew install openresty/brew/openresty luarocks lua@5.1 etcd curl git pcre openldap cpanminus

    # start etcd server
    brew services start etcd

官方最新源码中提供了脚本,位于utils/install-dependencies.sh。直接执行这个脚本就可以对上方的依赖进行安装和启动。

官方文档

  1. 修改环境变量

结合实际安装情况修改.bash_profile文件,并别忘了 source ~/.bash_profile

OPENRESTY_HOME=/usr/local/openresty
PATH=$OPENRESTY_HOME/nginx/sbin:$OPENRESTY_HOME/bin:$PATH
export OPENRESTY_HOME

  1. 下载APISIX源码
git clone https://github.com/apache/apisix.git
  1. 源码环境下处理
# 源码根目录下
git clone https://github.com/iresty/test-nginx.git
rm -rf test-nginx/.git
sudo cpanm --notest Test::Nginx IPC::Run > build.log 2>&1 || (cat build.log && exit 1)
export PERL5LIB=.:$PERL5LIB
make deps

我看官方推荐使用 make deps ENV_LUAROCKS_SERVER=https://luarocks.cn,但我试了,并没有起效,仍旧使用 https://luarocks.org,后来我通过修改luarocks配置文件 ~/.luarocks/config-5.1.lua ,生效了,大家也可以自己试试,在文章下方做个反馈。

APISIX文档

rocks_servers = {
    "https://luarocks.cn"
}

由于github的网络原因,make deps过程可能不是一帆风顺,有点耐心多试几次就可以了。

  1. 下载 toolkit 文件

t/目录下,执行

git clone https://github.com/api7/test-toolkit toolkit
rm -rf toolkit/.git

这里需要重命名为toolkit,否则引用找不到。

这里如果不下载这个源码会导致,toolkit.json 找不到的问题出现。

https://github.com/apache/apisix/issues/2936

  1. 启动APISIX服务(非必须,可以根据实际情况)

本人是通过Docker方式启动

git clone https://github.com/apache/apisix-docker.git

cd apisix-docker/compose/

docker-compose -f docker-compose.yaml -p docker-apisix up -d

这里需要📢注意,在上面我们已经启动了etcd,这里也会启动etcd,所以只要选择其中一个就可以了。

  1. 测试一下

在源码根目录下,执行

prove -Itest-nginx/lib -r t/plugin/limit-conn.t

正常情况下这样子就可以启动了。

但现实没有这么顺利,而且上面的流程已经是我采坑的结果了。

问题与解决方案

最新版本下无法使用 brew 安装 openresty

% brew install openresty/brew/openresty
Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 10 formulae.

==> Tapping openresty/brew
Cloning into '/opt/homebrew/Library/Taps/openresty/homebrew-brew'...
remote: Enumerating objects: 2866, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 2866 (delta 22), reused 10 (delta 6), pack-reused 2832
Receiving objects: 100% (2866/2866), 627.61 KiB | 638.00 KiB/s, done.
Resolving deltas: 100% (1610/1610), done.
Error: Invalid formula: /opt/homebrew/Library/Taps/openresty/homebrew-brew/Formula/php-session-nginx-module.rb
php-session-nginx-module: Calling bottle :unneeded is disabled! There is no replacement.
Please report this issue to the openresty/brew tap (not Homebrew/brew or Homebrew/core):
  /opt/homebrew/Library/Taps/openresty/homebrew-brew/Formula/php-session-nginx-module.rb:8

官方的解决方案是将brew版本。https://github.com/openresty/openresty/issues/814

而我选择手动安装openresty

tar -xzvf openresty-VERSION.tar.gz
// openresty-VERSION/ 目录

./configure --prefix=/usr/local/openresty --with-http_addition_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module --with-http_v2_module --with-pcre=/Users/demo/installapps/pcre-8.45 --with-openssl=/Users/demo/installapps/openssl

make

make install

安装 openresty 需要依赖pcre和openssl,所以这个我们也得手动安装

  • openssl:https://blog.csdn.net/qyee16/article/details/72799852
  • pcre:https://www.cnblogs.com/Jordandan/p/10402912.html

这里需要注意的是pcre要使用1,不要使用2,否则会有如下问题

src/core/ngx_regex.h:15:10: fatal error: 'pcre.h' file not found

#include <pcre.h>

^~~~~~~~

1 error generated.

make[1]: *** [objs/src/core/nginx.o] Error 1

make: *** [install] Error 2

参考文献

不要忘记修改环境变量哦。

make deps 时找不到路径问题

可以通过使用ln -s软链解决。

nginx报错

跑单测的时候,报如下错误

nginx: [emerg] "listen" directive is not allowed here in /Users/gjason/project/apisix-all/apisix/t/servroot/conf/nginx.conf:188

可能是数据格式错误导致 nginx.conf配置错误。

也可能是Nginx没有安装好,比如缺少上面提到的pcre和openssl导致的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值