Add recent/detail openstack-health api.

Added a new api to retrieve the full details of the most recent runs, rather
than just the summary. This data was already available in the code, the api
simply returns it to the caller. This api can be used to provide “Test %”
complete rather than a simple “Runs passed/failed” which is in the current
/recent api.

Moved the existing functionality of the recent api call to a wrapper
module. This will be called by the recent api and the recent/detail api.
The detail version will return all the information returned by
subunit2sql and will not calculate pass or fail.

Added a unit test for the new recent/detail api.

Change-Id: I5528444148f525285cafed3fa9ce0a94dbfd5306
This commit is contained in:
Nathan Karsten 2016-03-30 15:40:11 +00:00
parent 7f6be1db55
commit c9097cc3c9
2 changed files with 57 additions and 12 deletions

View File

@ -276,25 +276,40 @@ def get_runs_by_run_metadata_key(run_metadata_key, value):
@app.route('/runs/key/<path:run_metadata_key>/<path:value>/recent',
methods=['GET'])
def get_recent_runs(run_metadata_key, value):
runs = get_recent_runs_data(run_metadata_key, value)
return jsonify(runs)
@app.route('/runs/key/<path:run_metadata_key>/<path:value>/recent/detail',
methods=['GET'])
def get_recent_runs_detail(run_metadata_key, value):
runs = get_recent_runs_data(run_metadata_key, value, detail=True)
return jsonify(runs)
def get_recent_runs_data(run_metadata_key, value, detail=False):
num_runs = flask.request.args.get('num_runs', 10)
with session_scope() as session:
results = api.get_recent_runs_by_key_value_metadata(
run_metadata_key, value, num_runs, session)
runs = []
for result in results:
if result.passes > 0 and result.fails == 0:
status = 'success'
elif result.fails > 0:
status = 'fail'
if detail:
run = result.to_dict()
else:
continue
if result.passes > 0 and result.fails == 0:
status = 'success'
elif result.fails > 0:
status = 'fail'
else:
continue
run = {
'id': result.uuid,
'status': status,
'start_date': result.run_at.isoformat(),
'link': result.artifacts,
}
run = {
'id': result.uuid,
'status': status,
'start_date': result.run_at.isoformat(),
'link': result.artifacts,
}
run_meta = api.get_run_metadata(result.uuid, session)
for meta in run_meta:
@ -302,7 +317,7 @@ def get_recent_runs(run_metadata_key, value):
run['build_name'] = meta.value
break
runs.append(run)
return jsonify(runs)
return runs
@app.route('/tests/recent/<string:status>', methods=['GET'])

View File

@ -768,6 +768,36 @@ class TestRestAPI(base.TestCase):
}]
self.assertEqual(expected_res, response_data)
@mock.patch('subunit2sql.db.api.get_run_metadata',
return_value=[models.RunMetadata(key='build_name',
value='job')])
@mock.patch('subunit2sql.db.api.get_recent_runs_by_key_value_metadata',
return_value=[
models.Run(uuid='uuid', run_at=timestamp_a,
artifacts='http://fake_url', passes=2, fails=0,
id='a_id', run_time=174, skips=10),
])
def test_get_recent_runs_detail(self, api_mock, api_meta_mock):
api.Session = mock.MagicMock()
res = self.app.get('/runs/key/a_key/a_value/recent/detail')
self.assertEqual(200, res.status_code)
api_mock.assert_called_once_with('a_key', 'a_value',
10, api.Session())
response_data = json.loads(res.data.decode('utf-8'))
format_time = timestamp_a.strftime('%a, %d %b %Y %H:%M:%S GMT')
expected_res = [{
u'artifacts': u'http://fake_url',
u'id': u'a_id',
u'build_name': u'job',
u'fails': 0,
u'passes': 2,
u'skips': 10,
u'run_at': format_time,
u'run_time': 174,
u'uuid': u'uuid'
}]
self.assertEqual(expected_res, response_data)
@mock.patch('subunit2sql.db.api.get_recent_failed_runs',
return_value=['a_convincing_id'])
@mock.patch('subunit2sql.db.api.get_test_runs_by_status_for_run_ids',