diff options
author | zhihui wu <wu.zhihui1@zte.com.cn> | 2017-04-19 03:00:45 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-04-19 03:00:45 +0000 |
commit | 6dc3c500b3b6fb14b5098259dc57a2d646daaf2c (patch) | |
tree | eebd3889de7cdf3d8a6aca48078e3d6a11c2bca5 /tests/unit | |
parent | 2781bdb0a4d3802719f4dc6f862bb7e8cfbaed41 (diff) | |
parent | 82204f587d8abd112182dd914dc6862a0955ca8c (diff) |
Merge "Add unittests for testapi client"
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/reporter/test_testapi.py | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/tests/unit/reporter/test_testapi.py b/tests/unit/reporter/test_testapi.py new file mode 100644 index 00000000..85655274 --- /dev/null +++ b/tests/unit/reporter/test_testapi.py @@ -0,0 +1,116 @@ +############################################################################## +# Copyright (c) 2017 akhil.batra@research.iiit.ac.in and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + + +import mock +import pytest + +from qtip.reporter import testapi + + +@pytest.mark.parametrize("testapi_url, payload", [ + ("http://testresults.opnfv.org/test/api/v1", {'project_name': 'qtip', + 'case_name': 'fake-case', + 'pod_name': 'fake_pod', + 'installer': 'fake_installer', + 'version': '1', + 'scenario': 'fake_scenario', + 'criteria': 'fake_criteria', + 'build_tag': 'fake_tag', + 'start_date': 'fake_date', + 'stop_date': 'fake_date', + 'details': 'fake:details'}), +]) +@mock.patch('qtip.reporter.testapi.requests') +def test_testapi_unavailable(mock_request, testapi_url, payload): + mock_request.post.return_value.status_code = testapi.requests.codes.unavailable + mock_request.post.return_value.reason = 'Service unavailable' + testapi.push_results(testapi_url, payload) + mock_request.post.assert_called_with(testapi_url + '/results', json=payload) + mock_request.post.return_value.raise_for_status.assert_called() + + +@pytest.mark.parametrize("testapi_url, payload", [ + ("http://testresults.opnfv.org/test/api/v1", {'project_name': 'qtip', + 'case_name': 'fake-case', + 'pod_name': 'fake_pod', + 'installer': 'fake_installer', + 'version': '1', + 'scenario': 'fake_scenario', + 'criteria': 'fake_criteria', + 'build_tag': 'fake_tag', + 'start_date': 'fake_date', + 'stop_date': 'fake_date', + 'details': 'fake:details'}), +]) +@mock.patch('qtip.reporter.testapi.requests') +def test_push_results_success(mock_request, testapi_url, payload): + mock_request.post.return_value.status_code = testapi.requests.codes.ok + mock_request.post.return_value.json.return_value = {'href': 'mock_url'} + push_response = testapi.push_results(testapi_url, payload) + mock_request.post.assert_called_with(testapi_url + '/results', json=payload) + assert push_response['href'] == 'mock_url' + + +@pytest.mark.parametrize("testapi_url, payload", [ + ("http://testresults.opnfv.org/test/api/v1", {'project_name': 'qtip', + 'case_name': 'fake-case', + 'pod_name': 'fake_pod', + 'installer': 'fake_installer', + 'version': '1', + 'scenario': 'fake_scenario', + 'criteria': 'fake_criteria', + 'build_tag': 'fake_tag', + 'start_date': 'fake_date', + 'stop_date': 'fake_date', + 'details': 'fake:details'}), +]) +@mock.patch('qtip.reporter.testapi.requests') +def test_push_results_not_found(mock_request, testapi_url, payload): + mock_request.post.return_value.status_code = testapi.requests.codes.not_found + mock_request.post.return_value.reason = 'Not found' + testapi.push_results(testapi_url, payload) + mock_request.post.assert_called_with(testapi_url + '/results', json=payload) + mock_request.post.return_value.raise_for_status.assert_called() + + +@pytest.mark.parametrize("testapi_url, payload", [ + ("http://testresults.opnfv.org/test/api/v1", {'project_name': 'qtip', + 'case_name': 'fake-case', + 'pod_name': 'fake_pod', + 'installer': 'fake_installer', + 'version': '', + 'scenario': None, + 'criteria': 'fake_criteria', + 'build_tag': 'fake_tag', + 'start_date': 'fake_date', + 'stop_date': 'fake_date', + 'details': 'fake:details'}), +]) +def test_push_results_invalid_params(testapi_url, payload): + with pytest.raises(testapi.InvalidParamsError) as error_invalid: + testapi.push_results(testapi_url, payload) + assert set(error_invalid.value.params) == {'version', 'scenario'} + + +@pytest.mark.parametrize("testapi_url, payload", [ + ("http://testresults.opnfv.org/test/api/v1", {'project_name': 'qtip', + 'case_name': 'fake-case', + 'pod_name': 'fake_pod', + 'installer': 'fake_installer', + 'criteria': 'fake_criteria', + 'build_tag': 'fake_tag', + 'start_date': 'fake_date', + 'stop_date': 'fake_date', + 'details': 'fake:details'}), +]) +def test_push_results_missing_params(testapi_url, payload): + with pytest.raises(testapi.MissingParamsError) as error_missing: + testapi.push_results(testapi_url, payload) + assert set(error_missing.value.params) == {'version', 'scenario'} |