Skip to content

Stop convert windows path to posix style #7826

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions samcli/local/docker/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
DockerContainerCreationFailedException,
PortAlreadyInUse,
)
from samcli.local.docker.utils import NoFreePortsError, find_free_port, to_posix_path
from samcli.local.docker.utils import NoFreePortsError, find_free_port

LOG = logging.getLogger(__name__)


CONTAINER_CONNECTION_TIMEOUT = float(os.environ.get("SAM_CLI_CONTAINER_CONNECTION_TIMEOUT", 20))
DEFAULT_CONTAINER_HOST_INTERFACE = "127.0.0.1"

Expand Down Expand Up @@ -226,9 +227,6 @@ def create(self, context):
if self._additional_volumes:
kwargs["volumes"].update(self._additional_volumes)

# Make sure all mounts are of posix path style.
kwargs["volumes"] = {to_posix_path(host_dir): mount for host_dir, mount in kwargs["volumes"].items()}

if self._env_vars:
kwargs["environment"] = self._env_vars

Expand Down
35 changes: 0 additions & 35 deletions samcli/local/docker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
"""

import logging
import os
import pathlib
import platform
import posixpath
import random
import re
import socket

import docker
Expand All @@ -20,37 +16,6 @@
LOG = logging.getLogger(__name__)


def to_posix_path(code_path):
"""
Change the code_path to be of unix-style if running on windows when supplied with an absolute windows path.

Parameters
----------
code_path : str
Directory in the host operating system that should be mounted within the container.
Returns
-------
str
Posix equivalent of absolute windows style path.
Examples
--------
>>> to_posix_path('/Users/UserName/sam-app')
/Users/UserName/sam-app
>>> to_posix_path('C:\\\\Users\\\\UserName\\\\AppData\\\\Local\\\\Temp\\\\mydir')
/c/Users/UserName/AppData/Local/Temp/mydir
"""

return (
re.sub(
"^([A-Za-z])+:",
lambda match: posixpath.sep + match.group().replace(":", "").lower(),
pathlib.PureWindowsPath(code_path).as_posix(),
)
if os.name == "nt"
else code_path
)


def find_free_port(network_interface: str, start: int = 5000, end: int = 9000) -> int:
"""
Utility function which scans through a port range in a randomized manner
Expand Down
61 changes: 0 additions & 61 deletions tests/unit/local/docker/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,67 +181,6 @@ def test_must_create_container_including_all_optional_values(self, mock_resolve_
self.mock_docker_client.networks.get.assert_not_called()
mock_resolve_symlinks.assert_not_called() # When context is BUILD

@patch("samcli.local.docker.utils.os")
@patch("samcli.local.docker.container.Container._create_mapped_symlink_files")
def test_must_create_container_translate_volume_path(self, mock_resolve_symlinks, os_mock):
"""
Create a container with required and optional values, with windows style volume mount.
:return:
"""

os_mock.name = "nt"
host_dir = "C:\\Users\\Username\\AppData\\Local\\Temp\\tmp1337"
additional_volumes = {"C:\\Users\\Username\\AppData\\Local\\Temp\\tmp1338": {"blah": "blah value"}}

translated_volumes = {
"/c/Users/Username/AppData/Local/Temp/tmp1337": {"bind": self.working_dir, "mode": "ro,delegated"}
}

translated_additional_volumes = {"/c/Users/Username/AppData/Local/Temp/tmp1338": {"blah": "blah value"}}

translated_volumes.update(translated_additional_volumes)
expected_memory = "{}m".format(self.memory_mb)

generated_id = "fooobar"
self.mock_docker_client.containers.create.return_value = Mock()
self.mock_docker_client.containers.create.return_value.id = generated_id

container = Container(
self.image,
self.cmd,
self.working_dir,
host_dir,
memory_limit_mb=self.memory_mb,
exposed_ports=self.exposed_ports,
entrypoint=self.entrypoint,
env_vars=self.env_vars,
docker_client=self.mock_docker_client,
container_opts=self.container_opts,
additional_volumes=additional_volumes,
)

container_id = container.create(self.container_context)
self.assertEqual(container_id, generated_id)
self.assertEqual(container.id, generated_id)

self.mock_docker_client.containers.create.assert_called_with(
self.image,
command=self.cmd,
working_dir=self.working_dir,
volumes=translated_volumes,
tty=False,
use_config_proxy=True,
environment=self.env_vars,
ports={
container_port: ("127.0.0.1", host_port)
for container_port, host_port in {**self.exposed_ports, **self.always_exposed_ports}.items()
},
entrypoint=self.entrypoint,
mem_limit=expected_memory,
container="opts",
)
self.mock_docker_client.networks.get.assert_not_called()

@patch("samcli.local.docker.container.Container._create_mapped_symlink_files")
def test_must_connect_to_network_on_create(self, mock_resolve_symlinks):
"""
Expand Down
19 changes: 1 addition & 18 deletions tests/unit/local/docker/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,10 @@
from unittest.mock import patch, Mock

from samcli.lib.utils.architecture import ARM64, InvalidArchitecture, X86_64
from samcli.local.docker.utils import to_posix_path, find_free_port, get_rapid_name, get_docker_platform, get_image_arch
from samcli.local.docker.utils import find_free_port, get_rapid_name, get_docker_platform, get_image_arch
from samcli.local.docker.exceptions import NoFreePortsError


class TestPosixPath(TestCase):
def setUp(self):
self.ntpath = "C:\\Users\\UserName\\AppData\\Local\\Temp\\temp1337"
self.posixpath = "/c/Users/UserName/AppData/Local/Temp/temp1337"
self.current_working_dir = os.getcwd()

@patch("samcli.local.docker.utils.os")
def test_convert_posix_path_if_windows_style_path(self, mock_os):
mock_os.name = "nt"
self.assertEqual(self.posixpath, to_posix_path(self.ntpath))

@patch("samcli.local.docker.utils.os")
def test_do_not_convert_posix_path(self, mock_os):
mock_os.name = "posix"
self.assertEqual(self.current_working_dir, to_posix_path(self.current_working_dir))


class TestFreePorts(TestCase):
@parameterized.expand([("0.0.0.0",), ("127.0.0.1",)])
@patch("samcli.local.docker.utils.socket")
Expand Down