From 150cf3083e930d61ce6c64b18726204e09f5c16c Mon Sep 17 00:00:00 2001 From: Sriram Madapusi Vasudevan Date: Mon, 26 Nov 2018 13:53:48 -0800 Subject: [PATCH 1/2] fix: helpful error messages when runtime mismatch for build --- aws_lambda_builders/exceptions.py | 7 +++++-- aws_lambda_builders/validate.py | 6 ++++-- tests/unit/test_runtime.py | 5 +++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/aws_lambda_builders/exceptions.py b/aws_lambda_builders/exceptions.py index f11a30f12..ee82bd27e 100644 --- a/aws_lambda_builders/exceptions.py +++ b/aws_lambda_builders/exceptions.py @@ -16,8 +16,11 @@ class UnsupportedManifestError(LambdaBuilderError): class MisMatchRuntimeError(LambdaBuilderError): - MESSAGE = "A runtime version mismatch was found for the given language " \ - "'{language}', required runtime '{required_runtime}'" + MESSAGE = "{language} executable found in your path does not " \ + "match runtime. " \ + "\n Expected version: {required_runtime}, Found version: {found_runtime}. " \ + "\n Consider moving {required_runtime} up path hierarchy." \ + "\n Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30" class WorkflowNotFoundError(LambdaBuilderError): diff --git a/aws_lambda_builders/validate.py b/aws_lambda_builders/validate.py index 025a570f2..82d34d3a9 100644 --- a/aws_lambda_builders/validate.py +++ b/aws_lambda_builders/validate.py @@ -17,6 +17,7 @@ def validate_python_cmd(required_language, required_runtime_version): "python", "-c", "import sys; " + "sys.stdout.write('python' + str(sys.version_info.major) + '.' + str(sys.version_info.minor)); " "assert sys.version_info.major == {major} " "and sys.version_info.minor == {minor}".format( major=major, @@ -63,10 +64,11 @@ def validate_runtime(cls, required_language, required_runtime): p = subprocess.Popen(cmd, cwd=os.getcwd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) - p.communicate() + found_runtime, _ = p.communicate() if p.returncode != 0: raise MisMatchRuntimeError(language=required_language, - required_runtime=required_runtime) + required_runtime=required_runtime, + found_runtime=str(found_runtime.decode('utf-8'))) else: LOG.warning("'%s' runtime has not " "been validated!", required_language) diff --git a/tests/unit/test_runtime.py b/tests/unit/test_runtime.py index 25799806d..6f9e01168 100644 --- a/tests/unit/test_runtime.py +++ b/tests/unit/test_runtime.py @@ -13,7 +13,7 @@ def __init__(self, returncode): self.returncode = returncode def communicate(self): - pass + return b'python3,6', None class TestRuntime(TestCase): @@ -44,6 +44,7 @@ def test_runtime_validate_mismatch_version_runtime(self): def test_python_command(self): cmd = validate_python_cmd("python", "python2.7") - version_strings = ["sys.version_info.major == 2", "sys.version_info.minor == 7"] + version_strings = ["sys.stdout.write", "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])) From 60e89ce152718cd473d79eecefcc60910f0699d3 Mon Sep 17 00:00:00 2001 From: Sriram Madapusi Vasudevan Date: Mon, 26 Nov 2018 14:33:48 -0800 Subject: [PATCH 2/2] fix: add integration test to check output of runtime mismatch --- aws_lambda_builders/exceptions.py | 1 - .../workflows/python_pip/test_python_pip.py | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/aws_lambda_builders/exceptions.py b/aws_lambda_builders/exceptions.py index ee82bd27e..656763667 100644 --- a/aws_lambda_builders/exceptions.py +++ b/aws_lambda_builders/exceptions.py @@ -19,7 +19,6 @@ class MisMatchRuntimeError(LambdaBuilderError): MESSAGE = "{language} executable found in your path does not " \ "match runtime. " \ "\n Expected version: {required_runtime}, Found version: {found_runtime}. " \ - "\n Consider moving {required_runtime} up path hierarchy." \ "\n Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30" diff --git a/tests/integration/workflows/python_pip/test_python_pip.py b/tests/integration/workflows/python_pip/test_python_pip.py index 51fe6927b..c4a4f4e41 100644 --- a/tests/integration/workflows/python_pip/test_python_pip.py +++ b/tests/integration/workflows/python_pip/test_python_pip.py @@ -6,7 +6,7 @@ from unittest import TestCase from aws_lambda_builders.builder import LambdaBuilder -from aws_lambda_builders.exceptions import WorkflowFailedError +from aws_lambda_builders.exceptions import WorkflowFailedError, MisMatchRuntimeError class TestPythonPipWorkflow(TestCase): @@ -33,6 +33,12 @@ def setUp(self): language=self.builder.capability.language, major=sys.version_info.major, minor=sys.version_info.minor) + self.runtime_mismatch = { + 'python3.6': 'python2.7', + 'python3.7': 'python2.7', + 'python2.7': 'python3.6' + + } def tearDown(self): shutil.rmtree(self.artifacts_dir) @@ -46,6 +52,15 @@ def test_must_build_python_project(self): output_files = set(os.listdir(self.artifacts_dir)) self.assertEquals(expected_files, output_files) + def test_mismatch_runtime_python_project(self): + with self.assertRaises(MisMatchRuntimeError) as mismatch_error: + self.builder.build(self.source_dir, self.artifacts_dir, self.scratch_dir, self.manifest_path_valid, + runtime=self.runtime_mismatch[self.runtime]) + self.assertEquals(mismatch_error.msg, + MisMatchRuntimeError(language="python", + required_runtime=self.runtime_mismatch[self.runtime], + found_runtime=self.runtime).MESSAGE) + def test_runtime_validate_python_project_fail_open_unsupported_runtime(self): with self.assertRaises(WorkflowFailedError): self.builder.build(self.source_dir, self.artifacts_dir, self.scratch_dir, self.manifest_path_valid,