Skip to content

Commit

Permalink
tests(unit): add coverage for spec/01-unit (#10754)
Browse files Browse the repository at this point in the history
* tests(unit): add coverage workflow

run tests with luacov once a week
aggregate the results and print it

* tests(unit): parametrize build path + dispatchable

make build path parametric
enable dispatchable workflow in build & coverage
  • Loading branch information
samugi committed May 4, 2023
1 parent 86184c5 commit 62bcbcb
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 58 deletions.
69 changes: 69 additions & 0 deletions .ci/luacov-stats-aggregator.lua
@@ -0,0 +1,69 @@
-- Aggregates stats from multiple luacov stat files.
-- If different stats files contain coverage information of common
-- source files, it assumes the provided stats refer to the same
-- version of the source files.

-- Example stats for a 12 lines file `my/file.lua`
-- that received hits on lines 3, 4, 9:
--
-- ["my/file.lua"] = {
-- [3] = 1,
-- [4] = 3,
-- [9] = 2,
-- max = 12,
-- max_hits = 3
-- }
--

local luacov_stats = require "luacov.stats"
local luacov_reporter = require "luacov.reporter"

local all_stats = {}


-- load parameters
local params = {...}
local base_path = params[1] or "./luacov-stats-out-"
local file_name = params[2] or "luacov.stats.out"
local output = params[3] or file_name


-- load stats - appends incremental index to base_path to load all the artifacts
local loaded_stats = {}
local index = 0
repeat
index = index + 1
local stats_file = base_path .. index .. "/" .. file_name
local loaded = luacov_stats.load(stats_file)
if loaded then
loaded_stats[#loaded_stats + 1] = loaded
print("loading file: " .. stats_file)
end
until not loaded


-- aggregate stats by file name
for _, stat_data in ipairs(loaded_stats) do
for f_name, f_data in pairs(stat_data) do
if all_stats[f_name] then
assert(
all_stats[f_name].max == f_data.max,
"number of lines in file " .. f_name .. " is inconsistent"
)
-- combine stats (add line hits)
for i = 1, all_stats[f_name].max do
if all_stats[f_name][i] or f_data[i] then
all_stats[f_name][i] = (all_stats[f_name][i] or 0) + (f_data[i] or 0)
end
end
all_stats[f_name].max_hits = math.max(all_stats[f_name].max_hits, f_data.max_hits)

else
all_stats[f_name] = f_data
end
end
end
luacov_stats.save(output, all_stats)

-- generate report
luacov_reporter.report()
55 changes: 55 additions & 0 deletions .github/workflows/build.yml
@@ -0,0 +1,55 @@
name: Build
on:
workflow_call:
inputs:
build-root-suffix:
required: true
type: string

env:
BUILD_ROOT: ${{ github.workspace }}${{ inputs.build-root-suffix }}

jobs:
build:
name: Build dependencies
runs-on: ubuntu-22.04

steps:
- name: Checkout Kong source code
uses: actions/checkout@v3

- name: Lookup build cache
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: ${{ hashFiles('.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}

- name: Install packages
if: steps.cache-deps.outputs.cache-hit != 'true'
run: sudo apt update && sudo apt install libyaml-dev valgrind libprotobuf-dev

- name: Build Kong
if: steps.cache-deps.outputs.cache-hit != 'true'
run: |
make build-kong
BUILD_PREFIX=$BUILD_ROOT/kong-dev
export PATH="$BUILD_PREFIX/bin:$BUILD_PREFIX/openresty/nginx/sbin:$BUILD_PREFIX/openresty/bin:$PATH"
chmod +rw -R $BUILD_PREFIX
nginx -V
ldd $(which nginx)
luarocks
- name: Bazel Outputs
uses: actions/upload-artifact@v3
if: failure()
with:
name: bazel-outputs
path: |
bazel-out/_tmp/actions
- name: Build Dev Kong dependencies
if: steps.cache-deps.outputs.cache-hit != 'true'
run: |
make install-dev-rocks
84 changes: 84 additions & 0 deletions .github/workflows/build_and_coverage.yml
@@ -0,0 +1,84 @@
name: Build & Test Coverage
on:
schedule:
- cron: "15 0 * * 0"
workflow_dispatch:

env:
BUILD_ROOT: ${{ github.workspace }}/bazel-bin/build

jobs:
build:
uses: ./.github/workflows/build.yml
with:
build-root-suffix: /bazel-bin/build

unit-tests:
name: Unit tests
runs-on: ubuntu-22.04
needs: build

services:
postgres:
image: postgres:13
env:
POSTGRES_USER: kong
POSTGRES_DB: kong
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 5s --health-timeout 5s --health-retries 8

steps:
- name: Checkout Kong source code
uses: actions/checkout@v3

- name: Lookup build cache
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: ${{ hashFiles('.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}

- name: Unit tests
env:
KONG_TEST_PG_DATABASE: kong
KONG_TEST_PG_USER: kong
run: |
source ${{ env.BUILD_ROOT }}/kong-dev-venv.sh
bin/busted spec/01-unit --coverage
- name: Archive coverage stats file
uses: actions/upload-artifact@v3
if: success()
with:
name: luacov-stats-out-1
retention-days: 1
path: |
luacov.stats.out
# TODO: run jobs with the remaining tests (with coverage enabled) and archive each artifact as luacov-stats-out-{i}

aggregator:
needs: [unit-tests] # add dependencies for all the test jobs
name: Luacov stats aggregator
runs-on: ubuntu-22.04

steps:
- name: Checkout source code
uses: actions/checkout@v3

- name: Install requirements
run: |
sudo apt-get update && sudo apt-get install -y luarocks
sudo luarocks install luacov
# Download all archived coverage stats files
- uses: actions/download-artifact@v3

- name: Stats aggregation
shell: bash
run: |
lua .ci/luacov-stats-aggregator.lua "luacov-stats-out-" "luacov.stats.out"
awk '/Summary/,0' luacov.report.out
45 changes: 3 additions & 42 deletions .github/workflows/build_and_test.yml
Expand Up @@ -26,48 +26,9 @@ env:

jobs:
build:
name: Build dependencies
runs-on: ubuntu-22.04

steps:
- name: Checkout Kong source code
uses: actions/checkout@v3

- name: Lookup build cache
id: cache-deps
uses: actions/cache@v3
with:
path: |
${{ env.BUILD_ROOT }}
key: ${{ hashFiles('.requirements', 'kong-*.rockspec', '.bazelversion', '.bazelrc', 'build/**', 'BUILD.bazel', 'WORKSPACE', '.github/workflows/build_and_test.yml') }}

- name: Install packages
if: steps.cache-deps.outputs.cache-hit != 'true'
run: sudo apt update && sudo apt install libyaml-dev valgrind libprotobuf-dev

- name: Build Kong
if: steps.cache-deps.outputs.cache-hit != 'true'
run: |
make build-kong
BUILD_PREFIX=$BUILD_ROOT/kong-dev
export PATH="$BUILD_PREFIX/bin:$BUILD_PREFIX/openresty/nginx/sbin:$BUILD_PREFIX/openresty/bin:$PATH"
chmod +rw -R $BUILD_PREFIX
nginx -V
ldd $(which nginx)
luarocks
- name: Bazel Outputs
uses: actions/upload-artifact@v3
if: failure()
with:
name: bazel-outputs
path: |
bazel-out/_tmp/actions
- name: Build Dev Kong dependencies
if: steps.cache-deps.outputs.cache-hit != 'true'
run: |
make install-dev-rocks
uses: ./.github/workflows/build.yml
with:
build-root-suffix: /bazel-bin/build

lint-doc-and-unit-tests:
name: Lint, Doc and Unit tests
Expand Down
15 changes: 15 additions & 0 deletions .luacov
@@ -0,0 +1,15 @@
return {

runreport = true,

include = {
"kong$",
"kong%/.+$",
},

exclude = {
"bazel%-bin/build",
"^spec/"
}

}
2 changes: 1 addition & 1 deletion Makefile
@@ -1,7 +1,7 @@
OS := $(shell uname | awk '{print tolower($$0)}')
MACHINE := $(shell uname -m)

DEV_ROCKS = "busted 2.1.2" "busted-htest 1.0.0" "luacheck 1.1.0" "lua-llthreads2 0.1.6" "http 0.4" "ldoc 1.4.6"
DEV_ROCKS = "busted 2.1.2" "busted-htest 1.0.0" "luacheck 1.1.0" "lua-llthreads2 0.1.6" "http 0.4" "ldoc 1.4.6" "luacov 0.15.0"
WIN_SCRIPTS = "bin/busted" "bin/kong" "bin/kong-health"
BUSTED_ARGS ?= -v
TEST_CMD ?= bin/busted $(BUSTED_ARGS)
Expand Down
15 changes: 0 additions & 15 deletions spec/01-unit/01-db/01-schema/01-schema_spec.lua
Expand Up @@ -3,21 +3,6 @@ local cjson = require "cjson"
local helpers = require "spec.helpers"


local luacov_ok = pcall(require, "luacov")
if luacov_ok then
local busted_it = it
-- luacheck: globals it
it = function(desc, fn)
busted_it(desc, function()
local luacov = require("luacov")
luacov.init()
fn()
luacov.save_stats()
end)
end
end


local SchemaKind = {
{ name = "schema", new = Schema.new, },
{ name = "subschema", new = function(definition)
Expand Down

1 comment on commit 62bcbcb

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:62bcbcb657f10b8226dfded19a12eee988b19424
Artifacts available https://github.com/Kong/kong/actions/runs/4882195700

Please sign in to comment.