Skip to content

Commit 6174eda

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 6babd39 commit 6174eda

3 files changed

Lines changed: 15 additions & 9 deletions

File tree

conftest.py

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

361-
@pytest.fixture(scope='session')
361+
@pytest.fixture(scope='function')
362362
def host_with_hsts(host: Host) -> Generator[Host, None, None]:
363363
host.enable_hsts_header()
364364
yield host

lib/common.py

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

191-
def wait_for(fn: Callable[[], object], msg: str | None = None, timeout_secs: int = 2 * 60, retry_delay_secs: int = 2,
192-
invert: bool = False) -> None:
191+
def wait_for(fn: Callable[[], T], msg: str | None = None, timeout_secs: int = 2 * 60, retry_delay_secs: int = 2,
192+
invert: bool = False) -> T:
193193
if msg is not None:
194194
logging.info(msg)
195195
start_time = time.perf_counter()
196196
while True:
197197
ret = fn()
198198
if not invert and ret:
199-
return
199+
return ret
200200
if invert and not ret:
201-
return
201+
return ret
202202
if time.perf_counter() - start_time >= timeout_secs:
203203
expected = 'True' if not invert else 'False'
204204
raise TimeoutError(

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)