-
Notifications
You must be signed in to change notification settings - Fork 159
fix: validate runtime #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! :)