Skip to content

Commit e90fbd5

Browse files
committed
ci: upload integration logs on failure
1 parent 8f772c8 commit e90fbd5

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

.github/workflows/integration-tests.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,33 @@ jobs:
9393
- name: Test with pytest
9494
env:
9595
EVENT_LOOP_MANAGER: ${{ matrix.event_loop_manager }}
96+
INTEGRATION_LOG_ARTIFACT_DIR: integration-test-logs
9697
PROTOCOL_VERSION: 4
9798
run: |
9899
if [[ "${{ matrix.python-version }}" =~ t$ ]]; then
99100
export PYTHON_GIL=0
100101
fi
101102
uv run pytest -v tests/integration/standard/ tests/integration/cqlengine/
103+
104+
- name: Upload ccm logs on failure
105+
if: failure()
106+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
107+
with:
108+
name: integration-logs-${{ matrix.event_loop_manager }}-${{ matrix.python-version }}
109+
path: |
110+
integration-test-logs/**
111+
tests/integration/ccm/**/*.conf
112+
tests/integration/ccm/**/*.log
113+
tests/integration/ccm/**/*.yaml
114+
tests/integration/ccm/**/*.yml
115+
tests/integration/ccm/**/conf/**
116+
tests/integration/ccm/**/logs/**
117+
/home/runner/.ccm/**/*.conf
118+
/home/runner/.ccm/**/*.log
119+
/home/runner/.ccm/**/*.yaml
120+
/home/runner/.ccm/**/*.yml
121+
/home/runner/.ccm/**/conf/**
122+
/home/runner/.ccm/**/logs/**
123+
if-no-files-found: ignore
124+
include-hidden-files: true
125+
retention-days: 14

tests/integration/conftest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import logging
3+
import shutil
34

45
import pytest
56
from ccmlib.cluster_factory import ClusterFactory as CCMClusterFactory
@@ -9,12 +10,56 @@
910
from . import CLUSTER_NAME, SINGLE_NODE_CLUSTER_NAME, MULTIDC_CLUSTER_NAME
1011
from . import path as ccm_path
1112

13+
_failed_reports = []
14+
_ccm_logs_preserved = False
15+
_log_artifact_dir = os.environ.get('INTEGRATION_LOG_ARTIFACT_DIR', 'integration-test-logs')
16+
_preserved_file_extensions = ('.conf', '.log', '.yaml', '.yml')
17+
18+
19+
def pytest_runtest_logreport(report):
20+
if report.failed:
21+
_failed_reports.append(report)
22+
23+
24+
def preserve_ccm_logs_on_failure():
25+
global _ccm_logs_preserved
26+
if _ccm_logs_preserved or not _failed_reports or not os.path.isdir(ccm_path):
27+
return
28+
29+
destination = os.path.abspath(_log_artifact_dir)
30+
ccm_destination = os.path.join(destination, 'ccm')
31+
os.makedirs(ccm_destination, exist_ok=True)
32+
33+
with open(os.path.join(destination, 'failed-tests.txt'), 'w') as failed_tests:
34+
for report in _failed_reports:
35+
failed_tests.write('%s %s\n' % (report.when, report.nodeid))
36+
37+
for root, _, files in os.walk(ccm_path):
38+
rel_root = os.path.relpath(root, ccm_path)
39+
path_parts = set(rel_root.split(os.sep))
40+
preserve_all = bool(path_parts.intersection(('conf', 'logs')))
41+
42+
for filename in files:
43+
if not preserve_all and not filename.endswith(_preserved_file_extensions):
44+
continue
45+
46+
source_file = os.path.join(root, filename)
47+
rel_file = os.path.relpath(source_file, ccm_path)
48+
destination_file = os.path.join(ccm_destination, rel_file)
49+
os.makedirs(os.path.dirname(destination_file), exist_ok=True)
50+
shutil.copy2(source_file, destination_file)
51+
52+
logging.info("Preserved ccm logs for failed tests under %s", destination)
53+
_ccm_logs_preserved = True
54+
1255

1356
@pytest.fixture(scope="session", autouse=True)
1457
def cleanup_clusters():
1558

1659
yield
1760

61+
preserve_ccm_logs_on_failure()
62+
1863
if not os.environ.get('DISABLE_CLUSTER_CLEANUP'):
1964
for cluster_name in [CLUSTER_NAME, SINGLE_NODE_CLUSTER_NAME, MULTIDC_CLUSTER_NAME,
2065
'cluster_tests', 'shared_aware', 'sni_proxy', 'test_ip_change', 'test_client_routes_replacement']:
@@ -29,4 +74,5 @@ def cleanup_clusters():
2974
def setup_and_teardown_packages():
3075
print('setup')
3176
yield
77+
preserve_ccm_logs_on_failure()
3278
teardown_package()

0 commit comments

Comments
 (0)