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

fix: Validate if pip is installed in the runtime path #549

Merged
merged 7 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
29 changes: 22 additions & 7 deletions aws_lambda_builders/workflows/python_pip/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

from aws_lambda_builders.exceptions import MisMatchRuntimeError
from aws_lambda_builders.validator import RuntimeValidator

from .utils import OSUtils
from aws_lambda_builders.workflows.python_pip.compat import pip_import_string
from aws_lambda_builders.workflows.python_pip.exceptions import MissingPipError
from aws_lambda_builders.workflows.python_pip.utils import OSUtils

LOG = logging.getLogger(__name__)

Expand All @@ -20,7 +21,7 @@ def __init__(self, runtime, architecture):
self.language = "python"
self._valid_runtime_path = None

def validate(self, runtime_path):
def validate(self, runtime_path: str) -> str:
"""
Checks if the language supplied matches the required lambda runtime

Expand All @@ -37,7 +38,8 @@ def validate(self, runtime_path):
Raises
------
MisMatchRuntimeError
Raise runtime is not support or runtime does not support architecture.
Raise runtime is not support or runtime does not support architecture or
if the Python runtime does not contain `pip`.
"""

runtime_path = super(PythonRuntimeValidator, self).validate(runtime_path)
Expand All @@ -48,11 +50,24 @@ def validate(self, runtime_path):
cmd, cwd=os.getcwd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=OSUtils().original_environ()
)
p.communicate()

if p.returncode != 0:
raise MisMatchRuntimeError(language=self.language, required_runtime=self.runtime, runtime_path=runtime_path)
else:
self._valid_runtime_path = runtime_path
return self._valid_runtime_path

try:
# call method to import `pip`
# ignoring method return values since we only want to check
# if `pip` is imported successfully
pip_import_string(runtime_path)
lucashuy marked this conversation as resolved.
Show resolved Hide resolved
except MissingPipError as ex:
LOG.debug(f"Invalid Python runtime {runtime_path}, runtime does not contain pip")

raise MisMatchRuntimeError(
language=self.language, required_runtime=self.runtime, runtime_path=runtime_path
) from ex

self._valid_runtime_path = runtime_path
return self._valid_runtime_path

def _validate_python_cmd(self, runtime_path):
major, minor = self.runtime.replace(self.language, "").split(".")
Expand Down
27 changes: 22 additions & 5 deletions tests/unit/workflows/python_pip/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from parameterized import parameterized

from aws_lambda_builders.exceptions import MisMatchRuntimeError
from aws_lambda_builders.workflows.python_pip.exceptions import MissingPipError
from aws_lambda_builders.workflows.python_pip.validator import PythonRuntimeValidator
from aws_lambda_builders.exceptions import UnsupportedRuntimeError, UnsupportedArchitectureError

Expand All @@ -19,36 +20,52 @@ class TestPythonRuntimeValidator(TestCase):
def setUp(self):
self.validator = PythonRuntimeValidator(runtime="python3.7", architecture="x86_64")

def test_runtime_validate_unsupported_language_fail_open(self):
@mock.patch("aws_lambda_builders.workflows.python_pip.validator.pip_import_string")
def test_runtime_validate_unsupported_language_fail_open(self, pip_mock):
validator = PythonRuntimeValidator(runtime="python2.6", architecture="arm64")
with self.assertRaises(UnsupportedRuntimeError):
validator.validate(runtime_path="/usr/bin/python2.6")

def test_runtime_validate_supported_version_runtime(self):
@mock.patch("aws_lambda_builders.workflows.python_pip.validator.pip_import_string")
def test_runtime_validate_supported_version_runtime(self, pip_mock):
with mock.patch("subprocess.Popen") as mock_subprocess:
mock_subprocess.return_value = MockSubProcess(0)
self.validator.validate(runtime_path="/usr/bin/python3.7")
self.assertTrue(mock_subprocess.call_count, 1)

def test_runtime_validate_mismatch_version_runtime(self):
@mock.patch("aws_lambda_builders.workflows.python_pip.validator.pip_import_string")
def test_runtime_validate_mismatch_version_runtime(self, pip_mock):
with mock.patch("subprocess.Popen") as mock_subprocess:
mock_subprocess.return_value = MockSubProcess(1)
with self.assertRaises(MisMatchRuntimeError):
self.validator.validate(runtime_path="/usr/bin/python3.9")
self.assertTrue(mock_subprocess.call_count, 1)

def test_python_command(self):
@mock.patch("aws_lambda_builders.workflows.python_pip.validator.pip_import_string")
def test_python_command(self, pip_mock):
cmd = self.validator._validate_python_cmd(runtime_path="/usr/bin/python3.7")
version_strings = ["sys.version_info.major == 3", "sys.version_info.minor == 7"]
for version_string in version_strings:
self.assertTrue(all([part for part in cmd if version_string in part]))

@mock.patch("aws_lambda_builders.workflows.python_pip.validator.pip_import_string")
@mock.patch("aws_lambda_builders.workflows.python_pip.validator.subprocess.Popen")
def test_runtime_exists_missing_pip(self, popen_mock, pip_mock):
popen_mock.return_code = 0
pip_mock.side_effect = [MissingPipError(python_path="message")]

with self.assertRaises(MisMatchRuntimeError) as ex:
self.validator.validate(runtime_path="/usr/bin/python3.7")

self.assertEqual(self.validator._valid_runtime_path, None)

@parameterized.expand(
[
("python3.7", "arm64"),
]
)
def test_runtime_validate_with_incompatible_architecture(self, runtime, architecture):
@mock.patch("aws_lambda_builders.workflows.python_pip.validator.pip_import_string")
def test_runtime_validate_with_incompatible_architecture(self, runtime, architecture, pip_mock):
validator = PythonRuntimeValidator(runtime=runtime, architecture=architecture)
with self.assertRaises(UnsupportedArchitectureError):
validator.validate(runtime_path="/usr/bin/python")
Loading