modify backend selection to allow multiple backends via comma delimiter

This commit is contained in:
Paul Kehrer 2014-01-18 09:22:21 -06:00
parent 681e7a5587
commit c421e636b1
3 changed files with 49 additions and 19 deletions

View file

@ -5,21 +5,21 @@ from cryptography.hazmat.backends.interfaces import (
HMACBackend, CipherBackend, HashBackend
)
from .utils import check_for_iface, check_backend_support, modify_backend_list
from .utils import check_for_iface, check_backend_support, select_backends
# copy all backends so we can mutate it.This variable is used in generate
# tests to allow us to target a single backend without changing _ALL_BACKENDS
_DESIRED_BACKENDS = list(_ALL_BACKENDS)
_SELECTED_BACKENDS = list(_ALL_BACKENDS)
def pytest_generate_tests(metafunc):
global _DESIRED_BACKENDS
name = metafunc.config.getoption("--backend")
modify_backend_list(name, _DESIRED_BACKENDS)
global _SELECTED_BACKENDS
names = metafunc.config.getoption("--backend")
_SELECTED_BACKENDS = select_backends(names, _SELECTED_BACKENDS)
@pytest.fixture(params=_DESIRED_BACKENDS)
@pytest.fixture(params=_SELECTED_BACKENDS)
def backend(request):
return request.param

View file

@ -21,7 +21,7 @@ import pytest
from .utils import (
load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors,
load_openssl_vectors, load_hash_vectors, check_for_iface,
check_backend_support, modify_backend_list
check_backend_support, select_backends
)
@ -29,25 +29,46 @@ class FakeInterface(object):
pass
def test_modify_backend_list_leave_one():
def test_select_one_backend():
b1 = pretend.stub(name="b1")
b2 = pretend.stub(name="b2")
b3 = pretend.stub(name="b3")
backends = [b1, b2, b3]
name = "b2"
modify_backend_list(name, backends)
select_backends(name, backends)
assert len(backends) == 1
assert backends[0] == b2
def test_modify_backend_list_error_none():
def test_select_no_backend():
b1 = pretend.stub(name="b1")
b2 = pretend.stub(name="b2")
b3 = pretend.stub(name="b3")
backends = [b1, b2, b3]
name = "back!"
with pytest.raises(ValueError):
modify_backend_list(name, backends)
select_backends(name, backends)
def test_select_backends_none():
b1 = pretend.stub(name="b1")
b2 = pretend.stub(name="b2")
b3 = pretend.stub(name="b3")
backends = [b1, b2, b3]
name = None
select_backends(name, backends)
assert len(backends) == 3
def test_select_two_backends():
b1 = pretend.stub(name="b1")
b2 = pretend.stub(name="b2")
b3 = pretend.stub(name="b3")
backends = [b1, b2, b3]
name = "b2 ,b1 "
select_backends(name, backends)
assert len(backends) == 2
assert backends == [b1, b2]
def test_check_for_iface():

View file

@ -16,14 +16,23 @@ import os
import pytest
def modify_backend_list(name, all_backends):
if name is not None:
backends = list(all_backends)
for backend in backends:
if backend.name != name:
all_backends.remove(backend)
if len(all_backends) == 0:
raise ValueError("No backends selected for testing")
def select_backends(names, backend_list):
if names is None:
return backend_list
split_names = [x.strip() for x in names.split(',')]
# this must be duplicated and then removed to preserve the metadata
# pytest associates. Appending backends to a new list doesn't seem to work
backends = list(backend_list)
for backend in backends:
if backend.name not in split_names:
backend_list.remove(backend)
if len(backend_list) > 0:
return backend_list
else:
raise ValueError(
"No backend selected. Tried to select: {0}".format(split_names)
)
def check_for_iface(name, iface, item):