Skip to content

Commit 016174e

Browse files
committed
Extend scope of fixtures
Allow reusing of a bunch of fixtures. Specifically the "factory fixtures" should be reusable accross test classes. So classes can provide a scenario including objects like repositories that can be asserted on from several independent tests.
1 parent 9c5dff6 commit 016174e

File tree

2 files changed

+70
-77
lines changed

2 files changed

+70
-77
lines changed

pulpcore/pytest_plugin.py

Lines changed: 67 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,10 @@ def make_url(self, path):
211211
return f"{protocol_handler}{self.host}:{self.port}{path}"
212212

213213

214-
@pytest.fixture
215-
def test_path():
216-
return os.getenv("PYTEST_CURRENT_TEST").split()[0]
217-
218-
219214
# Webserver Fixtures
220215

221216

222-
@pytest.fixture
217+
@pytest.fixture(scope="session")
223218
def unused_port():
224219
def _unused_port():
225220
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
@@ -229,7 +224,7 @@ def _unused_port():
229224
return _unused_port
230225

231226

232-
@pytest.fixture
227+
@pytest.fixture(scope="class")
233228
def gen_threaded_aiohttp_server(fixtures_cfg, unused_port):
234229
fixture_servers_data = []
235230

@@ -258,7 +253,7 @@ def _gen_threaded_aiohttp_server(app, ssl_ctx, call_record):
258253
fixture_server_data.thread.join()
259254

260255

261-
@pytest.fixture
256+
@pytest.fixture(scope="class")
262257
def gen_fixture_server(gen_threaded_aiohttp_server):
263258
def _gen_fixture_server(fixtures_root, ssl_ctx):
264259
app = web.Application()
@@ -271,14 +266,38 @@ def _gen_fixture_server(fixtures_root, ssl_ctx):
271266
# Proxy Fixtures
272267

273268

269+
class ProxyData:
270+
def __init__(self, *, host, port, username=None, password=None, ssl=False):
271+
self.host = host
272+
self.port = port
273+
274+
self.username = username
275+
self.password = password
276+
277+
self.ssl = ssl
278+
279+
if ssl:
280+
scheme = "https"
281+
else:
282+
scheme = "http"
283+
284+
self.proxy_url = str(
285+
URL.build(
286+
scheme=scheme,
287+
host=self.host,
288+
port=self.port,
289+
)
290+
)
291+
292+
274293
@pytest.fixture(scope="session")
275294
def _proxy_module():
276295
import proxy
277296

278297
return proxy
279298

280299

281-
@pytest.fixture
300+
@pytest.fixture(scope="class")
282301
def http_proxy(_proxy_module, fixtures_cfg, unused_port):
283302
host = fixtures_cfg.aiohttp_fixtures_origin
284303
port = unused_port()
@@ -297,7 +316,7 @@ def http_proxy(_proxy_module, fixtures_cfg, unused_port):
297316
yield proxy_data
298317

299318

300-
@pytest.fixture
319+
@pytest.fixture(scope="class")
301320
def http_proxy_with_auth(_proxy_module, fixtures_cfg, unused_port):
302321
host = fixtures_cfg.aiohttp_fixtures_origin
303322
port = unused_port()
@@ -322,7 +341,7 @@ def http_proxy_with_auth(_proxy_module, fixtures_cfg, unused_port):
322341
yield proxy_data
323342

324343

325-
@pytest.fixture
344+
@pytest.fixture(scope="class")
326345
def https_proxy(_proxy_module, fixtures_cfg, unused_port, proxy_tls_certificate_pem_path):
327346
host = fixtures_cfg.aiohttp_fixtures_origin
328347
port = unused_port()
@@ -346,30 +365,6 @@ def https_proxy(_proxy_module, fixtures_cfg, unused_port, proxy_tls_certificate_
346365
yield proxy_data
347366

348367

349-
class ProxyData:
350-
def __init__(self, *, host, port, username=None, password=None, ssl=False):
351-
self.host = host
352-
self.port = port
353-
354-
self.username = username
355-
self.password = password
356-
357-
self.ssl = ssl
358-
359-
if ssl:
360-
scheme = "https"
361-
else:
362-
scheme = "http"
363-
364-
self.proxy_url = str(
365-
URL.build(
366-
scheme=scheme,
367-
host=self.host,
368-
port=self.port,
369-
)
370-
)
371-
372-
373368
# Server Side TLS Fixtures
374369

375370

@@ -385,12 +380,12 @@ def tls_certificate_authority(_trustme_module):
385380
return _trustme_module.CA()
386381

387382

388-
@pytest.fixture
383+
@pytest.fixture(scope="session")
389384
def tls_certificate_authority_cert(tls_certificate_authority):
390385
return tls_certificate_authority.cert_pem.bytes().decode()
391386

392387

393-
@pytest.fixture
388+
@pytest.fixture(scope="session")
394389
def tls_certificate(fixtures_cfg, tls_certificate_authority):
395390
return tls_certificate_authority.issue_cert(
396391
fixtures_cfg.aiohttp_fixtures_origin,
@@ -405,14 +400,14 @@ def proxy_tls_certificate_authority(_trustme_module):
405400
return _trustme_module.CA()
406401

407402

408-
@pytest.fixture
403+
@pytest.fixture(scope="session")
409404
def proxy_tls_certificate(fixtures_cfg, client_tls_certificate_authority):
410405
return client_tls_certificate_authority.issue_cert(
411406
fixtures_cfg.aiohttp_fixtures_origin,
412407
)
413408

414409

415-
@pytest.fixture
410+
@pytest.fixture(scope="session")
416411
def proxy_tls_certificate_pem_path(proxy_tls_certificate):
417412
with proxy_tls_certificate.private_key_and_cert_chain_pem.tempfile() as cert_pem:
418413
yield cert_pem
@@ -426,40 +421,40 @@ def client_tls_certificate_authority(_trustme_module):
426421
return _trustme_module.CA()
427422

428423

429-
@pytest.fixture
424+
@pytest.fixture(scope="session")
430425
def client_tls_certificate_authority_pem_path(client_tls_certificate_authority):
431426
with client_tls_certificate_authority.cert_pem.tempfile() as client_ca_pem:
432427
yield client_ca_pem
433428

434429

435-
@pytest.fixture
430+
@pytest.fixture(scope="session")
436431
def client_tls_certificate(fixtures_cfg, client_tls_certificate_authority):
437432
return client_tls_certificate_authority.issue_cert(
438433
fixtures_cfg.aiohttp_fixtures_origin,
439434
)
440435

441436

442-
@pytest.fixture
437+
@pytest.fixture(scope="session")
443438
def client_tls_certificate_cert_pem(client_tls_certificate):
444439
return client_tls_certificate.cert_chain_pems[0].bytes().decode()
445440

446441

447-
@pytest.fixture
442+
@pytest.fixture(scope="session")
448443
def client_tls_certificate_key_pem(client_tls_certificate):
449444
return client_tls_certificate.private_key_pem.bytes().decode()
450445

451446

452447
# SSL Context Fixtures
453448

454449

455-
@pytest.fixture
450+
@pytest.fixture(scope="session")
456451
def ssl_ctx(tls_certificate):
457452
ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
458453
tls_certificate.configure_cert(ssl_ctx)
459454
return ssl_ctx
460455

461456

462-
@pytest.fixture
457+
@pytest.fixture(scope="session")
463458
def ssl_ctx_req_client_auth(
464459
tls_certificate, client_tls_certificate, client_tls_certificate_authority_pem_path
465460
):
@@ -474,15 +469,15 @@ def ssl_ctx_req_client_auth(
474469
# Object factories
475470

476471

477-
@pytest.fixture
472+
@pytest.fixture(scope="class")
478473
def role_factory(pulpcore_bindings, gen_object_with_cleanup):
479474
def _role_factory(**kwargs):
480475
return gen_object_with_cleanup(pulpcore_bindings.RolesApi, kwargs)
481476

482477
return _role_factory
483478

484479

485-
@pytest.fixture
480+
@pytest.fixture(scope="class")
486481
def gen_user(bindings_cfg, pulpcore_bindings, gen_object_with_cleanup):
487482
class user_context:
488483
def __init__(self, username=None, model_roles=None, object_roles=None, domain_roles=None):
@@ -596,7 +591,7 @@ def random_artifact(random_artifact_factory):
596591
return random_artifact_factory()
597592

598593

599-
@pytest.fixture
594+
@pytest.fixture(scope="session")
600595
def backend_settings_factory(pulp_settings):
601596
def _settings_factory(storage_class=None, storage_settings=None):
602597
if not pulp_settings.DOMAIN_ENABLED:
@@ -639,13 +634,14 @@ def _settings_factory(storage_class=None, storage_settings=None):
639634
return _settings_factory
640635

641636

642-
@pytest.fixture()
637+
@pytest.fixture(scope="class")
643638
def domain_factory(
644639
pulpcore_bindings, pulp_domain_enabled, backend_settings_factory, gen_object_with_cleanup
645640
):
641+
if not pulp_domain_enabled:
642+
pytest.skip("Domains not enabled")
643+
646644
def _domain_factory(**kwargs):
647-
if not pulp_domain_enabled:
648-
pytest.skip("Domains not enabled")
649645

650646
storage_class, storage_settings = backend_settings_factory(
651647
storage_class=kwargs.pop("storage_class", None),
@@ -658,6 +654,21 @@ def _domain_factory(**kwargs):
658654
return _domain_factory
659655

660656

657+
@pytest.fixture(scope="class")
658+
def openpgp_keyring_factory(pulpcore_bindings, gen_object_with_cleanup):
659+
def _openpgp_keyring_factory(**kwargs):
660+
extra_args = {}
661+
if pulp_domain := kwargs.pop("pulp_domain", None):
662+
extra_args["pulp_domain"] = pulp_domain
663+
body = {"name": str(uuid.uuid4())}
664+
body.update(kwargs)
665+
return gen_object_with_cleanup(
666+
pulpcore_bindings.RepositoriesOpenpgpKeyringApi, body, **extra_args
667+
)
668+
669+
return _openpgp_keyring_factory
670+
671+
661672
# Random other fixtures
662673

663674

@@ -821,7 +832,7 @@ def pulp_versions(pulp_status):
821832
return {item.component: parse_version(item.version) for item in pulp_status.versions}
822833

823834

824-
@pytest.fixture
835+
@pytest.fixture(scope="session")
825836
def needs_pulp_plugin(pulp_versions):
826837
"""Skip test if a component is not available in the specified version range"""
827838

@@ -836,7 +847,7 @@ def _needs_pulp_plugin(plugin, min=None, max=None):
836847
return _needs_pulp_plugin
837848

838849

839-
@pytest.fixture
850+
@pytest.fixture(scope="session")
840851
def has_pulp_plugin(pulp_versions):
841852
def _has_pulp_plugin(plugin, min=None, max=None):
842853
if plugin not in pulp_versions:
@@ -973,7 +984,7 @@ async def _send_request():
973984
return _http_get
974985

975986

976-
@pytest.fixture
987+
@pytest.fixture(scope="session")
977988
def wget_recursive_download_on_host():
978989
def _wget_recursive_download_on_host(url, destination):
979990
subprocess.check_output(
@@ -1089,7 +1100,7 @@ def signing_gpg_homedir_path(tmp_path_factory):
10891100
return tmp_path_factory.mktemp("gpghome")
10901101

10911102

1092-
@pytest.fixture
1103+
@pytest.fixture(scope="session")
10931104
def sign_with_ascii_armored_detached_signing_service(signing_script_path, signing_gpg_metadata):
10941105
"""
10951106
Runs the test signing script manually, locally, and returns the signature file produced.
@@ -1210,24 +1221,9 @@ def ascii_armored_detached_signing_service(
12101221
).results[0]
12111222

12121223

1213-
@pytest.fixture(scope="class")
1214-
def openpgp_keyring_factory(pulpcore_bindings, gen_object_with_cleanup):
1215-
def _openpgp_keyring_factory(**kwargs):
1216-
extra_args = {}
1217-
if pulp_domain := kwargs.pop("pulp_domain", None):
1218-
extra_args["pulp_domain"] = pulp_domain
1219-
body = {"name": str(uuid.uuid4())}
1220-
body.update(kwargs)
1221-
return gen_object_with_cleanup(
1222-
pulpcore_bindings.RepositoriesOpenpgpKeyringApi, body, **extra_args
1223-
)
1224-
1225-
return _openpgp_keyring_factory
1226-
1227-
12281224
# if content_origin == None, base_url will return the relative path and
12291225
# we need to add the hostname to run the tests
1230-
@pytest.fixture
1226+
@pytest.fixture(scope="session")
12311227
def distribution_base_url(bindings_cfg):
12321228
def _distribution_base_url(base_url):
12331229
if base_url.startswith("http"):

pulpcore/tests/functional/api/test_status.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858

5959
@pytest.mark.parallel
60-
def test_get_authenticated(test_path, pulpcore_bindings, pulp_settings):
60+
def test_get_authenticated(pulpcore_bindings, pulp_settings):
6161
"""GET the status path with valid credentials.
6262
6363
Verify the response with :meth:`verify_get_response`.
@@ -67,7 +67,7 @@ def test_get_authenticated(test_path, pulpcore_bindings, pulp_settings):
6767

6868

6969
@pytest.mark.parallel
70-
def test_get_unauthenticated(test_path, pulpcore_bindings, anonymous_user, pulp_settings):
70+
def test_get_unauthenticated(pulpcore_bindings, anonymous_user, pulp_settings):
7171
"""GET the status path with no credentials.
7272
7373
Verify the response with :meth:`verify_get_response`.
@@ -79,7 +79,6 @@ def test_get_unauthenticated(test_path, pulpcore_bindings, anonymous_user, pulp_
7979

8080
@pytest.mark.parallel
8181
def test_post_authenticated(
82-
test_path,
8382
pulp_api_v3_path,
8483
pulp_api_v3_url,
8584
pulpcore_bindings,
@@ -94,9 +93,7 @@ def test_post_authenticated(
9493
assert post_attr not in attrs
9594
# Try anyway to POST to /status/
9695
status_url = f"{pulp_api_v3_url}status/"
97-
response = pulpcore_bindings.client.rest_client.request(
98-
"POST", status_url, headers={"User-Agent": test_path}
99-
)
96+
response = pulpcore_bindings.client.rest_client.request("POST", status_url)
10097
assert response.status == 405
10198

10299

0 commit comments

Comments
 (0)