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

Commit

Permalink
bug: Use static UAIDs for preflight, clean up after.
Browse files Browse the repository at this point in the history
Preflight would create new accounts which could throw off metrics.
This patch solves this in two ways; It uses static UAIDs matching
"deadbeef00000000deadbeef\d{8}" and deletes the created data once
the preflight is done.

Closes #434
  • Loading branch information
jrconlin committed Apr 20, 2016
1 parent cf1afd6 commit e19329d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
8 changes: 6 additions & 2 deletions autopush/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ def get_storage_table(tablename="storage", read_throughput=5,
write_throughput)


def preflight_check(storage, router):
def preflight_check(storage, router, uaid="deadbeef00000000deadbeef00000000"):
"""Performs a pre-flight check of the storage/router/message to ensure
appropriate permissions for operation.
Failure to run correctly will raise an exception.
"""
uaid = uuid.uuid4().hex
# Use a distinct UAID so it doesn't interfere with metrics
chid = uuid.uuid4().hex
node_id = "mynode:2020"
connected_at = 0
Expand All @@ -186,7 +186,11 @@ def preflight_check(storage, router):
router_type="simplepush"))
item = router.get_uaid(uaid)
assert item.get("node_id") == node_id
# Clean up the preflight data.
router.clear_node(item)
router.drop_user(uaid)
storage.table.delete_item(uaid=uaid, chid=chid)
storage.table.delete_item(uaid=uaid, chid=" ")


def track_provisioned(func):
Expand Down
6 changes: 6 additions & 0 deletions autopush/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ def connection_main(sysargs=None, use_files=True):
sentry_dsn=sentry_dsn,
firehose_delivery_stream=args.firehose_stream_name
)
# Add some entropy to prevent potential conflicts.
postfix = os.urandom(4).encode('hex').ljust(8, '0')
settings = make_settings(
args,
port=args.port,
Expand All @@ -381,6 +383,7 @@ def connection_main(sysargs=None, use_files=True):
router_port=args.router_port,
env=args.env,
hello_timeout=args.hello_timeout,
preflight_uaid="deadbeef000000000deadbeef" + postfix,
)

r = RouterHandler
Expand Down Expand Up @@ -480,6 +483,8 @@ def endpoint_main(sysargs=None, use_files=True):
firehose_delivery_stream=args.firehose_stream_name
)

# Add some entropy to prevent potential conflicts.
postfix = os.urandom(4).encode('hex').ljust(8, '0')
settings = make_settings(
args,
endpoint_scheme=args.endpoint_scheme,
Expand All @@ -490,6 +495,7 @@ def endpoint_main(sysargs=None, use_files=True):
senderid_expry=args.senderid_expry,
senderid_list=senderid_list,
bear_hash_key=args.auth_key,
preflight_uaid="deadbeef000000000deadbeef" + postfix,
)

# Endpoint HTTP router
Expand Down
3 changes: 2 additions & 1 deletion autopush/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(self,
senderid_list={},
hello_timeout=0,
bear_hash_key=None,
preflight_uaid="deadbeef00000000deadbeef000000000",
):
"""Initialize the Settings object
Expand Down Expand Up @@ -172,7 +173,7 @@ def __init__(self,
self.create_initial_message_tables()

# Run preflight check
preflight_check(self.storage, self.router)
preflight_check(self.storage, self.router, preflight_uaid)

# CORS
self.cors = enable_cors
Expand Down
23 changes: 17 additions & 6 deletions autopush/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,29 @@ def tearDown():


class DbCheckTestCase(unittest.TestCase):
def test_preflight_check(self):
router_table = get_router_table()
storage_table = get_storage_table()
def test_preflight_check_fail(self):
router = Router(get_router_table(), SinkMetrics())
storage = Storage(get_storage_table(), SinkMetrics())

def raise_exc(*args, **kwargs): # pragma: no cover
raise Exception("Oops")

router_table.clear_node = Mock()
router_table.clear_node.side_effect = raise_exc
router.clear_node = Mock()
router.clear_node.side_effect = raise_exc

with self.assertRaises(Exception):
preflight_check(storage_table, router_table)
preflight_check(storage, router)

def test_preflight_check(self):
router = Router(get_router_table(), SinkMetrics())
storage = Storage(get_storage_table(), SinkMetrics())

pf_uaid = "deadbeef00000000deadbeef01010101"
preflight_check(storage, router, pf_uaid)
# now check that the database reports no entries.
notifs = storage.fetch_notifications(pf_uaid)
eq_(len(notifs), 0)
self.assertRaises(ItemNotFound, router.get_uaid, pf_uaid)

def test_get_month(self):
from autopush.db import get_month
Expand Down

0 comments on commit e19329d

Please sign in to comment.