Example #1
0
def test_wsman_fault_error():
    with pytest.raises(WSManFaultError) as exc:
        raise WSManFaultError(1234, "machine", "reason", "provider", "path",
                              "fault")
    assert str(exc.value) == "Received a WSManFault message. (Code: 1234, " \
                             "Machine: machine, Reason: reason, " \
                             "Provider: provider, Provider Path: path, " \
                             "Provider Fault: fault)"
    assert exc.value.code == 1234
    assert exc.value.machine == "machine"
    assert exc.value.reason == "reason"
    assert exc.value.provider == "provider"
    assert exc.value.provider_path == "path"
    assert exc.value.provider_fault == "fault"
Example #2
0
    def _parse_wsman_fault(xml_text: str) -> WSManFaultError:
        xml = ET.fromstring(xml_text)
        code: typing.Optional[typing.Union[str, int]] = None
        reason = None
        machine = None
        provider = None
        provider_path = None
        provider_fault = None

        fault = xml.find("s:Body/s:Fault", namespaces=NAMESPACES)
        if fault is not None:
            code_info = fault.find("s:Code/s:Subcode/s:Value", namespaces=NAMESPACES)
            if code_info is not None:
                code = code_info.text
            else:
                code_info = fault.find("s:Code/s:Value", namespaces=NAMESPACES)
                if code_info is not None:
                    code = code_info.text

            reason_info = fault.find("s:Reason/s:Text", namespaces=NAMESPACES)
            if reason_info is not None:
                reason = reason_info.text

        wsman_fault = fault.find("s:Detail/wsmanfault:WSManFault", namespaces=NAMESPACES) if fault else None
        if wsman_fault is not None:
            code = wsman_fault.attrib.get("Code", code)
            machine = wsman_fault.attrib.get("Machine")

            message_info = wsman_fault.find("wsmanfault:Message", namespaces=NAMESPACES)
            if message_info is not None:
                # message may still not be set, fall back to the existing
                # reason value from the base soap Fault element
                reason = message_info.text or reason

            provider_info = wsman_fault.find("wsmanfault:Message/wsmanfault:ProviderFault", namespaces=NAMESPACES)
            if provider_info is not None:
                provider = provider_info.attrib.get("provider")
                provider_path = provider_info.attrib.get("path")
                provider_fault = provider_info.text

        # lastly try and cleanup the value of the parameters
        try:
            code = int(code or "")
        except (TypeError, ValueError):
            pass

        reason = reason.strip() if reason else None
        provider_fault = provider_fault.strip() if provider_fault else None

        return WSManFaultError(code, machine, reason, provider, provider_path, provider_fault)
Example #3
0
def test_wsman_fault_error_empty():
    with pytest.raises(WSManFaultError) as exc:
        raise WSManFaultError(None, None, None, None, None, None)
    assert str(exc.value) == "Received a WSManFault message. (No details " \
                             "returned by the server)"
Example #4
0
    def _parse_wsman_fault(xml_text):
        xml = ET.fromstring(xml_text)
        code = None
        reason = None
        machine = None
        provider = None
        provider_path = None
        provider_fault = None

        fault = xml.find("s:Body/s:Fault", namespaces=NAMESPACES)
        if fault is not None:
            code_info = fault.find("s:Code/s:Subcode/s:Value",
                                   namespaces=NAMESPACES)
            if code_info is not None:
                code = code_info.text
            else:
                code_info = fault.find("s:Code/s:Value", namespaces=NAMESPACES)
                if code_info is not None:
                    code = code_info.text

            reason_info = fault.find("s:Reason/s:Text", namespaces=NAMESPACES)
            if reason_info is not None:
                reason = reason_info.text

        wsman_fault = fault.find("s:Detail/wsmanfault:WSManFault",
                                 namespaces=NAMESPACES)
        if wsman_fault is not None:
            code = wsman_fault.attrib.get('Code', code)
            machine = wsman_fault.attrib.get('Machine')

            message_info = wsman_fault.find("wsmanfault:Message",
                                            namespaces=NAMESPACES)
            if message_info is not None:
                # message may still not be set, fall back to the existing
                # reason value from the base soap Fault element
                reason = message_info.text if message_info.text else reason

            provider_info = wsman_fault.find(
                "wsmanfault:Message/"
                "wsmanfault:ProviderFault",
                namespaces=NAMESPACES)
            if provider_info is not None:
                provider = provider_info.attrib.get('provider')
                provider_path = provider_info.attrib.get('path')
                provider_fault = provider_info.text

        # lastly try and cleanup the value of the parameters
        try:
            code = int(code)
        except (TypeError, ValueError):
            pass

        try:
            reason = reason.strip()
        except AttributeError:
            pass

        try:
            provider_fault = provider_fault.strip()
        except AttributeError:
            pass

        return WSManFaultError(code, machine, reason, provider, provider_path,
                               provider_fault)