Add API method to get a list of recent runs by metadata
This commit adds a new rest api method to return a list of recent runs for a given run_metadata key value pair. It will be useful in constructing lists with links to recent runs. Change-Id: Ic5557e42ab158b865c024dc539d291c061a69dce Depends-On: I855c47e0056f459b1f71e919d0331c2e8ecbd3a8
This commit is contained in:
parent
e1034aa95e
commit
edeb4fc851
|
@ -258,6 +258,39 @@ def get_runs_by_run_metadata_key(run_metadata_key, value):
|
|||
return jsonify(_aggregate_runs(aggregated_runs))
|
||||
|
||||
|
||||
@app.route('/runs/key/<path:run_metadata_key>/<path:value>/recent',
|
||||
methods=['GET'])
|
||||
def get_recent_runs(run_metadata_key, value):
|
||||
session = get_session()
|
||||
|
||||
num_runs = flask.request.args.get('num_runs', 10)
|
||||
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'
|
||||
else:
|
||||
continue
|
||||
|
||||
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:
|
||||
if meta.key == 'build_name':
|
||||
run['build_name'] = meta.value
|
||||
break
|
||||
runs.append(run)
|
||||
return jsonify(runs)
|
||||
|
||||
|
||||
@app.route('/run/<string:run_id>/tests', methods=['GET'])
|
||||
def get_tests_from_run(run_id):
|
||||
session = get_session()
|
||||
|
|
|
@ -699,3 +699,36 @@ class TestRestAPI(base.TestCase):
|
|||
api_mock.assert_called_once_with('fake.test.id', start_date=None,
|
||||
stop_date=None,
|
||||
session=api.Session())
|
||||
|
||||
@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='a_uuid', run_at=timestamp_a,
|
||||
artifacts='http://fake_url', passes=2, fails=0),
|
||||
models.Run(uuid='b_uuid', run_at=timestamp_b,
|
||||
artifacts='http://less_fake_url', fails=1,
|
||||
passes=42)
|
||||
])
|
||||
def test_get_recent_runs(self, api_mock, api_meta_mock):
|
||||
api.Session = mock.MagicMock()
|
||||
res = self.app.get('/runs/key/a_key/a_value/recent')
|
||||
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)
|
||||
expected_res = [{
|
||||
u'id': u'a_uuid',
|
||||
u'build_name': u'job',
|
||||
u'start_date': timestamp_a.isoformat(),
|
||||
u'link': u'http://fake_url',
|
||||
u'status': 'success'
|
||||
}, {
|
||||
u'id': u'b_uuid',
|
||||
u'build_name': u'job',
|
||||
u'start_date': timestamp_b.isoformat(),
|
||||
u'link': u'http://less_fake_url',
|
||||
u'status': 'fail'
|
||||
}]
|
||||
self.assertEqual(expected_res, response_data)
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
# process, which may cause wedges in the gate later.
|
||||
pbr<2.0,>=1.6
|
||||
flask
|
||||
subunit2sql>=1.0.2
|
||||
subunit2sql>=1.2.0
|
||||
sqlalchemy
|
||||
flask-jsonpify
|
||||
PyMySQL>=0.6.2
|
||||
|
|
Loading…
Reference in New Issue