-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ci_agent.py
More file actions
96 lines (81 loc) · 3.17 KB
/
test_ci_agent.py
File metadata and controls
96 lines (81 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
import os
import sys
import json
import requests
import argparse
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger("ci-agent-tester")
def test_ci_agent(repo_url, branch="main", agent_url="http://localhost:9001"):
"""
Test the CI agent by sending a request to generate a Jenkinsfile for a repository
"""
try:
# Prepare request payload
payload = {
"repository": {
"url": repo_url,
"branch": branch
}
}
logger.info(f"Testing CI agent at {agent_url} with repo {repo_url} (branch: {branch})")
# Make request to CI agent
response = requests.post(
f"{agent_url}/execute",
json=payload,
headers={"Content-Type": "application/json"},
timeout=60 # Give it a longer timeout as cloning might take time
)
# Check response
if response.status_code == 200:
result = response.json()
logger.info(f"Success! CI agent response: {json.dumps(result, indent=2)}")
return True
else:
logger.error(f"Failed with status code {response.status_code}: {response.text}")
return False
except Exception as e:
logger.error(f"Error testing CI agent: {str(e)}")
return False
def run_health_check(agent_url="http://localhost:9001"):
"""
Run a health check on the CI agent
"""
try:
logger.info(f"Running health check on CI agent at {agent_url}")
# Make request to CI agent health endpoint
response = requests.get(f"{agent_url}/health", timeout=10)
# Check response
if response.status_code == 200:
result = response.json()
logger.info(f"Health check passed: {json.dumps(result, indent=2)}")
return True
else:
logger.error(f"Health check failed with status code {response.status_code}: {response.text}")
return False
except Exception as e:
logger.error(f"Error running health check: {str(e)}")
return False
if __name__ == "__main__":
# Parse command line arguments
parser = argparse.ArgumentParser(description="Test the CI agent")
parser.add_argument("--repo", "-r", help="Repository URL to test with", required=True)
parser.add_argument("--branch", "-b", help="Branch to check out", default="main")
parser.add_argument("--url", "-u", help="CI agent URL", default="http://localhost:9001")
parser.add_argument("--health", "-H", help="Run health check only", action="store_true")
args = parser.parse_args()
# Run requested tests
if args.health:
success = run_health_check(args.url)
else:
health_ok = run_health_check(args.url)
if not health_ok:
logger.warning("Health check failed, but proceeding with main test anyway")
success = test_ci_agent(args.repo, args.branch, args.url)
# Exit with appropriate code
sys.exit(0 if success else 1)