def test_run_shell_command(self): inventory = make_inventory(hosts=("@chroot/not-a-chroot", )) State(inventory, Config()) host = inventory.get_host("@chroot/not-a-chroot") host.connect() command = "echo hoi" self.fake_popen_mock().returncode = 0 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 = "chroot /not-a-chroot 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_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( "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\" " # shell and export bit "&& echo hi'\"'\"''" # command bit )
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_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_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_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_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_su_command(self): command = make_unix_command("uptime", su_user="******") assert command.get_raw_value() == "su 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_custom_shell_command(self): command = make_unix_command("uptime", shell_executable="bash") assert command.get_raw_value() == "bash -c uptime"
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_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_sudo_command(self): command = make_unix_command("uptime", sudo=True) assert command.get_raw_value() == "sudo -H -n sh -c uptime"
def test_doas_user_command(self): command = make_unix_command("uptime", doas=True, doas_user="******") assert command.get_raw_value() == "doas -n -u pyinfra sh -c uptime"
def test_doas_command(self): command = make_unix_command("uptime", doas=True) assert command.get_raw_value() == "doas -n sh -c uptime"
def test_command(self): command = make_unix_command("echo Šablony") assert command.get_raw_value() == "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_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"