From 9403991171462902801f27382fdb76d40e5b388f Mon Sep 17 00:00:00 2001 From: sslypushenko Date: Wed, 14 Jan 2015 18:20:24 +0200 Subject: [PATCH] Updating tests results format according to spec Spec: https://github.com/stackforge/refstack/blob/master/specs/approved/api-v1.md Update patch: https://review.openstack.org/#/c/146680/ Change-Id: I75c62090a45d742b88acdfe080e24d62658976ce --- refstack_client/refstack_client.py | 5 ++- refstack_client/subunit_processor.py | 4 ++- .../tests/unit/.testrepository/0.json | 2 +- refstack_client/tests/unit/test_client.py | 31 +++++++++++++------ test-requirements.txt | 1 + 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/refstack_client/refstack_client.py b/refstack_client/refstack_client.py index 36d482f..28fe33f 100755 --- a/refstack_client/refstack_client.py +++ b/refstack_client/refstack_client.py @@ -168,8 +168,11 @@ class RefstackClient: self.logger.debug('API request content: %s ' % content) try: url = '%s/v1/results/' % self.args.url + headers = {'Content-type': 'application/json'} - response = requests.post(url, data={'data': json.dumps(content)}) + response = requests.post(url, + data=json.dumps(content), + headers=headers) self.logger.info(url + " Response: " + str(response.text)) except Exception as e: self.logger.critical('Failed to post %s - %s ' % (url, e)) diff --git a/refstack_client/subunit_processor.py b/refstack_client/subunit_processor.py index b0afdb5..aa653c8 100644 --- a/refstack_client/subunit_processor.py +++ b/refstack_client/subunit_processor.py @@ -35,7 +35,9 @@ class TempestSubunitTestResultPassOnly(testtools.TestResult): """Overwrite super class method for additional data processing.""" super(TempestSubunitTestResultPassOnly, self).addSuccess(testcase) # Remove any [] and () from the test ID before appending it. - self.results.append(re.sub('[\(\[].*[\]\)]', '', testcase.id())) + self.results.append( + {'name': re.sub('[\(\[].*[\]\)]', '', testcase.id())} + ) def get_results(self): return self.results diff --git a/refstack_client/tests/unit/.testrepository/0.json b/refstack_client/tests/unit/.testrepository/0.json index 5b24fb4..c91b93c 100644 --- a/refstack_client/tests/unit/.testrepository/0.json +++ b/refstack_client/tests/unit/.testrepository/0.json @@ -2,7 +2,7 @@ "duration_seconds": 0, "cpid": "test-id", "results": [ - "tempest.passed.test" + {"name": "tempest.passed.test"} ] } diff --git a/refstack_client/tests/unit/test_client.py b/refstack_client/tests/unit/test_client.py index a034830..aef0367 100755 --- a/refstack_client/tests/unit/test_client.py +++ b/refstack_client/tests/unit/test_client.py @@ -20,10 +20,12 @@ import os import tempfile import subprocess +import httmock import mock from mock import MagicMock import unittest + import refstack_client.refstack_client as rc @@ -55,7 +57,7 @@ class TestRefstackClient(unittest.TestCase): argv = ['test', '-c', conf_file_name, '--test-cases', 'tempest.api.compute', - '--url', '0.0.0.0'] + '--url', 'http://127.0.0.1'] if verbose: argv.append(verbose) return argv @@ -216,7 +218,7 @@ class TestRefstackClient(unittest.TestCase): client = rc.RefstackClient(args) subunit_file = self.test_path + "/.testrepository/0" results = client.get_passed_tests(subunit_file) - expected = ['tempest.passed.test'] + expected = [{'name': 'tempest.passed.test'}] self.assertEqual(expected, results) def test_run_tempest(self): @@ -232,20 +234,29 @@ class TestRefstackClient(unittest.TestCase): return_value=MagicMock(returncode=0)) self.patch("os.path.isfile", return_value=True) self.mock_keystone() - client.get_passed_tests = MagicMock(return_value=['test']) - client.post_results = MagicMock() + client.get_passed_tests = MagicMock(return_value=[{'name': 'test'}]) + client.logger.info = MagicMock() client._save_json_results = MagicMock() - client.test() + + expected_content = json.dumps({'test_id': 42}) + + @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v1/results/') + def refstack_api_mock(url, request): + return expected_content + + with httmock.HTTMock(refstack_api_mock): + client.test() + mock_popen.assert_called_with( ('%s/run_tempest.sh' % self.test_path, '-C', self.conf_file_name, '-V', '-t', '--', 'tempest.api.compute'), stderr=None ) - expected_content = {'duration_seconds': mock.ANY, - 'cpid': 'test-id', - 'results': ['test']} - client.post_results.assert_called_with('0.0.0.0', expected_content) + client.logger.info.assert_called_with( + 'http://127.0.0.1/v1/results/ Response: ' + '%s' % expected_content + ) def test_run_tempest_offline(self): """ @@ -319,7 +330,7 @@ class TestRefstackClient(unittest.TestCase): client.upload() expected_json = {'duration_seconds': 0, 'cpid': 'test-id', - 'results': ['tempest.passed.test']} + 'results': [{'name': 'tempest.passed.test'}]} client.post_results.assert_called_with('http://api.test.org', expected_json) diff --git a/test-requirements.txt b/test-requirements.txt index 30a009b..6aae138 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -7,3 +7,4 @@ testrepository>=0.0.18 testtools>=0.9.34 mock coverage +httmock \ No newline at end of file