From c9097cc3c947a1ee7fda37292411fcab4a51e5ec Mon Sep 17 00:00:00 2001 From: Nathan Karsten Date: Wed, 30 Mar 2016 15:40:11 +0000 Subject: [PATCH] Add recent/detail openstack-health api. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- openstack_health/api.py | 39 +++++++++++++++++++++--------- openstack_health/tests/test_api.py | 30 +++++++++++++++++++++++ 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/openstack_health/api.py b/openstack_health/api.py index a348dddf..93a2416a 100644 --- a/openstack_health/api.py +++ b/openstack_health/api.py @@ -276,25 +276,40 @@ def get_runs_by_run_metadata_key(run_metadata_key, value): @app.route('/runs/key///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///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/', methods=['GET']) diff --git a/openstack_health/tests/test_api.py b/openstack_health/tests/test_api.py index bdacea11..18a3bd20 100644 --- a/openstack_health/tests/test_api.py +++ b/openstack_health/tests/test_api.py @@ -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',