def test_run_shell_command(self): inventory = make_inventory(hosts=('@chroot/not-a-chroot', )) state = State(inventory, Config()) host = inventory.get_host('@chroot/not-a-chroot') host.connect(state) command = 'echo hoi' self.fake_popen_mock().returncode = 0 out = host.run_shell_command( state, command, stdin='hello', get_pty=True, print_output=True, ) assert len(out) == 3 assert out[0] is True command, _ = make_unix_command(command) command = shlex_quote(command) docker_command = 'chroot /not-a-chroot sh -c {0}'.format(command) shell_command, _ = make_unix_command(docker_command) self.fake_popen_mock.assert_called_with( shell_command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE, )
def test_run_shell_command(self): inventory = make_inventory(hosts=('@docker/not-an-image', )) State(inventory, Config()) command = 'echo hi' self.fake_popen_mock().returncode = 0 host = inventory.get_host('@docker/not-an-image') host.connect() out = host.run_shell_command( command, stdin='hello', get_pty=True, print_output=True, ) assert len(out) == 3 assert out[0] is True command = make_unix_command(command).get_raw_value() command = shlex_quote(command) docker_command = 'docker exec -it containerid sh -c {0}'.format( command) shell_command = make_unix_command(docker_command).get_raw_value() self.fake_popen_mock.assert_called_with( shell_command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE, )
def test_run_shell_command(self): inventory = make_inventory(hosts=('@local', )) state = State(inventory, Config()) host = inventory.get_host('@local') command = 'echo Šablony' self.fake_popen_mock().returncode = 0 out = host.run_shell_command(state, command, stdin='hello', print_output=True) assert len(out) == 3 status, stdout, stderr = out assert status is True self.fake_popen_mock().stdin.write.assert_called_with(b'hello\n') combined_out = host.run_shell_command( state, command, stdin='hello', print_output=True, return_combined_output=True, ) assert len(combined_out) == 2 shell_command = make_unix_command(command) self.fake_popen_mock.assert_called_with( shell_command, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE, )
def test_command_env(self): command, _ = make_unix_command('uptime', env={ 'key': 'value', 'anotherkey': 'anothervalue', }) assert command in [ "sh -c 'env key=value anotherkey=anothervalue uptime'", "sh -c 'env anotherkey=anothervalue key=value uptime'", ]
def test_command_env(self): command = make_unix_command('uptime', env={ 'key': 'value', 'anotherkey': 'anothervalue', }) assert command.get_raw_value() in [ "sh -c 'export key=value anotherkey=anothervalue && uptime'", "sh -c 'export anotherkey=anothervalue key=value && uptime'", ]
def test_mixed_command(self): command = make_unix_command( 'uptime', env={'key': 'value'}, sudo=True, sudo_user='******', preserve_sudo_env=True, su_user='******', shell_executable='bash', ) assert command.get_raw_value() == ( 'sudo -H -n -E -u root ' # sudo bit 'su pyinfra -s `which bash` -c ' # su bit "'env key=value uptime'" # command bit )
def test_mixed_command(self): command = make_unix_command( 'echo hi', chdir='/opt/somedir', env={'key': 'value'}, sudo=True, sudo_user='******', preserve_sudo_env=True, su_user='******', shell_executable='bash', ) assert command.get_raw_value() == ( 'sudo -H -n -E -u root ' # sudo bit 'su pyinfra -c ' # su bit "'bash -c '\"'\"'cd /opt/somedir && export key=value && echo hi'\"'\"''" # command bit )
def test_mixed_command(self): command = make_unix_command( 'uptime', env={'key': 'value'}, sudo=True, sudo_user='******', preserve_sudo_env=True, su_user='******', shell_executable='bash', ) self.assertEqual( command, ( 'sudo -S -H -n -E -u root ' # sudo bit 'su pyinfra -s `which bash` -c ' # su bit "'export key=value; uptime'" # command bit ), )
def test_sudo_command(self): command = make_unix_command('uptime', sudo=True) assert command.get_raw_value() == 'sudo -H -n sh -c uptime'
def test_command(self): command = make_unix_command('echo Šablony') assert command.get_raw_value() == "sh -c 'echo Šablony'"
def test_custom_shell_command(self): command = make_unix_command('uptime', shell_executable='bash') assert command.get_raw_value() == 'bash -c uptime'
def test_su_command(self): command = make_unix_command('uptime', su_user='******') assert command.get_raw_value() == "su pyinfra -c 'sh -c uptime'"
def test_use_su_login_command(self): command = make_unix_command('uptime', su_user='******', use_su_login=True) assert command.get_raw_value() == "su -l pyinfra -c 'sh -c uptime'"
def test_sudo_multi_arg_command(self): command = make_unix_command('echo hi', sudo=True, preserve_sudo_env=True) assert command.get_raw_value() == "sudo -H -n -E sh -c 'echo hi'"
def test_use_sudo_login_command(self): command = make_unix_command('uptime', sudo=True, use_sudo_login=True) assert command.get_raw_value() == 'sudo -H -n -i sh -c uptime'
def test_su_command(self): command = make_unix_command('uptime', su_user='******') self.assertEqual(command, 'su pyinfra -s `which sh` -c uptime')
def test_su_command(self): command = make_unix_command('uptime', su_user='******') assert command.get_raw_value() == 'su pyinfra -s `which sh` -c uptime'
def test_command_env(self): command = make_unix_command('uptime', env={'key': 'value'}) self.assertEqual(command, "sh -c 'export key=value; uptime'")
def test_use_su_login_command(self): command = make_unix_command('uptime', su_user='******', use_su_login=True) assert command == 'su -l pyinfra -s `which sh` -c uptime'
def test_sudo_command(self): command = make_unix_command('uptime', sudo=True) assert command == 'sudo -S -H -n sh -c uptime'
def test_command(self): command = make_unix_command('echo Šablony') assert command == "sh -c 'echo Šablony'"
def get_docker_command(command): shell_command = make_unix_command(command).get_raw_value() shell_command = shlex_quote(shell_command) docker_command = 'docker exec -it containerid sh -c {0}'.format( shell_command) return docker_command
def test_sudo_preserve_env_command(self): command = make_unix_command('uptime', sudo=True, preserve_sudo_env=True) assert command.get_raw_value() == 'sudo -H -n -E sh -c uptime'
def test_preserve_su_env_command(self): command = make_unix_command('uptime', su_user='******', preserve_su_env=True) assert command.get_raw_value() == "su -m pyinfra -c 'sh -c uptime'"
def test_sudo_user_command(self): command = make_unix_command('uptime', sudo=True, sudo_user='******') assert command.get_raw_value() == 'sudo -H -n -u pyinfra sh -c uptime'
def test_sudo_user_command(self): command = make_unix_command('uptime', sudo=True, sudo_user='******') self.assertEqual(command, 'sudo -S -H -n -u pyinfra sh -c uptime')
def test_su_multi_arg_command(self): command = make_unix_command('echo hi', su_user='******') assert command.get_raw_value( ) == "su pyinfra -c 'sh -c '\"'\"'echo hi'\"'\"''"
def test_command_chdir(self): command = make_unix_command('uptime', chdir='/opt/somedir') assert command.get_raw_value() == "sh -c 'cd /opt/somedir && uptime'"
def test_su_shell_command(self): command = make_unix_command('uptime', su_user='******', su_shell='bash') assert command.get_raw_value( ) == "su -s `which bash` pyinfra -c 'sh -c uptime'"
def test_custom_shell_command(self): command = make_unix_command('uptime', shell_executable='bash') self.assertEqual(command, 'bash -c uptime')