From ff7e6534f03fa264621bad6a05c2d020bce03964 Mon Sep 17 00:00:00 2001 From: Martin Kourim Date: Thu, 25 Jan 2018 15:25:23 +0100 Subject: [PATCH] ostriztools refactoring + more tests --- dump2polarion/ostriztools.py | 26 +++++++++---------- tests/conftest.py | 40 ++++++++++++++++++++--------- tests/data/ostriz_cmp.json | 1 + tests/data/ostriz_transform_cmp.xml | 24 +++++++++++++++++ tests/test_ostriztools.py | 39 +++++++++++++++++++++++++++- tests/test_utils.py | 10 ++++++++ 6 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 tests/data/ostriz_cmp.json create mode 100644 tests/data/ostriz_transform_cmp.xml diff --git a/dump2polarion/ostriztools.py b/dump2polarion/ostriztools.py index 39af5c5..62e810e 100644 --- a/dump2polarion/ostriztools.py +++ b/dump2polarion/ostriztools.py @@ -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): diff --git a/tests/conftest.py b/tests/conftest.py index 7e006e2..c852922 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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') diff --git a/tests/data/ostriz_cmp.json b/tests/data/ostriz_cmp.json new file mode 100644 index 0000000..a6382b5 --- /dev/null +++ b/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": ""}, "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": ""}, "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"}}} diff --git a/tests/data/ostriz_transform_cmp.xml b/tests/data/ostriz_transform_cmp.xml new file mode 100644 index 0000000..53b2bfb --- /dev/null +++ b/tests/data/ostriz_transform_cmp.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/test_ostriztools.py b/tests/test_ostriztools.py index d33d36e..4d99c83 100644 --- a/tests/test_ostriztools.py +++ b/tests/test_ostriztools.py @@ -7,6 +7,8 @@ import io import pytest + +from mock import Mock, patch from tests import conf from dump2polarion import ostriztools @@ -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') @@ -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): @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py index 826123d..4f9023e 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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)