APISIX單元測試準備工作與問題解決方案

跡_Jason發表於2022-03-10

最近電腦更新,換了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/api...

  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

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,所以這個我們也得手動安裝

這裡需要注意的是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導致的。

相關文章