From 51552e12d46c7993318117ace8ad0100a4857a06 Mon Sep 17 00:00:00 2001 From: Ryan Gard Date: Thu, 19 Apr 2018 21:06:10 -0700 Subject: [PATCH] ASC-412 Fix Broken Regex Matcher Fixed "_generate_module_hierarchy" function to use "search" instead of "match" for regex testing the "classname" attribute. Added exception handling for invalid "classname" attribute plus a test for coverage. Bumped version in anticipation of a patch release. --- setup.cfg | 2 +- setup.py | 2 +- tests/conftest.py | 23 +++++++++++++++++++++++ tests/test_zigzag.py | 10 ++++++++++ zigzag/__init__.py | 2 +- zigzag/zigzag.py | 7 +++++-- 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/setup.cfg b/setup.cfg index 752e024..ae8a6c5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.7.0 +current_version = 0.7.1 commit = false tag = false diff --git a/setup.py b/setup.py index 8527694..5e27b53 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ setup( name='rpc-zigzag', - version='0.7.0', + version='0.7.1', author="rcbops", author_email='rcb-deploy@lists.rackspace.com', maintainer='rcbops', diff --git a/tests/conftest.py b/tests/conftest.py index 820ea69..5cad7a2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -406,3 +406,26 @@ def missing_build_url_xml(tmpdir_factory): f.write(junit_xml) return filename + + +@pytest.fixture(scope='session') +def invalid_classname_xml(tmpdir_factory): + """JUnitXML sample representing a testcase that has an invalid 'classname' attribute which is used to build the + results hierarchy in the '_generate_module_hierarchy' function.""" + + filename = tmpdir_factory.mktemp('data').join('invalid_classname.xml').strpath + junit_xml = \ + """ + + {global_properties} + + {testcase_properties} + + + """.format(global_properties=DEFAULT_GLOBAL_PROPERTIES, testcase_properties=DEFAULT_TESTCASE_PROPERTIES) + + with open(filename, 'w') as f: + f.write(junit_xml) + + return filename diff --git a/tests/test_zigzag.py b/tests/test_zigzag.py index f05903f..12985a5 100644 --- a/tests/test_zigzag.py +++ b/tests/test_zigzag.py @@ -229,6 +229,16 @@ def test_junit_xml_attachment(self, single_passing_xml): assert attachment_exp_content_type == test_log_dict['attachments'][0]['content_type'] assert attachment_exp_data_md5 == md5(test_log_dict['attachments'][0]['data'].encode('UTF-8')).hexdigest() + def test_invalid_classname(self, invalid_classname_xml): + """Verify that JUnitXML that has an invalid 'classname' attribute for a testcase raises a RuntimeError.""" + + # Setup + junit_xml = zigzag._load_input_file(invalid_classname_xml) + + # Test + with pytest.raises(RuntimeError): + zigzag._generate_test_logs(junit_xml) + # noinspection PyUnresolvedReferences class TestGenerateAutoRequest(object): diff --git a/zigzag/__init__.py b/zigzag/__init__.py index 8e7e535..ea78931 100644 --- a/zigzag/__init__.py +++ b/zigzag/__init__.py @@ -4,4 +4,4 @@ __author__ = """rcbops""" __email__ = 'rcb-deploy@lists.rackspace.com' -__version__ = '0.7.0' +__version__ = '0.7.1' diff --git a/zigzag/zigzag.py b/zigzag/zigzag.py index 1523aaa..26cd15f 100644 --- a/zigzag/zigzag.py +++ b/zigzag/zigzag.py @@ -75,7 +75,8 @@ def _generate_module_hierarchy(testcase_xml, testsuite_props): list(str): An ordered list of strings to use for the qTest results hierarchy. Raises: - KeyError: missing property. + KeyError: missing test suite property. + AttributeError: the testcase 'classname' attribute is invalid """ module_hierarchy = [testsuite_props['RPC_RELEASE'], # RPC Release Version (e.g. 16.0.0) @@ -83,7 +84,7 @@ def _generate_module_hierarchy(testcase_xml, testsuite_props): testsuite_props['MOLECULE_TEST_REPO'], # (e.g. molecule-validate-neutron-deploy) testsuite_props['MOLECULE_SCENARIO_NAME']] # (e.g. "default") - testcase_groups = TESTCASE_GROUP_RGX.match(testcase_xml.attrib['classname']).groups() + testcase_groups = TESTCASE_GROUP_RGX.search(testcase_xml.attrib['classname']).groups() module_hierarchy.append(testcase_groups[0]) # Always append at least the filename of the test grouping. if testcase_groups[1]: @@ -123,6 +124,8 @@ def _generate_test_logs(junit_xml): test_log.module_names = _generate_module_hierarchy(testcase_xml, testsuite_props) except KeyError as e: raise RuntimeError("Test suite is missing the required property!\n\n{}".format(str(e))) + except AttributeError: + raise RuntimeError("Test case '{}' has an invalid 'classname' attribute!".format(test_log.name)) try: test_log.name = TESTCASE_NAME_RGX.match(testcase_xml.attrib['name']).group(1)