Skip to content

Commit

Permalink
Use exotic AWS region for unit tests (#1498)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadove-ucsc authored and hannes-ucsc committed May 5, 2020
1 parent b177977 commit 6722202
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
13 changes: 13 additions & 0 deletions test/azul_test_case.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
from unittest import TestCase
from unittest.mock import (
patch,
)

import boto3.session
from botocore.credentials import Credentials
Expand Down Expand Up @@ -53,6 +56,9 @@ class AzulTestCase(TestCase):
get_credentials_boto3 = None
_saved_boto3_default_session = None
aws_account_id = None
# We almost certainly won't have access to this region
_aws_test_region = 'us-gov-west-1'
_aws_region_mock = None

@classmethod
def setUpClass(cls) -> None:
Expand Down Expand Up @@ -94,8 +100,15 @@ def dummy_get_credentials(self):
botocore.session.Session.get_credentials = dummy_get_credentials
boto3.session.Session.get_credentials = dummy_get_credentials

# Ensure that mock leakages fail by targeting a region we don't have acces to.
# Subclasses can override the selected region if moto rejects the default one.
cls._aws_region_mock = patch.dict(os.environ, AWS_DEFAULT_REGION=cls._aws_test_region)
print(cls._aws_test_region)
cls._aws_region_mock.start()

@classmethod
def tearDownClass(cls) -> None:
cls._aws_region_mock.stop()
boto3.session.Session.get_credentials = cls.get_credentials_boto3
botocore.session.Session.get_credentials = cls.get_credentials_botocore
boto3.DEFAULT_SESSION = cls._saved_boto3_default_session
Expand Down
2 changes: 1 addition & 1 deletion test/dynamo_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUpClass(cls):
try:
endpoint = f'http://{host}:{port}'
with mock_sts():
cls.dynamo_accessor = DynamoDataAccessor(endpoint, 'us-east-1')
cls.dynamo_accessor = DynamoDataAccessor(endpoint)
except BaseException: # no coverage
cls._kill_containers()
raise
6 changes: 2 additions & 4 deletions test/health_check_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,7 @@ def _mock_service_endpoints(self, helper: ResponsesHelper, endpoint_states: Mapp
url=config.service_endpoint() + endpoint,
status=200 if endpoint_up else 503,
json={}))
# boto3.resource('sqs') requires an AWS region to be set
with patch.dict(os.environ, AWS_DEFAULT_REGION='us-east-1'):
yield
yield

def _mock_other_lambdas(self, helper: ResponsesHelper, up: bool):
for lambda_name in self._other_lambda_names():
Expand All @@ -264,7 +262,7 @@ def _mock_other_lambdas(self, helper: ResponsesHelper, up: bool):
json={'up': up}))

def _create_mock_queues(self):
sqs = boto3.resource('sqs', region_name='us-east-1')
sqs = boto3.resource('sqs')
for queue_name in config.all_queue_names:
sqs.create_queue(QueueName=queue_name)

Expand Down
13 changes: 6 additions & 7 deletions test/indexer/test_hca_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,16 +1182,15 @@ def _test(self, body, endpoint, valid_auth):
helper.add_passthru(self.base_url)
hmac_creds = {'key': b'good key', 'key_id': 'the id'}
with patch('azul.deployment.aws.get_hmac_key_and_id', return_value=hmac_creds):
with patch.dict(os.environ, AWS_DEFAULT_REGION='us-east-1'):
if valid_auth:
auth = hmac.prepare()
else:
auth = HTTPSignatureAuth(key=b'bad key', key_id='the id')
return requests.post(self.base_url + endpoint, json=body, auth=auth)
if valid_auth:
auth = hmac.prepare()
else:
auth = HTTPSignatureAuth(key=b'bad key', key_id='the id')
return requests.post(self.base_url + endpoint, json=body, auth=auth)

@staticmethod
def _create_mock_notify_queue():
sqs = boto3.resource('sqs', region_name='us-east-1')
sqs = boto3.resource('sqs')
sqs.create_queue(QueueName=config.notify_queue_name)


Expand Down
2 changes: 2 additions & 0 deletions test/version_table_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

@mock_dynamodb2
class VersionTableTestCase(AzulTestCase):
# Moto's dynamodb backend doesn't support government regions.
_aws_test_region = 'ap-south-1'

def setUp(self):
self.ddb_client = boto3.client('dynamodb')
Expand Down

0 comments on commit 6722202

Please sign in to comment.