1
1
"""Global fixtures go here."""
2
- import os
2
+
3
+ from pathlib import Path
3
4
from random import choice , random
4
- import shutil
5
5
from string import ascii_letters
6
- import subprocess
7
6
8
7
import pytest
9
8
@@ -16,26 +15,7 @@ def pytest_addoption(parser):
16
15
parser .addoption ("--install-path" , action = "store" , default = "dev.json" )
17
16
18
17
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" )
39
19
def get_exec (pytestconfig : pytest .Config ) -> str :
40
20
exec_basis = ""
41
21
use_local = pytestconfig .getoption ("use_local_launcher" )
@@ -44,52 +24,72 @@ def get_exec(pytestconfig: pytest.Config) -> str:
44
24
return exec_basis
45
25
46
26
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 :
49
29
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
+
53
37
if use_local :
54
- tmp_service = Service (
38
+ adr_service = Service (
55
39
ansys_installation = pytestconfig .getoption ("install_path" ),
56
40
docker_image = DOCKER_DEV_REPO_URL ,
57
- db_directory = db_dir ,
41
+ db_directory = str ( db_dir ) ,
58
42
port = 8000 + int (random () * 4000 ),
59
43
)
60
44
else :
61
- cleanup_docker (request )
62
- tmp_service = Service (
45
+ adr_service = Service (
63
46
ansys_installation = "docker" ,
64
47
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 ) ,
67
50
port = 8000 + int (random () * 4000 ),
68
51
)
69
- return tmp_service
70
52
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 ()
71
63
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 :
74
67
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
+
80
74
if use_local :
81
75
ansys_installation = pytestconfig .getoption ("install_path" )
82
76
else :
83
- cleanup_docker (request )
84
77
ansys_installation = "docker"
85
- tmp_service = Service (
78
+
79
+ adr_service = Service (
86
80
ansys_installation = ansys_installation ,
87
81
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 ) ,
90
84
port = 8000 + int (random () * 4000 ),
91
85
)
86
+
92
87
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