Skip to content

Commit cf678d6

Browse files
sriram-mvsanathkr
authored andcommitted
fix: helpful error messages when runtime mismatch for build (#37)
1 parent 7708187 commit cf678d6

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

aws_lambda_builders/exceptions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ class UnsupportedManifestError(LambdaBuilderError):
1616

1717

1818
class MisMatchRuntimeError(LambdaBuilderError):
19-
MESSAGE = "A runtime version mismatch was found for the given language " \
20-
"'{language}', required runtime '{required_runtime}'"
19+
MESSAGE = "{language} executable found in your path does not " \
20+
"match runtime. " \
21+
"\n Expected version: {required_runtime}, Found version: {found_runtime}. " \
22+
"\n Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30"
2123

2224

2325
class WorkflowNotFoundError(LambdaBuilderError):

aws_lambda_builders/validate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def validate_python_cmd(required_language, required_runtime_version):
1717
"python",
1818
"-c",
1919
"import sys; "
20+
"sys.stdout.write('python' + str(sys.version_info.major) + '.' + str(sys.version_info.minor)); "
2021
"assert sys.version_info.major == {major} "
2122
"and sys.version_info.minor == {minor}".format(
2223
major=major,
@@ -63,10 +64,11 @@ def validate_runtime(cls, required_language, required_runtime):
6364
p = subprocess.Popen(cmd,
6465
cwd=os.getcwd(),
6566
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
66-
p.communicate()
67+
found_runtime, _ = p.communicate()
6768
if p.returncode != 0:
6869
raise MisMatchRuntimeError(language=required_language,
69-
required_runtime=required_runtime)
70+
required_runtime=required_runtime,
71+
found_runtime=str(found_runtime.decode('utf-8')))
7072
else:
7173
LOG.warning("'%s' runtime has not "
7274
"been validated!", required_language)

tests/integration/workflows/python_pip/test_python_pip.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from unittest import TestCase
77

88
from aws_lambda_builders.builder import LambdaBuilder
9-
from aws_lambda_builders.exceptions import WorkflowFailedError
9+
from aws_lambda_builders.exceptions import WorkflowFailedError, MisMatchRuntimeError
1010

1111

1212
class TestPythonPipWorkflow(TestCase):
@@ -33,6 +33,12 @@ def setUp(self):
3333
language=self.builder.capability.language,
3434
major=sys.version_info.major,
3535
minor=sys.version_info.minor)
36+
self.runtime_mismatch = {
37+
'python3.6': 'python2.7',
38+
'python3.7': 'python2.7',
39+
'python2.7': 'python3.6'
40+
41+
}
3642

3743
def tearDown(self):
3844
shutil.rmtree(self.artifacts_dir)
@@ -46,6 +52,15 @@ def test_must_build_python_project(self):
4652
output_files = set(os.listdir(self.artifacts_dir))
4753
self.assertEquals(expected_files, output_files)
4854

55+
def test_mismatch_runtime_python_project(self):
56+
with self.assertRaises(MisMatchRuntimeError) as mismatch_error:
57+
self.builder.build(self.source_dir, self.artifacts_dir, self.scratch_dir, self.manifest_path_valid,
58+
runtime=self.runtime_mismatch[self.runtime])
59+
self.assertEquals(mismatch_error.msg,
60+
MisMatchRuntimeError(language="python",
61+
required_runtime=self.runtime_mismatch[self.runtime],
62+
found_runtime=self.runtime).MESSAGE)
63+
4964
def test_runtime_validate_python_project_fail_open_unsupported_runtime(self):
5065
with self.assertRaises(WorkflowFailedError):
5166
self.builder.build(self.source_dir, self.artifacts_dir, self.scratch_dir, self.manifest_path_valid,

tests/unit/test_runtime.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, returncode):
1313
self.returncode = returncode
1414

1515
def communicate(self):
16-
pass
16+
return b'python3,6', None
1717

1818

1919
class TestRuntime(TestCase):
@@ -44,6 +44,7 @@ def test_runtime_validate_mismatch_version_runtime(self):
4444

4545
def test_python_command(self):
4646
cmd = validate_python_cmd("python", "python2.7")
47-
version_strings = ["sys.version_info.major == 2", "sys.version_info.minor == 7"]
47+
version_strings = ["sys.stdout.write", "sys.version_info.major == 2",
48+
"sys.version_info.minor == 7"]
4849
for version_string in version_strings:
4950
self.assertTrue(any([part for part in cmd if version_string in part]))

0 commit comments

Comments
 (0)