Skip to content

Commit

Permalink
test: test building npm and Typescript projects using external manife…
Browse files Browse the repository at this point in the history
…st file. (aws#5283)

* test: test building npm and Typescript projects using external manifest file.

* fix mypy issues

* remove node 12.x, and add the new node versions

* run make format
  • Loading branch information
moelasmar authored and lucashuy committed Jun 22, 2023
1 parent 3c1b6dd commit 59ffe93
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 4 deletions.
24 changes: 20 additions & 4 deletions tests/integration/buildcmd/build_integ_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,18 @@ class BuildIntegEsbuildBase(BuildIntegBase):
FUNCTION_LOGICAL_ID = "Function"
# Everything should be minifed to one line and a second line for the sourcemap mapping
MAX_MINIFIED_LINE_COUNT = 2
MANIFEST_PATH: Optional[str] = None

def _test_with_default_package_json(
self, runtime, use_container, code_uri, expected_files, handler, architecture=None, build_in_source=None
):
overrides = self.get_override(runtime, code_uri, architecture, handler)
manifest_path = str(Path(self.test_data_path, self.MANIFEST_PATH)) if self.MANIFEST_PATH else None
cmdlist = self.get_command_list(
use_container=use_container, parameter_overrides=overrides, build_in_source=build_in_source
use_container=use_container,
parameter_overrides=overrides,
build_in_source=build_in_source,
manifest_path=manifest_path,
)

LOG.info("Running Command: {}".format(cmdlist))
Expand Down Expand Up @@ -420,12 +425,17 @@ def _verify_built_artifact(self, build_dir, function_logical_id, expected_files)
class BuildIntegNodeBase(BuildIntegBase):
EXPECTED_FILES_PROJECT_MANIFEST = {"node_modules", "main.js"}
EXPECTED_NODE_MODULES = {"minimal-request-promise"}

CODE_URI = "Node"
FUNCTION_LOGICAL_ID = "Function"
TEST_INVOKE = False
MANIFEST_PATH: Optional[str] = None

def _test_with_default_package_json(self, runtime, use_container, relative_path, architecture=None):
overrides = self.get_override(runtime, "Node", architecture, "ignored")
cmdlist = self.get_command_list(use_container=use_container, parameter_overrides=overrides)
overrides = self.get_override(runtime, self.CODE_URI, architecture, "main.lambdaHandler")
manifest_path = str(Path(self.test_data_path, self.MANIFEST_PATH)) if self.MANIFEST_PATH else None
cmdlist = self.get_command_list(
use_container=use_container, parameter_overrides=overrides, manifest_path=manifest_path
)

LOG.info("Running Command: {}".format(cmdlist))
run_command(cmdlist, cwd=self.working_dir)
Expand All @@ -437,6 +447,12 @@ def _test_with_default_package_json(self, runtime, use_container, relative_path,
self.EXPECTED_NODE_MODULES,
)

expected = {"body": '{"message":"hello world!"}', "statusCode": 200}
if not SKIP_DOCKER_TESTS and self.TEST_INVOKE:
self._verify_invoke_built_function(
self.built_template, self.FUNCTION_LOGICAL_ID, self._make_parameter_override_arg(overrides), expected
)

self._verify_resource_property(
str(self.built_template),
"OtherRelativePathResource",
Expand Down
82 changes: 82 additions & 0 deletions tests/integration/buildcmd/test_build_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,23 @@ def test_building_default_package_json(self, runtime, use_container):
self._test_with_default_package_json(runtime, use_container, self.test_data_path)


class TestBuildCommand_NodeFunctions_With_External_Manifest(BuildIntegNodeBase):
CODE_URI = "Node_without_manifest"
TEST_INVOKE = True
MANIFEST_PATH = "npm_manifest/package.json"

@parameterized.expand(
[
("nodejs14.x",),
("nodejs16.x",),
("nodejs18.x",),
]
)
@pytest.mark.flaky(reruns=3)
def test_building_default_package_json(self, runtime):
self._test_with_default_package_json(runtime, False, self.test_data_path)


class TestBuildCommand_EsbuildFunctions(BuildIntegEsbuildBase):
template = "template_with_metadata_esbuild.yaml"

Expand Down Expand Up @@ -608,6 +625,71 @@ def test_building_default_package_json(
self._test_with_default_package_json(runtime, use_container, code_uri, expected_files, handler, architecture)


class TestBuildCommand_EsbuildFunctions_With_External_Manifest(BuildIntegEsbuildBase):
template = "template_with_metadata_esbuild.yaml"
MANIFEST_PATH = "Esbuild/npm_manifest/package.json"

@parameterized.expand(
[
(
"nodejs14.x",
"Esbuild/Node_without_manifest",
{"main.js", "main.js.map"},
"main.lambdaHandler",
False,
"x86_64",
),
(
"nodejs16.x",
"Esbuild/Node_without_manifest",
{"main.js", "main.js.map"},
"main.lambdaHandler",
False,
"arm64",
),
(
"nodejs18.x",
"Esbuild/Node_without_manifest",
{"main.js", "main.js.map"},
"main.lambdaHandler",
False,
"arm64",
),
(
"nodejs14.x",
"Esbuild/TypeScript_without_manifest",
{"app.js", "app.js.map"},
"app.lambdaHandler",
False,
"x86_64",
),
(
"nodejs16.x",
"Esbuild/TypeScript_without_manifest",
{"app.js", "app.js.map"},
"app.lambdaHandler",
False,
"arm64",
),
(
"nodejs18.x",
"Esbuild/TypeScript_without_manifest",
{"app.js", "app.js.map"},
"app.lambdaHandler",
False,
"arm64",
),
]
)
@pytest.mark.flaky(reruns=3)
def test_building_default_package_json(
self, runtime, code_uri, expected_files, handler, use_container, architecture
):
if use_container and (SKIP_DOCKER_TESTS or SKIP_DOCKER_BUILD):
self.skipTest(SKIP_DOCKER_MESSAGE)
self._test_with_default_package_json(runtime, use_container, code_uri, expected_files, handler, architecture)


@skipIf(
((IS_WINDOWS and RUNNING_ON_CI) and not CI_OVERRIDE),
"Skip build tests on windows when running in CI unless overridden",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.lambdaHandler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'hello world!',
}),
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
let response: APIGatewayProxyResult;

try {
response = {
statusCode: 200,
body: JSON.stringify({
message: 'hello world!',
}),
};
} catch (err) {
console.log(err);
response = {
statusCode: 500,
body: JSON.stringify({
message: 'some error happened',
}),
};
}

return response;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
let response: APIGatewayProxyResult;

try {
response = {
statusCode: 200,
body: JSON.stringify({
message: 'hello world!',
}),
};
} catch (err) {
console.log(err);
response = {
statusCode: 500,
body: JSON.stringify({
message: 'some error happened',
}),
};
}

return response;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "npmdeps",
"version": "1.0.0",
"description": "",
"keywords": [],
"author": "",
"license": "APACHE2.0",
"main": "main.js",
"dependencies": {
"@types/aws-lambda": "^8.10.92",
"minimal-request-promise": "*",
"esbuild": "^0.14.14"
}
}
13 changes: 13 additions & 0 deletions tests/integration/testdata/buildcmd/Node_without_manifest/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

exports.lambdaHandler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'hello world!',
}),
}
};

exports.secondLambdaHandler = async (event, context) => {
return 'Hello Mars'
};
11 changes: 11 additions & 0 deletions tests/integration/testdata/buildcmd/npm_manifest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "npmdeps",
"version": "1.0.0",
"description": "",
"keywords": [],
"author": "",
"license": "APACHE2.0",
"dependencies": {
"minimal-request-promise": "*"
}
}

0 comments on commit 59ffe93

Please sign in to comment.