Skip to content

Commit

Permalink
Add Dockerfiles to allow tests to be run in a lambda build container
Browse files Browse the repository at this point in the history
  • Loading branch information
kolanos authored and pselle committed Aug 17, 2017
1 parent a7187ed commit 38ac6eb
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 62 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
@@ -0,0 +1,3 @@
.gitignore
.git
README.md
12 changes: 12 additions & 0 deletions Dockerfile.python2
@@ -0,0 +1,12 @@
FROM lambci/lambda:build-python2.7

RUN mkdir -p /var/lib/iopipe

WORKDIR /var/lib/iopipe

RUN pip install pytest

COPY . /var/lib/iopipe

RUN python setup.py install
RUN pytest
12 changes: 12 additions & 0 deletions Dockerfile.python3
@@ -0,0 +1,12 @@
FROM lambci/lambda:build-python3.6

RUN mkdir -p /var/lib/iopipe

WORKDIR /var/lib/iopipe

RUN pip install pytest

COPY . /var/lib/iopipe

RUN python setup.py install
RUN pytest
11 changes: 11 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,11 @@
version: '2'
services:
python2:
build:
context: .
dockerfile: Dockerfile.python2

python3:
build:
context: .
dockerfile: Dockerfile.python3
154 changes: 92 additions & 62 deletions iopipe/tests/test_agent.py
@@ -1,104 +1,134 @@
import time

import pytest

from iopipe.iopipe import IOpipe

from .MockContext import MockContext

global advancedUsage
global advancedUsageErr

iopipe = IOpipe('test-suite', 'https://metrics-api.iopipe.com', True)
context = MockContext('handler', '$LATEST')
@pytest.fixture
def iopipe():
return IOpipe('test-suite', 'https://metrics-api.iopipe.com', True)


@iopipe.decorator
def handler(event, context):
pass
@pytest.fixture
def mock_context():
return MockContext('handler', '$LATEST')


@iopipe.decorator
def handlerWithEvents(event, context):
iopipe.log('somekey', 2)
iopipe.log('anotherkey', 'qualitative value')
@pytest.fixture
def handler(iopipe):
@iopipe.decorator
def _handler(event, context):
pass
return iopipe, _handler


@iopipe.decorator
def handlerThatErrors(event, context):
raise ValueError("Behold, a value error")
@pytest.fixture
def handler_with_events(iopipe):
@iopipe.decorator
def _handler_with_events(event, context):
iopipe.log('somekey', 2)
iopipe.log('anotherkey', 'qualitative value')
return iopipe, _handler_with_events


def setup_function():
handler(None, context)
@pytest.fixture
def handler_that_errors(iopipe):
@iopipe.decorator
def _handler_that_errors(event, context):
raise ValueError("Behold, a value error")
return iopipe, _handler_that_errors


# N.B. this must be the first test!
def test_coldstarts():
def test_coldstarts(handler, mock_context):
iopipe, handler = handler
handler(None, mock_context)
assert iopipe.report.coldstart
handler(None, context)

handler(None, mock_context)
assert not iopipe.report.coldstart


def test_client_id_is_configured():
def test_client_id_is_configured(handler, mock_context):
iopipe, handler = handler
handler(None, mock_context)
assert iopipe.report.client_id == 'test-suite'


def test_function_name_from_context():
def test_function_name_from_context(handler, mock_context):
iopipe, handler = handler
handler(None, mock_context)
assert iopipe.report.aws['functionName'] == 'handler'


def test_custom_metrics():
handlerWithEvents(None, context)
def test_custom_metrics(handler_with_events, mock_context):
iopipe, handler = handler_with_events
handler(None, mock_context)
assert len(iopipe.report.custom_metrics) == 2


def test_erroring():
def test_erroring(handler_that_errors, mock_context):
iopipe, handler = handler_that_errors
try:
handlerThatErrors(None, context)
handler(None, mock_context)
except:
pass
assert iopipe.report.errors['name'] == 'ValueError'
assert iopipe.report.errors['message'] == 'Behold, a value error'


# Advanced reporting
def advancedHandler(event, context):
# make reference for testing
global advancedUsage
iopipe = IOpipe('test-suite')
advancedUsage = iopipe
timestamp = time.time()
report = iopipe.create_report(timestamp, context)
try:
pass
except Exception as e:
iopipe.err(e)
report.send()


def test_advanced_reporting():
new_context = MockContext('advancedHandler', '1')
advancedHandler(None, new_context)
assert(advancedUsage.report.aws['functionName']) == 'advancedHandler'


def advancedHandlerWithErr(event, context):
global advancedUsageErr
iopipe = IOpipe('test-suite2')
advancedUsageErr = iopipe
timestamp = time.time()
report = iopipe.create_report(timestamp, context)
iopipe.log('name', 'foo')
try:
raise TypeError('Type error raised!')
except Exception as e:
iopipe.err(e)
report.send()


def test_advanced_erroring():
@pytest.fixture
def advanced_handler(iopipe):
mock_context = MockContext('advancedHandler', '1')

@iopipe.decorator
def _advanced_handler(event, context):
nested_iopipe = IOpipe('test-suite')
timestamp = time.time()
report = nested_iopipe.create_report(timestamp, context)
try:
pass
except Exception as e:
nested_iopipe.err(e)
# FIXME: For some reason the finally block isn't called for a nested
report.update_data(context, timestamp)
report.send()
return nested_iopipe

return _advanced_handler(None, mock_context)


def test_advanced_reporting(advanced_handler):
assert advanced_handler.aws['functionName'] == 'advancedHandler'


@pytest.fixture
def advanced_handler_with_error(iopipe):
@iopipe.decorator
def _advanced_handler_with_error(event, context):
nested_iopipe = IOpipe('test-suite2')
timestamp = time.time()
report = nested_iopipe.create_report(timestamp, context)
nested_iopipe.log('name', 'foo')
try:
raise TypeError('Type error raised!')
except Exception as e:
nested_iopipe.err(e)
report.update_data(context, timestamp)
report.send()
return nested_iopipe

return _advanced_handler_with_error


def test_advanced_erroring(advanced_handler_with_error, mock_context):
try:
advancedHandlerWithErr(None, context)
nested_iopipe = advanced_handler_with_error(None, mock_context)
except:
pass
assert advancedUsageErr.report.errors['name'] == 'TypeError'
assert advancedUsageErr.report.errors['message'] == 'Type error raised!'
assert nested_iopipe.report.errors['name'] == 'TypeError'
assert nested_iopipe.report.errors['message'] == 'Type error raised!'

0 comments on commit 38ac6eb

Please sign in to comment.