Skip to content

Commit

Permalink
ostriztools refactoring + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoura committed Jan 25, 2018
1 parent bb720b9 commit ff7e653
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 26 deletions.
26 changes: 13 additions & 13 deletions dump2polarion/ostriztools.py
Expand Up @@ -20,21 +20,21 @@

def _get_json(location):
"""Reads JSON data from file or URL."""
try:
json_data = requests.get(location)
except ValueError:
pass
else:
return json.loads(json_data.text, object_pairs_hook=OrderedDict).get('tests')

location = os.path.expanduser(location)
if os.path.isfile(location):
with io.open(location, encoding='utf-8') as json_data:
try:
try:
if os.path.isfile(location):
with io.open(location, encoding='utf-8') as json_data:
return json.load(json_data, object_pairs_hook=OrderedDict).get('tests')
except Exception as err:
raise Dump2PolarionException(
"Failed to parse JSON from {}: {}".format(location, err))
elif 'http' in location:
json_data = requests.get(location)
if not json_data:
raise Dump2PolarionException("Failed to download")
return json.loads(json_data.text, object_pairs_hook=OrderedDict).get('tests')
else:
raise Dump2PolarionException("Invalid location")
except Exception as err:
raise Dump2PolarionException(
"Failed to parse JSON from {}: {}".format(location, err))


def _get_testrun_id(version):
Expand Down
40 changes: 28 additions & 12 deletions tests/conftest.py
Expand Up @@ -12,20 +12,36 @@
from tests import conf


GENERIC_CONF = {
'xunit_target': 'https://polarion.example.com/import/xunit',
'testcase_taget': 'https://polarion.example.com/import/testcase',
'xunit_queue': 'https://polarion.example.com/import/xunit-queue',
'testcase_queue': 'https://polarion.example.com/import/testcase-queue'
}

RHCF3_PROPS = {
'polarion-dry-run': False,
'polarion-project-id': 'RHCF3',
'polarion-testrun-status-id': 'inprogress',
'polarion-response-test': 'test'
}

RHCF3_CONF = GENERIC_CONF.copy()
RHCF3_CONF['xunit_import_properties'] = RHCF3_PROPS

CMP_CONF = GENERIC_CONF.copy()
CMP_CONF['xunit_import_properties'] = RHCF3_PROPS.copy()
CMP_CONF['xunit_import_properties']['polarion-project-id'] = 'CMP'


@pytest.fixture(scope='module')
def config_prop():
return {
'xunit_import_properties': {
'polarion-dry-run': False,
'polarion-project-id': 'RHCF3',
'polarion-testrun-status-id': 'inprogress',
'polarion-response-test': 'test'
},
'xunit_target': 'https://polarion.example.com/import/xunit',
'testcase_taget': 'https://polarion.example.com/import/testcase',
'xunit_queue': 'https://polarion.example.com/import/xunit-queue',
'testcase_queue': 'https://polarion.example.com/import/testcase-queue'
}
return RHCF3_CONF


@pytest.fixture(scope='module')
def config_prop_cmp():
return CMP_CONF


@pytest.fixture(scope='function')
Expand Down
1 change: 1 addition & 0 deletions tests/data/ostriz_cmp.json
@@ -0,0 +1 @@
{"tests": {"cfme/tests/containers/test_tables_fields.py/test_tables_fields[ocp-v1-ImageRegistry (CMP-9985)]": {"requirement": "None", "run": "22", "stream": "downstream-59z", "test_module": "cfme/tests/containers/test_tables_fields.py", "durations": {"setup": 0.8646628856658936, "call": 26.643051862716675}, "test_name": "test_tables_fields[ocp-v1-ImageRegistry (CMP-9985)]", "tier": 1, "test_id": "CMP-9985", "slaveid": null, "statuses": {"overall": "passed", "setup": ["passed", false], "call": ["passed", false]}, "start_time": 1516743120.886384, "name": "cfme/tests/containers/test_tables_fields.py/test_tables_fields[ocp-v1-ImageRegistry (CMP-9985)]", "polarion": ["CMP-9985"], "finish_time": 1516743148.266379, "source": "jenkins", "version": "5.9.0.17", "params": {"browserName": "firefox", "provider": "ocp-v1", "browserVersion": "45.5.0", "browserPlatform": "LINUX", "test_item": "<cfme.containers.provider.ContainersTestItem object at 0x7fa29ab347d0>"}, "issues": [], "jenkins": {"build_url": "https://cfmeqe-jenkins.rhev-ci-vms.eng.rdu2.redhat.com/job/downstream-59z-cmqe-juwatts-poc/22/", "build_number": "22", "job_name": "downstream-59z-cmqe-juwatts-poc", "git_commit": "325707c384819521eb0d6594f7be882766eac9ba"}, "method": "automated", "build": "5.9.0.17-20180116225234_ac8b6f5"}, "cfme/tests/containers/test_tables_fields.py/test_tables_fields[ocp-v2-Container (CMP-9943)]": {"requirement": "None", "run": "22", "stream": "downstream-59z", "test_module": "cfme/tests/containers/test_tables_fields.py", "durations": {"setup": 0.7888579368591309, "call": 38.275193214416504}, "test_name": "test_tables_fields[ocp-v2-Container (CMP-9943)]", "tier": 1, "test_id": "CMP-9943", "slaveid": null, "statuses": {"overall": "passed", "setup": ["passed", false], "call": ["passed", false]}, "start_time": 1516743444.207331, "name": "cfme/tests/containers/test_tables_fields.py/test_tables_fields[ocp-v2-Container (CMP-9943)]", "polarion": ["CMP-9943"], "finish_time": 1516743483.123513, "source": "jenkins", "version": "5.9.0.17", "params": {"browserName": "firefox", "provider": "ocp-v2", "browserVersion": "45.5.0", "browserPlatform": "LINUX", "test_item": "<cfme.containers.provider.ContainersTestItem object at 0x7fa29ab34710>"}, "issues": [], "jenkins": {"build_url": "https://cfmeqe-jenkins.rhev-ci-vms.eng.rdu2.redhat.com/job/downstream-59z-cmqe-juwatts-poc/22/", "build_number": "22", "job_name": "downstream-59z-cmqe-juwatts-poc", "git_commit": "325707c384819521eb0d6594f7be882766eac9ba"}, "method": "automated", "build": "5.9.0.17-20180116225234_ac8b6f5"}}}
24 changes: 24 additions & 0 deletions tests/data/ostriz_transform_cmp.xml
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
<!--Generated for testrun 5_8_0_17-->
<properties>
<property name="polarion-testrun-id" value="5_8_0_17"/>
<property name="polarion-dry-run" value="False"/>
<property name="polarion-project-id" value="CMP"/>
<property name="polarion-response-test" value="test"/>
<property name="polarion-testrun-status-id" value="inprogress"/>
<property name="polarion-lookup-method" value="id"/>
</properties>
<testsuite errors="0" failures="0" name="Import for CMP - 5_8_0_17 testrun" skipped="0" tests="2" time="66.2962">
<testcase name="test_tables_fields[ocp-v1-ImageRegistry (CMP-9985)]" time="27.379995">
<properties>
<property name="polarion-testcase-id" value="CMP-9985"/>
</properties>
</testcase>
<testcase name="test_tables_fields[ocp-v2-Container (CMP-9943)]" time="38.916182">
<properties>
<property name="polarion-testcase-id" value="CMP-9943"/>
</properties>
</testcase>
</testsuite>
</testsuites>
39 changes: 38 additions & 1 deletion tests/test_ostriztools.py
Expand Up @@ -7,6 +7,8 @@
import io

import pytest

from mock import Mock, patch
from tests import conf

from dump2polarion import ostriztools
Expand All @@ -20,6 +22,12 @@ def records_json():
return ostriztools.import_ostriz(json_file)


@pytest.fixture(scope="module")
def records_json_cmp():
json_file = os.path.join(conf.DATA_PATH, 'ostriz_cmp.json')
return ostriztools.import_ostriz(json_file)


class TestOstriz(object):
def test_testrun_id_simple(self):
testrun_id = ostriztools._get_testrun_id('5.8.0.17')
Expand Down Expand Up @@ -60,12 +68,33 @@ def test_import_orig_data(self, records_json):
def test_invalid_json(self):
fname = 'junit-report.xml'
with pytest.raises(Dump2PolarionException) as excinfo:
ostriztools._get_json(os.path.join(conf.DATA_PATH, fname))
ostriztools.import_ostriz(os.path.join(conf.DATA_PATH, fname))
assert 'Failed to parse JSON' in str(excinfo.value)

def test_remote_invalid_json(self):
with patch('requests.get', return_value=False):
with pytest.raises(Dump2PolarionException) as excinfo:
ostriztools.import_ostriz('https://foo')
assert 'Failed to parse JSON' in str(excinfo.value)

def test_remote_json(self, records_json):
json_file = os.path.join(conf.DATA_PATH, 'ostriz.json')
with io.open(json_file, encoding='utf-8') as input_json:
parsed = input_json.read()
retval = Mock()
retval.text = parsed
with patch('requests.get', return_value=retval):
loaded_json = ostriztools.import_ostriz('https://foo')
assert loaded_json == records_json

def test_no_json(self):
with pytest.raises(Dump2PolarionException) as excinfo:
ostriztools.import_ostriz('NONEXISTENT.json')
assert 'Invalid location' in str(excinfo.value)

def test_empty_json(self):
with pytest.raises(Dump2PolarionException) as excinfo:
ostriztools._parse_ostriz('')
assert 'No data to import' in str(excinfo.value)

def test_e2e_ids_notransform(self, config_prop, records_json):
Expand All @@ -84,3 +113,11 @@ def test_e2e_ids_transform(self, config_prop, records_json):
with io.open(os.path.join(conf.DATA_PATH, fname), encoding='utf-8') as input_xml:
parsed = input_xml.read()
assert complete == parsed

def test_e2e_cmp_ids_transform(self, config_prop_cmp, records_json_cmp):
exporter = XunitExport('5_8_0_17', records_json_cmp, config_prop_cmp)
complete = exporter.export()
fname = 'ostriz_transform_cmp.xml'
with io.open(os.path.join(conf.DATA_PATH, fname), encoding='utf-8') as input_xml:
parsed = input_xml.read()
assert complete == parsed
10 changes: 10 additions & 0 deletions tests/test_utils.py
Expand Up @@ -134,3 +134,13 @@ def test_write_xml_no_data(self, tmpdir):
with pytest.raises(Dump2PolarionException) as excinfo:
utils.write_xml('', filename=os.path.join(dirname, 'output123.xml'))
assert 'No data to write' in str(excinfo.value)

def test_invalid_xml_root(self):
with pytest.raises(Dump2PolarionException) as excinfo:
utils.get_xml_root('NONEXISTENT.xml')
assert 'Failed to parse XML file' in str(excinfo.value)

def test_invalid_xml_str(self):
with pytest.raises(Dump2PolarionException) as excinfo:
utils.get_xml_root_from_str(None)
assert 'Failed to parse XML file' in str(excinfo.value)

0 comments on commit ff7e653

Please sign in to comment.