Skip to content

Commit a89e790

Browse files
committed
Make HSTS test infrastructure more robust
Enhance wait_for to return the callback value and use it to retry file server header retrieval until the server is ready. Change host_with_hsts fixture to function scope for test isolation. Signed-off-by: Gaëtan Lehmann <gaetan.lehmann@vates.tech>
1 parent ca9eb28 commit a89e790

4 files changed

Lines changed: 16 additions & 10 deletions

File tree

conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def host_less_than_8_3(host: Host) -> None:
426426
if not host.xcp_version < version.parse(version_str):
427427
pytest.skip(f"This test requires an XCP-ng < {version_str} host")
428428

429-
@pytest.fixture(scope='session')
429+
@pytest.fixture(scope='function')
430430
def host_with_hsts(host: Host) -> Generator[Host, None, None]:
431431
host.enable_hsts_header()
432432
yield host

lib/common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,17 @@ def callable_marker(value: T | Callable[..., T], request: pytest.FixtureRequest)
192192
else:
193193
return value
194194

195-
def wait_for(fn: Callable[[], object], msg: str | None = None, timeout_secs: int = 2 * 60, retry_delay_secs: int = 2,
196-
invert: bool = False) -> None:
195+
def wait_for(fn: Callable[[], T], msg: str | None = None, timeout_secs: int = 2 * 60, retry_delay_secs: int = 2,
196+
invert: bool = False) -> T:
197197
if msg is not None:
198198
logging.info(msg)
199199
start_time = time.perf_counter()
200200
while True:
201201
ret = fn()
202202
if not invert and ret:
203-
return
203+
return ret
204204
if invert and not ret:
205-
return
205+
return ret
206206
if time.perf_counter() - start_time >= timeout_secs:
207207
expected = 'True' if not invert else 'False'
208208
raise TimeoutError(

lib/installer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def inner():
9696
if failed:
9797
raise InstallationFailed(failed)
9898
return cmd()
99-
return wait_for(inner, msg, timeout_secs=timeout_secs)
99+
wait_for(inner, msg, timeout_secs=timeout_secs)
100100

101101
def monitor_install(*, ip: str) -> None:
102102
# wait for "yum install" phase to finish

tests/misc/test_file_server.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44

55
import lib.commands as commands
6+
from lib.common import wait_for
67
from lib.host import Host
78
from lib.netutil import wrap_ip
89

@@ -31,10 +32,15 @@ class TestHSTS:
3132

3233
@staticmethod
3334
def __get_header(host: Host) -> list[str]:
34-
res = commands.local_cmd(
35-
["curl", "-s", "-XGET", "-k", "-I", "https://" + wrap_ip(host.hostname_or_ip)]
36-
)
37-
return res.stdout.splitlines()
35+
def get_or_none():
36+
res = commands.local_cmd(["curl", "-s", "-XGET", "-k", "-I", "https://"
37+
+ wrap_ip(host.hostname_or_ip)], check=False)
38+
if res.returncode != 0:
39+
return None
40+
return res.stdout.splitlines()
41+
headers = wait_for(get_or_none)
42+
assert headers is not None
43+
return headers
3844

3945
def test_fileserver_hsts_default(self, host: Host) -> None:
4046
# By default HSTS header should not be set

0 commit comments

Comments
 (0)