Exemple #1
0
    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,
        )
Exemple #2
0
    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,
        )
Exemple #3
0
    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,
        )
Exemple #4
0
 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'",
     ]
Exemple #5
0
 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'",
     ]
Exemple #6
0
 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
     )
Exemple #7
0
 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
     )
Exemple #8
0
 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
         ),
     )
Exemple #9
0
 def test_sudo_command(self):
     command = make_unix_command('uptime', sudo=True)
     assert command.get_raw_value() == 'sudo -H -n sh -c uptime'
Exemple #10
0
 def test_command(self):
     command = make_unix_command('echo Šablony')
     assert command.get_raw_value() == "sh -c 'echo Šablony'"
Exemple #11
0
 def test_custom_shell_command(self):
     command = make_unix_command('uptime', shell_executable='bash')
     assert command.get_raw_value() == 'bash -c uptime'
Exemple #12
0
 def test_su_command(self):
     command = make_unix_command('uptime', su_user='******')
     assert command.get_raw_value() == "su pyinfra -c 'sh -c uptime'"
Exemple #13
0
 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'"
Exemple #14
0
 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'"
Exemple #15
0
 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'
Exemple #16
0
 def test_su_command(self):
     command = make_unix_command('uptime', su_user='******')
     self.assertEqual(command, 'su pyinfra -s `which sh` -c uptime')
Exemple #17
0
 def test_su_command(self):
     command = make_unix_command('uptime', su_user='******')
     assert command.get_raw_value() == 'su pyinfra -s `which sh` -c uptime'
Exemple #18
0
 def test_command_env(self):
     command = make_unix_command('uptime', env={'key': 'value'})
     self.assertEqual(command, "sh -c 'export key=value; uptime'")
Exemple #19
0
 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'
Exemple #20
0
 def test_sudo_command(self):
     command = make_unix_command('uptime', sudo=True)
     assert command == 'sudo -S -H -n sh -c uptime'
Exemple #21
0
 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
Exemple #23
0
 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'
Exemple #24
0
 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'"
Exemple #25
0
 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'
Exemple #26
0
 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')
Exemple #27
0
 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'\"'\"''"
Exemple #28
0
 def test_command_chdir(self):
     command = make_unix_command('uptime', chdir='/opt/somedir')
     assert command.get_raw_value() == "sh -c 'cd /opt/somedir && uptime'"
Exemple #29
0
 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'"
Exemple #30
0
 def test_custom_shell_command(self):
     command = make_unix_command('uptime', shell_executable='bash')
     self.assertEqual(command, 'bash -c uptime')