openresty開發 test-nginx 使用文檔

test-nginx 使用文檔

install
git clone https://github.com/agentzh/test-nginx.git
cd test-nginx & perl Makefile.PL
sudo make install

若是缺乏包的話,能夠經過 cpan 裝;例如 裝 Test:Morenginx

sudo perl -MCPAN -e shell
cpan[1]> install Test:LongString
geting started

文件目錄組織:
通常是在一個項目下新建 t 目錄來存放測試集
以lua-resty-redis 爲例:https://github.com/agentzh/lua-resty-redis
有 Makefile ,test 選項裏有重要的一句:
prove -I../test-nginx/lib -r t
這裏有調用 perl 的 prove 指令,prove 會去執行測試
t 目錄下有 不少個 .t 文件,裏面就是測試案例
編寫測試案例:git

一個測試案例主要有三部份內容:
程序代碼: nginx location 的配置內容
輸入: http 的 request 信息
輸出檢查: status,header,body,error_log 檢查github

https://github.com/agentzh/lua-resty-redis/blob/master/t/sanity.t
仍是以 lua-resty-redis 的一個爲例,第 25 行以後的纔是每一個測試案例的內容
即完成一個測試案例裏的那三項內容
=== Test 1:
— request 表示輸入是 發一塊兒 http GET 請求 /t
GET /t
— config 表示 nginx.conf 裏 location /t 裏的內容redis


— response_body 表示對 http 返回的 body 進行匹配檢查
— no_error_log 表示會對 nginx 的 error.log 檢查,必須沒有 EORROR 級別的記錄
更完成的輸入,輸出檢查項 定義,能夠看 https://github.com/agentzh/test-nginx/blob/master/lib/Test/Nginx/Socket.pm
1870 行以後有完整的文檔
進一步解釋, test-nginx 執行流程:shell

  1. 組裝 nginx.conf,啓動 nginx 實例進程
    根據咱們在 Makefile 裏配置的 PATH,和每個 .t 文件最前面的一些配置項網絡

會組裝一個完整的 nginx.conf 文件
把 t/servroot 當成 nginx 的工做目錄,啓動 nginx 實例tcp

  1. 發起 http 請求,根據每一個測試案例的提供的信息測試

  2. 檢查 http 的返回項
    包括 http status,http response header, http response bodylua

更多測試項:
etcproxy 能夠在 tcp 層上作一個延時轉發,模擬測試 bad 網絡環境
valgrind 內存泄露檢查
stap 檢查; 調用 systemtap 作輸出檢查rest

相關文章
相關標籤/搜索