def test_winrm_run_cmd(self): mock_protocol = mock.MagicMock() mock_protocol.open_shell.return_value = 123 mock_protocol.run_command.return_value = 456 mock_protocol._raw_get_command_output.return_value = (b'output', b'error', 9, True) mock_session = mock.MagicMock(protocol=mock_protocol) self._init_runner() result = self._runner._winrm_run_cmd(mock_session, "fake-command", args=['arg1', 'arg2'], env={'PATH': 'C:\\st2\\bin'}, cwd='C:\\st2') expected_response = Response((b'output', b'error', 9)) expected_response.timeout = False self.assertEquals(result.__dict__, expected_response.__dict__) mock_protocol.open_shell.assert_called_with( env_vars={'PATH': 'C:\\st2\\bin'}, working_directory='C:\\st2') mock_protocol.run_command.assert_called_with(123, 'fake-command', ['arg1', 'arg2']) mock_protocol._raw_get_command_output.assert_called_with(123, 456) mock_protocol.cleanup_command.assert_called_with(123, 456) mock_protocol.close_shell.assert_called_with(123)
def test_winrm_run_cmd_timeout(self, mock_get_command_output): mock_protocol = mock.MagicMock() mock_protocol.open_shell.return_value = 123 mock_protocol.run_command.return_value = 456 mock_session = mock.MagicMock(protocol=mock_protocol) mock_get_command_output.side_effect = WinRmRunnerTimoutError( Response(("", "", 5))) self._init_runner() result = self._runner._winrm_run_cmd( mock_session, "fake-command", args=["arg1", "arg2"], env={"PATH": "C:\\st2\\bin"}, cwd="C:\\st2", ) expected_response = Response(("", "", 5)) expected_response.timeout = True self.assertEqual(result.__dict__, expected_response.__dict__) mock_protocol.open_shell.assert_called_with( env_vars={"PATH": "C:\\st2\\bin"}, working_directory="C:\\st2") mock_protocol.run_command.assert_called_with(123, "fake-command", ["arg1", "arg2"]) mock_protocol.cleanup_command.assert_called_with(123, 456) mock_protocol.close_shell.assert_called_with(123)
def test_winrm_run_cmd(self): mock_protocol = mock.MagicMock() mock_protocol.open_shell.return_value = 123 mock_protocol.run_command.return_value = 456 mock_protocol._raw_get_command_output.return_value = ( b"output", b"error", 9, True, ) mock_session = mock.MagicMock(protocol=mock_protocol) self._init_runner() result = self._runner._winrm_run_cmd( mock_session, "fake-command", args=["arg1", "arg2"], env={"PATH": "C:\\st2\\bin"}, cwd="C:\\st2", ) expected_response = Response((b"output", b"error", 9)) expected_response.timeout = False self.assertEqual(result.__dict__, expected_response.__dict__) mock_protocol.open_shell.assert_called_with( env_vars={"PATH": "C:\\st2\\bin"}, working_directory="C:\\st2") mock_protocol.run_command.assert_called_with(123, "fake-command", ["arg1", "arg2"]) mock_protocol._raw_get_command_output.assert_called_with(123, 456) mock_protocol.cleanup_command.assert_called_with(123, 456) mock_protocol.close_shell.assert_called_with(123)
def _winrm_run_cmd(self, session, command, args=(), env=None, cwd=None): # NOTE: this is copied from pywinrm because it doesn't support # passing env and working_directory from the Session.run_cmd. # It also doesn't support timeouts. All of these things have been # added shell_id = session.protocol.open_shell(env_vars=env, working_directory=cwd) command_id = session.protocol.run_command(shell_id, command, args) # try/catch is for custom timeout handing (StackStorm custom) try: rs = Response( self._winrm_get_command_output(session.protocol, shell_id, command_id)) rs.timeout = False except WinRmRunnerTimoutError as e: rs = e.response rs.timeout = True # end stackstorm custom session.protocol.cleanup_command(shell_id, command_id) session.protocol.close_shell(shell_id) return rs
def test_translate_response_timeout(self): response = Response(('output1', 'error1', 123)) response.timeout = True result = self._runner._translate_response(response) self.assertEquals(result, ('timeout', { 'failed': True, 'succeeded': False, 'return_code': -1, 'stdout': 'output1', 'stderr': 'error1' }, None))
def _winrm_run_cmd(self, session, command, args=(), env=None, cwd=None): # NOTE: this is copied from pywinrm because it doesn't support # passing env and working_directory from the Session.run_cmd. # It also doesn't support timeouts. All of these things have been # added shell_id = session.protocol.open_shell(env_vars=env, working_directory=cwd) command_id = session.protocol.run_command(shell_id, command, args) # try/catch is for custom timeout handing (StackStorm custom) try: rs = Response(self._winrm_get_command_output(session.protocol, shell_id, command_id)) rs.timeout = False except WinRmRunnerTimoutError as e: rs = e.response rs.timeout = True # end stackstorm custom session.protocol.cleanup_command(shell_id, command_id) session.protocol.close_shell(shell_id) return rs
def test_translate_response_timeout(self): response = Response(('output1', 'error1', 123)) response.timeout = True result = self._runner._translate_response(response) self.assertEquals(result, ('timeout', {'failed': True, 'succeeded': False, 'return_code': -1, 'stdout': 'output1', 'stderr': 'error1'}, None))
def test_translate_response_timeout(self): response = Response(("output1", "error1", 123)) response.timeout = True result = self._runner._translate_response(response) self.assertEqual( result, ( "timeout", { "failed": True, "succeeded": False, "return_code": -1, "stdout": "output1", "stderr": "error1", }, None, ), )
def test_winrm_run_cmd_timeout(self, mock_get_command_output): mock_protocol = mock.MagicMock() mock_protocol.open_shell.return_value = 123 mock_protocol.run_command.return_value = 456 mock_session = mock.MagicMock(protocol=mock_protocol) mock_get_command_output.side_effect = WinRmRunnerTimoutError(Response(('', '', 5))) self._init_runner() result = self._runner._winrm_run_cmd(mock_session, "fake-command", args=['arg1', 'arg2'], env={'PATH': 'C:\\st2\\bin'}, cwd='C:\\st2') expected_response = Response(('', '', 5)) expected_response.timeout = True self.assertEquals(result.__dict__, expected_response.__dict__) mock_protocol.open_shell.assert_called_with(env_vars={'PATH': 'C:\\st2\\bin'}, working_directory='C:\\st2') mock_protocol.run_command.assert_called_with(123, 'fake-command', ['arg1', 'arg2']) mock_protocol.cleanup_command.assert_called_with(123, 456) mock_protocol.close_shell.assert_called_with(123)
def test_winrm_run_cmd(self): mock_protocol = mock.MagicMock() mock_protocol.open_shell.return_value = 123 mock_protocol.run_command.return_value = 456 mock_protocol._raw_get_command_output.return_value = (b'output', b'error', 9, True) mock_session = mock.MagicMock(protocol=mock_protocol) self._init_runner() result = self._runner._winrm_run_cmd(mock_session, "fake-command", args=['arg1', 'arg2'], env={'PATH': 'C:\\st2\\bin'}, cwd='C:\\st2') expected_response = Response((b'output', b'error', 9)) expected_response.timeout = False self.assertEquals(result.__dict__, expected_response.__dict__) mock_protocol.open_shell.assert_called_with(env_vars={'PATH': 'C:\\st2\\bin'}, working_directory='C:\\st2') mock_protocol.run_command.assert_called_with(123, 'fake-command', ['arg1', 'arg2']) mock_protocol._raw_get_command_output.assert_called_with(123, 456) mock_protocol.cleanup_command.assert_called_with(123, 456) mock_protocol.close_shell.assert_called_with(123)