From 84666edf145dd3a5bb27f1038b704fe1682f451b Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Mon, 12 Dec 2016 17:32:47 +0800 Subject: refactor qtip-server structure JIRA: QTIP-186 Change-Id: I4ecdcdc2869de0ef717ebf4bd1346e7c428b5b19 Signed-off-by: SerenaFeng --- tests/unit/api/qtip_server_test.py | 117 ----------------------------------- tests/unit/api/test_server.py | 123 +++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 117 deletions(-) delete mode 100644 tests/unit/api/qtip_server_test.py create mode 100644 tests/unit/api/test_server.py (limited to 'tests/unit/api') diff --git a/tests/unit/api/qtip_server_test.py b/tests/unit/api/qtip_server_test.py deleted file mode 100644 index 96544c95..00000000 --- a/tests/unit/api/qtip_server_test.py +++ /dev/null @@ -1,117 +0,0 @@ -import qtip.api.qtip_server as server -import pytest -import json -import mock -import time - - -@pytest.fixture -def app(): - return server.app - - -@pytest.fixture -def app_client(app): - client = app.test_client() - return client - - -def side_effect_sleep(sleep_time): - time.sleep(sleep_time) - - -def side_effect_pass(): - pass - - -class TestClass: - @pytest.mark.parametrize("body, expected", [ - ({'installer_type': 'fuel', - 'installer_ip': '10.20.0.2'}, - {'job_id': '', - 'installer_type': 'fuel', - 'installer_ip': '10.20.0.2', - 'pod_name': 'default', - 'suite_name': 'compute', - 'max_minutes': 60, - 'type': 'BM', - 'testdb_url': None, - 'node_name': None, - 'state': 'finished', - 'state_detail': [{'state': 'finished', 'benchmark': 'dhrystone_bm.yaml'}, - {'state': 'finished', 'benchmark': 'whetstone_bm.yaml'}, - {'state': 'finished', 'benchmark': 'ramspeed_bm.yaml'}, - {'state': 'finished', 'benchmark': 'dpi_bm.yaml'}, - {'state': 'finished', 'benchmark': 'ssl_bm.yaml'}], - 'result': 0}), - ({'installer_type': 'fuel', - 'installer_ip': '10.20.0.2', - 'pod_name': 'default', - 'max_minutes': 20, - 'suite_name': 'compute', - 'type': 'VM', - 'benchmark_name': 'dhrystone_vm.yaml', - 'testdb_url': 'http://testresults.opnfv.org/test/api/v1', - 'node_name': 'zte-pod2'}, - {'job_id': '', - 'installer_type': 'fuel', - 'installer_ip': '10.20.0.2', - 'pod_name': 'default', - 'suite_name': 'compute', - 'max_minutes': 20, - 'type': 'VM', - 'testdb_url': 'http://testresults.opnfv.org/test/api/v1', - 'node_name': 'zte-pod2', - 'state': 'finished', - 'state_detail': [{u'state': u'finished', u'benchmark': u'dhrystone_vm.yaml'}], - 'result': 0}) - ]) - @mock.patch('qtip.api.qtip_server.args_handler.prepare_and_run_benchmark') - def test_post_get_delete_job_successful(self, mock_args_handler, app_client, body, expected): - mock_args_handler.return_value = {'result': 0, - 'detail': {'host': [(u'10.20.6.14', {'unreachable': 0, - 'skipped': 13, - 'ok': 27, - 'changed': 26, - 'failures': 0}), - ('localhost', {'unreachable': 0, - 'skipped': 0, - 'ok': 6, - 'changed': 6, - 'failures': 0}), - (u'10.20.6.13', {'unreachable': 0, - 'skipped': 13, - 'ok': 27, - 'changed': 26, - 'failures': 0})]}} - - reply = app_client.post("/api/v1.0/jobs", data=body) - print(reply.data) - id = json.loads(reply.data)['job_id'] - expected['job_id'] = id - post_process = '' - while post_process != 'finished': - get_reply = app_client.get("/api/v1.0/jobs/%s" % id) - reply_data = json.loads(get_reply.data) - post_process = reply_data['state'] - print(reply_data) - assert len(filter(lambda x: reply_data[x] == expected[x], expected.keys())) == len(expected) - delete_reply = app_client.delete("/api/v1.0/jobs/%s" % id) - assert "successful" in delete_reply.data - - @pytest.mark.parametrize("body, expected", [ - ([{'installer_type': 'fuel', - 'installer_ip': '10.20.0.2'}, - {'installer_type': 'compass', - 'installer_ip': '192.168.20.50'}], - ['job_id', - 'It already has one job running now!']) - ]) - @mock.patch('qtip.api.qtip_server.args_handler.prepare_and_run_benchmark', - side_effect=[side_effect_sleep(0.5), side_effect_pass]) - def test_post_two_jobs_unsuccessful(self, mock_args_hanler, app_client, body, expected): - reply_1 = app_client.post("/api/v1.0/jobs", data=body[0]) - reply_2 = app_client.post("/api/v1.0/jobs", data=body[1]) - assert expected[0] in json.loads(reply_1.data).keys() - app_client.delete("/api/v1.0/jobs/%s" % json.loads(reply_1.data)['job_id']) - assert expected[1] in json.dumps(reply_2.data) diff --git a/tests/unit/api/test_server.py b/tests/unit/api/test_server.py new file mode 100644 index 00000000..e9364d3d --- /dev/null +++ b/tests/unit/api/test_server.py @@ -0,0 +1,123 @@ +import json +import time + +import mock +import pytest + +import qtip.api.cmd.server as server + + +def setup_module(): + server.add_routers() + + +@pytest.fixture +def app(): + return server.app + + +@pytest.fixture +def app_client(app): + client = app.test_client() + return client + + +def side_effect_sleep(sleep_time): + time.sleep(sleep_time) + + +def side_effect_pass(): + pass + + +class TestClass: + @pytest.mark.parametrize("body, expected", [ + ({'installer_type': 'fuel', + 'installer_ip': '10.20.0.2'}, + {'job_id': '', + 'installer_type': 'fuel', + 'installer_ip': '10.20.0.2', + 'pod_name': 'default', + 'suite_name': 'compute', + 'max_minutes': 60, + 'type': 'BM', + 'testdb_url': None, + 'node_name': None, + 'state': 'finished', + 'state_detail': [{'state': 'finished', 'benchmark': 'dhrystone_bm.yaml'}, + {'state': 'finished', 'benchmark': 'whetstone_bm.yaml'}, + {'state': 'finished', 'benchmark': 'ramspeed_bm.yaml'}, + {'state': 'finished', 'benchmark': 'dpi_bm.yaml'}, + {'state': 'finished', 'benchmark': 'ssl_bm.yaml'}], + 'result': 0}), + ({'installer_type': 'fuel', + 'installer_ip': '10.20.0.2', + 'pod_name': 'default', + 'max_minutes': 20, + 'suite_name': 'compute', + 'type': 'VM', + 'benchmark_name': 'dhrystone_vm.yaml', + 'testdb_url': 'http://testresults.opnfv.org/test/api/v1', + 'node_name': 'zte-pod2'}, + {'job_id': '', + 'installer_type': 'fuel', + 'installer_ip': '10.20.0.2', + 'pod_name': 'default', + 'suite_name': 'compute', + 'max_minutes': 20, + 'type': 'VM', + 'testdb_url': 'http://testresults.opnfv.org/test/api/v1', + 'node_name': 'zte-pod2', + 'state': 'finished', + 'state_detail': [{u'state': u'finished', u'benchmark': u'dhrystone_vm.yaml'}], + 'result': 0}) + ]) + @mock.patch('qtip.utils.args_handler.prepare_and_run_benchmark') + def test_post_get_delete_job_successful(self, mock_args_handler, app_client, body, expected): + mock_args_handler.return_value = {'result': 0, + 'detail': {'host': [(u'10.20.6.14', {'unreachable': 0, + 'skipped': 13, + 'ok': 27, + 'changed': 26, + 'failures': 0}), + ('localhost', {'unreachable': 0, + 'skipped': 0, + 'ok': 6, + 'changed': 6, + 'failures': 0}), + (u'10.20.6.13', {'unreachable': 0, + 'skipped': 13, + 'ok': 27, + 'changed': 26, + 'failures': 0})]}} + + reply = app_client.post("/api/v1.0/jobs", data=body) + print(reply.data) + id = json.loads(reply.data)['job_id'] + expected['job_id'] = id + post_process = '' + while post_process != 'finished': + get_reply = app_client.get("/api/v1.0/jobs/%s" % id) + reply_data = json.loads(get_reply.data) + post_process = reply_data['state'] + print(reply_data) + assert len(filter(lambda x: reply_data[x] == expected[x], expected.keys())) == len(expected) + delete_reply = app_client.delete("/api/v1.0/jobs/%s" % id) + assert "successful" in delete_reply.data + + @pytest.mark.parametrize("body, expected", [ + ([{'installer_type': 'fuel', + 'installer_ip': '10.20.0.2'}, + {'installer_type': 'compass', + 'installer_ip': '192.168.20.50'}], + ['job_id', + 'It already has one job running now!']) + ]) + @mock.patch('qtip.utils.args_handler.prepare_and_run_benchmark', + side_effect=[side_effect_sleep(0.5), side_effect_pass]) + def test_post_two_jobs_unsuccessful(self, mock_args_hanler, app_client, body, expected): + reply_1 = app_client.post("/api/v1.0/jobs", data=body[0]) + reply_2 = app_client.post("/api/v1.0/jobs", data=body[1]) + assert expected[0] in json.loads(reply_1.data).keys() + app_client.delete("/api/v1.0/jobs/%s" % json.loads(reply_1.data)['job_id']) + assert expected[1] in json.dumps(reply_2.data) -- cgit 1.2.3-korg