| 1 |
|
| 2 |
""" |
| 3 |
Unified test runner for Ducktape Producer and Consumer tests |
| 4 |
""" |
| 5 |
import sys |
| 6 |
import os |
| 7 |
import subprocess |
| 8 |
import tempfile |
| 9 |
import argparse |
| 10 |
from datetime import datetime |
| 11 |
|
| 12 |
import ducktape |
| 13 |
|
| 14 |
|
| 15 |
def get_test_info(test_type): |
| 16 |
"""Get test file and description based on test type""" |
| 17 |
test_info = { |
| 18 |
'producer': { |
| 19 |
'file': 'test_producer.py', |
| 20 |
'description': 'Producer Benchmark Tests' |
| 21 |
}, |
| 22 |
'consumer': { |
| 23 |
'file': 'test_consumer.py', |
| 24 |
'description': 'Consumer Benchmark Tests' |
| 25 |
}, |
| 26 |
'transactions': { |
| 27 |
'file': 'test_transactions.py', |
| 28 |
'description': 'Transactional Producer and Consumer Tests' |
| 29 |
} |
| 30 |
} |
| 31 |
return test_info.get(test_type) |
| 32 |
|
| 33 |
|
| 34 |
def run_single_test_type(args): |
| 35 |
"""Run a single test type""" |
| 36 |
test_info = get_test_info(args.test_type) |
| 37 |
|
| 38 |
|
| 39 |
print(f"Confluent Kafka Python - {test_info['description']}") |
| 40 |
print(f"Timestamp: {datetime.now().isoformat()}") |
| 41 |
print("=" * 70) |
| 42 |
|
| 43 |
try: |
| 44 |
print(f"Using ducktape version: {ducktape.__version__}") |
| 45 |
except AttributeError: |
| 46 |
|
| 47 |
try: |
| 48 |
import pkg_resources |
| 49 |
version = pkg_resources.get_distribution('ducktape').version |
| 50 |
print(f"Using ducktape version: {version}") |
| 51 |
except Exception: |
| 52 |
print("Using ducktape version: unknown") |
| 53 |
|
| 54 |
|
| 55 |
try: |
| 56 |
import confluent_kafka |
| 57 |
print(f"Using confluent-kafka version: {confluent_kafka.version()}") |
| 58 |
except ImportError: |
| 59 |
print("ERROR: confluent_kafka is not installed.") |
| 60 |
print("Install it with: pip install confluent-kafka") |
| 61 |
return 1 |
| 62 |
|
| 63 |
|
| 64 |
script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 65 |
test_file = os.path.join(script_dir, test_info['file']) |
| 66 |
project_root = os.path.dirname(os.path.dirname(script_dir)) |
| 67 |
|
| 68 |
if not os.path.exists(test_file): |
| 69 |
print(f"ERROR: Test file not found: {test_file}") |
| 70 |
return 1 |
| 71 |
|
| 72 |
|
| 73 |
os.chdir(project_root) |
| 74 |
|
| 75 |
|
| 76 |
results_dir = os.path.join(tempfile.gettempdir(), "ducktape_results") |
| 77 |
os.makedirs(results_dir, exist_ok=True) |
| 78 |
|
| 79 |
print(f"\nWorking directory: {os.getcwd()}") |
| 80 |
print(f"Test file: {test_file}") |
| 81 |
print(f"Results directory: {results_dir}") |
| 82 |
|
| 83 |
|
| 84 |
cmd = [ |
| 85 |
sys.executable, "-m", "ducktape", |
| 86 |
"--results-root", results_dir, |
| 87 |
"--cluster", "ducktape.cluster.localhost.LocalhostCluster", |
| 88 |
"--default-num-nodes", "1" |
| 89 |
] |
| 90 |
|
| 91 |
if args.debug: |
| 92 |
cmd.append("--debug") |
| 93 |
|
| 94 |
|
| 95 |
if args.test_method: |
| 96 |
cmd.append(f"{test_file}::{args.test_method}") |
| 97 |
print(f"\nRunning specific test: {args.test_method}") |
| 98 |
else: |
| 99 |
cmd.append(test_file) |
| 100 |
print(f"\nRunning all {args.test_type} tests...") |
| 101 |
|
| 102 |
|
| 103 |
env = os.environ.copy() |
| 104 |
env["PYTHONPATH"] = os.getcwd() |
| 105 |
|
| 106 |
print(f"Command: PYTHONPATH=. {' '.join(cmd[2:])}") |
| 107 |
print("-" * 50) |
| 108 |
|
| 109 |
|
| 110 |
try: |
| 111 |
result = subprocess.run(cmd, env=env) |
| 112 |
|
| 113 |
|
| 114 |
print("\n" + "=" * 50) |
| 115 |
if result.returncode == 0: |
| 116 |
print("All tests completed successfully!") |
| 117 |
else: |
| 118 |
print("Tests failed. Check the output above for details.") |
| 119 |
|
| 120 |
return result.returncode |
| 121 |
|
| 122 |
except KeyboardInterrupt: |
| 123 |
print("\nTest interrupted by user") |
| 124 |
return 1 |
| 125 |
except Exception as e: |
| 126 |
print(f"Error running test: {e}") |
| 127 |
return 1 |
| 128 |
|
| 129 |
|
| 130 |
def run_all_tests(args): |
| 131 |
"""Run all available test types""" |
| 132 |
test_types = ['producer', 'consumer'] |
| 133 |
overall_success = True |
| 134 |
|
| 135 |
print("Confluent Kafka Python - All Ducktape Tests") |
| 136 |
print(f"Timestamp: {datetime.now().isoformat()}") |
| 137 |
print("=" * 70) |
| 138 |
|
| 139 |
for test_type in test_types: |
| 140 |
print(f"\n{'='*20} Running {test_type.upper()} Tests {'='*20}") |
| 141 |
|
| 142 |
|
| 143 |
test_args = argparse.Namespace( |
| 144 |
test_type=test_type, |
| 145 |
test_method=args.test_method, |
| 146 |
debug=args.debug |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
result = run_single_test_type(test_args) |
| 151 |
if result != 0: |
| 152 |
overall_success = False |
| 153 |
print(f"\n❌ {test_type.upper()} tests failed!") |
| 154 |
else: |
| 155 |
print(f"\n✅ {test_type.upper()} tests passed!") |
| 156 |
|
| 157 |
print(f"\n{'='*70}") |
| 158 |
if overall_success: |
| 159 |
print("🎉 All tests completed successfully!") |
| 160 |
return 0 |
| 161 |
else: |
| 162 |
print("💥 Some tests failed. Check the output above for details.") |
| 163 |
return 1 |
| 164 |
|
| 165 |
|
| 166 |
def main(): |
| 167 |
"""Run the ducktape test based on specified type""" |
| 168 |
parser = argparse.ArgumentParser(description="Confluent Kafka Python - Ducktape Test Runner") |
| 169 |
parser.add_argument('test_type', nargs='?', choices=['producer', 'consumer', 'transactions'], |
| 170 |
help='Type of test to run (default: run all tests)') |
| 171 |
parser.add_argument('test_method', nargs='?', |
| 172 |
help='Specific test method to run (optional)') |
| 173 |
parser.add_argument('--debug', action='store_true', |
| 174 |
help='Enable debug output') |
| 175 |
|
| 176 |
args = parser.parse_args() |
| 177 |
|
| 178 |
|
| 179 |
if args.test_type is None: |
| 180 |
return run_all_tests(args) |
| 181 |
|
| 182 |
|
| 183 |
return run_single_test_type(args) |
| 184 |
|
| 185 |
|
| 186 |
if __name__ == "__main__": |
| 187 |
sys.exit(main()) |