11"""Global fixtures go here."""
2- import os
2+
3+ from pathlib import Path
34from random import choice , random
4- import shutil
55from string import ascii_letters
6- import subprocess
76
87import pytest
98
@@ -16,26 +15,7 @@ def pytest_addoption(parser):
1615 parser .addoption ("--install-path" , action = "store" , default = "dev.json" )
1716
1817
19- def cleanup_docker (request ) -> None :
20- # Stop and remove 'nexus' containers. This needs to be deleted once we address the issue
21- # in the pynexus code by giving unique names to the containers
22- try :
23- subprocess .run (["docker" , "stop" , "nexus" ])
24- subprocess .run (["docker" , "rm" , "nexus" ])
25- except Exception :
26- # There might not be a running nexus container. That is fine, just continue
27- pass
28- try :
29- querydb_dir = os .path .join (os .path .join (request .fspath .dirname , "test_data" ), "query_db" )
30- os .remove (os .path .join (querydb_dir , "nexus.log" ))
31- os .remove (os .path .join (querydb_dir , "nexus.status" ))
32- shutil .rmtree (os .path .join (querydb_dir , "nginx" ))
33- except Exception :
34- # There might not be these files / directories. In which case, nothing to do
35- pass
36-
37-
38- @pytest .fixture
18+ @pytest .fixture (scope = "module" )
3919def get_exec (pytestconfig : pytest .Config ) -> str :
4020 exec_basis = ""
4121 use_local = pytestconfig .getoption ("use_local_launcher" )
@@ -44,52 +24,72 @@ def get_exec(pytestconfig: pytest.Config) -> str:
4424 return exec_basis
4525
4626
47- @pytest .fixture
48- def adr_service_create (request , pytestconfig : pytest .Config ) -> Service :
27+ @pytest .fixture ( scope = "module" )
28+ def adr_service_create (pytestconfig : pytest .Config ) -> Service :
4929 use_local = pytestconfig .getoption ("use_local_launcher" )
50- dir_name = "auto_delete_" + "" .join (choice (ascii_letters ) for x in range (5 ))
51- db_dir = os .path .join (os .path .join (request .fspath .dirname , "test_data" ), dir_name )
52- tmp_docker_dir = os .path .join (os .path .join (request .fspath .dirname , "test_data" ), "tmp_docker" )
30+
31+ # Paths setup
32+ base_dir = Path (__file__ ).parent / "test_data"
33+ dir_name = "auto_delete_" + "" .join (choice (ascii_letters ) for _ in range (5 ))
34+ db_dir = base_dir / dir_name
35+ tmp_docker_dir = base_dir / "tmp_docker"
36+
5337 if use_local :
54- tmp_service = Service (
38+ adr_service = Service (
5539 ansys_installation = pytestconfig .getoption ("install_path" ),
5640 docker_image = DOCKER_DEV_REPO_URL ,
57- db_directory = db_dir ,
41+ db_directory = str ( db_dir ) ,
5842 port = 8000 + int (random () * 4000 ),
5943 )
6044 else :
61- cleanup_docker (request )
62- tmp_service = Service (
45+ adr_service = Service (
6346 ansys_installation = "docker" ,
6447 docker_image = DOCKER_DEV_REPO_URL ,
65- db_directory = db_dir ,
66- data_directory = tmp_docker_dir ,
48+ db_directory = str ( db_dir ) ,
49+ data_directory = str ( tmp_docker_dir ) ,
6750 port = 8000 + int (random () * 4000 ),
6851 )
69- return tmp_service
7052
53+ _ = adr_service .start (
54+ create_db = True ,
55+ exit_on_close = True ,
56+ delete_db = True ,
57+ )
58+
59+ yield adr_service # Return to running the test session
60+
61+ # Cleanup
62+ adr_service .stop ()
7163
72- @pytest .fixture
73- def adr_service_query (request , pytestconfig : pytest .Config ) -> Service :
64+
65+ @pytest .fixture (scope = "module" )
66+ def adr_service_query (pytestconfig : pytest .Config ) -> Service :
7467 use_local = pytestconfig .getoption ("use_local_launcher" )
75- local_db = os .path .join ("test_data" , "query_db" )
76- db_dir = os .path .join (request .fspath .dirname , local_db )
77- tmp_docker_dir = os .path .join (
78- os .path .join (request .fspath .dirname , "test_data" ), "tmp_docker_query"
79- )
68+
69+ # Paths setup
70+ base_dir = Path (__file__ ).parent / "test_data"
71+ local_db = base_dir / "query_db"
72+ tmp_docker_dir = base_dir / "tmp_docker_query"
73+
8074 if use_local :
8175 ansys_installation = pytestconfig .getoption ("install_path" )
8276 else :
83- cleanup_docker (request )
8477 ansys_installation = "docker"
85- tmp_service = Service (
78+
79+ adr_service = Service (
8680 ansys_installation = ansys_installation ,
8781 docker_image = DOCKER_DEV_REPO_URL ,
88- db_directory = db_dir ,
89- data_directory = tmp_docker_dir ,
82+ db_directory = str ( local_db ) ,
83+ data_directory = str ( tmp_docker_dir ) ,
9084 port = 8000 + int (random () * 4000 ),
9185 )
86+
9287 if not use_local :
93- tmp_service ._container .save_config ()
94- tmp_service .start (create_db = False , exit_on_close = True , delete_db = False )
95- return tmp_service
88+ adr_service ._container .save_config ()
89+
90+ adr_service .start (create_db = False , exit_on_close = True , delete_db = False )
91+
92+ yield adr_service # Return to running the test session
93+
94+ # Cleanup
95+ adr_service .stop ()
0 commit comments