From f4d38348d83ce34d5d771a5a2f37dbd793357b5c Mon Sep 17 00:00:00 2001 From: cong Date: Thu, 14 Nov 2024 19:14:54 +0800 Subject: [PATCH] add luacov --- .cspell.json | 1 + .github/workflows/build.yaml | 59 ++++++++++++++++++- .gitignore | 2 + .luacov | 8 +++ README.md | 2 + scripts/alb-test-actions.sh | 2 +- scripts/run-like-ci-nginx.sh | 1 + scripts/run-like-github-actions.sh | 2 + template/actions/alb-nginx-install-deps.sh | 19 +++++- template/actions/alb-nginx.sh | 19 ++++-- template/nginx/lua/balancer/balance.lua | 1 + template/nginx/lua/config/cache.lua | 12 ++++ template/nginx/lua/config/policy_fetch.lua | 3 + .../nginx/lua/phase/init_worker_phase.lua | 2 +- template/t/AlaudaLib.pm | 24 +++++++- template/t/e2e/error_page/error.t | 4 -- template/t/e2e/https_upgrade/test.lua | 6 +- template/t/e2e/https_upgrade/test.t | 2 +- template/t/e2e/metrics/metrics.t | 3 +- template/t/e2e/ping/ping.t | 2 - template/t/e2e/trace/trace.lua | 45 +++++++------- template/t/e2e/trace/trace.t | 4 -- template/t/lib/mock_worker_init.lua | 36 +++++++---- template/t/lib/policy_helper.lua | 3 + template/t/unit/unit_test.t | 2 +- 25 files changed, 202 insertions(+), 62 deletions(-) create mode 100644 .luacov diff --git a/.cspell.json b/.cspell.json index f1799217..1287550b 100644 --- a/.cspell.json +++ b/.cspell.json @@ -3,6 +3,7 @@ "caseSensitive": false, "words": [ //proper noun + "luacov", "strftime", "ncruces", "satyrius", diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 5668fa22..16869bec 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -39,6 +39,9 @@ jobs: build-alb: name: Build alb runs-on: ubuntu-22.04 + outputs: + go-coverage: ${{ steps.alb-test.outputs.GO_COVERAGE }} + lua-coverage: ${{ steps.nginx-test.outputs.LUA_COVERAGE }} strategy: matrix: platform: @@ -91,6 +94,7 @@ jobs: name: ${{ env.artifactName }} path: ${{ env.artifactPath }} - name: test alb go + id: alb-test if: ${{ (matrix.platform == 'linux/amd64' ) && (!inputs.skip_test) }} env: MATRIX_PLATFORM: "${{ matrix.platform }}" @@ -98,13 +102,22 @@ jobs: set -x echo "FROM --platform=\${{ matrix.platform }} | $MATRIX_PLATFORM " ./scripts/run-like-github-actions.sh test-alb-go + go_cov=$(grep total ./coverage.report | awk '{print $3}' | awk -F% '{print $1}' ) + echo "go_cov is |||$go_cov|||" + echo "GO_COVERAGE=$go_cov" >> $GITHUB_OUTPUT + cat $GITHUB_OUTPUT - name: test alb nginx + id: nginx-test if: ${{ (matrix.platform == 'linux/amd64' ) && (!inputs.skip_test) }} env: MATRIX_PLATFORM: "${{matrix.platform }}" run: | set -x ./scripts/run-like-github-actions.sh test-alb-nginx + cat ./luacov.summary + line_cov=$(cat ./luacov.summary |grep Total|awk '{print $4}'|awk -F% '{print $1}') + echo "LUA_COVERAGE=$line_cov" >> $GITHUB_OUTPUT + cat $GITHUB_OUTPUT release-alb: name: Release alb runs-on: ubuntu-22.04 @@ -156,6 +169,8 @@ jobs: run: | echo "gen alb chart" env + export branch=$(echo "${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" | sed 's|/|-|g') + echo "$branch" ./scripts/run-like-github-actions.sh gen-chart-artifact - uses: actions/upload-artifact@v4 with: @@ -170,4 +185,46 @@ jobs: DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} DOCKER_CLI_EXPERIMENTAL: enabled run: | - ./scripts/run-like-github-actions.sh release-alb \ No newline at end of file + ./scripts/run-like-github-actions.sh release-alb + update-coverage: + name: update coverage + runs-on: ubuntu-22.04 + needs: [build-alb,release-alb] + env: + GO_COVERAGE: ${{ needs.build-alb.outputs.go-coverage }} + LUA_COVERAGE: ${{ needs.build-alb.outputs.lua-coverage }} + steps: + - name: debug + run: | + env + set -x + echo "GO_COVERAGE=$GO_COVERAGE" + echo "LUA_COVERAGE=$LUA_COVERAGE" + - name: Checkout gh-pages + uses: actions/checkout@v3 + with: + ref: gh-pages + - name: Create Badges + shell: bash + run: | + set -x + npm i -g badgen-cli + GO_COLOR=$(node -p '+process.env.GO_COVERAGE >= 95 ? `green` : `orange`') + LUA_COLOR=$(node -p '+process.env.LUA_COVERAGE >= 95 ? `green` : `orange`') + mkdir -p badges + badgen -j go-coverage -s "$GO_COVERAGE%" -c $GO_COLOR > badges/go-coverage.svg + badgen -j lua-coverage -s $LUA_COVERAGE% -c $LUA_COLOR > badges/lua-coverage.svg + - name: Deploy Badges + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "Update badges [skip ci]" + branch: gh-pages + skip_fetch: true + skip_checkout: true + # Without this, will get Error: + # Can't find 'action.yml', 'action.yaml' or 'Dockerfile' under '/home/runner/work/coverage-badge-action/coverage-badge-action/action.yml'. + # Did you forget to run actions/checkout before running your local action? + - name: Checkout Back + uses: actions/checkout@v3 + with: + ref: ${{ github.ref }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 618690c3..8504eb33 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,5 @@ template/nginx/lua/vendor/opentelemetry *.kconf docs/.obsidian template/share/dhparam.pem +luacov-html/ +luacov.stats.out diff --git a/.luacov b/.luacov new file mode 100644 index 00000000..01e52bd8 --- /dev/null +++ b/.luacov @@ -0,0 +1,8 @@ +reporter = "html" +html = {} +include = { + ".*/template/nginx/lua/.*" +} +exclude = { + ".*/template/nginx/lua/vendor/.*", +} diff --git a/README.md b/README.md index d8f937f0..4e81295c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # ALB -- Another Load Balancer +[![go-cov](https://alauda.github.io/alb/badges/go-coverage.svg)](https://alauda.github.io/alb/badges/go-coverage.svg) +[![lua-cov](https://alauda.github.io/alb/badges/lua-coverage.svg)](https://alauda.github.io/alb/badges/lua-coverage.svg) ALB (Another Load Balancer) is a Kubernetes Gateway powered by [OpenResty](https://github.com/openresty/) with years of production experience from Alauda. diff --git a/scripts/alb-test-actions.sh b/scripts/alb-test-actions.sh index 1d5e40e0..0551d736 100644 --- a/scripts/alb-test-actions.sh +++ b/scripts/alb-test-actions.sh @@ -31,7 +31,7 @@ function alb-go-test-all-with-coverage() { sed -e '1i\mode: atomic' ./coverage.txt >./coverage.txt.all cat ./coverage.txt.all | grep -v main.go | grep -v test_utils | grep -v "albctl/cmd" | grep -v "deepcopy.go" | grep -v "pkg/apis/alauda/" >./coverage.txt - alb-go-coverage-gen ./coverage.txt + alb-go-coverage-gen ./coverage.txt | tee ./coverage.report alb-go-coverage-show ./coverage.txt } diff --git a/scripts/run-like-ci-nginx.sh b/scripts/run-like-ci-nginx.sh index 857f3223..364d5b22 100755 --- a/scripts/run-like-ci-nginx.sh +++ b/scripts/run-like-ci-nginx.sh @@ -11,3 +11,4 @@ fi platform=${MATRIX_PLATFORM:-linux/amd64} echo "platform $platform" docker run --user root --network=host --platform $platform -v $PWD:/acp-alb-test -t $image sh -c 'cd /acp-alb-test ;/acp-alb-test/scripts/nginx-test.sh' +# docker run --user root --network=host --platform $platform -v $PWD:/acp-alb-test -it $image sh diff --git a/scripts/run-like-github-actions.sh b/scripts/run-like-github-actions.sh index 144100eb..bf4706ff 100755 --- a/scripts/run-like-github-actions.sh +++ b/scripts/run-like-github-actions.sh @@ -19,6 +19,8 @@ function alb-gh-test-alb-nginx() ( local image=$(docker images | grep theseedoaa/alb | head -1 | awk '{printf "%s:%s",$1,$2}') echo "test nginx $image" ./scripts/run-like-ci-nginx.sh $image + ls + cat ./luacov.summary ) function alb-gh-build-alb() ( diff --git a/template/actions/alb-nginx-install-deps.sh b/template/actions/alb-nginx-install-deps.sh index 170a7069..fcce80e0 100755 --- a/template/actions/alb-nginx-install-deps.sh +++ b/template/actions/alb-nginx-install-deps.sh @@ -14,11 +14,13 @@ openresty=$1 if [[ -n "$OPENRESTY_BUILD_TRARGRT_DIR" ]]; then openresty=$OPENRESTY_BUILD_TRARGRT_DIR fi -if [[ -z "$openresty" && -f "/usr/local/openresty" ]]; then + +if [[ -z "$openresty" && -d "/usr/local/openresty" ]]; then openresty="/usr/local/openresty" echo "use default $openresty " fi -if [[ -z "$openresty" && -f "/opt/openresty" ]]; then + +if [[ -z "$openresty" && -d "/opt/openresty" ]]; then openresty="/opt/openresty" echo "use default $openresty " fi @@ -28,7 +30,13 @@ if [ -z "$openresty" ]; then exit 1 fi -export PATH=$openresty/bin:$PATH +function alb-ng-install-test-deps() ( + luarocks --lua-version 5.1 --tree $openresty/luajit install luacov + luarocks --lua-version 5.1 --tree $openresty/luajit install cluacov + luarocks --lua-version 5.1 install luacov + luarocks --lua-version 5.1 install luacov-console + luarocks --lua-version 5.1 install luacov-html +) function alb-ng-install-deps() ( env @@ -168,6 +176,7 @@ function install-lua-resty-http() ( rm -rf ./.$name return ) + function install-opentelemetry-lua() ( # md5sum ./opentelemetry-lua-0.2.6.zip # 77f4488e669c80d53c3d9977f35017ed ./opentelemetry-lua-0.2.6.zip @@ -204,6 +213,10 @@ function install-lua-resty-mlcache() ( rm -rf ./lua-resty-mlcache-$LUA_RESTY_MLCACHE_VERSION* ) +function install-lua-cov() ( + return +) + function install-lua-resty-cookie() ( # md5sum ./lua-resty-cookie-0.01.opm.tar cfd011d1eb1712b47abd9cdffb7bc90b local online="https://opm.openresty.org/api/pkg/fetch?account=xiangnanscu&name=lua-resty-cookie&op=eq&version=$LUA_RESTY_COOKIE_VERSION" diff --git a/template/actions/alb-nginx.sh b/template/actions/alb-nginx.sh index 520ea9dd..1c518673 100755 --- a/template/actions/alb-nginx.sh +++ b/template/actions/alb-nginx.sh @@ -7,8 +7,15 @@ if [[ -n "$CUR_ALB_BASE" ]]; then fi function alb-install-nginx-test-dependency() { - apk update && apk add luarocks luacheck lua perl-app-cpanminus wget curl make build-base perl-dev git neovim bash yq jq tree fd openssl + apk update && apk add luarocks luacheck lua-dev lua perl-app-cpanminus wget curl make build-base perl-dev git neovim bash yq jq tree fd openssl + mkdir /tmp && export TMP=/tmp # luarocks need this + cp /usr/bin/luarocks-5.1 /usr/bin/luarocks cpanm --mirror-only --mirror https://mirrors.tuna.tsinghua.edu.cn/CPAN/ -v --notest Test::Nginx IPC::Run YAML::PP + luarocks + ( + source ./template/actions/alb-nginx-install-deps.sh + alb-ng-install-test-deps + ) } function alb-install-nginx-test-dependency-ubuntu() { @@ -41,8 +48,7 @@ fi function alb-test-all-in-ci-nginx() { # base image build-harbor.alauda.cn/3rdparty/alb-nginx:v3.9-57-gb40a7de - - set -e # exit on err + set -ex # exit on err echo alb is $ALB export PATH=$PATH:/alb/tools/ which tweak_gen @@ -54,12 +60,17 @@ function alb-test-all-in-ci-nginx() { local end_install=$(date +"%Y %m %e %T.%6N") # alb-lint-lua # TODO local end_check=$(date +"%Y %m %e %T.%6N") + export LUACOV=true test-nginx-in-ci local end_test=$(date +"%Y %m %e %T.%6N") echo "start " $start echo "install " $end_install echo "check" $end_check echo "test" $end_test + pwd + luacov-console $PWD/template/nginx/lua/ + luacov-console $PWD/template/nginx/lua/ -s + luacov-console $PWD/template/nginx/lua/ -s >./luacov.summary } function test-nginx-local() { @@ -169,7 +180,7 @@ function configmap_to_file() { function alb-nginx-watch-log() ( echo "watch log" - tail -F ./template/servroot/logs/error.http.log | python -u -c ' + tail -F ./template/servroot/logs/error.log | python -u -c ' import sys for line in sys.stdin: if "keepalive connection" in line: diff --git a/template/nginx/lua/balancer/balance.lua b/template/nginx/lua/balancer/balance.lua index f2af1b4d..9e646611 100644 --- a/template/nginx/lua/balancer/balance.lua +++ b/template/nginx/lua/balancer/balance.lua @@ -86,6 +86,7 @@ function _M.sync_backends() local backends_data = shm.get_backends() if not backends_data then balancers = {} + ngx_log(ngx.ERR, "no backends data") return end diff --git a/template/nginx/lua/config/cache.lua b/template/nginx/lua/config/cache.lua index 2a27e7c2..8e678daf 100644 --- a/template/nginx/lua/config/cache.lua +++ b/template/nginx/lua/config/cache.lua @@ -37,6 +37,18 @@ local ipc = { function _M.init_l4() local cache = _M + local ok, err = ev.configure { + shm = subsystem .. "_ipc_shared_dict", -- defined by "lua_shared_dict" + timeout = 5, -- life time of event data in shm + interval = 1, -- poll interval (seconds) + + wait_interval = 0.010, -- wait before retry fetching event data + wait_max = 0.5 -- max wait time before discarding event + } + if not ok then + ngx.log(ngx.ERR, "failed to start event system: ", err) + return + end cache.init_mlcache("rule_cache", subsystem .. "_alb_cache", {lru_size = 2000, ttl = 30, neg_ttl = 5, ipc = ipc}) end diff --git a/template/nginx/lua/config/policy_fetch.lua b/template/nginx/lua/config/policy_fetch.lua index 97da725a..f89b0af4 100644 --- a/template/nginx/lua/config/policy_fetch.lua +++ b/template/nginx/lua/config/policy_fetch.lua @@ -182,6 +182,9 @@ function _M.update_policy(policy_raw, via) local old_policy_raw = shm.get_policy_raw() local old_policy_data = common.json_decode(old_policy_raw) + if old_policy_raw == policy_raw then + return + end if common.table_equals(policy_data, old_policy_data) then return diff --git a/template/nginx/lua/phase/init_worker_phase.lua b/template/nginx/lua/phase/init_worker_phase.lua index 9a63aceb..9f158a22 100644 --- a/template/nginx/lua/phase/init_worker_phase.lua +++ b/template/nginx/lua/phase/init_worker_phase.lua @@ -18,7 +18,7 @@ local cache = require "config.cache" if subsys.is_http_subsystem() then cache.init_l7() else - cache.init_l7() + cache.init_l4() end if ngx_worker.id() == 0 then diff --git a/template/t/AlaudaLib.pm b/template/t/AlaudaLib.pm index 082346cc..3b470241 100644 --- a/template/t/AlaudaLib.pm +++ b/template/t/AlaudaLib.pm @@ -13,11 +13,15 @@ our @EXPORT_OK = qw( gen_main_config gen_ngx_tmpl_via_block gen_http_only gen_lu my $ALB_BASE = $ENV{'TEST_BASE'}; +my $LUACOV = $ENV{'LUACOV'}; + sub tgl_log(@msgs) { warn "[tgl_log] @msgs\n"; } +tgl_log("lua cov $LUACOV"); + sub gen_lua_test($block) { my $lua_test_mode = "false"; my $lua_test_full = ''; @@ -95,11 +99,26 @@ sub write_file($policy,$p) { } sub gen_custom_init($block) { - my $init_worker = " init_worker_by_lua_file $ALB_BASE/nginx/lua/phase/init_worker_phase.lua; "; + my $init_worker = <<__END; +init_worker_by_lua_block { + require("mock_worker_init").init_worker() +} +__END + if (defined $block->disable_init_worker) { $init_worker = ""; } my $init = ""; + if ($LUACOV eq "true") { + $init = <<__END; +init_by_lua_block { + if ngx.config.subsystem == "http" then + require 'luacov.tick' + jit.off() + end +} +__END + } if (defined $block->enable_nyi) { my $name = $block->enable_nyi; @@ -245,7 +264,8 @@ sub gen_ngx_tmpl_via_block($block) { my $mock_backend = gen_mock_backend($block->mock_backend // ""); my $http_config = $block->http_config // ""; my $lua_test_full = gen_lua_test($block); - my $lua_path= "/usr/local/lib/lua/?.lua;$ALB_BASE/nginx/lua/?.lua;$ALB_BASE/t/?.lua;$ALB_BASE/t/lib/?.lua;$ALB_BASE/nginx/lua/vendor/?.lua;;"; + my $default_lua_path = "/usr/local/lib/lua/?.lua;$ALB_BASE/nginx/lua/?.lua;$ALB_BASE/nginx/lua/vendor/?.lua;"; + my $lua_path= "$default_lua_path;$ALB_BASE/t/?.lua;$ALB_BASE/?.lua;$ALB_BASE/t/lib/?.lua;;"; my $default_ngx_cfg = gen_ngx_tmpl_conf($init_full,$stream_config,$lua_path,$mock_backend,$http_config,$lua_test_full); $default_ngx_cfg = gen_https_port_config($block->alb_https_port // "",$default_ngx_cfg); diff --git a/template/t/e2e/error_page/error.t b/template/t/e2e/error_page/error.t index cb12978d..01ba7fa4 100644 --- a/template/t/e2e/error_page/error.t +++ b/template/t/e2e/error_page/error.t @@ -15,7 +15,6 @@ _EOC_ log_level("info"); -workers(4); master_process_enabled("on"); no_shuffle(); no_root_location(); @@ -24,9 +23,6 @@ run_tests(); __DATA__ === TEST 1: error page test ---- policy eval: "" --- http_config eval: $::http_config ---- disable_init_worker ---- init_worker_eval: require("mock_worker_init").init_worker() --- timeout: 9999999 --- lua_test_eval: require("e2e.error_page.error").test() diff --git a/template/t/e2e/https_upgrade/test.lua b/template/t/e2e/https_upgrade/test.lua index 9dea0570..57265030 100644 --- a/template/t/e2e/https_upgrade/test.lua +++ b/template/t/e2e/https_upgrade/test.lua @@ -1,6 +1,7 @@ local _M = {} function _M.test() + ngx.log(ngx.INFO, "life: test https upgrade") local F = require("F");local u = require("util");local h = require("test-helper");local httpc = require("resty.http").new(); local res, err = httpc:request_uri("https://127.0.0.1/lua",{ headers = { @@ -23,9 +24,10 @@ function _M.test() -- https://trac.nginx.org/nginx/ticket/1992 h.assert_not_contains(out,"upgrade",F"curl 1.1 to https with http2") end - do - local out = u.shell_curl([[ curl -k -H "xxx: curl" -H "Upgrade: websocket" "https://127.0.0.1:3443/curl" ]]) + do + local out,err = u.shell_curl([[ curl -v -k -H "xxx: curl" -H "Upgrade: websocket" "https://127.0.0.1:3443/curl" ]]) local curl_https_without_http2 = string.find(out, "upgrade") ~= nil + u.log("\nout "..tostring(out).."\nerr\n"..tostring(err).."\n") u.log("\ncurl 1.1 to https without http2 "..tostring(curl_https_without_http2).."\n") h.assert_contains(out,"upgrade",F"curl 1.1 to https without http2") end diff --git a/template/t/e2e/https_upgrade/test.t b/template/t/e2e/https_upgrade/test.t index 7d458b87..dfe84c7e 100644 --- a/template/t/e2e/https_upgrade/test.t +++ b/template/t/e2e/https_upgrade/test.t @@ -121,8 +121,8 @@ run_tests(); __DATA__ === TEST 1: test upgrade ---- timeout: 100 --- certificate eval: $::cert +--- timeout: 99999999 --- policy eval: $::policy --- http_config eval: $::http_config --- lua_test_eval: require('e2e.https_upgrade.test').test() diff --git a/template/t/e2e/metrics/metrics.t b/template/t/e2e/metrics/metrics.t index 66459534..03ef23ca 100644 --- a/template/t/e2e/metrics/metrics.t +++ b/template/t/e2e/metrics/metrics.t @@ -8,7 +8,7 @@ use Test::Nginx::Socket; log_level("info"); master_process_enabled("on"); no_shuffle(); -workers(4); # test for mutli metrics +# workers(4); # test for mutli metrics # currently mock init_worker has no way to sync backend in each worker.. no_root_location(); run_tests(); @@ -16,6 +16,5 @@ __DATA__ === TEST 1: clean-metrics --- mock_backend: 1880 e2e.metrics.metrics ---- init_worker_eval: require("mock_worker_init").init_worker() --- timeout: 9999999 --- lua_test_eval: require("e2e.metrics.metrics").test() diff --git a/template/t/e2e/ping/ping.t b/template/t/e2e/ping/ping.t index 576c9670..22996a2b 100644 --- a/template/t/e2e/ping/ping.t +++ b/template/t/e2e/ping/ping.t @@ -14,7 +14,5 @@ __DATA__ === TEST 1: ping --- mock_backend: 1880 e2e.ping.ping ---- disable_init_worker ---- init_worker_eval: require("mock_worker_init").init_worker() --- timeout: 9999999 --- lua_test_eval: require("e2e.ping.ping").test() diff --git a/template/t/e2e/trace/trace.lua b/template/t/e2e/trace/trace.lua index dfafaf49..12dcbc51 100644 --- a/template/t/e2e/trace/trace.lua +++ b/template/t/e2e/trace/trace.lua @@ -6,6 +6,7 @@ local h = require "test-helper" local common = require "utils.common" local dsl = require "match_engine.dsl" local ups = require "match_engine.upstream" +local ph = require "t.lib.policy_helper" local default_443_cert = "-----BEGIN CERTIFICATE-----\nMIIFFTCCAv2gAwIBAgIUNcaMWCswms56XCvj8nxC/5AKxtUwDQYJKoZIhvcNAQEL\nBQAwGjEYMBYGA1UEAwwPNDQzLmRlZmF1bHQuY29tMB4XDTIyMDUxOTA5MjEzMVoX\nDTMyMDUxNjA5MjEzMVowGjEYMBYGA1UEAwwPNDQzLmRlZmF1bHQuY29tMIICIjAN\nBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAvMqeEYs9K/DrbZ3FXj7yZaVRexub\na4OF0S/jg2qXrTK8FwPQ1MJiVxPL2jNeE7PT1fCNujb3+fZ/way99FJ0KpmbqEeP\nGt8490oqHZl7LuiEklrSiNp5qOJERsxgNrtq5RILIC1wH9eu0dNilwnCldzEXqFJ\n4+vZqPQfNxk//0vSOxsapl/nEPze6aMy+sUnyFJoq3ti/O02sV/p5sOQX3NcPoXU\n23PTr1xMDVQ7IpuR4GkxbmIVdAMuGWA2udYN0H3ou1VVy+je3RVF7xD2V/lMI3RL\nzLinfWxyNBUOoswylWRjdgwfrz5EkGuN58uT+o28Lx0APw06gwZ0eXb0cKdaYM0X\n04p4d3r//KLgm5WZpvDrjCC3aP02Yk1rITAu9owx+fNjIuEuPJtfin6r9Cjed7xL\n9CgdlFONDkNPxMz52qnf9Jbuf4HPTa/jDw7ICG8FAR8RwljJ7ohFCmullfXtumpX\nbT8+4DK1+H1fqkLV4lCWtQwn8ULqqCDQJZszco5KcnNnenqKgNPLVe7t6/ZxDZ8j\nMYAyGIR+DMDp0tLjfHD26IjEzF/n3E0pZiTXFaRirjKcFd523qEWvZeZc0nSykhH\nBUYXgxh2Nqi3Cv9VxA6sHVto5GvQBWq0kl6Qo9IGof51+4HCm++8/bCpf2Gcv/kI\ny39JIHMSGCa4ztcCAwEAAaNTMFEwHQYDVR0OBBYEFDawzxBvztJOekhp/DU9GKo+\nsnc9MB8GA1UdIwQYMBaAFDawzxBvztJOekhp/DU9GKo+snc9MA8GA1UdEwEB/wQF\nMAMBAf8wDQYJKoZIhvcNAQELBQADggIBACd2Z9XyESvQ4MfYMUID2DCmuVGBDhyo\n8cN88nuy+plrcYSpsthp55C+dhhfJRocES0NkpIVojpiQiPQdAyLFKb1M1Mcd9bg\n+qtYrOH2lS0Uem2s366D8LLJSOzWv/f75wUHe3eyivzW73zcM3znr5TrAFrCkUBF\npkK90G1VEznpD+VDvXYfcXklTZ7lMVZJ1ck2MDYPkh3nGtCyY6z+r41vJo/OcW8A\ncxicgsKXjEiXOH42B8ugad5gK27gA/FKwtTNPPU4K0UeDCAJaY+L7USjbrUgeQ17\nmjCOrY53OjyjjD4YjsE9EqsU/Hc9lqIUdCktZEDrLKfjGT1raaqDlSzEYYcs/oai\n0Ka3MXao2czYEJz6YZIOtp7FatRUBajCZ3NJeTgPFMZn10g7CktJR5QJDvvqbUBs\nHCddmahNPdgQwjxGVfoAI5SDH2QnIlj3bLivU+4oqR7hO7Nmhx9BtNRdHhM+M+wp\nsLvVETvtZdHC3RX4rX4pAl/r7pjhC7n0tbn3XyK96yZ4Yu/E+d/Cqhs0+rssqLzH\nDtMZCMOsaZi1AUEtc2cmZweOXEHeEoyPn3nJeVLfW2+dThlK/i9RaZbPThTS/GdK\nCU530BEDG+y/I5p6dndYySm2+LJiC0Xso1S1gLa7NccV8Y1E9Y8026J3lpvMilhP\nBwA4jE77yBPI\n-----END CERTIFICATE-----"; @@ -101,13 +102,8 @@ function _M.as_backend() ngx.say "from backend" end -local p = require "config.policy_fetch" -function _M.set_policy(policy) - p.update_policy(policy, "manual") -end - function _M.set_policy_lua(policy_table) - require("policy_helper").set_policy_lua(policy_table) + ph.set_policy_lua(policy_table) end function _M.test_policy_cache() @@ -116,8 +112,6 @@ function _M.test_policy_cache() h.assert_is_nil(err) h.assert_eq(up, "test-upstream-1") u.logs(up, policy, err) - -- TODO FIXME wait backend sync - ngx.sleep(3) local httpc = require "resty.http".new() do @@ -292,22 +286,6 @@ function _M.test_trace() end end -function _M.test() - local s = _M - u.logs "in test" - - s.set_policy_lua(default_policy()) - s.test_error_reason() - s.set_policy_lua(default_policy()) - s.test_policy_cache() - - s.set_policy_lua(default_policy()) - s.test_trace() - - s.set_policy_lua(default_policy()) - s.test_metrics() -end - local function get_metrics() local res, err = u.httpc():request_uri("https://127.0.0.1:1936/metrics", { ssl_verify = false }) h.assert_is_nil(err) @@ -346,4 +324,23 @@ function _M.test_metrics() end end +function _M.test() + local s = _M + u.logs "in test" + u.logs "test error reason" + s.set_policy_lua(default_policy()) + u.logs "after test error reason" + s.test_error_reason() + u.logs "test policy cache. set policy" + s.set_policy_lua(default_policy()) + u.logs "test policy cache. after set policy" + s.test_policy_cache() + + s.set_policy_lua(default_policy()) + s.test_trace() + u.logs "test metrics" + s.set_policy_lua(default_policy()) + s.test_metrics() +end + return _M diff --git a/template/t/e2e/trace/trace.t b/template/t/e2e/trace/trace.t index 139181a1..117104f6 100644 --- a/template/t/e2e/trace/trace.t +++ b/template/t/e2e/trace/trace.t @@ -18,7 +18,6 @@ _EOC_ log_level("info"); -workers(1); master_process_enabled("on"); no_shuffle(); no_root_location(); @@ -27,9 +26,6 @@ run_tests(); __DATA__ === TEST 1: trace test ---- policy eval: "" --- http_config eval: $::http_config ---- disable_init_worker ---- init_worker_eval: require("mock_worker_init").init_worker() --- timeout: 9999999 --- lua_test_eval: require("e2e.trace.trace").test() diff --git a/template/t/lib/mock_worker_init.lua b/template/t/lib/mock_worker_init.lua index ef2f16ca..1def70ef 100644 --- a/template/t/lib/mock_worker_init.lua +++ b/template/t/lib/mock_worker_init.lua @@ -1,27 +1,43 @@ -- format:on local M = {} -local u = require("util") local balancer = require("balancer.balance") +local ph = require("config.policy_fetch") local cache = require("config.cache") +local u = require("t.lib.util") +local subsys = require "utils.subsystem" +local shm = require "config.shmap" -function M.init_worker() - u.log("init worker " .. tostring(ngx.worker.id())) - local subsys = require "utils.subsystem" +-- we donot want to interval to update policy in mock mode +-- it may cause luacov stuck forever +function M.init_worker(cfg) + ngx.update_time() + cfg = cfg or {} + u.log("life: init worker " .. tostring(ngx.worker.id())) if subsys.is_http_subsystem() then cache.init_l7() else cache.init_l4() end - balancer.sync_backends() - u.log "sync backend ok" - local _, err = ngx.timer.every(1, balancer.sync_backends) - if err then - ngx.log(ngx.ERR, string.format("error when setting up timer.every for sync_backends: %s", tostring(err))) + local policy_raw, err = u.file_read_to_string(os.getenv("TEST_BASE") .. "/policy.new") + if err ~= nil then + ngx.exit(0) end - + -- speed up for luacov + ngx.update_time() + shm.set_policy_raw("{}") + ngx.update_time() + ph.update_policy(policy_raw, "manual") + ngx.update_time() + u.log "life: update policy ok" + balancer.sync_backends() + ngx.update_time() + u.log "life: sync backend ok" if subsys.is_http_subsystem() then + ngx.update_time() require("metrics").init() + ngx.update_time() + u.log "life: init metrics ok" end end diff --git a/template/t/lib/policy_helper.lua b/template/t/lib/policy_helper.lua index 055ba933..3096dfe6 100644 --- a/template/t/lib/policy_helper.lua +++ b/template/t/lib/policy_helper.lua @@ -1,10 +1,13 @@ local common = require "utils.common" local p = require "config.policy_fetch" +local balancer = require("balancer.balance") local M = {} function M.set_policy_json_str(policy) p.update_policy(policy, "manual") ngx.sleep(1) + -- TODO it will only update cache for the current worker.. + balancer.sync_backends() end function M.set_policy_lua(policy_table) diff --git a/template/t/unit/unit_test.t b/template/t/unit/unit_test.t index 31ce0051..1b1879dd 100644 --- a/template/t/unit/unit_test.t +++ b/template/t/unit/unit_test.t @@ -14,8 +14,8 @@ run_tests(); __DATA__ === TEST 1: unit tests ---- policy eval: "" --- http_config eval: "" +--- disable_init_worker --- lua_test_eval: require("unit.unit_test").test()