Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
feat: remove use of moto dynamodb2 from tests
Browse files Browse the repository at this point in the history
Moves the local DynamoDB run to the package level and all tests use
it instead of running moto's incomplete dynamodb mock.

This also fixes a bug in the tests that left autopush.db.key_hash
set after an endpoint test ran.
  • Loading branch information
bbangert committed Jul 31, 2017
1 parent 635583f commit 6efb366
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 138 deletions.
53 changes: 53 additions & 0 deletions autopush/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
import logging
import os
import signal
import subprocess

import boto
import psutil

from autopush.db import create_rotating_message_table

here_dir = os.path.abspath(os.path.dirname(__file__))
root_dir = os.path.dirname(os.path.dirname(here_dir))
ddb_dir = os.path.join(root_dir, "ddb")
ddb_lib_dir = os.path.join(ddb_dir, "DynamoDBLocal_lib")
ddb_jar = os.path.join(ddb_dir, "DynamoDBLocal.jar")
ddb_process = None


def setUp():
logging.getLogger('boto').setLevel(logging.CRITICAL)
boto_path = os.path.join(root_dir, "automock", "boto.cfg")
boto.config.load_from_path(boto_path)
global ddb_process
cmd = " ".join([
"java", "-Djava.library.path=%s" % ddb_lib_dir,
"-jar", ddb_jar, "-sharedDb", "-inMemory"
])
ddb_process = subprocess.Popen(cmd, shell=True, env=os.environ)

# Setup the necessary message tables
message_table = os.environ.get("MESSAGE_TABLE", "message_int_test")
create_rotating_message_table(prefix=message_table, delta=-1)
create_rotating_message_table(prefix=message_table)


def tearDown():
global ddb_process
# This kinda sucks, but its the only way to nuke the child procs
proc = psutil.Process(pid=ddb_process.pid)
child_procs = proc.children(recursive=True)
for p in [proc] + child_procs:
os.kill(p.pid, signal.SIGTERM)
ddb_process.wait()

# Clear out the boto config that was loaded so the rest of the tests run
# fine
for section in boto.config.sections():
boto.config.remove_section(section)


_multiprocess_shared_ = True


class MockAssist(object):
def __init__(self, results):
self.cur = 0
Expand Down
10 changes: 0 additions & 10 deletions autopush/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@
dummy_chid = str(uuid.UUID("deadbeef00000000decafbad00000000"))


def setUp():
from .test_integration import setUp
setUp()


def tearDown():
from .test_integration import tearDown
tearDown()


def make_webpush_notification(uaid, chid, ttl=100):
message_id = str(uuid.uuid4())
return WebPushNotification(
Expand Down
12 changes: 0 additions & 12 deletions autopush/tests/test_diagnostic_cli.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import unittest

from mock import Mock, patch
from moto import mock_dynamodb2
from nose.tools import eq_, ok_


mock_dynamodb2 = mock_dynamodb2()


def setUp():
mock_dynamodb2.start()


def tearDown():
mock_dynamodb2.stop()


class FakeDict(dict):
pass

Expand Down
12 changes: 0 additions & 12 deletions autopush/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
ProvisionedThroughputExceededException,
Message,
ItemNotFound,
create_rotating_message_table,
has_connected_this_month,
)
from autopush.exceptions import RouterException
Expand All @@ -37,17 +36,6 @@
dummy_token = dummy_uaid.hex + ":" + str(dummy_chid)


def setUp():
from .test_integration import setUp
setUp()
create_rotating_message_table()


def tearDown():
from .test_integration import tearDown
tearDown()


class FileConsumer(object): # pragma: no cover
def __init__(self, fileObj):
self.file = fileObj
Expand Down
5 changes: 0 additions & 5 deletions autopush/tests/test_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import twisted.internet.base
from boto.dynamodb2.exceptions import InternalServerError
from mock import Mock
from moto import mock_dynamodb2
from nose.tools import eq_
from twisted.internet.defer import inlineCallbacks
from twisted.logger import globalLogPublisher
Expand All @@ -25,10 +24,6 @@ def setUp(self):
self.timeout = 0.5
twisted.internet.base.DelayedCall.debug = True

self.mock_dynamodb2 = mock_dynamodb2()
self.mock_dynamodb2.start()
self.addCleanup(self.mock_dynamodb2.stop)

settings = AutopushSettings(
hostname="localhost",
statsd_host=None,
Expand Down
39 changes: 0 additions & 39 deletions autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import logging
import os
import random
import signal
import subprocess
import sys
import time
import urlparse
Expand All @@ -18,9 +16,7 @@

from zope.interface import implementer

import boto
import ecdsa
import psutil
import twisted.internet.base
import websocket
from cryptography.fernet import Fernet
Expand All @@ -39,7 +35,6 @@
import autopush.db as db
from autopush import __version__
from autopush.db import (
create_rotating_message_table,
get_month,
has_connected_this_month
)
Expand All @@ -52,48 +47,14 @@
from autopush.websocket import PushServerFactory

log = logging.getLogger(__name__)
here_dir = os.path.abspath(os.path.dirname(__file__))
root_dir = os.path.dirname(os.path.dirname(here_dir))
ddb_dir = os.path.join(root_dir, "ddb")
ddb_lib_dir = os.path.join(ddb_dir, "DynamoDBLocal_lib")
ddb_jar = os.path.join(ddb_dir, "DynamoDBLocal.jar")
ddb_process = None

twisted.internet.base.DelayedCall.debug = True


def setUp():
logging.getLogger('boto').setLevel(logging.CRITICAL)
boto_path = os.path.join(root_dir, "automock", "boto.cfg")
boto.config.load_from_path(boto_path)
if "SKIP_INTEGRATION" in os.environ: # pragma: nocover
raise SkipTest("Skipping integration tests")
global ddb_process
cmd = " ".join([
"java", "-Djava.library.path=%s" % ddb_lib_dir,
"-jar", ddb_jar, "-sharedDb", "-inMemory"
])
ddb_process = subprocess.Popen(cmd, shell=True, env=os.environ)

# Setup the necessary message tables
message_table = os.environ.get("MESSAGE_TABLE", "message_int_test")
create_rotating_message_table(prefix=message_table, delta=-1)
create_rotating_message_table(prefix=message_table)


def tearDown():
global ddb_process
# This kinda sucks, but its the only way to nuke the child procs
proc = psutil.Process(pid=ddb_process.pid)
child_procs = proc.children(recursive=True)
for p in [proc] + child_procs:
os.kill(p.pid, signal.SIGTERM)
ddb_process.wait()

# Clear out the boto config that was loaded so the rest of the tests run
# fine
for section in boto.config.sections():
boto.config.remove_section(section)


def _get_vapid(key=None, payload=None):
Expand Down
12 changes: 2 additions & 10 deletions autopush/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json

from mock import Mock, patch
from moto import mock_dynamodb2
from nose.tools import (
assert_raises,
eq_,
Expand All @@ -14,6 +13,7 @@
import hyper
import hyper.tls

import autopush.db
from autopush.db import DatabaseManager, get_rotating_message_table
from autopush.exceptions import InvalidSettings
from autopush.http import skip_request_logging
Expand All @@ -27,15 +27,6 @@

connection_main = ConnectionApplication.main
endpoint_main = EndpointApplication.main
mock_dynamodb2 = mock_dynamodb2()


def setUp():
mock_dynamodb2.start()


def tearDown():
mock_dynamodb2.stop()


class SettingsTestCase(unittest.TestCase):
Expand Down Expand Up @@ -292,6 +283,7 @@ def setUp(self):
def tearDown(self):
for mock in self.mocks.values():
mock.stop()
autopush.db.key_hash = ""

def test_basic(self):
endpoint_main([
Expand Down
14 changes: 0 additions & 14 deletions autopush/tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from autopush.utils import WebPushNotification
from mock import Mock, PropertyMock, patch
from moto import mock_dynamodb2
from nose.tools import eq_, ok_, assert_raises
from twisted.trial import unittest
from twisted.internet.error import ConnectError, ConnectionRefusedError
Expand All @@ -24,7 +23,6 @@
Message,
ProvisionedThroughputExceededException,
ItemNotFound,
create_rotating_message_table,
)
from autopush.exceptions import RouterException
from autopush.metrics import SinkMetrics
Expand All @@ -42,18 +40,6 @@
from autopush.web.base import Notification


mock_dynamodb2 = mock_dynamodb2()


def setUp():
mock_dynamodb2.start()
create_rotating_message_table()


def tearDown():
mock_dynamodb2.stop()


class RouterInterfaceTestCase(TestCase):
def test_not_implemented(self):
assert_raises(NotImplementedError, IRouter, None, None)
Expand Down
9 changes: 9 additions & 0 deletions autopush/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def test_trusted_vapid(self):
data = extract_jwt(vapid_info['auth'], 'invalid_key', is_trusted=True)
eq_(data['sub'], 'mailto:foo@example.com')

@patch("requests.get")
def test_get_amid_unknown(self, request_mock):
import requests
from autopush.utils import get_amid

request_mock.side_effect = requests.HTTPError
result = get_amid()
eq_(result, "Unknown")

@patch("requests.get")
def test_get_ec2_instance_id_unknown(self, request_mock):
import requests
Expand Down
12 changes: 0 additions & 12 deletions autopush/tests/test_web_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
from jose import jws
from marshmallow import Schema, fields
from mock import Mock, patch
from moto import mock_dynamodb2
from nose.tools import eq_, ok_, assert_raises
from twisted.internet.defer import inlineCallbacks
from twisted.trial import unittest

from autopush.db import create_rotating_message_table
from autopush.metrics import SinkMetrics
from autopush.exceptions import InvalidRequest, InvalidTokenException
from autopush.tests.support import test_db
Expand All @@ -27,16 +25,6 @@
dummy_uaid = str(uuid.UUID("abad1dea00000000aabbccdd00000000"))
dummy_chid = str(uuid.UUID("deadbeef00000000decafbad00000000"))
dummy_token = dummy_uaid + ":" + dummy_chid
mock_dynamodb2 = mock_dynamodb2()


def setUp():
mock_dynamodb2.start()
create_rotating_message_table()


def tearDown():
mock_dynamodb2.stop()


class InvalidSchema(Schema):
Expand Down
13 changes: 1 addition & 12 deletions autopush/tests/test_web_webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

from cryptography.fernet import Fernet
from mock import Mock
from moto import mock_dynamodb2
from nose.tools import eq_, ok_
from twisted.internet.defer import inlineCallbacks
from twisted.trial import unittest

from autopush.db import Message, create_rotating_message_table
from autopush.db import Message
from autopush.http import EndpointHTTPFactory
from autopush.router.interface import IRouter, RouterResponse
from autopush.settings import AutopushSettings
Expand All @@ -18,16 +17,6 @@
dummy_uaid = str(uuid.UUID("abad1dea00000000aabbccdd00000000"))
dummy_chid = str(uuid.UUID("deadbeef00000000decafbad00000000"))
dummy_token = dummy_uaid + ":" + dummy_chid
mock_dynamodb2 = mock_dynamodb2()


def setUp():
mock_dynamodb2.start()
create_rotating_message_table()


def tearDown():
mock_dynamodb2.stop()


class TestWebpushHandler(unittest.TestCase):
Expand Down
Loading

0 comments on commit 6efb366

Please sign in to comment.