Skip to content

Commit

Permalink
fixup! fixup! [wip] Add tests using AWS' SAM CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed Apr 11, 2024
1 parent 2534389 commit cb22ffe
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 75 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/maze-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ jobs:
ruby-version: '3.3'
bundler-cache: true

- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- run: bundle exec maze-runner --no-source
env:
PYTHON_TEST_VERSION: ${{ matrix.python-version }}
49 changes: 20 additions & 29 deletions features/aws-lambda/handled.feature
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
Feature: Handled exceptions in AWS Lambda

@not-python-3.5
@not-python-3.6
@not-python-3.7
@not-python-3.8
@not-python-3.10
@not-python-3.11
@not-python-3.12
# 3.9 is currently the minimum python version with a lambda runtime
@not-python-3.5 @not-python-3.6 @not-python-3.7 @not-python-3.8
Scenario: Handled exceptions are delivered in an AWS Lambda app
Given I build the "HelloWorldFunction" lambda in "features/fixtures/aws-lambda"
And I invoke the "HelloWorldFunction" lambda in "features/fixtures/aws-lambda" with the "events/event.json" event
Then the lambda response "body.message" equals "Did not crash!"
And the lambda response "statusCode" equals 200
And the SAM exit code equals 0
When I wait to receive an error
Then the error is valid for the error reporting API version "4.0" for the "Python Bugsnag Notifier" notifier
And the event "unhandled" is false
And the event "severity" equals "warning"
And the event "severityReason.type" equals "handledException"
And the exception "errorClass" equals "Exception"
And the exception "message" equals "hello there"
And the exception "type" equals "python"
And the "file" of stack frame 0 equals "app.py"
And the event "metaData.AWS Lambda Context.function_name" equals "HelloWorldFunction"
And the event "metaData.AWS Lambda Context.aws_request_id" is not null
And the event "metaData.AWS Lambda Event.path" equals "/hello"
And the event "metaData.AWS Lambda Event.httpMethod" equals "GET"
When I wait to receive a session
Then the session is valid for the session reporting API version "4.0" for the "Python Bugsnag Notifier" notifier
And the session payload has a valid sessions array
And the sessionCount "sessionsStarted" equals 1
Given I run the "HelloWorldFunction" lambda
When I wait to receive an error
Then the error is valid for the error reporting API version "4.0" for the "Python Bugsnag Notifier" notifier
And the event "unhandled" is false
And the event "severity" equals "warning"
And the event "severityReason.type" equals "handledException"
And the exception "errorClass" equals "Exception"
And the exception "message" equals "hello there"
And the exception "type" equals "python"
And the "file" of stack frame 0 equals "handled.py"
And the event "metaData.AWS Lambda Context.function_name" equals "HelloWorldFunction"
And the event "metaData.AWS Lambda Context.aws_request_id" is not null
And the event "metaData.AWS Lambda Event.path" equals "/hello"
And the event "metaData.AWS Lambda Event.httpMethod" equals "GET"
When I wait to receive a session
Then the session is valid for the session reporting API version "4.0" for the "Python Bugsnag Notifier" notifier
And the session payload has a valid sessions array
And the sessionCount "sessionsStarted" equals 1
10 changes: 10 additions & 0 deletions features/fixtures/aws-lambda/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ARG PYTHON_TEST_VERSION
FROM python:$PYTHON_TEST_VERSION

# install the SAM CLI
ENV SAM_CLI_TELEMETRY=0
RUN pip install --upgrade pip && pip install aws-sam-cli

COPY temp-bugsnag-python/ /usr/src/bugsnag

WORKDIR /usr/src/app
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


@bugsnag.aws_lambda_handler
def lambda_handler(event, context):
def handler(event, context):
bugsnag.notify(Exception("hello there"))

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/src/bugsnag
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ Description: app
Globals:
Function:
Timeout: 30
MemorySize: 128
Environment:
Variables:
BUGSNAG_API_KEY:
BUGSNAG_ERROR_ENDPOINT:
BUGSNAG_SESSION_ENDPOINT:

Parameters:
Runtime:
Type: String

Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.9
Handler: handled.handler
Runtime: !Ref Runtime
Events:
HelloWorld:
Type: Api
Expand Down
Empty file.
1 change: 0 additions & 1 deletion features/fixtures/aws-lambda/hello_world/requirements.txt

This file was deleted.

31 changes: 0 additions & 31 deletions features/fixtures/aws-lambda/samconfig.toml

This file was deleted.

15 changes: 15 additions & 0 deletions features/fixtures/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,18 @@ services:
- BUGSNAG_SESSION_ENDPOINT
extra_hosts:
- "host.docker.internal:host-gateway"

aws-lambda:
build:
context: aws-lambda
args:
- PYTHON_TEST_VERSION
environment:
- BUGSNAG_API_KEY
- BUGSNAG_ERROR_ENDPOINT
- BUGSNAG_SESSION_ENDPOINT
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
- "./aws-lambda/app:/usr/src/app"
25 changes: 21 additions & 4 deletions features/steps/steps.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
Given("I build the {string} lambda in {string}") do |name, path|
command = "sam build #{name}"
# PYTHON_TEST_VERSION is defined in env.rb
PARAMETER_OVERRIDES = "--parameter-overrides ParameterKey=Runtime,ParameterValue=python#{PYTHON_TEST_VERSION}"

_, exit_code = Maze::Runner.run_command("cd #{path} && #{command}")
Given("I build the {string} lambda") do |lambda_name|
step(%Q{I run the service "aws-lambda" with the command "sam build #{lambda_name} #{PARAMETER_OVERRIDES}"})
end

Given("I invoke the {string} lambda") do |lambda_name|
command = [
"sam local invoke #{lambda_name}",
"--container-host host.docker.internal",
"--docker-volume-basedir $PWD/features/fixtures/aws-lambda/app/.aws-sam/build",
PARAMETER_OVERRIDES,
]

step(%Q{I run the service "aws-lambda" with the command "#{command.join(" ")}"})
end

raise "Unable to build #{name}" unless exit_code.zero?
Given("I run the {string} lambda") do |lambda_name|
steps("
Given I build the '#{lambda_name}' lambda
And I invoke the '#{lambda_name}' lambda
")
end
4 changes: 2 additions & 2 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "fileutils"

PYTHON_TEST_VERSION = ENV.fetch("PYTHON_TEST_VERSION")

Maze.hooks.before_all do
# log to console, not the filesystem
Maze.config.file_log = false
Expand Down Expand Up @@ -45,8 +47,6 @@
Maze::Runner.environment["BUGSNAG_SESSION_ENDPOINT"] = "http://#{host}:#{Maze.config.port}/sessions"
end

PYTHON_TEST_VERSION = ENV.fetch("PYTHON_TEST_VERSION")

5.upto(100) do |minor_version|
Before("@not-python-3.#{minor_version}") do
skip_this_scenario if PYTHON_TEST_VERSION == "3.#{minor_version}"
Expand Down

0 comments on commit cb22ffe

Please sign in to comment.