Skip to content

Commit 79c6ef0

Browse files
Merge pull request #169 from minrk/simplify-conftest
simplify some test fixtures
2 parents 4c5867d + f00b056 commit 79c6ef0

File tree

1 file changed

+49
-96
lines changed

1 file changed

+49
-96
lines changed

tests/conftest.py

Lines changed: 49 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def no_auth_consul_proxy(launch_consul):
9898

9999

100100
@pytest.fixture
101-
async def auth_consul_proxy(launch_consul_acl):
101+
async def auth_consul_proxy(launch_consul_auth):
102102
"""
103103
Fixture returning a configured TraefikConsulProxy.
104104
Consul acl enabled.
@@ -130,7 +130,7 @@ async def no_auth_etcd_proxy(launch_etcd):
130130

131131

132132
@pytest.fixture
133-
async def auth_etcd_proxy(enable_auth_in_etcd):
133+
async def auth_etcd_proxy(launch_etcd_auth):
134134
"""
135135
Fixture returning a configured TraefikEtcdProxy
136136
Etcd has credentials set up
@@ -142,10 +142,6 @@ async def auth_etcd_proxy(enable_auth_in_etcd):
142142

143143

144144
def _make_etcd_proxy(auth=False, **extra_kwargs):
145-
grpc_options = [
146-
("grpc.ssl_target_name_override", "localhost"),
147-
("grpc.default_authority", "localhost"),
148-
]
149145
kwargs = dict(
150146
public_url=Config.public_url,
151147
traefik_api_password=Config.traefik_api_pass,
@@ -155,7 +151,10 @@ def _make_etcd_proxy(auth=False, **extra_kwargs):
155151
if auth:
156152
kwargs.update(
157153
dict(
158-
grpc_options=grpc_options,
154+
grpc_options=[
155+
("grpc.ssl_target_name_override", "localhost"),
156+
("grpc.default_authority", "localhost"),
157+
],
159158
etcd_url="https://localhost:2379",
160159
etcd_client_ca_cert=f"{config_files}/fake-ca-cert.crt",
161160
etcd_insecure_skip_verify=True,
@@ -234,7 +233,7 @@ async def external_file_proxy_toml(launch_traefik_file):
234233

235234

236235
@pytest.fixture
237-
async def external_consul_proxy(launch_consul, configure_consul, launch_traefik_consul):
236+
async def external_consul_proxy(launch_traefik_consul):
238237
proxy = TraefikConsulProxy(
239238
public_url=Config.public_url,
240239
consul_url=f"http://127.0.0.1:{Config.consul_port}",
@@ -248,9 +247,7 @@ async def external_consul_proxy(launch_consul, configure_consul, launch_traefik_
248247

249248

250249
@pytest.fixture
251-
async def auth_external_consul_proxy(
252-
launch_consul_acl, configure_consul_auth, launch_traefik_consul_auth
253-
):
250+
async def auth_external_consul_proxy(launch_traefik_consul_auth):
254251
proxy = TraefikConsulProxy(
255252
public_url=Config.public_url,
256253
consul_url=f"http://127.0.0.1:{Config.consul_auth_port}",
@@ -298,15 +295,15 @@ def launch_traefik_file():
298295

299296

300297
@pytest.fixture
301-
def launch_traefik_etcd(launch_etcd, configure_etcd):
298+
def launch_traefik_etcd(launch_etcd):
302299
env = Config.etcdctl_env
303300
proc = _launch_traefik_cli("--providers.etcd", env=env)
304301
yield proc
305302
shutdown_traefik(proc)
306303

307304

308305
@pytest.fixture
309-
def launch_traefik_etcd_auth(launch_etcd_auth, configure_etcd_auth):
306+
def launch_traefik_etcd_auth(launch_etcd_auth):
310307
extra_args = (
311308
"--providers.etcd.tls.insecureSkipVerify=true",
312309
"--providers.etcd.tls.ca=" + f"{config_files}/fake-ca-cert.crt",
@@ -319,14 +316,14 @@ def launch_traefik_etcd_auth(launch_etcd_auth, configure_etcd_auth):
319316

320317

321318
@pytest.fixture
322-
def launch_traefik_consul():
319+
def launch_traefik_consul(launch_consul):
323320
proc = _launch_traefik_cli("--providers.consul")
324321
yield proc
325322
shutdown_traefik(proc)
326323

327324

328325
@pytest.fixture
329-
def launch_traefik_consul_auth():
326+
def launch_traefik_consul_auth(launch_consul_auth):
330327
extra_args = (
331328
f"--providers.consul.endpoints=http://127.0.0.1:{Config.consul_auth_port}",
332329
)
@@ -364,22 +361,6 @@ def _launch_traefik(*extra_args, env=None):
364361
##################################
365362

366363

367-
@pytest.fixture
368-
def configure_etcd(launch_etcd):
369-
"""Load traefik api rules into the etcd kv store"""
370-
yield _config_etcd()
371-
372-
373-
@pytest.fixture
374-
def configure_etcd_auth(launch_etcd_auth, enable_auth_in_etcd):
375-
"""Load traefik api rules into the etcd kv store, with authentication"""
376-
yield _config_etcd(
377-
"--user=" + Config.etcd_user + ":" + Config.etcd_password,
378-
"--insecure-skip-tls-verify=true",
379-
"--insecure-transport=false",
380-
)
381-
382-
383364
def _config_etcd(*extra_args):
384365
data_store_cmd = ("etcdctl", "txn", "--debug") + extra_args
385366
# Load a pre-baked dynamic configuration into the etcd store.
@@ -397,15 +378,10 @@ def _config_etcd(*extra_args):
397378
), f"{data_store_cmd} exited with status {proc.returncode}"
398379

399380

400-
@pytest.fixture
401-
def enable_auth_in_etcd(launch_etcd_auth):
381+
def _enable_auth_in_etcd(*common_args):
382+
common_args = list(common_args)
402383
user = Config.etcd_user
403384
pw = Config.etcd_password
404-
common_args = [
405-
"--insecure-skip-tls-verify=true",
406-
"--insecure-transport=false",
407-
"--debug",
408-
]
409385
subprocess.check_call(
410386
["etcdctl", "user", "add", f"{user}:{pw}"] + common_args, env=Config.etcdctl_env
411387
)
@@ -421,24 +397,6 @@ def enable_auth_in_etcd(launch_etcd_auth):
421397
.strip()
422398
== "Authentication Enabled"
423399
)
424-
yield
425-
426-
assert (
427-
subprocess.check_output(
428-
["etcdctl", "--user", f"{user}:{pw}", "auth", "disable"] + common_args,
429-
env=Config.etcdctl_env,
430-
)
431-
.decode(sys.stdout.encoding)
432-
.strip()
433-
== "Authentication Disabled"
434-
)
435-
subprocess.call(
436-
["etcdctl", "user", "revoke-role", "root", user] + common_args,
437-
env=Config.etcdctl_env,
438-
)
439-
subprocess.call(
440-
["etcdctl", "user", "delete", user] + common_args, env=Config.etcdctl_env
441-
)
442400

443401

444402
@pytest.fixture
@@ -458,14 +416,17 @@ async def launch_etcd_auth():
458416
"--log-level=debug",
459417
],
460418
)
419+
etcdctl_args = [
420+
"--user",
421+
f"{Config.etcd_user}:{Config.etcd_password}",
422+
"--insecure-skip-tls-verify=true",
423+
"--insecure-transport=false",
424+
"--debug",
425+
]
461426
try:
462-
await _wait_for_etcd(
463-
"--user",
464-
f"{Config.etcd_user}:{Config.etcd_password}",
465-
"--insecure-skip-tls-verify=true",
466-
"--insecure-transport=false",
467-
"--debug",
468-
)
427+
await _wait_for_etcd(*etcdctl_args)
428+
_enable_auth_in_etcd(*etcdctl_args)
429+
_config_etcd(*etcdctl_args)
469430
yield etcd_proc
470431
finally:
471432
shutdown_etcd(etcd_proc)
@@ -480,6 +441,7 @@ async def launch_etcd():
480441
)
481442
try:
482443
await _wait_for_etcd("--debug=true")
444+
_config_etcd()
483445
yield etcd_proc
484446
finally:
485447
shutdown_etcd(etcd_proc)
@@ -509,13 +471,6 @@ def check():
509471
await exponential_backoff(check, "etcd health check", timeout=10)
510472

511473

512-
# @pytest.fixture(scope="function", autouse=True)
513-
# Is this referenced anywhere??
514-
# @pytest.fixture
515-
def clean_etcd():
516-
subprocess.run(["etcdctl", "del", '""', "--from-key=true"], env=Config.etcdctl_env)
517-
518-
519474
# Consul Launchers and configurers #
520475
####################################
521476

@@ -533,15 +488,18 @@ def launch_consul():
533488
],
534489
cwd=consul_path,
535490
)
536-
asyncio.run(
537-
_wait_for_consul(token=Config.consul_token, port=Config.consul_port)
538-
)
539-
yield consul_proc
540-
shutdown_consul(consul_proc)
491+
try:
492+
asyncio.run(
493+
_wait_for_consul(token=Config.consul_token, port=Config.consul_port)
494+
)
495+
_config_consul()
496+
yield consul_proc
497+
finally:
498+
shutdown_consul(consul_proc)
541499

542500

543501
@pytest.fixture(scope="module")
544-
def launch_consul_acl():
502+
def launch_consul_auth():
545503
with TemporaryDirectory() as consul_path:
546504
consul_proc = subprocess.Popen(
547505
[
@@ -563,13 +521,21 @@ def launch_consul_acl():
563521
],
564522
cwd=consul_path,
565523
)
566-
asyncio.run(
567-
_wait_for_consul(token=Config.consul_token, port=Config.consul_auth_port)
568-
)
569-
yield consul_proc
570-
shutdown_consul(
571-
consul_proc, secret=Config.consul_token, port=Config.consul_auth_port
572-
)
524+
try:
525+
# asyncio.run instead of await because this fixture's scope
526+
# is module-scoped, while event_loop is 'function'-scoped
527+
asyncio.run(
528+
_wait_for_consul(
529+
token=Config.consul_token, port=Config.consul_auth_port
530+
)
531+
)
532+
533+
_config_consul(secret=Config.consul_token, port=Config.consul_auth_port)
534+
yield consul_proc
535+
finally:
536+
shutdown_consul(
537+
consul_proc, secret=Config.consul_token, port=Config.consul_auth_port
538+
)
573539

574540

575541
async def _wait_for_consul(token=None, **kwargs):
@@ -598,18 +564,6 @@ async def _check_consul():
598564
)
599565

600566

601-
@pytest.fixture
602-
def configure_consul():
603-
"""Load an initial config into the consul KV store"""
604-
yield _config_consul()
605-
606-
607-
@pytest.fixture
608-
def configure_consul_auth():
609-
"""Load an initial config into the consul KV store, using authentication"""
610-
yield _config_consul(secret=Config.consul_token, port=Config.consul_auth_port)
611-
612-
613567
def _config_consul(secret=None, port=8500):
614568
proc_env = None
615569
if secret is not None:
@@ -651,7 +605,6 @@ def shutdown_consul(consul_proc, secret=None, port=8500):
651605

652606

653607
def shutdown_etcd(etcd_proc):
654-
clean_etcd()
655608
terminate_process(etcd_proc, timeout=20)
656609

657610
# Remove the default.etcd folder, so no settings left over

0 commit comments

Comments
 (0)