Skip to content

Commit 2c40a9b

Browse files
committed
Update go modules path resolvers and validators
Update the go models path resolvers and validators to the latest framework merged in aws#55
1 parent dd837e0 commit 2c40a9b

File tree

8 files changed

+46
-87
lines changed

8 files changed

+46
-87
lines changed

aws_lambda_builders/workflows/go_modules/actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class GoModulesBuildAction(BaseAction):
1010

1111
NAME = "Build"
1212
DESCRIPTION = "Building Go package with Go Modules"
13-
PURPOSE = Purpose.RESOLVE_DEPENDENCIES
13+
PURPOSE = Purpose.COMPILE_SOURCE
1414

1515
def __init__(self, source_dir, output_path, builder):
1616
self.source_dir = source_dir

aws_lambda_builders/workflows/go_modules/builder.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ def __init__(self, **kwargs):
1515

1616

1717
class GoModulesBuilder(object):
18-
def __init__(self, osutils, runtime_path):
18+
19+
LANGUAGE = "go"
20+
21+
def __init__(self, osutils, binaries):
1922
"""Initialize a GoModulesBuilder.
2023
2124
:type osutils: :class:`lambda_builders.utils.OSUtils`
2225
:param osutils: A class used for all interactions with the
2326
outside OS.
2427
25-
:type runtime_path: str
26-
:param runtime_path: The path to the go runtime.
28+
:type binaries: dict
29+
:param binaries: A dict of language binaries
2730
"""
2831
self.osutils = osutils
29-
self.runtime_path = runtime_path
32+
self.binaries = binaries
3033

3134
def build(self, source_dir_path, output_path):
3235
"""Builds a go project onto an output path.
@@ -40,7 +43,8 @@ def build(self, source_dir_path, output_path):
4043
env = {}
4144
env.update(self.osutils.environ)
4245
env.update({"GOOS": "linux", "GOARCH": "amd64"})
43-
cmd = [self.runtime_path, "build", "-o", output_path, source_dir_path]
46+
runtime_path = self.binaries[self.LANGUAGE].binary_path
47+
cmd = [runtime_path, "build", "-o", output_path, source_dir_path]
4448

4549
p = self.osutils.popen(
4650
cmd,

aws_lambda_builders/workflows/go_modules/path_resolver.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

aws_lambda_builders/workflows/go_modules/validator.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ class GoRuntimeValidator(object):
1616
"go1.x"
1717
}
1818

19-
def __init__(self, runtime, runtime_path):
19+
def __init__(self, runtime):
2020
self.language = "go"
2121
self.runtime = runtime
22-
self.runtime_path = runtime_path
22+
self._valid_runtime_path = None
2323

2424
def has_runtime(self):
2525
"""
@@ -29,29 +29,35 @@ def has_runtime(self):
2929
"""
3030
return self.runtime in self.SUPPORTED_RUNTIMES
3131

32-
def validate_runtime(self):
32+
def validate(self, runtime_path):
3333
"""
3434
Checks if the language supplied matches the required lambda runtime
3535
:param string runtime_path: runtime to check eg: /usr/bin/go
3636
:raises MisMatchRuntimeError: Version mismatch of the language vs the required runtime
3737
"""
3838
if not self.has_runtime():
3939
LOG.warning("'%s' runtime is not "
40-
"a supported runtime", self.runtime_path)
41-
return
40+
"a supported runtime", self.runtime)
41+
return None
4242

4343
expected_major_version = self.runtime.replace(self.language, "").split('.')[0]
4444

45-
p = subprocess.Popen([self.runtime_path, "version"],
45+
p = subprocess.Popen([runtime_path, "version"],
4646
cwd=os.getcwd(),
4747
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
4848
out, _ = p.communicate()
4949

5050
mismatched = p.returncode != 0 \
5151
or len(out.split()) < 3 \
52-
or out.split()[2].replace(self.language, "").split('.')[0] != expected_major_version
52+
or out.split()[2].decode().replace(self.language, "").split('.')[0] != expected_major_version
5353
if mismatched:
5454
raise MisMatchRuntimeError(language=self.language,
55-
found_runtime=self.runtime_path,
5655
required_runtime=self.runtime,
57-
runtime_path=self.runtime_path)
56+
runtime_path=runtime_path)
57+
else:
58+
self._valid_runtime_path = runtime_path
59+
return self._valid_runtime_path
60+
61+
@property
62+
def validated_runtime_path(self):
63+
return self._valid_runtime_path if self._valid_runtime_path is not None else None

aws_lambda_builders/workflows/go_modules/workflow.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from .actions import GoModulesBuildAction
77
from .builder import GoModulesBuilder
8-
from .path_resolver import GoPathResolver
98
from .validator import GoRuntimeValidator
109
from .utils import OSUtils
1110

@@ -43,13 +42,10 @@ def __init__(self,
4342

4443
output_path = osutils.joinpath(artifacts_dir, handler)
4544

46-
builder = GoModulesBuilder(osutils, runtime_path=self.get_executable())
45+
builder = GoModulesBuilder(osutils, binaries=self.binaries)
4746
self.actions = [
4847
GoModulesBuildAction(source_dir, output_path, builder),
4948
]
5049

51-
def get_executable(self):
52-
return GoPathResolver(runtime=self.runtime).exec_path
53-
54-
def get_validator(self):
55-
return GoRuntimeValidator(runtime=self.runtime, runtime_path=self.get_executable())
50+
def get_validators(self):
51+
return [GoRuntimeValidator(runtime=self.runtime)]

tests/unit/workflows/go_modules/test_builder.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from unittest import TestCase
2-
from mock import patch
32

3+
from mock import patch, Mock
4+
5+
from aws_lambda_builders.binary_path import BinaryPath
46
from aws_lambda_builders.workflows.go_modules.builder import GoModulesBuilder, BuilderError
57

68

@@ -22,14 +24,18 @@ def setUp(self, OSUtilMock):
2224
self.osutils.pipe = 'PIPE'
2325
self.popen = FakePopen()
2426
self.osutils.popen.side_effect = [self.popen]
25-
self.under_test = GoModulesBuilder(self.osutils, "go")
27+
self.binaries = {
28+
"go": BinaryPath(resolver=Mock(), validator=Mock(),
29+
binary="go", binary_path="/path/to/go")
30+
}
31+
self.under_test = GoModulesBuilder(self.osutils, self.binaries)
2632

2733
def test_run_executes_bundler_on_nixes(self):
2834
self.osutils.is_windows.side_effect = [False]
29-
self.under_test = GoModulesBuilder(self.osutils, "go")
35+
self.under_test = GoModulesBuilder(self.osutils, self.binaries)
3036
self.under_test.build("source_dir", "output_path")
3137
self.osutils.popen.assert_called_with(
32-
["go", "build", "-o", "output_path", "source_dir"],
38+
["/path/to/go", "build", "-o", "output_path", "source_dir"],
3339
cwd="source_dir",
3440
env={'GOOS': 'linux', 'GOARCH': 'amd64'},
3541
stderr='PIPE',

tests/unit/workflows/go_modules/test_path_resolver.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/unit/workflows/go_modules/test_validator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ def communicate(self):
2121
class TestGoRuntimeValidator(TestCase):
2222

2323
def setUp(self):
24-
self.validator = GoRuntimeValidator(runtime="go1.x", runtime_path="/usr/bin/go")
24+
self.validator = GoRuntimeValidator(runtime="go1.x")
2525

2626
@parameterized.expand([
27-
("go1.x", "/usr/bin/go"),
27+
"go1.x",
2828
])
29-
def test_supported_runtimes(self, runtime, runtime_path):
30-
validator = GoRuntimeValidator(runtime=runtime, runtime_path=runtime_path)
29+
def test_supported_runtimes(self, runtime):
30+
validator = GoRuntimeValidator(runtime=runtime)
3131
self.assertTrue(validator.has_runtime())
3232

3333
def test_runtime_validate_unsupported_language_fail_open(self):
34-
validator = GoRuntimeValidator(runtime='go2.x', runtime_path='/usr/bin/go2')
35-
validator.validate_runtime()
34+
validator = GoRuntimeValidator(runtime='go2.x')
35+
validator.validate(runtime_path='/usr/bin/go2')
3636

3737
def test_runtime_validate_supported_version_runtime(self):
3838
with mock.patch('subprocess.Popen') as mock_subprocess:
3939
mock_subprocess.return_value = MockSubProcess(0, out='go version go1.11.2 test')
40-
self.validator.validate_runtime()
40+
self.validator.validate(runtime_path='/usr/bin/go')
4141
self.assertTrue(mock_subprocess.call_count, 1)
4242

4343
def test_runtime_validate_mismatch_version_runtime(self):
4444
with mock.patch('subprocess.Popen') as mock_subprocess:
4545
mock_subprocess.return_value = MockSubProcess(1)
4646
with self.assertRaises(MisMatchRuntimeError):
47-
self.validator.validate_runtime()
47+
self.validator.validate(runtime_path='/usr/bin/go')
4848
self.assertTrue(mock_subprocess.call_count, 1)

0 commit comments

Comments
 (0)