def test_status_bad(self): self.m_stdout_buf.channel.recv_exit_status.return_value = 42 with raises(CommandFailedError) as exc: run.run( client=self.m_ssh, args=['foo'], ) assert str(exc.value) == "Command failed on name with status 42: 'foo'"
def test_status_crash(self): self.m_stdout_buf.channel.recv_exit_status.return_value = -1 with raises(CommandCrashedError) as exc: run.run( client=self.m_ssh, args=['foo'], ) assert str(exc.value) == "Command crashed: 'foo'"
def test_run_cwd(self): self.m_stdout_buf.channel.recv_exit_status.return_value = 0 run.run( client=self.m_ssh, args=['foo_bar_baz'], cwd='/cwd/test', ) self.m_ssh.exec_command.assert_called_with('(cd /cwd/test && exec foo_bar_baz)')
def test_status_lost_socket(self): m_transport = MagicMock() m_transport.getpeername.side_effect = socket.error self.m_ssh.get_transport.return_value = m_transport with raises(ConnectionLostError) as exc: run.run( client=self.m_ssh, args=['foo'], ) assert str(exc.value) == "SSH connection was lost: 'foo'"
def test_status_lost(self): m_transport = MagicMock() m_transport.getpeername.return_value = ('name', 22) m_transport.is_active.return_value = False self.m_stdout_buf.channel.recv_exit_status.return_value = -1 self.m_ssh.get_transport.return_value = m_transport with raises(ConnectionLostError) as exc: run.run( client=self.m_ssh, args=['foo'], ) assert str(exc.value) == "SSH connection to name was lost: 'foo'"
def test_exitstatus(self): self.m_stdout_buf.channel.recv_exit_status.return_value = 0 proc = run.run( client=self.m_ssh, args=['foo', 'bar baz'], ) assert proc.exitstatus == 0
def test_status_crash_nocheck(self): self.m_stdout_buf.channel.recv_exit_status.return_value = -1 proc = run.run( client=self.m_ssh, args=['foo'], check_status=False, ) assert proc.exitstatus == -1
def test_and(self): ssh = connection.connect(HOST) r = run.run( client=ssh, args=['true', run.Raw('&&'), 'echo', 'yup'], stdout=StringIO(), ) assert r.stdout.getvalue() == 'yup\n'
def test_and(self): ssh = connection.connect(HOST) r = run.run( client=ssh, args=['true', run.Raw('&&'), 'echo', 'yup'], stdout=StringIO(), ) assert r.stdout.getvalue() == 'yup\n'
def test_stdin_pipe(self): self.m_stdout_buf.channel.recv_exit_status.return_value = 0 proc = run.run(client=self.m_ssh, args=['foo'], stdin=run.PIPE, wait=False) assert proc.poll() == 0 code = proc.wait() assert code == 0 assert proc.exitstatus == 0
def test_status_bad_nowait(self): self.m_stdout_buf.channel.recv_exit_status.return_value = 42 proc = run.run( client=self.m_ssh, args=['foo'], wait=False, ) with raises(CommandFailedError) as exc: proc.wait() assert proc.returncode == 42 assert str(exc.value) == "Command failed on name with status 42: 'foo'"
def test_pipe(self): ssh = connection.connect(HOST) r = run.run(client=ssh, args=["cat"], stdin=run.PIPE, stdout=StringIO(), wait=False) assert r.stdout.getvalue() == "" r.stdin.write("foo\n") r.stdin.write("bar\n") r.stdin.close() r.wait() got = r.exitstatus assert got == 0 assert r.stdout.getvalue() == "foo\nbar\n"
def test_status_lost_nocheck(self): m_transport = MagicMock() m_transport.getpeername.return_value = ('name', 22) m_transport.is_active.return_value = False self.m_stdout_buf.channel.recv_exit_status.return_value = -1 self.m_ssh.get_transport.return_value = m_transport proc = run.run( client=self.m_ssh, args=['foo'], check_status=False, ) assert proc.exitstatus == -1
def test_capture_stderr_newline(self): output = 'foo\nbar\n' set_buffer_contents(self.m_stderr_buf, output) self.m_stderr_buf.channel.recv_exit_status.return_value = 0 stderr = StringIO() proc = run.run( client=self.m_ssh, args=['foo', 'bar baz'], stderr=stderr, ) assert proc.stderr is stderr assert proc.stderr.read() == output assert proc.stderr.getvalue() == output
def test_capture_stdout(self): output = 'foo\nbar' set_buffer_contents(self.m_stdout_buf, output) self.m_stdout_buf.channel.recv_exit_status.return_value = 0 stdout = BytesIO() proc = run.run( client=self.m_ssh, args=['foo', 'bar baz'], stdout=stdout, ) assert proc.stdout is stdout assert ensure_str(proc.stdout.read()) == output assert ensure_str(proc.stdout.getvalue()) == output
def test_stderr_pipe(self): self.m_stdout_buf.channel.recv_exit_status.return_value = 0 lines = [b'one\n', b'two', b''] set_buffer_contents(self.m_stderr_buf, lines) proc = run.run(client=self.m_ssh, args=['foo'], stderr=run.PIPE, wait=False) assert proc.poll() == 0 assert proc.stderr.readline() == lines[0] assert proc.stderr.readline() == lines[1] assert proc.stderr.readline() == lines[2] code = proc.wait() assert code == 0 assert proc.exitstatus == 0
def test_pipe(self): ssh = connection.connect(HOST) r = run.run( client=ssh, args=['cat'], stdin=run.PIPE, stdout=StringIO(), wait=False, ) assert r.stdout.getvalue() == '' r.stdin.write('foo\n') r.stdin.write('bar\n') r.stdin.close() r.wait() got = r.exitstatus assert got == 0 assert r.stdout.getvalue() == 'foo\nbar\n'
def test_and(self): ssh = connection.connect(HOST) r = run.run(client=ssh, args=["true", run.Raw("&&"), "echo", "yup"], stdout=StringIO()) assert r.stdout.getvalue() == "yup\n"