Skip to content

Commit

Permalink
tests(CI): copy and upload failed test logs (#13115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon authored Jun 12, 2024
1 parent 7302e73 commit e431bb2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ jobs:
run: |
echo FAILED_TEST_FILES_FILE=failed-tests.json >> $GITHUB_ENV
echo TEST_FILE_RUNTIME_FILE=test-runtime.json >> $GITHUB_ENV
echo SPEC_ERRLOG_CACHE_DIR=/tmp/${{ github.run_id }}/build_test/${{ matrix.runner }} >> $GITHUB_ENV
- name: Build & install dependencies
run: |
Expand Down Expand Up @@ -290,13 +291,22 @@ jobs:
DD_CIVISIBILITY_AGENTLESS_ENABLED: true
DD_TRACE_GIT_METADATA_ENABLED: true
DD_API_KEY: ${{ secrets.DATADOG_API_KEY }}
SPEC_ERRLOG_CACHE_DIR: ${{ env.SPEC_ERRLOG_CACHE_DIR }}
uses: Kong/gateway-test-scheduler/runner@69f0c2a562ac44fc3650b8bfa62106b34094b5ce # v3
with:
tests-to-run-file: test-chunk.${{ matrix.runner }}.json
failed-test-files-file: ${{ env.FAILED_TEST_FILES_FILE }}
test-file-runtime-file: ${{ env.TEST_FILE_RUNTIME_FILE }}
setup-venv-path: ${{ env.BUILD_ROOT }}

- name: Upload error logs
if: failure()
uses: actions/upload-artifact@v3
with:
name: busted-test-errlogs-${{ matrix.runner }}
path: ${{ env.SPEC_ERRLOG_CACHE_DIR }}
retention-days: 1

- name: Upload test rerun information
if: always()
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -361,6 +371,10 @@ jobs:
$CPAN_DOWNLOAD/cpanm --notest --local-lib=$HOME/perl5 local::lib && eval $(perl -I $HOME/perl5/lib/perl5/ -Mlocal::lib)
$CPAN_DOWNLOAD/cpanm --notest Test::Nginx
- name: Generate environment variables
run: |
echo SPEC_ERRLOG_CACHE_DIR=/tmp/${{ github.run_id }}/PDK_test >> $GITHUB_ENV
- name: Tests
env:
TEST_SUITE: pdk
Expand All @@ -372,6 +386,14 @@ jobs:
eval $(perl -I $HOME/perl5/lib/perl5/ -Mlocal::lib)
prove -I. -r t
- name: Upload error logs
if: failure()
uses: actions/upload-artifact@v3
with:
name: PDK-test-errlogs
path: ${{ env.SPEC_ERRLOG_CACHE_DIR }}
retention-days: 1

- name: Archive coverage stats file
uses: actions/upload-artifact@v3
if: ${{ always() && (inputs.coverage == true || github.event_name == 'schedule') }}
Expand Down
30 changes: 30 additions & 0 deletions spec/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ local resty_signal = require "resty.signal"
local lfs = require "lfs"
local luassert = require "luassert.assert"
local uuid = require("kong.tools.uuid").uuid
local colors = require "ansicolors"

ffi.cdef [[
int setenv(const char *name, const char *value, int overwrite);
Expand Down Expand Up @@ -2473,6 +2474,33 @@ local deep_sort do
end


local function copy_errlog(errlog_path)
local file_path = "Unknown path"
local line_number = "Unknown line"
local errlog_cache_dir = os.getenv("SPEC_ERRLOG_CACHE_DIR") or "/tmp/kong_errlog_cache"

local ok, err = pl_dir.makepath(errlog_cache_dir)
assert(ok, "makepath failed: " .. tostring(err))

local info = debug.getinfo(4, "Sl")
if info then
file_path = info.source:gsub("^@", "")
line_number = info.currentline
end

if string.find(file_path, '/', nil, true) then
file_path = string.gsub(file_path, '/', '_')
end
file_path = errlog_cache_dir .. "/" .. file_path:gsub("%.lua$", "_") .. "line_" .. line_number .. '.log'

ok, err = pl_file.copy(errlog_path, file_path)
if ok then
print(colors("%{yellow}Log saved as: " .. file_path .. "%{reset}"))
else
print(colors("%{red}Failed to save error log for test " .. file_path .. ": " .. err))
end
end

--- Assertion to check the status-code of a http response.
-- @function status
-- @param expected the expected status code
Expand Down Expand Up @@ -2505,6 +2533,8 @@ local function res_status(state, args)
args.n = 3

if res.status == 500 then
copy_errlog(conf.nginx_err_logs)

-- on HTTP 500, we can try to read the server's error logs
-- for debugging purposes (very useful for travis)
local str = pl_file.read(conf.nginx_err_logs)
Expand Down

1 comment on commit e431bb2

@github-actions
Copy link
Contributor

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:e431bb2ac6e043d6a1dde98d72a44e0ed19bdfab
Artifacts available https://github.com/Kong/kong/actions/runs/9475875569

Please sign in to comment.