summaryrefslogtreecommitdiffstats
path: root/tests/unit/reporter/test_testapi.py
blob: 8565527418364b82e53330e56bf30b158d0826a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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'}