from winrm import Session def test_run_cmd(protocol_fake): # TODO this test should cover __init__ method s = Session('windows-host', auth=('john.smith', 'secret')) s.protocol = protocol_fake r = s.run_cmd('ipconfig', ['/all']) assert r.status_code == 0 assert b'Windows IP Configuration' in r.std_out assert len(r.std_err) == 0 def test_run_ps_with_error(protocol_fake): # TODO this test should cover __init__ method s = Session('windows-host', auth=('john.smith', 'secret')) s.protocol = protocol_fake r = s.run_ps('Write-Error "Error"') assert r.status_code == 1 assert b'Write-Error "Error"' in r.std_err assert len(r.std_out) == 0 def test_target_as_hostname(): s = Session('windows-host', auth=('john.smith', 'secret')) assert s.url == 'http://windows-host:5985/wsman' def test_target_as_hostname_then_port(): s = Session('windows-host:1111', auth=('john.smith', 'secret')) assert s.url == 'http://windows-host:1111/wsman' def test_target_as_schema_then_hostname(): s = Session('http://windows-host', auth=('john.smith', 'secret')) assert s.url == 'http://windows-host:5985/wsman' def test_target_as_schema_then_hostname_then_port(): s = Session('http://windows-host:1111', auth=('john.smith', 'secret')) assert s.url == 'http://windows-host:1111/wsman' def test_target_as_full_url(): s = Session('http://windows-host:1111/wsman', auth=( 'john.smith', 'secret')) assert s.url == 'http://windows-host:1111/wsman' def test_target_with_dots(): s = Session('windows-host.example.com', auth=('john.smith', 'secret')) assert s.url == 'http://windows-host.example.com:5985/wsman' def test_decode_clixml_error(): s = Session('windows-host.example.com', auth=('john.smith', 'secret')) msg = b'#< CLIXML\r\nSystem.Management.Automation.PSCustomObjectSystem.Object1Preparing modules for first use.0-1-1Completed-1 1Preparing modules for first use.0-1-1Completed-1 fake : The term \'fake\' is not recognized as the name of a cmdlet, function, script file, or operable program. Check _x000D__x000A_the spelling of the name, or if a path was included, verify that the path is correct and try again._x000D__x000A_At line:1 char:1_x000D__x000A_+ fake cmdlet_x000D__x000A_+ ~~~~_x000D__x000A_ + CategoryInfo : ObjectNotFound: (fake:String) [], CommandNotFoundException_x000D__x000A_ + FullyQualifiedErrorId : CommandNotFoundException_x000D__x000A_ _x000D__x000A_' expected = b"fake : The term 'fake' is not recognized as the name of a cmdlet, function, script file, or operable program. Check \nthe spelling of the name, or if a path was included, verify that the path is correct and try again.\nAt line:1 char:1\n+ fake cmdlet\n+ ~~~~\n + CategoryInfo : ObjectNotFound: (fake:String) [], CommandNotFoundException\n + FullyQualifiedErrorId : CommandNotFoundException" actual = s._clean_error_msg(msg) assert actual == expected def test_decode_clixml_no_clixml(): s = Session('windows-host.example.com', auth=('john.smith', 'secret')) msg = b"stderr line" expected = b"stderr line" actual = s._clean_error_msg(msg) assert actual == expected def test_decode_clixml_no_errors(): s = Session('windows-host.example.com', auth=('john.smith', 'secret')) msg = b'#< CLIXML\r\nSystem.Management.Automation.PSCustomObjectSystem.Object1Preparing modules for first use.0-1-1Completed-1 1Preparing modules for first use.0-1-1Completed-1 ' expected = msg actual = s._clean_error_msg(msg) assert actual == expected