Ejemplo n.º 1
0
    def test_custom_user(
        self,
        dcos_node: Node,
    ) -> None:
        """
        Commands can be run as a custom user.
        """
        testuser = str(uuid.uuid4().hex)
        dcos_node.run(args=['useradd', testuser])
        dcos_node.run(
            args=['cp', '-R', '$HOME/.ssh', '/home/{}/'.format(testuser)],
            shell=True,
        )

        echo_result = dcos_node.popen(
            args=['echo', '$HOME'],
            user=testuser,
            shell=True,
        )
        stdout, stderr = echo_result.communicate()
        assert echo_result.returncode == 0
        assert stdout.strip().decode() == '/home/' + testuser
        assert stderr.strip().decode() == ''

        dcos_node.run(args=['userdel', '-r', testuser])
Ejemplo n.º 2
0
def _dump_stdout_to_file(node: Node, cmd: List[str], file_path: Path) -> None:
    """
    Dump ``stdout`` of the given command to ``file_path``.

    Raises:
        CalledProcessError: If an error occurs when running the given command.
    """
    chunk_size = 2048
    proc = node.popen(args=cmd)
    with open(str(file_path), 'wb') as dumpfile:
        while True:
            chunk = proc.stdout.read(chunk_size)
            if chunk:
                dumpfile.write(chunk)
            else:
                break
    proc.wait()
    if proc.returncode != 0:
        exception = CalledProcessError(
            returncode=proc.returncode,
            cmd=cmd,
            output=bytes(proc.stdout),
            stderr=bytes(proc.stderr),
        )
        message = (
            'Failed to complete "{cmd}": {exception}'
        ).format(
            cmd=cmd,
            exception=exception,
        )
        LOGGER.warn(message)
        raise exception
Ejemplo n.º 3
0
def _dump_stdout_to_file(node: Node, cmd: List[str], file_path: Path) -> None:
    """
    Dump ``stdout`` of the given command to ``file_path``.

    Raises:
        CalledProcessError: If an error occurs when running the given command.
    """
    chunk_size = 2048
    proc = node.popen(args=cmd)
    with open(str(file_path), 'wb') as dumpfile:
        while True:
            chunk = proc.stdout.read(chunk_size)
            if chunk:
                dumpfile.write(chunk)
            else:
                break
    proc.wait()
    if proc.returncode != 0:
        exception = CalledProcessError(
            returncode=proc.returncode,
            cmd=cmd,
            output=bytes(proc.stdout),
            stderr=bytes(proc.stderr),
        )
        message = ('Failed to complete "{cmd}": {exception}').format(
            cmd=cmd,
            exception=exception,
        )
        LOGGER.warn(message)
        raise exception
Ejemplo n.º 4
0
 def test_stderr(self, dcos_node: Node) -> None:
     """
     ``stderr`` is send to the result's ``stderr`` property.
     """
     echo_result = dcos_node.popen(args=['echo', '1', '1>&2'], shell=True)
     stdout, stderr = echo_result.communicate()
     assert echo_result.returncode == 0
     assert stdout.strip().decode() == ''
     assert stderr.strip().decode() == '1'
Ejemplo n.º 5
0
    def test_async(self, dcos_node: Node) -> None:
        """
        It is possible to run commands asynchronously.
        """
        proc_1 = dcos_node.popen(
            args=['(mkfifo /tmp/pipe | true)', '&&', '(cat /tmp/pipe)'],
            shell=True,
        )

        proc_2 = dcos_node.popen(
            args=[
                '(mkfifo /tmp/pipe | true)',
                '&&',
                '(echo $HOME > /tmp/pipe)',
            ],
            shell=True,
        )

        try:
            # An arbitrary timeout to avoid infinite wait times.
            stdout, _ = proc_1.communicate(timeout=15)
        except TimeoutExpired:  # pragma: no cover
            proc_1.kill()
            stdout, _ = proc_1.communicate()

        return_code_1 = proc_1.poll()

        # Needed to cleanly terminate second subprocess
        try:
            # An arbitrary timeout to avoid infinite wait times.
            proc_2.communicate(timeout=15)
        except TimeoutExpired:  # pragma: no cover
            proc_2.kill()
            proc_2.communicate()
            raise

        return_code_2 = proc_2.poll()

        assert stdout.strip().decode() == '/' + dcos_node.default_user
        assert return_code_1 == 0
        assert return_code_2 == 0

        dcos_node.run(['rm', '-f', '/tmp/pipe'])
Ejemplo n.º 6
0
 def test_literal(
     self,
     dcos_node: Node,
 ) -> None:
     """
     When shell=False, preserve arguments as literal values.
     """
     echo_result = dcos_node.popen(
         args=['echo', 'Hello, ', '&&', 'echo', 'World!'], )
     stdout, stderr = echo_result.communicate()
     assert echo_result.returncode == 0
     assert stdout.strip() == b'Hello,  && echo World!'
     assert stderr == b''
Ejemplo n.º 7
0
 def test_shell(
     self,
     dcos_node: Node,
 ) -> None:
     """
     When shell=True, interpret spaces and special characters.
     """
     echo_result = dcos_node.popen(
         args=['echo', 'Hello, ', '&&', 'echo', 'World!'],
         shell=True,
     )
     stdout, stderr = echo_result.communicate()
     assert echo_result.returncode == 0
     assert stdout.strip().decode() == 'Hello,\nWorld!'
     assert stderr.strip().decode() == ''
Ejemplo n.º 8
0
 def test_pass_env(
     self,
     dcos_node: Node,
 ) -> None:
     """
     Environment variables can be passed to the remote execution
     """
     echo_result = dcos_node.popen(
         args=['echo', '$MYVAR'],
         env={'MYVAR': 'hello, world'},
         shell=True,
     )
     stdout, stderr = echo_result.communicate()
     assert echo_result.returncode == 0
     assert stdout.strip().decode() == 'hello, world'
     assert stderr.strip().decode() == ''