Skip to content

Commit a41522e

Browse files
author
AntoniaSzecsi
committed
Fix integration tests to be compatible with poetry and change the linter to ruff
1 parent 7baa894 commit a41522e

File tree

13 files changed

+543
-89
lines changed

13 files changed

+543
-89
lines changed

.pre-commit-config.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
repos:
2-
- repo: https://github.com/python/black
3-
rev: 19.3b0
2+
- repo: local
43
hooks:
5-
- id: black
6-
language_version: python3.9
7-
exclude_types: ['markdown', 'ini', 'toml', 'rst']
4+
- id: ruff-check
5+
name: ruff
6+
entry: poetry run ruff check --fix
7+
language: system
8+
types: [python]
9+
- id: ruff-format
10+
name: ruff format
11+
entry: poetry run ruff format
12+
language: system
13+
types: [python]

Makefile

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,28 @@ target:
55

66
.PHONY: init
77
init:
8-
python scripts/dev.py init
8+
python3 scripts/dev.py init
99

1010
.PHONY: test
1111
test:
12-
python scripts/dev.py test
12+
python3 scripts/dev.py test
1313

1414
.PHONY: lint
1515
lint:
16-
python scripts/dev.py lint
16+
python3 scripts/dev.py lint
1717

1818
.PHONY: clean
1919
clean:
20-
python scripts/dev.py clean
20+
python3 scripts/dev.py clean
2121

2222
.PHONY: build
2323
build: clean
2424
ifeq ($(shell uname),Linux)
25-
BUILD=true python scripts/dev.py build
25+
BUILD=true python3 scripts/dev.py build
2626
else
27-
python scripts/dev.py build
27+
python3 scripts/dev.py build
2828
endif
2929

30-
.PHONY: local-test
31-
local-test:
32-
python scripts/dev.py local-test
33-
3430
.PHONY: setup-codebuild-agent
3531
setup-codebuild-agent:
3632
docker build -t codebuild-agent - < tests/integration/codebuild-local/Dockerfile.agent
@@ -81,6 +77,4 @@ TARGETS
8177
lint Run all linters via scripts/dev.py.
8278
test-smoke Run smoke tests inside Docker.
8379
test-integ Run all integration tests.
84-
local-test Run the Lambda handler locally using AWS RIE.
85-
8680
endef

poetry.lock

Lines changed: 350 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,48 @@ coverage = ">=4.4.0"
1717
pytest = ">=3.0.7"
1818
mock = ">=2.0.0"
1919
parameterized = ">=0.9.0"
20+
ruff = "^0.1.0"
21+
bandit = "^1.7.5"
22+
pre-commit = "^3.0.0"
23+
24+
# Development scripts
25+
[tool.poetry.scripts]
26+
init = "scripts.dev:init"
27+
test = "scripts.dev:test"
28+
lint = "scripts.dev:lint"
29+
format = "scripts.dev:format_code"
30+
clean = "scripts.dev:clean"
31+
build = "scripts.dev:build"
32+
local-test = "scripts.dev:local_test"
2033

2134
[build-system]
2235
requires = ["poetry-core>=2.0.0,<3.0.0", "setuptools>=68", "wheel"]
2336
build-backend = "poetry.core.masonry.api"
37+
38+
# Ruff configuration
39+
[tool.ruff]
40+
target-version = "py39"
41+
line-length = 88
42+
43+
[tool.ruff.lint]
44+
select = [
45+
"E", # pycodestyle errors
46+
"W", # pycodestyle warnings
47+
"F", # pyflakes
48+
"I", # isort
49+
"UP", # pyupgrade
50+
"C90", # mccabe complexity
51+
]
52+
53+
# Ignore rules that are too strict for existing codebases
54+
ignore = [
55+
"E501", # Line too long (handled by formatter)
56+
"PLR0913", # Too many arguments
57+
"E722", # Bare except
58+
"PLW0603", # Global statement
59+
"UP031", # % formatting vs f-strings
60+
"E402", # Module import not at top
61+
]
62+
63+
[tool.ruff.format]
64+
quote-style = "double"

scripts/dev.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
ROOT = Path(__file__).resolve().parent.parent
1010

1111
def run(cmd, check=True, env=None):
12-
print(f"\n$ {' '.join(cmd) if isinstance(cmd, list) else cmd}")
12+
print("\n$ {}".format(' '.join(cmd) if isinstance(cmd, list) else cmd))
1313
result = subprocess.run(cmd, shell=isinstance(cmd, str), check=check, env=env)
1414
if result.returncode != 0 and check:
1515
sys.exit(result.returncode)
@@ -26,51 +26,55 @@ def test():
2626

2727

2828
def lint():
29-
print("Running lint checks")
30-
run(["poetry", "run", "flake8", "awslambdaric"])
31-
run(["poetry", "run", "pylint", "awslambdaric"])
32-
run(["poetry", "run", "black", "--check", "awslambdaric"])
33-
run(["poetry", "run", "bandit", "-r", "awslambdaric"])
29+
print("Running linters")
30+
run(["poetry", "run", "ruff", "check", "awslambdaric/", "tests/"])
31+
32+
33+
def format_code():
34+
print("Formatting code")
35+
run(["poetry", "run", "black", "awslambdaric/", "tests/"])
3436

3537

3638
def clean():
37-
print("Cleaning build and cache files")
38-
patterns = ["__pycache__", "*.pyc", "*.pyo", ".pytest_cache", ".mypy_cache"]
39-
for pattern in patterns:
40-
for path in ROOT.rglob(pattern):
39+
print("Cleaning build artifacts")
40+
dirs_to_remove = ["build", "dist", "*.egg-info"]
41+
for pattern in dirs_to_remove:
42+
for path in ROOT.glob(pattern):
4143
if path.is_dir():
4244
shutil.rmtree(path)
45+
print("Removed directory: {}".format(path))
4346
elif path.is_file():
4447
path.unlink()
48+
print("Removed file: {}".format(path))
4549

46-
for folder in ["dist", "build", "awslambdaric.egg-info"]:
47-
dir_path = ROOT / folder
48-
if dir_path.exists():
49-
shutil.rmtree(dir_path)
5050

5151
def build():
5252
print("Building package")
5353
env = os.environ.copy()
54-
if sys.platform.startswith("linux"):
55-
env["BUILD"] = "true"
54+
env["BUILD"] = "true"
5655
run([sys.executable, "setup.py", "sdist", "bdist_wheel"], env=env)
5756

58-
def local_test():
59-
print("Running local tests")
60-
# will be implemented later using RIE
6157

62-
if __name__ == "__main__":
63-
parser = argparse.ArgumentParser(description="Development command-line tool")
64-
parser.add_argument("command", choices=["init", "test", "lint", "clean", "build", "local-test"])
58+
def main():
59+
parser = argparse.ArgumentParser(description="Development scripts")
60+
parser.add_argument("command", choices=[
61+
"init", "test", "lint", "format", "clean", "build"
62+
])
63+
6564
args = parser.parse_args()
66-
commands = {
67-
"init": init,
68-
"test": test,
69-
"lint": lint,
70-
"clean": clean,
71-
"build": build,
72-
"local-test": local_test,
73-
}
74-
75-
commands[args.command]()
65+
66+
command_map = {
67+
"init": init,
68+
"test": test,
69+
"lint": lint,
70+
"format": format_code,
71+
"clean": clean,
72+
"build": build,
73+
}
74+
75+
command_map[args.command]()
76+
77+
78+
if __name__ == "__main__":
79+
main()
7680

setup.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
import os
22
import platform
3+
import sys
34
from subprocess import check_call, check_output
4-
from setuptools import Extension, setup
5+
from setuptools import Extension, setup, find_packages
6+
7+
if sys.version_info >= (3, 11):
8+
import tomllib
9+
else:
10+
try:
11+
import tomli as tomllib
12+
except ImportError:
13+
import subprocess
14+
subprocess.check_call([sys.executable, "-m", "pip", "install", "tomli"])
15+
import tomli as tomllib
16+
17+
def get_metadata():
18+
with open("pyproject.toml", "rb") as f:
19+
pyproject = tomllib.load(f)
20+
21+
poetry_config = pyproject["tool"]["poetry"]
22+
return {
23+
"name": poetry_config["name"],
24+
"version": poetry_config["version"],
25+
"description": poetry_config["description"],
26+
"author": poetry_config["authors"][0] if poetry_config["authors"] else "",
27+
"license": poetry_config["license"],
28+
"python_requires": poetry_config["dependencies"]["python"],
29+
"install_requires": [
30+
f"{pkg}{version}" if not version.startswith("^") and not version.startswith("~") else f"{pkg}>={version[1:]}"
31+
for pkg, version in poetry_config["dependencies"].items()
32+
if pkg != "python"
33+
]
34+
}
535

636
def get_curl_extra_linker_flags():
737
if platform.system() != "Linux" or os.getenv("BUILD") != "true":
@@ -25,6 +55,16 @@ def get_runtime_client_extension():
2555
include_dirs=["deps/artifacts/include"],
2656
)]
2757

58+
metadata = get_metadata()
59+
2860
setup(
61+
name=metadata["name"],
62+
version=metadata["version"],
63+
description=metadata["description"],
64+
author=metadata["author"],
65+
license=metadata["license"],
66+
packages=find_packages(),
67+
python_requires=metadata["python_requires"],
68+
install_requires=metadata["install_requires"],
2969
ext_modules=get_runtime_client_extension(),
3070
)

tests/.DS_Store

6 KB
Binary file not shown.

tests/integration/.DS_Store

6 KB
Binary file not shown.

tests/integration/docker/Dockerfile.echo.alpine

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ RUN apk add --no-cache \
2222
elfutils-dev \
2323
make \
2424
cmake \
25-
libcurl
25+
libcurl \
26+
curl
2627

2728
# Include global args in this stage of the build
2829
ARG RIC_BUILD_DIR="/home/build/"
@@ -32,8 +33,13 @@ RUN mkdir -p ${RIC_BUILD_DIR}
3233
WORKDIR ${RIC_BUILD_DIR}
3334
COPY . .
3435
RUN pip3 install setuptools
35-
RUN make init build test && \
36-
mv ./dist/awslambdaric-*.tar.gz ./dist/awslambdaric-test.tar.gz
36+
# Install Poetry
37+
RUN curl -sSL https://install.python-poetry.org | python3 - && \
38+
ln -s /root/.local/bin/poetry /usr/local/bin/poetry
39+
40+
RUN make init build
41+
42+
RUN ls -la ./dist/
3743

3844
# Include global args in this stage of the build
3945
ARG FUNCTION_DIR="/home/app/"
@@ -45,7 +51,7 @@ COPY tests/integration/test-handlers/echo/* ${FUNCTION_DIR}
4551
# Install the function's dependencies
4652
WORKDIR ${FUNCTION_DIR}
4753
RUN python${RUNTIME_VERSION} -m pip install \
48-
${RIC_BUILD_DIR}/dist/awslambdaric-test.tar.gz \
54+
${RIC_BUILD_DIR}/dist/*.whl \
4955
--target ${FUNCTION_DIR}
5056

5157

tests/integration/docker/Dockerfile.echo.amazonlinux2

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ RUN yum install -y \
2222
openssl11-devel \
2323
bzip2-devel \
2424
libffi-devel \
25-
sqlite-devel
25+
sqlite-devel \
26+
curl
2627

2728
RUN RUNTIME_LATEST_VERSION=${RUNTIME_VERSION}.$(curl -s https://www.python.org/ftp/python/ | \
2829
grep -oE "href=\"$(echo ${RUNTIME_VERSION} | sed "s/\\./\\\./g")\.[0-9]+" | \
@@ -72,6 +73,9 @@ RUN wget --quiet -O cmake-install https://github.com/Kitware/CMake/releases/down
7273
ENV PATH=/usr/local/bin:$PATH
7374
ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
7475

76+
# Create symlinks for consistent python usage
77+
RUN ln -sf /usr/local/bin/python${RUNTIME_VERSION} /usr/local/bin/python3 && \
78+
ln -sf /usr/local/bin/python${RUNTIME_VERSION} /usr/local/bin/python
7579

7680
# Include global args in this stage of the build
7781
ARG RIC_BUILD_DIR="/home/build/"
@@ -81,24 +85,29 @@ RUN mkdir -p ${RIC_BUILD_DIR}
8185
WORKDIR ${RIC_BUILD_DIR}
8286
COPY . .
8387

84-
RUN make init build test && \
85-
mv ./dist/awslambdaric-*.tar.gz ./dist/awslambdaric-test.tar.gz
88+
# Install wheel and poetry using the correct python version
89+
RUN /usr/local/bin/python${RUNTIME_VERSION} -m pip install wheel poetry
90+
91+
# Configure poetry to use the correct python version
92+
RUN poetry config virtualenvs.create false
93+
RUN poetry env use /usr/local/bin/python${RUNTIME_VERSION}
94+
95+
RUN make init build
96+
97+
RUN ls -la ./dist/
8698

8799
# Include global args in this stage of the build
88100
ARG FUNCTION_DIR="/home/app/"
89101
# Create function directory
90102
RUN mkdir -p ${FUNCTION_DIR}
91103
# Copy function code
92104
COPY tests/integration/test-handlers/echo/* ${FUNCTION_DIR}
93-
# Copy Runtime Interface Client .tgz
94-
RUN cp ./dist/awslambdaric-test.tar.gz ${FUNCTION_DIR}/awslambdaric-test.tar.gz
95105

96106
# Install the function's dependencies
97107
WORKDIR ${FUNCTION_DIR}
98108
RUN python${RUNTIME_VERSION} -m pip install \
99-
awslambdaric-test.tar.gz \
100-
--target ${FUNCTION_DIR} && \
101-
rm awslambdaric-test.tar.gz
109+
${RIC_BUILD_DIR}/dist/*.whl \
110+
--target ${FUNCTION_DIR}
102111

103112

104113
# Stage 4 - final runtime interface client image

0 commit comments

Comments
 (0)