Exemplo n.º 1
0
def test_missing_required_marks(testdir, undecorated_test_function):
    """Verify that XSD will enforce the presence of 'test_id' and 'jira_id' properties for test cases."""

    # Setup
    testdir.makepyfile(
        undecorated_test_function.format(test_name='test_typo_global'))

    xml_doc = run_and_parse(testdir).xml_doc
    xmlschema = etree.XMLSchema(etree.parse(get_xsd()))

    # Test
    assert xmlschema.validate(xml_doc) is False
Exemplo n.º 2
0
def _load_input_file(file_path, pprint_on_fail=False):
    """Read and validate the input file contents.

    Args:
        file_path (str): A string representing a valid file path.
        pprint_on_fail (bool): A flag for enabling debug pretty print on schema failure.

    Returns:
        ElementTree: An ET object already pointed at the root "testsuite" element.

    Raises:
        RuntimeError: invalid path.
    """

    root_element = 'testsuite'
    junit_xsd = pytest_rpc.get_xsd()

    try:
        if os.path.getsize(file_path) > MAX_FILE_SIZE:
            raise RuntimeError(
                "Input file '{}' is larger than allowed max file size!".format(
                    file_path))

        junit_xml_doc = etree.parse(file_path)
    except (IOError, OSError):
        raise RuntimeError(
            "Invalid path '{}' for JUnitXML results file!".format(file_path))
    except etree.ParseError:
        raise RuntimeError(
            "The file '{}' does not contain valid XML!".format(file_path))

    try:
        xmlschema = etree.XMLSchema(etree.parse(junit_xsd))
        xmlschema.assertValid(junit_xml_doc)
        junit_xml = junit_xml_doc.getroot()
    except etree.DocumentInvalid as e:
        debug = "\n\n---DEBUG XML PRETTY PRINT---\n\n"
        error_message = "The file '{}' does not conform to schema!" \
                        "\n\nSchema Violation:\n{}".format(file_path, str(e))
        if pprint_on_fail:
            error_message = "{0}{1}{2}{1}".format(
                error_message, debug,
                etree.tostring(junit_xml_doc, pretty_print=True))
        raise RuntimeError("The file '{}' does not conform to schema!"
                           "\n\nSchema Violation:\n{}".format(
                               file_path, error_message))

    if junit_xml.tag != root_element:
        raise RuntimeError(
            "The file '{}' does not have JUnitXML '{}' root element!".format(
                file_path, root_element))

    return junit_xml
Exemplo n.º 3
0
def test_missing_uuid_mark(testdir, single_decorated_test_function):
    """Verify that XSD will enforce the presence of 'test_id' property for test cases."""

    # Setup
    testdir.makepyfile(
        single_decorated_test_function.format(test_name='test_missing_uuid',
                                              mark_type='jira',
                                              mark_arg='ASC-123'))

    xml_doc = run_and_parse(testdir).xml_doc
    xmlschema = etree.XMLSchema(etree.parse(get_xsd()))

    # Test
    assert xmlschema.validate(xml_doc) is False
Exemplo n.º 4
0
def test_happy_path_asc(testdir, properly_decorated_test_function):
    """Verify that 'get_xsd' returns an XSD stream that can be used to validate JUnitXML."""

    # Setup
    testdir.makepyfile(
        properly_decorated_test_function.format(
            test_name='test_happy_path',
            test_id='123e4567-e89b-12d3-a456-426655440000',
            jira_id='ASC-123'))

    xml_doc = run_and_parse(testdir).xml_doc
    xmlschema = etree.XMLSchema(etree.parse(get_xsd()))

    # Test
    xmlschema.assertValid(xml_doc)
Exemplo n.º 5
0
def test_multiple_jira_references(testdir):
    """Verify that 'get_xsd' returns an XSD stream when a testcase is decorated Jira mark with multiple
    arguments.
    """

    # Setup
    testdir.makepyfile("""
                import pytest
                @pytest.mark.jira('ASC-123', 'ASC-124')
                @pytest.mark.test_id('123e4567-e89b-12d3-a456-426655440000')
                def test_xsd():
                    pass
    """)

    xml_doc = run_and_parse(testdir).xml_doc
    xmlschema = etree.XMLSchema(etree.parse(get_xsd()))

    # Test
    xmlschema.assertValid(xml_doc)
Exemplo n.º 6
0
def test_typo_property(testdir, properly_decorated_test_function):
    """Verify that XSD will enforce the only certain property names are allowed for the testcase."""

    # Setup
    testdir.makepyfile(
        properly_decorated_test_function.format(
            test_name='test_typo_mark',
            test_id='123e4567-e89b-12d3-a456-426655440000',
            jira_id='ASC-123'))

    xml_doc = run_and_parse(testdir).xml_doc

    # Add another property element for the testcase.
    xml_doc.find(
        './testcase/properties/property').attrib['name'] = 'wrong_test_id'
    xmlschema = etree.XMLSchema(etree.parse(get_xsd()))

    # Test
    assert xmlschema.validate(xml_doc) is False
Exemplo n.º 7
0
def test_happy_path_mk8s(testdir, properly_decorated_test_function):
    """Verify that 'get_xsd' returns an XSD stream that can be used to validate JUnitXML when configured with mk8s."""

    # Setup
    testdir.makepyfile(
        properly_decorated_test_function.format(
            test_name='test_happy_path',
            test_id='123e4567-e89b-12d3-a456-426655440000',
            jira_id='ASC-123'))
    config = \
"""
[pytest]
ci-environment=mk8s
"""  # noqa

    xml_doc = run_and_parse_with_config(testdir, config).xml_doc
    xmlschema = etree.XMLSchema(etree.parse(get_xsd('mk8s')))

    # Test
    xmlschema.assertValid(xml_doc)
Exemplo n.º 8
0
def test_missing_global_property(testdir, properly_decorated_test_function,
                                 mocker):
    """Verify that XSD will enforce the presence of all required global test suite properties."""

    # Mock
    # Missing 'BUILD_URL'
    mock_env_vars = [x for x in TEST_ENV_VARS if x != 'BUILD_URL']

    mocker.patch('pytest_rpc.ASC_ENV_VARS', mock_env_vars)

    # Setup
    testdir.makepyfile(
        properly_decorated_test_function.format(
            test_name='test_missing_global',
            test_id='123e4567-e89b-12d3-a456-426655440000',
            jira_id='ASC-123'))

    xml_doc = run_and_parse(testdir).xml_doc
    xmlschema = etree.XMLSchema(etree.parse(get_xsd()))

    # Test
    assert xmlschema.validate(xml_doc) is False
Exemplo n.º 9
0
def test_extra_testcase_property(testdir, properly_decorated_test_function):
    """Verify that XSD will enforce the strict presence of only required test case properties."""

    # Setup
    testdir.makepyfile(
        properly_decorated_test_function.format(
            test_name='test_extra_mark',
            test_id='123e4567-e89b-12d3-a456-426655440000',
            jira_id='ASC-123'))

    xml_doc = run_and_parse(testdir).xml_doc

    # Add another property element for the testcase.
    xml_doc.find('./testcase/properties').append(
        etree.Element('property', attrib={
            'name': 'extra',
            'value': 'fail'
        }))
    xmlschema = etree.XMLSchema(etree.parse(get_xsd()))

    # Test
    assert xmlschema.validate(xml_doc) is False
Exemplo n.º 10
0
def test_typo_global_property(testdir, properly_decorated_test_function,
                              mocker):
    """Verify that XSD will enforce the only certain property names are allowed for the test suite."""

    # Mock
    # Typo for RPC_RELEASE
    mock_env_vars = [x for x in TEST_ENV_VARS if x != 'RPC_RELEASE'
                     ] + ['RCP_RELEASE']

    mocker.patch('pytest_rpc.ASC_ENV_VARS', mock_env_vars)

    # Setup
    testdir.makepyfile(
        properly_decorated_test_function.format(
            test_name='test_typo_global',
            test_id='123e4567-e89b-12d3-a456-426655440000',
            jira_id='ASC-123'))

    xml_doc = run_and_parse(testdir).xml_doc
    xmlschema = etree.XMLSchema(etree.parse(get_xsd()))

    # Test
    assert xmlschema.validate(xml_doc) is False