Skip to content

Commit

Permalink
Merge pull request #46 from mattsb42-aws/friendlier-tests
Browse files Browse the repository at this point in the history
Make the experience running tests outside of tox friendlier
  • Loading branch information
mattsb42-aws committed Apr 4, 2018
2 parents 5de683c + de517dd commit cdf85a7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ matrix:
env: TOXENV=py36-accept
- python: 3.6
env: TOXENV=py36-examples
- python: 3.6
env: TOXENV=nocmk
# disabling Bandit run in Travis pending resolution of https://bugs.launchpad.net/bandit/+bug/1749603
# - python: 3.6
# env: TOXENV=bandit
Expand Down
11 changes: 10 additions & 1 deletion test/integration/integration_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProvider

AWS_KMS_KEY_ID = 'AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_KEY_ID'
_KMS_MKP = None


def get_cmk_arn():
Expand All @@ -32,9 +33,17 @@ def get_cmk_arn():
raise ValueError('KMS CMK ARN provided for integration tests much be a key not an alias')


def setup_kms_master_key_provider():
def setup_kms_master_key_provider(cache=True):
"""Reads the test_values config file and builds the requested KMS Master Key Provider."""
global _KMS_MKP # pylint: disable=global-statement
if cache and _KMS_MKP is not None:
return _KMS_MKP

cmk_arn = get_cmk_arn()
kms_master_key_provider = KMSMasterKeyProvider()
kms_master_key_provider.add_master_key(cmk_arn)

if cache:
_KMS_MKP = kms_master_key_provider

return kms_master_key_provider
8 changes: 6 additions & 2 deletions test/integration/test_i_thread_safety.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from six.moves import queue # six.moves confuses pylint: disable=import-error

import aws_encryption_sdk
from .integration_test_utils import setup_kms_master_key_provider
from .integration_test_utils import get_cmk_arn, setup_kms_master_key_provider

pytestmark = [pytest.mark.integ]

Expand All @@ -49,7 +49,7 @@ def crypto_thread_worker(crypto_function, start_pause, input_value, output_queue
:param cache: Cache to use with master key provider (optional)
"""
time.sleep(start_pause)
kms_master_key_provider = setup_kms_master_key_provider()
kms_master_key_provider = setup_kms_master_key_provider(cache=False)
if cache is None:
# For simplicity, always use a caching CMM; just use a null cache if no cache is specified.
cache = aws_encryption_sdk.NullCryptoMaterialsCache()
Expand Down Expand Up @@ -105,6 +105,8 @@ def random_pause_time(max_seconds=3):

def test_threading_loop():
"""Test thread safety of client."""
# Check for the CMK ARN first to fail fast if it is not available
get_cmk_arn()
rounds = 20
plaintext_inputs = [
dict(input_value=copy.copy(PLAINTEXT), start_pause=random_pause_time())
Expand All @@ -130,6 +132,8 @@ def test_threading_loop():

def test_threading_loop_with_common_cache():
"""Test thread safety of client while using common cryptographic materials cache across all threads."""
# Check for the CMK ARN first to fail fast if it is not available
get_cmk_arn()
rounds = 20
cache = aws_encryption_sdk.LocalCryptoMaterialsCache(capacity=40)
plaintext_inputs = [
Expand Down
10 changes: 4 additions & 6 deletions test/integration/test_i_xcompat_kms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def _file_root():


def _generate_test_cases():
kms_key_provider = setup_kms_master_key_provider()
try:
root_dir = os.path.abspath(file_root())
except Exception: # pylint: disable=broad-except
Expand Down Expand Up @@ -65,22 +64,21 @@ def _generate_test_cases():
if key['provider_id'] == 'aws-kms' and key['decryptable']:
_test_cases.append((
os.path.join(base_dir, test_case['plaintext']['filename']),
os.path.join(base_dir, test_case['ciphertext']['filename']),
kms_key_provider
os.path.join(base_dir, test_case['ciphertext']['filename'])
))
break
return _test_cases


@pytest.mark.parametrize('plaintext_filename,ciphertext_filename,key_provider', _generate_test_cases())
def test_decrypt_from_file(plaintext_filename, ciphertext_filename, key_provider):
@pytest.mark.parametrize('plaintext_filename, ciphertext_filename', _generate_test_cases())
def test_decrypt_from_file(plaintext_filename, ciphertext_filename):
"""Tests decrypt from known good files."""
with open(ciphertext_filename, 'rb') as infile:
ciphertext = infile.read()
with open(plaintext_filename, 'rb') as infile:
plaintext = infile.read()
decrypted_ciphertext, _header = aws_encryption_sdk.decrypt(
source=ciphertext,
key_provider=key_provider
key_provider=setup_kms_master_key_provider()
)
assert decrypted_ciphertext == plaintext
4 changes: 4 additions & 0 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mock
pytest>=3.3.1
pytest-cov
pytest-mock
28 changes: 17 additions & 11 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
envlist =
py{27,34,35,36}-{local,integ,accept,examples},
py{27,34,35,36}-{local,integ,accept,examples}, nocmk,
bandit, doc8, readme, docs,
{flake8,pylint}{,-tests,-examples},
vulture
Expand All @@ -18,6 +18,9 @@ envlist =
# test-release :: Builds dist files and uploads to testpypi pypirc profile.
# release :: Builds dist files and uploads to pypi pypirc profile.

[testenv:base-command]
commands = pytest --basetemp={envtmpdir} -l --cov aws_encryption_sdk {posargs}

[testenv]
passenv =
# Identifies AWS KMS key id to use in integration tests
Expand All @@ -27,17 +30,20 @@ passenv =
# Pass through AWS profile name (useful for local testing)
AWS_PROFILE
sitepackages = False
deps =
mock
pytest>=3.3.1
pytest-cov
pytest-mock
deps = -rtest/requirements.txt
commands =
local: pytest --cov aws_encryption_sdk -m local -l {posargs}
integ: pytest --cov aws_encryption_sdk -m integ -l {posargs}
accept: pytest --cov aws_encryption_sdk -m accept -l {posargs}
all: pytest --cov aws_encryption_sdk -l {posargs}
examples: pytest --cov examples/test/ -m examples -l {posargs}
local: {[testenv:base-command]commands} -m local
integ: {[testenv:base-command]commands} -m integ
accept: {[testenv:base-command]commands} -m accept
all: {[testenv:base-command]commands}
examples: {[testenv:base-command]commands} -m examples

# Verify that local tests work without environment variables present
[testenv:nocmk]
basepython = python3
sitepackages = False
deps = -rtest/requirements.txt
commands = {[testenv:base-command]commands} -m local

# Linters
[testenv:flake8]
Expand Down

0 comments on commit cdf85a7

Please sign in to comment.