Skip to content

Commit 0bf651c

Browse files
committed
Add SSH connector tests for using sudo password w/prompt.
1 parent be6b97c commit 0bf651c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/test_connectors/test_ssh.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ def test_connect_exceptions(self, fake_key_open):
8888

8989
assert len(state.active_hosts) == 0
9090

91+
# SSH key tests
92+
#
93+
9194
def test_connect_with_rsa_ssh_key(self):
9295
state = State(make_inventory(hosts=(
9396
('somehost', {'ssh_key': 'testkey'}),
@@ -356,6 +359,9 @@ def test_connect_with_missing_ssh_key(self):
356359

357360
self.assertTrue(e.exception.args[0].startswith('No such private key file:'))
358361

362+
# SSH command tests
363+
#
364+
359365
@patch('pyinfra.api.connectors.ssh.SSHClient')
360366
def test_run_shell_command(self, fake_ssh_client):
361367
fake_ssh = MagicMock()
@@ -463,6 +469,51 @@ def test_run_shell_command_error(self, fake_ssh_client):
463469
assert len(out) == 3
464470
assert out[0] is False
465471

472+
@patch('pyinfra.api.connectors.util.getpass')
473+
@patch('pyinfra.api.connectors.util.get_sudo_askpass_exe')
474+
@patch('pyinfra.api.connectors.ssh.SSHClient')
475+
@patch('pyinfra.api.connectors.ssh.SFTPClient')
476+
def test_run_shell_command_sudo_password_prompt(
477+
self,
478+
fake_sftp_client,
479+
fake_ssh_client,
480+
fake_get_sudo_askpass_exe,
481+
fake_getpass,
482+
):
483+
fake_ssh = MagicMock()
484+
fake_stdin = MagicMock()
485+
fake_stdout = MagicMock()
486+
fake_ssh.exec_command.return_value = fake_stdin, fake_stdout, MagicMock()
487+
488+
fake_ssh_client.return_value = fake_ssh
489+
fake_getpass.return_value = 'password'
490+
491+
inventory = make_inventory(hosts=('somehost',))
492+
State(inventory, Config())
493+
host = inventory.get_host('somehost')
494+
host.connect()
495+
496+
command = 'echo Šablony'
497+
fake_stdout.channel.recv_exit_status.return_value = 0
498+
499+
out = host.run_shell_command(command, use_sudo_password=True, print_output=True)
500+
assert len(out) == 3
501+
502+
status, stdout, stderr = out
503+
assert status is True
504+
505+
fake_sftp_client.from_transport().putfo.assert_called_with(
506+
fake_get_sudo_askpass_exe.return_value, 'pyinfra-sudo-askpass',
507+
)
508+
509+
fake_ssh.exec_command.assert_called_with((
510+
'env SUDO_ASKPASS=pyinfra-sudo-askpass PYINFRA_SUDO_PASSWORD=password '
511+
"sh -c 'echo Šablony'"
512+
), get_pty=False)
513+
514+
# SSH file put/get tests
515+
#
516+
466517
@patch('pyinfra.api.connectors.ssh.SSHClient')
467518
@patch('pyinfra.api.connectors.ssh.SFTPClient')
468519
def test_put_file(self, fake_sftp_client, fake_ssh_client):

0 commit comments

Comments
 (0)