Skip to content

Commit

Permalink
Remove to_posix_path function and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
y-stm committed Jan 14, 2025
1 parent 047edfe commit fc11bf2
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 114 deletions.
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 @@ -177,67 +177,6 @@ def test_must_create_container_including_all_optional_values(self, mock_resolve_
)
self.mock_docker_client.networks.get.assert_not_called()

@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.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

0 comments on commit fc11bf2

Please sign in to comment.