Skip to content
Snippets Groups Projects
Commit 08e1f847 authored by kaiyou's avatar kaiyou
Browse files

Refactor test temporary files management

parent 5a2427a9
No related branches found
No related tags found
No related merge requests found
...@@ -7,33 +7,34 @@ import time ...@@ -7,33 +7,34 @@ import time
import requests import requests
@pytest.fixture @pytest.fixture(scope="module")
def temp(): def temp():
"""Write data to a temporary file and return the handle """Write a temporary directory with module scope and offer
This is a fixture instead of a utility function so that to write files in it, with optional subpath
temporary files have a test-scoped lifetime
""" """
handles = [] #directory = tempfile.TemporaryDirectory()
def fun(data): directory = tempfile.mkdtemp()
handle = tempfile.NamedTemporaryFile(delete=False) def write_and_get_filename(data, path=None):
if type(data) is str: if type(data) is str:
data = data.encode("utf8") data = data.encode("utf8")
handle.write(data) if path is None:
handle.flush() path = str(random.randbytes(3).hex())
handles.append(handle) filepath = os.path.join(directory, path)
return handle.name os.makedirs(os.path.dirname(filepath), 0o755, True)
yield fun with open(filepath, "wb") as handle:
del handles # Useless but explicit handle.write(data)
return filepath
return write_and_get_filename
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def app(username, password): def app(username, password, temp):
"""Run an isolated application instance and return the URL""" """Run an isolated application instance and return the URL"""
data = tempfile.TemporaryDirectory()
port = 5000 + random.randint(1, 999) # Port = 5XXX port = 5000 + random.randint(1, 999) # Port = 5XXX
url = f"http://localhost:{port}" url = f"http://localhost:{port}"
db = temp(b"", "hiboo.db")
env = os.environ env = os.environ
env.update(FLASK_APP="hiboo", SQLALCHEMY_DATABASE_URI=f"sqlite:///{data.name}/hiboo.db") env.update(FLASK_APP="hiboo", SQLALCHEMY_DATABASE_URI=f"sqlite:///{db}")
subprocess.run(["flask", "db", "upgrade"], env=env) subprocess.run(["flask", "db", "upgrade"], env=env)
subprocess.run(["flask", "user", "create", username, password], env=env) subprocess.run(["flask", "user", "create", username, password], env=env)
subprocess.run(["flask", "user", "promote", username], env=env) subprocess.run(["flask", "user", "promote", username], env=env)
...@@ -50,7 +51,6 @@ def app(username, password): ...@@ -50,7 +51,6 @@ def app(username, password):
break break
proc.terminate() proc.terminate()
proc.wait() proc.wait()
del data
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
......
...@@ -12,12 +12,18 @@ HTTPD_CONFIG = """ ...@@ -12,12 +12,18 @@ HTTPD_CONFIG = """
ServerName localhost ServerName localhost
ServerRoot /usr/lib/httpd ServerRoot /usr/lib/httpd
PidFile /tmp/apache.pid PidFile /tmp/apache.pid
<IfModule !unixd_module>
LoadModule unixd_module modules/mod_unixd.so
</IfModule>
<IfModule !log_config_module>
LoadModule log_config_module modules/mod_log_config.so
</IfModule>
LoadModule mpm_event_module modules/mod_mpm_event.so LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule env_module modules/mod_env.so LoadModule env_module modules/mod_env.so
LoadModule dir_module modules/mod_dir.so LoadModule dir_module modules/mod_dir.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule authn_core_module modules/mod_authn_core.so LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_core_module modules/mod_authz_core.so LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_user_module modules/mod_authz_user.so LoadModule authz_user_module modules/mod_authz_user.so
...@@ -31,7 +37,7 @@ HTTPD_CONFIG = """ ...@@ -31,7 +37,7 @@ HTTPD_CONFIG = """
DirectoryIndex index.html DirectoryIndex index.html
""" """
SAML_CONFIG = """" SAML_CONFIG = """
LoadModule auth_mellon_module modules/mod_auth_mellon.so LoadModule auth_mellon_module modules/mod_auth_mellon.so
<Location /> <Location />
Require valid-user Require valid-user
...@@ -46,7 +52,7 @@ SAML_CONFIG = """" ...@@ -46,7 +52,7 @@ SAML_CONFIG = """"
""" """
OIDC_CONFIG = """" OIDC_CONFIG = """
LoadModule auth_openidc_module modules/mod_auth_openidc.so LoadModule auth_openidc_module modules/mod_auth_openidc.so
OIDCProviderMetadataURL {metadata} OIDCProviderMetadataURL {metadata}
...@@ -69,18 +75,15 @@ def httpd(temp): ...@@ -69,18 +75,15 @@ def httpd(temp):
Multiple servers may run and will all be closed at the end, they all share Multiple servers may run and will all be closed at the end, they all share
a root directory""" a root directory"""
proc = [] proc = []
rootDirectory = tempfile.TemporaryDirectory() root = os.path.dirname(temp("Hello world!", "root/index.html"))
with open(os.path.join(rootDirectory.name, "index.html"), "w") as handle:
handle.write("Hello world!")
def start(config): def start(config):
config_file = temp(HTTPD_CONFIG.format(root=rootDirectory.name) + config) config_file = temp(HTTPD_CONFIG.format(root=root) + config)
proc.append(subprocess.Popen(["httpd", "-DFOREGROUND", "-f", config_file])) proc.append(subprocess.Popen(["httpd", "-DFOREGROUND", "-f", config_file]))
time.sleep(1) # Sleep so apache can start time.sleep(1) # Sleep so apache can start
yield start yield start
for pid in proc: for pid in proc:
pid.terminate() pid.terminate()
pid.wait() pid.wait()
del rootDirectory
def do_login(page, username, password): def do_login(page, username, password):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment