text: # flake8: noqa
  import os
  import uuid
  import xmltodict
  from pytest import skip, fixture
  from mock import patch
  
  
  open_shell_request = """\
  <?xml version="1.0" encoding="utf-8"?>
  <env:Envelope xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
    <env:Header>
      <a:To>http://windows-host:5985/wsman</a:To>
      <a:ReplyTo>
        <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
      </a:ReplyTo>
      <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
      <a:MessageID>uuid:11111111-1111-1111-1111-111111111111</a:MessageID>
      <w:Locale mustUnderstand="false" xml:lang="en-US" />
      <p:DataLocale mustUnderstand="false" xml:lang="en-US" />
      <w:OperationTimeout>PT20S</w:OperationTimeout>
      <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
      <a:Action mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/09/transfer/Create</a:Action>
      <w:OptionSet>
        <w:Option Name="WINRS_NOPROFILE">FALSE</w:Option>
        <w:Option Name="WINRS_CODEPAGE">437</w:Option>
      </w:OptionSet>
    </env:Header>
    <env:Body>
      <rsp:Shell>
        <rsp:InputStreams>stdin</rsp:InputStreams>
        <rsp:OutputStreams>stdout stderr</rsp:OutputStreams>
      </rsp:Shell>
    </env:Body>
  </env:Envelope>"""
  
  open_shell_response = """\
  <?xml version="1.0" ?>
  <s:Envelope xml:lang="en-US" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer">
      <s:Header>
          <a:Action>http://schemas.xmlsoap.org/ws/2004/09/transfer/CreateResponse</a:Action>
          <a:MessageID>uuid:11111111-1111-1111-1111-111111111112</a:MessageID>
          <a:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:To>
          <a:RelatesTo>uuid:11111111-1111-1111-1111-111111111111</a:RelatesTo>
      </s:Header>
      <s:Body>
          <x:ResourceCreated>
              <a:Address>http://windows-host:5985/wsman</a:Address>
              <a:ReferenceParameters>
                  <w:ResourceURI>http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
                  <w:SelectorSet>
                      <w:Selector Name="ShellId">11111111-1111-1111-1111-111111111113</w:Selector>
                  </w:SelectorSet>
              </a:ReferenceParameters>
          </x:ResourceCreated>
      </s:Body>
  </s:Envelope>"""
  
  close_shell_request = """\
  <?xml version="1.0" encoding="utf-8"?>
  <env:Envelope xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
    <env:Header>
      <a:To>http://windows-host:5985/wsman</a:To>
      <a:ReplyTo>
        <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
      </a:ReplyTo>
      <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
      <a:MessageID>uuid:11111111-1111-1111-1111-111111111111</a:MessageID>
      <w:Locale mustUnderstand="false" xml:lang="en-US" />
      <p:DataLocale mustUnderstand="false" xml:lang="en-US" />
      <w:OperationTimeout>PT20S</w:OperationTimeout>
      <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
      <a:Action mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete</a:Action>
      <w:SelectorSet>
        <w:Selector Name="ShellId">11111111-1111-1111-1111-111111111113</w:Selector>
      </w:SelectorSet>
    </env:Header>
    <env:Body>
    </env:Body>
  </env:Envelope>"""
  
  close_shell_response = """\
  <?xml version="1.0" ?>
  <s:Envelope xml:lang="en-US" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
      <s:Header>
          <a:Action>http://schemas.xmlsoap.org/ws/2004/09/transfer/DeleteResponse</a:Action>
          <a:MessageID>uuid:11111111-1111-1111-1111-111111111112</a:MessageID>
          <a:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:To>
          <a:RelatesTo>uuid:11111111-1111-1111-1111-111111111111</a:RelatesTo>
      </s:Header>
      <s:Body/>
  </s:Envelope>
  """
  
  run_cmd_with_args_request = """\
  <?xml version="1.0" encoding="utf-8"?>
  <env:Envelope xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
    <env:Header>
      <a:To>http://windows-host:5985/wsman</a:To>
      <a:ReplyTo>
        <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
      </a:ReplyTo>
      <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
      <a:MessageID>uuid:11111111-1111-1111-1111-111111111111</a:MessageID>
      <w:Locale mustUnderstand="false" xml:lang="en-US" />
      <p:DataLocale mustUnderstand="false" xml:lang="en-US" />
      <w:OperationTimeout>PT20S</w:OperationTimeout>
      <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
      <a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command</a:Action>
      <w:SelectorSet>
        <w:Selector Name="ShellId">11111111-1111-1111-1111-111111111113</w:Selector>
      </w:SelectorSet>
      <w:OptionSet>
        <w:Option Name="WINRS_CONSOLEMODE_STDIN">TRUE</w:Option>
        <w:Option Name="WINRS_SKIP_CMD_SHELL">FALSE</w:Option>
      </w:OptionSet>
    </env:Header>
    <env:Body>
      <rsp:CommandLine>
        <rsp:Command>ipconfig</rsp:Command>
        <rsp:Arguments>/all</rsp:Arguments>
      </rsp:CommandLine>
    </env:Body>
  </env:Envelope>"""
  
  run_cmd_wo_args_request = """\
  <?xml version="1.0" encoding="utf-8"?>
  <env:Envelope xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
    <env:Header>
      <a:To>http://windows-host:5985/wsman</a:To>
      <a:ReplyTo>
        <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
      </a:ReplyTo>
      <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
      <a:MessageID>uuid:11111111-1111-1111-1111-111111111111</a:MessageID>
      <w:Locale mustUnderstand="false" xml:lang="en-US" />
      <p:DataLocale mustUnderstand="false" xml:lang="en-US" />
      <w:OperationTimeout>PT20S</w:OperationTimeout>
      <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
      <a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command</a:Action>
      <w:SelectorSet>
        <w:Selector Name="ShellId">11111111-1111-1111-1111-111111111113</w:Selector>
      </w:SelectorSet>
      <w:OptionSet>
        <w:Option Name="WINRS_CONSOLEMODE_STDIN">TRUE</w:Option>
        <w:Option Name="WINRS_SKIP_CMD_SHELL">FALSE</w:Option>
      </w:OptionSet>
    </env:Header>
    <env:Body>
      <rsp:CommandLine>
        <rsp:Command>hostname</rsp:Command>
      </rsp:CommandLine>
    </env:Body>
  </env:Envelope>"""
  
  run_cmd_response = """\
  <?xml version="1.0" ?>
  <s:Envelope xml:lang="en-US" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer">
      <s:Header>
          <a:Action>http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandResponse</a:Action>
          <a:MessageID>uuid:11111111-1111-1111-1111-111111111112</a:MessageID>
          <a:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:To>
          <a:RelatesTo>uuid:11111111-1111-1111-1111-111111111111</a:RelatesTo>
      </s:Header>
      <s:Body>
          <rsp:CommandResponse>
              <rsp:CommandId>11111111-1111-1111-1111-111111111114</rsp:CommandId>
          </rsp:CommandResponse>
      </s:Body>
  </s:Envelope>"""
  
  cleanup_cmd_request = """\
  <?xml version="1.0" encoding="utf-8"?>
  <env:Envelope xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
    <env:Header>
      <a:To>http://windows-host:5985/wsman</a:To>
      <a:ReplyTo>
        <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
      </a:ReplyTo>
      <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
      <a:MessageID>uuid:11111111-1111-1111-1111-111111111111</a:MessageID>
      <w:Locale mustUnderstand="false" xml:lang="en-US" />
      <p:DataLocale mustUnderstand="false" xml:lang="en-US" />
      <w:OperationTimeout>PT20S</w:OperationTimeout>
      <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
      <a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal</a:Action>
      <w:SelectorSet>
        <w:Selector Name="ShellId">11111111-1111-1111-1111-111111111113</w:Selector>
      </w:SelectorSet>
    </env:Header>
    <env:Body>
      <rsp:Signal CommandId="11111111-1111-1111-1111-111111111114">
        <rsp:Code>http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate</rsp:Code>
      </rsp:Signal>
    </env:Body>
  </env:Envelope>"""
  
  cleanup_cmd_response = """\
  <?xml version="1.0" ?>
  <s:Envelope xml:lang="en-US" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer">
      <s:Header>
          <a:Action>http://schemas.microsoft.com/wbem/wsman/1/windows/shell/SignalResponse</a:Action>
          <a:MessageID>uuid:11111111-1111-1111-1111-111111111112</a:MessageID>
          <a:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:To>
          <a:RelatesTo>uuid:11111111-1111-1111-1111-111111111111</a:RelatesTo>
      </s:Header>
      <s:Body>
          <rsp:SignalResponse/>
      </s:Body>
  </s:Envelope>"""
  
  get_cmd_output_request = """\
  <?xml version="1.0" encoding="utf-8"?>
  <env:Envelope xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing">
    <env:Header>
      <a:To>http://windows-host:5985/wsman</a:To>
      <a:ReplyTo>
        <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
      </a:ReplyTo>
      <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
      <a:MessageID>uuid:11111111-1111-1111-1111-111111111111</a:MessageID>
      <w:Locale mustUnderstand="false" xml:lang="en-US" />
      <p:DataLocale mustUnderstand="false" xml:lang="en-US" />
      <w:OperationTimeout>PT20S</w:OperationTimeout>
      <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
      <a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive</a:Action>
      <w:SelectorSet>
        <w:Selector Name="ShellId">11111111-1111-1111-1111-111111111113</w:Selector>
      </w:SelectorSet>
    </env:Header>
    <env:Body>
      <rsp:Receive>
        <rsp:DesiredStream CommandId="11111111-1111-1111-1111-111111111114">stdout stderr</rsp:DesiredStream>
      </rsp:Receive>
    </env:Body>
  </env:Envelope>"""
  
  get_cmd_output_response = """\
  <?xml version="1.0" ?>
  <s:Envelope xml:lang="en-US" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
      <s:Header>
          <a:Action>http://schemas.microsoft.com/wbem/wsman/1/windows/shell/ReceiveResponse</a:Action>
          <a:MessageID>uuid:11111111-1111-1111-1111-111111111112</a:MessageID>
          <a:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:To>
          <a:RelatesTo>uuid:11111111-1111-1111-1111-111111111111</a:RelatesTo>
      </s:Header>
      <s:Body>
          <rsp:ReceiveResponse>
              <rsp:Stream CommandId="11111111-1111-1111-1111-111111111114" Name="stdout">DQpXaW5kb3dzIElQIENvbmZpZ3VyYXRpb24NCg0K</rsp:Stream>
              <rsp:Stream CommandId="11111111-1111-1111-1111-111111111114" Name="stdout">ICAgSG9zdCBOYW1lIC4gLiAuIC4gLiAuIC4gLiAuIC4gLiAuIDogV0lORE9XUy1IT1NUCiAgIFByaW1hcnkgRG5zIFN1ZmZpeCAgLiAuIC4gLiAuIC4gLiA6IAogICBOb2RlIFR5cGUgLiAuIC4gLiAuIC4gLiAuIC4gLiAuIC4gOiBIeWJyaWQKICAgSVAgUm91dGluZyBFbmFibGVkLiAuIC4gLiAuIC4gLiAuIDogTm8KICAgV0lOUyBQcm94eSBFbmFibGVkLiAuIC4gLiAuIC4gLiAuIDogTm8KCkV0aGVybmV0IGFkYXB0ZXIgTG9jYWwgQXJlYSBDb25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llcwogICBJUHY2IEFkZHJlc3MuIC4gLiAuIC4gLiAuIC4gLiAuIC4gOiAyMDAyOmFkYjk6OTk1ZDo6YWRiOTo5OTVkKFByZWZlcnJlZCkgCiAgIERlZmF1bHQgR2F0ZXdheSAuIC4gLiAuIC4gLiAuIC4gLiA6IDIwMDI6YzA1ODo2MzAxOjpjMDU4OjYzMDEKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMjAwMjpjMDU4OjYzMDE6OjEKICAgRE5TIFNlcnZlcnMgLiAuIC4gLiAuIC4gLiAuIC4gLiAuIDogMjA3LjkxLjUuMzIKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgMjA4LjY3LjIyMi4yMjIKICAgTmV0QklPUyBvdmVyIFRjcGlwLiAuIC4gLiAuIC4gLiAuIDogRGlzYWJsZWQKClR1bm5lbCBhZGFwdGVyIGlzYXRhcC57QkExNjBGQzUtNzAyOC00QjFGLUEwNEItMUFDODAyQjBGRjVBfToKCiAgIE1lZGlhIFN0YXRlIC4gLiAuIC4gLiAuIC4gLiAuIC4gLiA6IE1lZGlhIGRpc2Nvbm5lY3RlZAogICBDb25uZWN0aW9uLXNwZWNpZmljIEROUyBTdWZmaXggIC4gOiAKICAgRGVzY3JpcHRpb24gLiAuIC4gLiAuIC4gLiAuIC4gLiAuIDogTWljcm9zb2Z0IElTQVRBUCBBZGFwdGVyICMyCiAgIFBoeXNpY2FsIEFkZHJlc3MuIC4gLiAuIC4gLiAuIC4gLiA6IDAwLTAwLTAwLTAwLTAwLTAwLTAwLUUwCiAgIERIQ1AgRW5hYmxlZC4gLiAuIC4gLiAuIC4gLiAuIC4gLiA6IE5vCiAgIEF1dG9jb25maWd1cmF0aW9uIEVuYWJsZWQgLiAuIC4gLiA6IFllcwo=</rsp:Stream>
              <rsp:Stream CommandId="11111111-1111-1111-1111-111111111114" End="true" Name="stdout"/>
              <rsp:Stream CommandId="11111111-1111-1111-1111-111111111114" End="true" Name="stderr"/>
              <rsp:CommandState CommandId="11111111-1111-1111-1111-111111111114" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                  <rsp:ExitCode>0</rsp:ExitCode>
              </rsp:CommandState>
          </rsp:ReceiveResponse>
      </s:Body>
  </s:Envelope>"""
  
  run_ps_request = """\
  <?xml version="1.0" encoding="utf-8"?>
  <env:Envelope xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:b="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:cfg="http://schemas.microsoft.com/wbem/wsman/1/config" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <env:Header>
          <w:OptionSet>
              <w:Option Name="WINRS_CONSOLEMODE_STDIN">TRUE</w:Option>
              <w:Option Name="WINRS_SKIP_CMD_SHELL">FALSE</w:Option>
          </w:OptionSet>
          <w:MaxEnvelopeSize mustUnderstand="true">153600</w:MaxEnvelopeSize>
          <a:Action mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command</a:Action>
          <w:Locale mustUnderstand="false" xml:lang="en-US"/>
          <a:MessageID>uuid:11111111-1111-1111-1111-111111111111</a:MessageID>
          <w:OperationTimeout>PT20S</w:OperationTimeout>
          <a:ReplyTo>
              <a:Address mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
          </a:ReplyTo>
          <w:SelectorSet>
              <w:Selector Name="ShellId">11111111-1111-1111-1111-111111111113</w:Selector>
          </w:SelectorSet>
          <w:ResourceURI mustUnderstand="true">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</w:ResourceURI>
          <a:To>http://windows-host:5985/wsman</a:To>
          <p:DataLocale mustUnderstand="false" xml:lang="en-US"/>
      </env:Header>
      <env:Body>
          <rsp:CommandLine>
              <rsp:Command>powershell -command &quot;Invoke-Expression $([System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('CiAgICAgICAgJHN0ckNvbXB1dGVyID0gJEhvc3QKICAgICAgICBDbGVhcgogICAgICAgICRSQU0gPSBXbWlPYmplY3QgV2luMzJfQ29tcHV0ZXJTeXN0ZW0KICAgICAgICAkTUIgPSAxMDQ4NTc2CgogICAgICAgICJJbnN0YWxsZWQgTWVtb3J5OiAiICsgW2ludF0oJFJBTS5Ub3RhbFBoeXNpY2FsTWVtb3J5IC8kTUIpICsgIiBNQiIKICAgIA==')))&quot;</rsp:Command>
          </rsp:CommandLine>
      </env:Body>
  </env:Envelope>
  """
  
  run_ps_response = """\
  <s:Envelope xml:lang="en-US" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer">
      <s:Header>
          <a:Action>http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandResponse</a:Action>
          <a:MessageID>uuid:11111111-1111-1111-1111-111111111112</a:MessageID>
          <a:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:To>
          <a:RelatesTo>uuid:11111111-1111-1111-1111-111111111111</a:RelatesTo>
      </s:Header>
      <s:Body>
          <rsp:CommandResponse>
              <rsp:CommandId>21111111-1111-1111-1111-111111111114</rsp:CommandId>
          </rsp:CommandResponse>
      </s:Body>
  </s:Envelope>
  """
  
  get_ps_output_request = get_cmd_output_request.replace('CommandId="1', 'CommandId="2')
  get_ps_output_response = """\
  <s:Envelope xml:lang="en-US" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd" xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
      <s:Header>
          <a:Action>http://schemas.microsoft.com/wbem/wsman/1/windows/shell/ReceiveResponse</a:Action>
          <a:MessageID>uuid:11111111-1111-1111-1111-111111111112</a:MessageID>
          <a:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:To>
          <a:RelatesTo>uuid:11111111-1111-1111-1111-111111111111</a:RelatesTo>
      </s:Header>
      <s:Body>
          <rsp:ReceiveResponse>
              <rsp:Stream CommandId="21111111-1111-1111-1111-111111111114" Name="stdout">SW5zdGFsbGVkIE1lbW9yeTogMjA0NCBNQg==</rsp:Stream>
              <rsp:Stream CommandId="21111111-1111-1111-1111-111111111114" Name="stdout">DQo=</rsp:Stream>
              <rsp:Stream CommandId="21111111-1111-1111-1111-111111111114" End="true" Name="stdout"/>
              <rsp:Stream CommandId="21111111-1111-1111-1111-111111111114" End="true" Name="stderr"/>
              <rsp:CommandState CommandId="21111111-1111-1111-1111-111111111114" State="http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done">
                  <rsp:ExitCode>0</rsp:ExitCode>
              </rsp:CommandState>
          </rsp:ReceiveResponse>
      </s:Body>
  </s:Envelope>
  """
  
  ps_cleanup_cmd_request = cleanup_cmd_request.replace('CommandId="1', 'CommandId="2')
  ps_cleanup_cmd_response = cleanup_cmd_response
  
  
  def sort_dict(ordered_dict):
      items = sorted(ordered_dict.items(), key=lambda x: x[0])
      ordered_dict.clear()
      for key, value in items:
          if isinstance(value, dict):
              sort_dict(value)
          ordered_dict[key] = value
  
  
  def xml_str_compare(first, second):
      first_dict = xmltodict.parse(first)
      second_dict = xmltodict.parse(second)
      sort_dict(first_dict)
      sort_dict(second_dict)
      return first_dict == second_dict
  
  
  class TransportStub(object):
      def send_message(self, message):
          if xml_str_compare(message, open_shell_request):
              return open_shell_response
          elif xml_str_compare(message, close_shell_request):
              return close_shell_response
          elif xml_str_compare(
                  message, run_cmd_with_args_request) or xml_str_compare(
                  message, run_cmd_wo_args_request):
              return run_cmd_response
          elif xml_str_compare(message, cleanup_cmd_request):
              return cleanup_cmd_response
          elif xml_str_compare(message, get_cmd_output_request):
              return get_cmd_output_response
          elif xml_str_compare(message, run_ps_request):
              return run_ps_response
          elif xml_str_compare(message, get_ps_output_request):
              return get_ps_output_response
          elif xml_str_compare(message, ps_cleanup_cmd_request):
              return ps_cleanup_cmd_response
          else:
              raise Exception('Message was not expected')
  
  
  @fixture(scope='module')
  def protocol_fake(request):
      uuid4_patcher = patch('uuid.uuid4')
      uuid4_mock = uuid4_patcher.start()
      uuid4_mock.return_value = uuid.UUID(
          '11111111-1111-1111-1111-111111111111')
  
      from winrm.protocol import Protocol
  
      protocol_fake = Protocol(
          endpoint='http://windows-host:5985/wsman',
          transport='plaintext',
          username='john.smith',
          password='secret')
  
      protocol_fake.transport = TransportStub()
  
      def uuid4_patch_stop():
          uuid4_patcher.stop()
  
      request.addfinalizer(uuid4_patch_stop)
      return protocol_fake
  
  
  @fixture(scope='module')
  def protocol_real():
      endpoint = os.environ.get('WINRM_ENDPOINT', None)
      transport = os.environ.get('WINRM_TRANSPORT', None)
      username = os.environ.get('WINRM_USERNAME', None)
      password = os.environ.get('WINRM_PASSWORD', None)
      if endpoint:
          settings = dict(
              endpoint=endpoint,
              operation_timeout_sec=5,
              read_timeout_sec=7
          )
          if transport:
              settings['transport'] = transport
          if username:
              settings['username'] = username
          if password:
              settings['password'] = password
  
          from winrm.protocol import Protocol
          protocol = Protocol(**settings)
          return protocol
      else:
          skip('WINRM_ENDPOINT environment variable was not set. Integration tests will be skipped')
  