Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_copy_to_host is not platform independent #79

Open
schmerl opened this issue Oct 15, 2020 · 2 comments
Open

test_copy_to_host is not platform independent #79

schmerl opened this issue Oct 15, 2020 · 2 comments
Labels
bug Something isn't working windows

Comments

@schmerl
Copy link

schmerl commented Oct 15, 2020

The code to test symlink copy in test_copy_to_host doesn't work on Windows:

      # sym-linked file
        shell.run('ln -s /tmp/hello /tmp/linked')
        _, fn_host = tempfile.mkstemp()
        try:
            files.copy_to_host('/tmp/linked', fn_host)
            with open(fn_host, 'r') as f:
                assert f.read() == (content + '\n')
        finally:
            os.remove(fn_host)

Windows doesn't know about ln -s

@ChrisTimperley ChrisTimperley added bug Something isn't working windows labels Oct 15, 2020
@ChrisTimperley
Copy link
Owner

ln -s runs inside the container, so there must be another issue here.

@schmerl
Copy link
Author

schmerl commented Oct 15, 2020

Here is the stacktrace:

FAILED                                  [ 50%]Error: No such container:path: 4ac7c4cbc6a9739cc49793aa84f8797400e4e8d0960da77d1622b5cc2a24fa29:\tmp\tmp\hello

test\test_files.py:268 (test_copy_to_host)
self = FileSystem(container=Container(daemon=DockerDaemon(url=None), id='4ac7c4cbc6a9739cc49793aa84f8797400e4e8d0960da77d1622b5cc2a24fa29'))
path_container = '/tmp/linked'
path_host = 'C:\\Users\\schmerl\\AppData\\Local\\Temp\\tmpd8dlfocm\\linked'

    def copy_to_host(self,
                     path_container: str,
                     path_host: str
                     ) -> None:
        """
        Copies a given file or directory tree from the container to the host.
    
        Parameters
        ----------
        path_container: str
            the file that should be copied from the container.
        path_host: str
            the destination filepath on the host.
    
        Raises
        ------
        ContainerFileNotFound
            if no file or directory exists at the given path inside the
            container.
        HostFileNotFound
            if the parent directory of the host filepath does not exist.
        CopyFailed
            if the copy operation failed.
        """
        id_container: str = self.container.id
        path_host_parent: str = os.path.dirname(path_host)
        if not os.path.isdir(path_host_parent):
            raise exc.HostFileNotFound(path_host_parent)
    
        path_host_escaped = quote_host(path_host)
        path_container_escaped = quote_host(path_container)
    
        if _ON_WINDOWS:
            if path_container_escaped[0] != '"':
                path_container_escaped = f'"{path_container_escaped}"'
            if path_host_escaped[0] != '"':
                path_host_escaped = f'"{path_host_escaped}"'
    
        cmd: str = (f"docker cp -L "
                    f"{id_container}:{path_container_escaped} "
                    f"{path_host_escaped}")
        try:
>           subprocess.check_call(cmd, shell=True)

..\src\dockerblade\files.py:128: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

popenargs = ('docker cp -L 4ac7c4cbc6a9739cc49793aa84f8797400e4e8d0960da77d1622b5cc2a24fa29:"/tmp/linked" "C:\\Users\\schmerl\\AppData\\Local\\Temp\\tmpd8dlfocm\\linked"',)
kwargs = {'shell': True}, retcode = 1
cmd = 'docker cp -L 4ac7c4cbc6a9739cc49793aa84f8797400e4e8d0960da77d1622b5cc2a24fa29:"/tmp/linked" "C:\\Users\\schmerl\\AppData\\Local\\Temp\\tmpd8dlfocm\\linked"'

    def check_call(*popenargs, **kwargs):
        """Run command with arguments.  Wait for command to complete.  If
        the exit code was zero then return, otherwise raise
        CalledProcessError.  The CalledProcessError object will have the
        return code in the returncode attribute.
    
        The arguments are the same as for the call function.  Example:
    
        check_call(["ls", "-l"])
        """
        retcode = call(*popenargs, **kwargs)
        if retcode:
            cmd = kwargs.get("args")
            if cmd is None:
                cmd = popenargs[0]
>           raise CalledProcessError(retcode, cmd)
E           subprocess.CalledProcessError: Command 'docker cp -L 4ac7c4cbc6a9739cc49793aa84f8797400e4e8d0960da77d1622b5cc2a24fa29:"/tmp/linked" "C:\Users\schmerl\AppData\Local\Temp\tmpd8dlfocm\linked"' returned non-zero exit status 1.

C:\Users\schmerl\Miniconda3\lib\subprocess.py:363: CalledProcessError

During handling of the above exception, another exception occurred:

alpine_310 = Container(daemon=DockerDaemon(url=None), id='4ac7c4cbc6a9739cc49793aa84f8797400e4e8d0960da77d1622b5cc2a24fa29')

    def test_copy_to_host(alpine_310):
        content = "hello world"
        shell = alpine_310.shell()
        files = alpine_310.filesystem()
        shell.run(f'echo "{content}" > /tmp/hello')
        with tempfile.TemporaryDirectory() as tmp_dir:
            fn_host = os.path.join(tmp_dir, 'hello')
            files.copy_to_host('/tmp/hello', fn_host)
            with open(fn_host, 'r') as f:
                assert f.read() == (content + '\n')
    
        # sym-linked file
        shell.run('ln -s /tmp/hello /tmp/linked')
        with tempfile.TemporaryDirectory() as tmp_dir:
            fn_host = os.path.join(tmp_dir, 'linked')
>           files.copy_to_host('/tmp/linked', fn_host)

test_files.py:284: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = FileSystem(container=Container(daemon=DockerDaemon(url=None), id='4ac7c4cbc6a9739cc49793aa84f8797400e4e8d0960da77d1622b5cc2a24fa29'))
path_container = '/tmp/linked'
path_host = 'C:\\Users\\schmerl\\AppData\\Local\\Temp\\tmpd8dlfocm\\linked'

    def copy_to_host(self,
                     path_container: str,
                     path_host: str
                     ) -> None:
        """
        Copies a given file or directory tree from the container to the host.
    
        Parameters
        ----------
        path_container: str
            the file that should be copied from the container.
        path_host: str
            the destination filepath on the host.
    
        Raises
        ------
        ContainerFileNotFound
            if no file or directory exists at the given path inside the
            container.
        HostFileNotFound
            if the parent directory of the host filepath does not exist.
        CopyFailed
            if the copy operation failed.
        """
        id_container: str = self.container.id
        path_host_parent: str = os.path.dirname(path_host)
        if not os.path.isdir(path_host_parent):
            raise exc.HostFileNotFound(path_host_parent)
    
        path_host_escaped = quote_host(path_host)
        path_container_escaped = quote_host(path_container)
    
        if _ON_WINDOWS:
            if path_container_escaped[0] != '"':
                path_container_escaped = f'"{path_container_escaped}"'
            if path_host_escaped[0] != '"':
                path_host_escaped = f'"{path_host_escaped}"'
    
        cmd: str = (f"docker cp -L "
                    f"{id_container}:{path_container_escaped} "
                    f"{path_host_escaped}")
        try:
            subprocess.check_call(cmd, shell=True)
        except subprocess.CalledProcessError:
            if not self.exists(path_container):
                raise exc.ContainerFileNotFound(path=path_container,
                                                container_id=id_container)
    
            reason = (f"failed to copy file [{path_container}] "
                      f"from container [{id_container}] to host: "
                      f"{path_host}")
>           raise exc.CopyFailed(reason)
E           dockerblade.exceptions.CopyFailed: Copy operation failed: failed to copy file [/tmp/linked] from container [4ac7c4cbc6a9739cc49793aa84f8797400e4e8d0960da77d1622b5cc2a24fa29] to host: C:\Users\schmerl\AppData\Local\Temp\tmpd8dlfocm\linked

..\src\dockerblade\files.py:137: CopyFailed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working windows
Projects
None yet
Development

No branches or pull requests

2 participants