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
import requests
@pytest.fixture
@pytest.fixture(scope="module")
def temp():
"""Write data to a temporary file and return the handle
This is a fixture instead of a utility function so that
temporary files have a test-scoped lifetime
"""Write a temporary directory with module scope and offer
to write files in it, with optional subpath
"""
handles = []
def fun(data):
handle = tempfile.NamedTemporaryFile(delete=False)
#directory = tempfile.TemporaryDirectory()
directory = tempfile.mkdtemp()
def write_and_get_filename(data, path=None):
if type(data) is str:
data = data.encode("utf8")
handle.write(data)
handle.flush()
handles.append(handle)
return handle.name
yield fun
del handles # Useless but explicit
if path is None:
path = str(random.randbytes(3).hex())
filepath = os.path.join(directory, path)
os.makedirs(os.path.dirname(filepath), 0o755, True)
with open(filepath, "wb") as handle:
handle.write(data)
return filepath
return write_and_get_filename
@pytest.fixture(scope="module")
def app(username, password):
def app(username, password, temp):
"""Run an isolated application instance and return the URL"""
data = tempfile.TemporaryDirectory()
port = 5000 + random.randint(1, 999) # Port = 5XXX
url = f"http://localhost:{port}"
db = temp(b"", "hiboo.db")
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", "user", "create", username, password], env=env)
subprocess.run(["flask", "user", "promote", username], env=env)
......@@ -50,7 +51,6 @@ def app(username, password):
break
proc.terminate()
proc.wait()
del data
@pytest.fixture(scope="session")
......
......@@ -12,12 +12,18 @@ HTTPD_CONFIG = """
ServerName localhost
ServerRoot /usr/lib/httpd
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 unixd_module modules/mod_unixd.so
LoadModule env_module modules/mod_env.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 authz_core_module modules/mod_authz_core.so
LoadModule authz_user_module modules/mod_authz_user.so
......@@ -31,7 +37,7 @@ HTTPD_CONFIG = """
DirectoryIndex index.html
"""
SAML_CONFIG = """"
SAML_CONFIG = """
LoadModule auth_mellon_module modules/mod_auth_mellon.so
<Location />
Require valid-user
......@@ -46,7 +52,7 @@ SAML_CONFIG = """"
"""
OIDC_CONFIG = """"
OIDC_CONFIG = """
LoadModule auth_openidc_module modules/mod_auth_openidc.so
OIDCProviderMetadataURL {metadata}
......@@ -69,18 +75,15 @@ def httpd(temp):
Multiple servers may run and will all be closed at the end, they all share
a root directory"""
proc = []
rootDirectory = tempfile.TemporaryDirectory()
with open(os.path.join(rootDirectory.name, "index.html"), "w") as handle:
handle.write("Hello world!")
root = os.path.dirname(temp("Hello world!", "root/index.html"))
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]))
time.sleep(1) # Sleep so apache can start
yield start
for pid in proc:
pid.terminate()
pid.wait()
del rootDirectory
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