Skip to content
Merged
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
15 changes: 15 additions & 0 deletions aws_lambda_builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging

from aws_lambda_builders.registry import get_workflow, DEFAULT_REGISTRY
from aws_lambda_builders.validate import RuntimeValidator
from aws_lambda_builders.workflow import Capability

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -89,6 +90,9 @@ def build(self, source_dir, artifacts_dir, scratch_dir, manifest_path,
:param options:
Optional dictionary of options ot pass to build action. **Not supported**.
"""
if runtime:
self._validate_runtime(runtime)


workflow = self.selected_workflow_cls(source_dir,
artifacts_dir,
Expand All @@ -100,5 +104,16 @@ def build(self, source_dir, artifacts_dir, scratch_dir, manifest_path,

return workflow.run()

def _validate_runtime(self, runtime):
"""
validate runtime and local runtime version to make sure they match

:type runtime: str
:param runtime:
String matching a lambda runtime eg: python3.6
"""
RuntimeValidator.validate_runtime(required_language=self.capability.language,
required_runtime=runtime)

def _clear_workflows(self):
DEFAULT_REGISTRY.clear()
5 changes: 5 additions & 0 deletions aws_lambda_builders/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class UnsupportedManifestError(LambdaBuilderError):
MESSAGE = "A builder for the given capabilities '{capabilities}' was not found"


class MisMatchRuntimeError(LambdaBuilderError):
MESSAGE = "A runtime version mismatch was found for the given language " \
"'{language}', required runtime '{required_runtime}'"


class WorkflowNotFoundError(LambdaBuilderError):
"""
Raised when a workflow matching the given capabilities was not found
Expand Down
71 changes: 71 additions & 0 deletions aws_lambda_builders/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
Supported Runtimes and their validations.
"""

import logging
import os
import subprocess

from aws_lambda_builders.exceptions import MisMatchRuntimeError

LOG = logging.getLogger(__name__)


def validate_python_cmd(required_language, required_runtime_version):
major, minor = required_runtime_version.replace(required_language, "").split('.')
cmd = [
"python",
"-c",
"import sys; "
"assert sys.version_info.major == {major} "
"and sys.version_info.minor == {minor}".format(
major=major,
minor=minor)]
return cmd


_RUNTIME_VERSION_RESOLVER = {
"python": validate_python_cmd
}


class RuntimeValidator(object):
SUPPORTED_RUNTIMES = [
"python2.7",
"python3.6"
]

@classmethod
def has_runtime(cls, runtime):
"""
Checks if the runtime is supported.
:param string runtime: Runtime to check
:return bool: True, if the runtime is supported.
"""
return runtime in cls.SUPPORTED_RUNTIMES

@classmethod
def validate_runtime(cls, required_language, required_runtime):
"""
Checks if the language supplied matches the required lambda runtime
:param string required_language: language to check eg: python
:param string required_runtime: runtime to check eg: python3.6
:raises MisMatchRuntimeError: Version mismatch of the language vs the required runtime
"""
if required_language in _RUNTIME_VERSION_RESOLVER:
if not RuntimeValidator.has_runtime(required_runtime):
LOG.warning("'%s' runtime is not "
"a supported runtime", required_runtime)
return
cmd = _RUNTIME_VERSION_RESOLVER[required_language](required_language, required_runtime)

p = subprocess.Popen(cmd,
cwd=os.getcwd(),
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()
if p.returncode != 0:
raise MisMatchRuntimeError(language=required_language,
required_runtime=required_runtime)
else:
LOG.warning("'%s' runtime has not "
"been validated!", required_language)
1 change: 1 addition & 0 deletions tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_run_hello_workflow(self):
}
}).encode('utf-8')


env = copy.deepcopy(os.environ)
env["PYTHONPATH"] = self.python_path

Expand Down
18 changes: 15 additions & 3 deletions tests/integration/workflows/python_pip/test_python_pip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import os
import shutil
import sys
import tempfile
from unittest import TestCase

Expand Down Expand Up @@ -28,31 +29,42 @@ def setUp(self):
self.builder = LambdaBuilder(language="python",
dependency_manager="pip",
application_framework=None)
self.runtime = "{language}{major}.{minor}".format(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!! this makes the test work across multiple runtimes :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! :)

language=self.builder.capability.language,
major=sys.version_info.major,
minor=sys.version_info.minor)

def tearDown(self):
shutil.rmtree(self.artifacts_dir)
shutil.rmtree(self.scratch_dir)

def test_must_build_python_project(self):
self.builder.build(self.source_dir, self.artifacts_dir, None, self.manifest_path_valid,
runtime="python2.7")
runtime=self.runtime)

expected_files = self.test_data_files.union({"numpy", "numpy-1.15.4.data", "numpy-1.15.4.dist-info"})
output_files = set(os.listdir(self.artifacts_dir))
self.assertEquals(expected_files, output_files)

def test_runtime_validate_python_project_fail_open_unsupported_runtime(self):
self.builder.build(self.source_dir, self.artifacts_dir, None, self.manifest_path_valid,
runtime="python2.8")
expected_files = self.test_data_files.union({"numpy", "numpy-1.15.4.data", "numpy-1.15.4.dist-info"})
output_files = set(os.listdir(self.artifacts_dir))
self.assertEquals(expected_files, output_files)

def test_must_fail_to_resolve_dependencies(self):

with self.assertRaises(WorkflowFailedError) as ctx:
self.builder.build(self.source_dir, self.artifacts_dir, None, self.manifest_path_invalid,
runtime="python2.7")
runtime=self.runtime)

self.assertIn("Invalid requirement: 'adfasf=1.2.3'", str(ctx.exception))

def test_must_fail_if_requirements_not_found(self):

with self.assertRaises(WorkflowFailedError) as ctx:
self.builder.build(self.source_dir, self.artifacts_dir, None, os.path.join("non", "existent", "manifest"),
runtime="python2.7")
runtime=self.runtime)

self.assertIn("Requirements file not found", str(ctx.exception))
14 changes: 7 additions & 7 deletions tests/unit/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ def setUp(self):
@patch('aws_lambda_builders.builder.importlib')
@patch('aws_lambda_builders.builder.get_workflow')
def test_with_mocks(self, get_workflow_mock, importlib_mock):

workflow_cls = Mock()
workflow_instance = workflow_cls.return_value = Mock()

get_workflow_mock.return_value = workflow_cls

builder = LambdaBuilder(self.lang, self.lang_framework, self.app_framework, supported_workflows=[])
with patch.object(LambdaBuilder, "_validate_runtime"):
builder = LambdaBuilder(self.lang, self.lang_framework, self.app_framework, supported_workflows=[])

builder.build("source_dir", "artifacts_dir", "scratch_dir", "manifest_path",
runtime="runtime", optimizations="optimizations", options="options")
builder.build("source_dir", "artifacts_dir", "scratch_dir", "manifest_path",
runtime="runtime", optimizations="optimizations", options="options")

workflow_cls.assert_called_with("source_dir", "artifacts_dir", "scratch_dir", "manifest_path",
runtime="runtime", optimizations="optimizations", options="options")
workflow_instance.run.assert_called_once()
workflow_cls.assert_called_with("source_dir", "artifacts_dir", "scratch_dir", "manifest_path",
runtime="runtime", optimizations="optimizations", options="options")
workflow_instance.run.assert_called_once()
49 changes: 49 additions & 0 deletions tests/unit/test_runtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from unittest import TestCase

import mock

from aws_lambda_builders.exceptions import MisMatchRuntimeError
from aws_lambda_builders.validate import validate_python_cmd
from aws_lambda_builders.validate import RuntimeValidator


class MockSubProcess(object):

def __init__(self, returncode):
self.returncode = returncode

def communicate(self):
pass


class TestRuntime(TestCase):

def test_supported_runtimes(self):
self.assertTrue(RuntimeValidator.has_runtime("python2.7"))
self.assertTrue(RuntimeValidator.has_runtime("python3.6"))
self.assertFalse(RuntimeValidator.has_runtime("test_language"))

def test_runtime_validate_unsupported_language_fail_open(self):
RuntimeValidator.validate_runtime("test_language", "test_language2.7")

def test_runtime_validate_unsupported_runtime_version_fail_open(self):
RuntimeValidator.validate_runtime("python", "python2.8")

def test_runtime_validate_supported_version_runtime(self):
with mock.patch('subprocess.Popen') as mock_subprocess:
mock_subprocess.return_value = MockSubProcess(0)
RuntimeValidator.validate_runtime("python", "python3.6")
self.assertTrue(mock_subprocess.call_count, 1)

def test_runtime_validate_mismatch_version_runtime(self):
with mock.patch('subprocess.Popen') as mock_subprocess:
mock_subprocess.return_value = MockSubProcess(1)
with self.assertRaises(MisMatchRuntimeError):
RuntimeValidator.validate_runtime("python", "python2.7")
self.assertTrue(mock_subprocess.call_count, 1)

def test_python_command(self):
cmd = validate_python_cmd("python", "python2.7")
version_strings = ["sys.version_info.major == 2", "sys.version_info.minor == 7"]
for version_string in version_strings:
self.assertTrue(any([part for part in cmd if version_string in part]))
1 change: 0 additions & 1 deletion tests/unit/workflows/python_pip/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ def test_must_raise_exception_on_failure(self, PythonPipDependencyBuilderMock):

with self.assertRaises(ActionFailedError):
action.execute()