Skip to content

Commit

Permalink
add luacov
Browse files Browse the repository at this point in the history
  • Loading branch information
woodgear committed Nov 15, 2024
1 parent f14d9f2 commit f4d3834
Show file tree
Hide file tree
Showing 25 changed files with 202 additions and 62 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"caseSensitive": false,
"words": [
//proper noun
"luacov",
"strftime",
"ncruces",
"satyrius",
Expand Down
59 changes: 58 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -91,20 +94,30 @@ 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 }}"
run: |
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
Expand Down Expand Up @@ -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:
Expand All @@ -170,4 +185,46 @@ jobs:
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_CLI_EXPERIMENTAL: enabled
run: |
./scripts/run-like-github-actions.sh release-alb
./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 }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ template/nginx/lua/vendor/opentelemetry
*.kconf
docs/.obsidian
template/share/dhparam.pem
luacov-html/
luacov.stats.out
8 changes: 8 additions & 0 deletions .luacov
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
reporter = "html"
html = {}
include = {
".*/template/nginx/lua/.*"
}
exclude = {
".*/template/nginx/lua/vendor/.*",
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
2 changes: 1 addition & 1 deletion scripts/alb-test-actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions scripts/run-like-ci-nginx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions scripts/run-like-github-actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() (
Expand Down
19 changes: 16 additions & 3 deletions template/actions/alb-nginx-install-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
19 changes: 15 additions & 4 deletions template/actions/alb-nginx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions template/nginx/lua/balancer/balance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions template/nginx/lua/config/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions template/nginx/lua/config/policy_fetch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion template/nginx/lua/phase/init_worker_phase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions template/t/AlaudaLib.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions template/t/e2e/error_page/error.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ _EOC_


log_level("info");
workers(4);
master_process_enabled("on");
no_shuffle();
no_root_location();
Expand All @@ -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()
6 changes: 4 additions & 2 deletions template/t/e2e/https_upgrade/test.lua
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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
Expand Down
Loading

0 comments on commit f4d3834

Please sign in to comment.