-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathmetrics-regress.py
executable file
·117 lines (97 loc) · 4.12 KB
/
metrics-regress.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python3
import json
import http.client
import argparse
import os
import time
import math
def make_http_request( req_type, endpoint, params=None ):
headers = { 'Content-Type': 'application/json',
'Private-Token': str(os.environ['METRICS_CI_TOKEN'])
}
conn = http.client.HTTPSConnection(server)
conn.request(req_type, endpoint, params, headers)
response = conn.getresponse()
data = response.read()
regressionData = json.loads(data.decode('utf-8'))
conn.close()
return response, regressionData
## Parse arguments to get regression name and project ID
parser = argparse.ArgumentParser(prog='metrics-regress',
description='Launch a regression on the Metrics platform and query results')
parser.add_argument('regressionName', help='The name of the regression to run')
parser.add_argument('projectId', help='The ID of the Metrics project')
args = parser.parse_args()
## Server
server = 'chipsalliance.metrics.ca:443'
## API Endpoints
postRegression = '/api/v1/projects/'+args.projectId+'/regressionRuns'
getRegressionRunInfo = '/api/v1/projects/'+args.projectId+'/regressionRuns/'
## Start regression
reqParams = {}
reqParams['regressionName'] = args.regressionName
# Determine the git reference to pass to Metrics. For PRs, the reference
# is of the format refs/pull/<PR-number>/merge
if str(os.environ['GITHUB_EVENT_NAME']) == 'pull_request_target':
reqParams['branch'] = 'refs/pull/' + str(os.environ['PR_NUMBER']) + '/merge'
else:
reqParams['branch'] = str(os.environ['GITHUB_REF'])
params = json.dumps(reqParams)
response, regressionData = make_http_request('POST', postRegression, params)
## Check response
if response.status != 201:
print('Error, regression was not started. Response: ' + str(response.status) + ':' \
+ str(response.reason) + ' ' + str(regressionData))
print('Exit with code 1')
exit(1)
else:
print('Regression started. Id = ' + regressionData['id'])
## Start polling regression status
regressionRunId = regressionData['id']
while True:
time.sleep(10)
response, regressionData = make_http_request('GET', getRegressionRunInfo+regressionRunId)
if response.status == 200:
if 'complete' in regressionData['status']:
print('Regression complete')
break
if 'buildFailed' in regressionData['status']:
print('A build has failed. No tests will be run')
print('Debug at: https://chipsalliance.metrics.ca/' + args.projectId + \
'/results/regressionRuns/' + regressionRunId)
exit(1)
## Print test status
print('\n')
print('Regression results')
print('==================')
print('Total number of tests: ' + str(regressionData['testRuns']['total']))
print('Passed tests: ' + str(regressionData['testRuns']['passed']))
print('Failed tests: ' + str(regressionData['testRuns']['failed']))
print('Incomplete tests: ' + str(regressionData['testRuns']['incomplete']))
print('\n')
## Poll for coverage data
#while True:
# time.sleep(10)
# response, regressionData = make_http_request('GET', getRegressionRunInfo+regressionRunId)
# if response.status is 200:
# if regressionData['functionalCoverage'] is not None :
# break
## Print functional coverage
#print('Coverage results')
#print('================')
#print('Functional: ' + str(math.trunc(regressionData['functionalCoverage']*100)/100))
#if regressionData['assertionCoverage'] is not None:
# print('Assertion: ' + str(math.trunc(regressionData['assertionCoverage']*100) /100))
#if regressionData['lineCoverage'] is not None:
# print('Code (block): ' + str(math.trunc(regressionData['lineCoverage']*100) /100))
#print('\n')
print('Full results at: https://chipsalliance.metrics.ca/' + args.projectId + \
'/results/regressionRuns/' + regressionRunId)
## Set the exit code to be used by github action
if regressionData['testRuns']['failed'] > 0 or \
regressionData['testRuns']['incomplete'] > 0:
print('One or more tests has failed/is incomplete. Exit with code 1.')
exit(1)
else:
print('All tests have passed. Exit with code 0.')
exit(0)