diff --git a/.gitignore b/.gitignore index adee324..22826fd 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,5 @@ reports/ # virtualenv venv/ ENV/ + +aws-lambda-rie diff --git a/Dockerfile b/Dockerfile index 8c99c9e..e3eac89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,7 +37,7 @@ RUN export MAKEFLAGS="-j$(nproc)" && \ # Install our app COPY app.py /app -# Ser version to our TAG +# Set internal __version__ to our own container TAG RUN sed -i -e "s/^__version__ =.*/__version__ = \"${TAG}\"/" /app/app.py # Stage 3 - final runtime image diff --git a/Dockerfile.test b/Dockerfile.test deleted file mode 100644 index 3315010..0000000 --- a/Dockerfile.test +++ /dev/null @@ -1,26 +0,0 @@ -FROM setviacmdline:latest - -# Install additional tools for tests -COPY dev-requirements.txt .flake8 . -RUN export MAKEFLAGS="-j$(nproc)" && \ - pip install -r dev-requirements.txt - -# Unit Tests / Static / Style etc. -COPY tests/ tests/ -RUN flake8 app.py tests && \ - codespell app.py tests - -# Get aws-lambda run time emulator -ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/local/bin/aws-lambda-rie -RUN chmod 0755 /usr/local/bin/aws-lambda-rie && \ - mkdir -p tests - -# Install pytest -RUN pip install pytest --target /app - -# Add our tests -ADD tests /app/tests - -# Run tests -ENTRYPOINT [] -CMD /usr/local/bin/python -m pytest tests -c tests/pytest.ini --capture=tee-sys diff --git a/Makefile b/Makefile index ab67e12..a1e299b 100644 --- a/Makefile +++ b/Makefile @@ -3,3 +3,21 @@ IMAGE := sns-alert-hub REGION := us-east-1 include .ci/podman.mk + +SOURCE := app.py tests/test_aws-lambda-rie.py + +test:: aws-lambda-rie + ./run_tests.sh "$(IMAGE):$(TAG)-$(_ARCH)" + +fmt:: + autopep8 -i -a $(SOURCE) + +lint:: + flake8 $(SOURCE) + codespell $(SOURCE) + +clean:: + rm -rf .pytest_cache __pycache__ aws-lambda-rie + +aws-lambda-rie: + wget https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && chmod 0755 aws-lambda-rie diff --git a/app.py b/app.py index c568d8f..1738ec7 100644 --- a/app.py +++ b/app.py @@ -106,7 +106,8 @@ def handler(event, context): if "AlarmName" in msg: title = "AWS Cloudwatch Alarm" - # Discard NewStateValue == OK && OldStateValue == INSUFFICIENT_DATA as these are triggered by installing new Alarms and only cause confusion + # Discard NewStateValue == OK && OldStateValue == INSUFFICIENT_DATA as + # these are triggered by installing new Alarms and only cause confusion if msg["NewStateValue"] == "OK" and msg["OldStateValue"] == "INSUFFICIENT_DATA": logger.info( "Discarding Cloudwatch Metrics Alarm as state is OK and previous state was insufficient data, most likely new alarm being installed" diff --git a/SNSAlertHub.yaml b/deploy/SNSAlertHub.yaml similarity index 100% rename from SNSAlertHub.yaml rename to deploy/SNSAlertHub.yaml diff --git a/dev-requirements.txt b/dev-requirements.txt index 451c559..2fa2719 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,3 +1,4 @@ pytest +autopep8 flake8 codespell diff --git a/ecr_public_lifecycle.py b/ecr_public_lifecycle.py deleted file mode 100755 index c5bf73f..0000000 --- a/ecr_public_lifecycle.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import boto3 - -parser = argparse.ArgumentParser(description='Implement basic public ECR lifecycle policy') -parser.add_argument('--repo', dest='repositoryName', action='store', required=True, - help='Name of the public ECR repository') -parser.add_argument('--keep', dest='keep', action='store', default=10, type=int, - help='number of tagged images to keep, default 10') -parser.add_argument('--dev', dest='delete_dev', action='store_true', - help='also delete in-development images only having tags like v0.1.1-commitNr-githash') - -args = parser.parse_args() - -client = boto3.client('ecr-public', region_name='us-east-1') - -images = client.describe_images(repositoryName=args.repositoryName)["imageDetails"] - -untagged = [] -kept = 0 - -# actual Image -# imageManifestMediaType: 'application/vnd.oci.image.manifest.v1+json' -# image Index -# imageManifestMediaType: 'application/vnd.oci.image.index.v1+json' - -# Sort by date uploaded -for image in sorted(images, key=lambda d: d['imagePushedAt'], reverse=True): - # Remove all untagged - # if registry uses image index all actual images will be untagged anyways - if 'imageTags' not in image: - untagged.append({"imageDigest": image['imageDigest']}) - #print("Delete untagged image {}".format(image["imageDigest"])) - continue - - # check for dev tags - if args.delete_dev: - _delete=True - for tag in image["imageTags"]: - # Look for at least one tag NOT beign a SemVer dev tag - if "-" not in tag: - _delete=False - if _delete: - print("Deleting development image {}".format(image["imageTags"])) - untagged.append({"imageDigest": image['imageDigest']}) - continue - - if kept < args.keep: - kept=kept+1 - print("Keeping tagged image {}".format(image["imageTags"])) - continue - else: - print("Deleting tagged image {}".format(image["imageTags"])) - untagged.append({"imageDigest": image['imageDigest']}) - -deleted_images = client.batch_delete_image(repositoryName=args.repositoryName, imageIds=untagged) - -if deleted_images["imageIds"]: - print("Deleted images: {}".format(deleted_images["imageIds"])) diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 0000000..fb40eca --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,17 @@ +#!/bin/sh -ex + +IMAGE=$1 + +ctr=$(buildah from $IMAGE) +trap "buildah rm $ctr" EXIT + +buildah copy $ctr dev-requirements.txt .flake8 . +buildah copy $ctr aws-lambda-rie +buildah copy $ctr tests/ tests/ + +buildah run $ctr pip install -r dev-requirements.txt --target . + +buildah run $ctr python -m flake8 app.py +buildah run $ctr python -m codespell_lib app.py + +buildah run $ctr python -m pytest tests -c tests/pytest.ini --capture=tee-sys diff --git a/tests/test_aws-lambda-rie.py b/tests/test_aws-lambda-rie.py index 6b2b9cf..ca1a1fb 100755 --- a/tests/test_aws-lambda-rie.py +++ b/tests/test_aws-lambda-rie.py @@ -18,7 +18,7 @@ class Test: @classmethod def setup_class(cls): cls.p = subprocess.Popen( - "aws-lambda-rie python -m awslambdaric app.handler", shell=True + "./aws-lambda-rie python -m awslambdaric app.handler", shell=True ) @classmethod