Skip to content

Commit cdf85a7

Browse files
authored
Merge pull request #46 from mattsb42-aws/friendlier-tests
Make the experience running tests outside of tox friendlier
2 parents 5de683c + de517dd commit cdf85a7

File tree

6 files changed

+43
-20
lines changed

6 files changed

+43
-20
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ matrix:
3434
env: TOXENV=py36-accept
3535
- python: 3.6
3636
env: TOXENV=py36-examples
37+
- python: 3.6
38+
env: TOXENV=nocmk
3739
# disabling Bandit run in Travis pending resolution of https://bugs.launchpad.net/bandit/+bug/1749603
3840
# - python: 3.6
3941
# env: TOXENV=bandit

test/integration/integration_test_utils.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from aws_encryption_sdk.key_providers.kms import KMSMasterKeyProvider
1717

1818
AWS_KMS_KEY_ID = 'AWS_ENCRYPTION_SDK_PYTHON_INTEGRATION_TEST_AWS_KMS_KEY_ID'
19+
_KMS_MKP = None
1920

2021

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

3435

35-
def setup_kms_master_key_provider():
36+
def setup_kms_master_key_provider(cache=True):
3637
"""Reads the test_values config file and builds the requested KMS Master Key Provider."""
38+
global _KMS_MKP # pylint: disable=global-statement
39+
if cache and _KMS_MKP is not None:
40+
return _KMS_MKP
41+
3742
cmk_arn = get_cmk_arn()
3843
kms_master_key_provider = KMSMasterKeyProvider()
3944
kms_master_key_provider.add_master_key(cmk_arn)
45+
46+
if cache:
47+
_KMS_MKP = kms_master_key_provider
48+
4049
return kms_master_key_provider

test/integration/test_i_thread_safety.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from six.moves import queue # six.moves confuses pylint: disable=import-error
2323

2424
import aws_encryption_sdk
25-
from .integration_test_utils import setup_kms_master_key_provider
25+
from .integration_test_utils import get_cmk_arn, setup_kms_master_key_provider
2626

2727
pytestmark = [pytest.mark.integ]
2828

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

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

131133
def test_threading_loop_with_common_cache():
132134
"""Test thread safety of client while using common cryptographic materials cache across all threads."""
135+
# Check for the CMK ARN first to fail fast if it is not available
136+
get_cmk_arn()
133137
rounds = 20
134138
cache = aws_encryption_sdk.LocalCryptoMaterialsCache(capacity=40)
135139
plaintext_inputs = [

test/integration/test_i_xcompat_kms.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def _file_root():
3434

3535

3636
def _generate_test_cases():
37-
kms_key_provider = setup_kms_master_key_provider()
3837
try:
3938
root_dir = os.path.abspath(file_root())
4039
except Exception: # pylint: disable=broad-except
@@ -65,22 +64,21 @@ def _generate_test_cases():
6564
if key['provider_id'] == 'aws-kms' and key['decryptable']:
6665
_test_cases.append((
6766
os.path.join(base_dir, test_case['plaintext']['filename']),
68-
os.path.join(base_dir, test_case['ciphertext']['filename']),
69-
kms_key_provider
67+
os.path.join(base_dir, test_case['ciphertext']['filename'])
7068
))
7169
break
7270
return _test_cases
7371

7472

75-
@pytest.mark.parametrize('plaintext_filename,ciphertext_filename,key_provider', _generate_test_cases())
76-
def test_decrypt_from_file(plaintext_filename, ciphertext_filename, key_provider):
73+
@pytest.mark.parametrize('plaintext_filename, ciphertext_filename', _generate_test_cases())
74+
def test_decrypt_from_file(plaintext_filename, ciphertext_filename):
7775
"""Tests decrypt from known good files."""
7876
with open(ciphertext_filename, 'rb') as infile:
7977
ciphertext = infile.read()
8078
with open(plaintext_filename, 'rb') as infile:
8179
plaintext = infile.read()
8280
decrypted_ciphertext, _header = aws_encryption_sdk.decrypt(
8381
source=ciphertext,
84-
key_provider=key_provider
82+
key_provider=setup_kms_master_key_provider()
8583
)
8684
assert decrypted_ciphertext == plaintext

test/requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mock
2+
pytest>=3.3.1
3+
pytest-cov
4+
pytest-mock

tox.ini

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tox]
22
envlist =
3-
py{27,34,35,36}-{local,integ,accept,examples},
3+
py{27,34,35,36}-{local,integ,accept,examples}, nocmk,
44
bandit, doc8, readme, docs,
55
{flake8,pylint}{,-tests,-examples},
66
vulture
@@ -18,6 +18,9 @@ envlist =
1818
# test-release :: Builds dist files and uploads to testpypi pypirc profile.
1919
# release :: Builds dist files and uploads to pypi pypirc profile.
2020

21+
[testenv:base-command]
22+
commands = pytest --basetemp={envtmpdir} -l --cov aws_encryption_sdk {posargs}
23+
2124
[testenv]
2225
passenv =
2326
# Identifies AWS KMS key id to use in integration tests
@@ -27,17 +30,20 @@ passenv =
2730
# Pass through AWS profile name (useful for local testing)
2831
AWS_PROFILE
2932
sitepackages = False
30-
deps =
31-
mock
32-
pytest>=3.3.1
33-
pytest-cov
34-
pytest-mock
33+
deps = -rtest/requirements.txt
3534
commands =
36-
local: pytest --cov aws_encryption_sdk -m local -l {posargs}
37-
integ: pytest --cov aws_encryption_sdk -m integ -l {posargs}
38-
accept: pytest --cov aws_encryption_sdk -m accept -l {posargs}
39-
all: pytest --cov aws_encryption_sdk -l {posargs}
40-
examples: pytest --cov examples/test/ -m examples -l {posargs}
35+
local: {[testenv:base-command]commands} -m local
36+
integ: {[testenv:base-command]commands} -m integ
37+
accept: {[testenv:base-command]commands} -m accept
38+
all: {[testenv:base-command]commands}
39+
examples: {[testenv:base-command]commands} -m examples
40+
41+
# Verify that local tests work without environment variables present
42+
[testenv:nocmk]
43+
basepython = python3
44+
sitepackages = False
45+
deps = -rtest/requirements.txt
46+
commands = {[testenv:base-command]commands} -m local
4147

4248
# Linters
4349
[testenv:flake8]

0 commit comments

Comments
 (0)