Skip to content

Commit 95ee4c9

Browse files
authored
Merge branch 'main' into pre-commit-ci-update-config
2 parents d332bf3 + 1d7f477 commit 95ee4c9

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/pytest_ansible_network_integration/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import json
55
import logging
66
import os
7+
import sys
78
import time
89

910
from pathlib import Path
@@ -26,15 +27,15 @@
2627
from .utils import playbook
2728

2829

30+
logger = logging.getLogger(__name__)
31+
2932
# Configure logging
3033
logging.basicConfig(
3134
level=logging.DEBUG,
3235
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", # cspell:ignore levelname
33-
handlers=[logging.FileHandler("pytest-network.log"), logging.StreamHandler()],
36+
handlers=[logging.FileHandler("pytest-network.log"), logging.StreamHandler(sys.stdout)],
3437
)
3538

36-
logger = logging.getLogger(__name__)
37-
3839

3940
@pytest.fixture(scope="function")
4041
def network_test_vars(request: pytest.FixtureRequest) -> Dict[str, Any]:
@@ -104,6 +105,7 @@ def pytest_addoption(parser: pytest.Parser) -> None:
104105
action="store",
105106
help="The comma delimited negative search substring to filter the roles",
106107
)
108+
parser.addoption("--wait-extra", action="store", help="Add extra wait time in seconds.")
107109

108110

109111
OPTIONS = None
@@ -357,8 +359,21 @@ def _appliance_dhcp_address(env_vars: Dict[str, str]) -> Generator[str, None, No
357359
port=int(env_vars["cml_ssh_port"]),
358360
)
359361

362+
# Handle the wait_extra_time
363+
wait_extra_time = OPTIONS.wait_extra
364+
wait_seconds = 0
365+
if wait_extra_time:
366+
try:
367+
wait_seconds = int(wait_extra_time)
368+
except ValueError:
369+
logger.warning(
370+
"Invalid wait_extra value: '%s'. Expected an integer. Skipping extra wait.",
371+
wait_extra_time,
372+
)
373+
wait_seconds = 0
374+
360375
try:
361-
ip_address = virsh.get_dhcp_lease(lab_id)
376+
ip_address = virsh.get_dhcp_lease(lab_id, wait_seconds)
362377
except PytestNetworkError as exc:
363378
logger.error("Failed to get DHCP lease for the appliance")
364379
virsh.close()

src/pytest_ansible_network_integration/defs.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,11 @@ def __init__(self, host: str, user: str, password: str, port: int) -> None:
277277
self.ssh.connect()
278278
logger.info("Connected to virsh host %s", host)
279279

280-
def get_dhcp_lease(self, current_lab_id: str) -> str:
280+
def get_dhcp_lease(self, current_lab_id: str, wait_extra: int) -> str:
281281
"""Get the DHCP lease for the specified lab.
282282
283283
:param current_lab_id: The current lab ID.
284+
:param wait_extra: The number of seconds to wait before finding the DHCP lease.
284285
:raises PytestNetworkError: If the DHCP lease cannot be found.
285286
:return: The IP address associated with the lab.
286287
"""
@@ -290,9 +291,14 @@ def get_dhcp_lease(self, current_lab_id: str) -> str:
290291
macs = self._extract_macs(current_lab)
291292
logger.info("Found MAC addresses: %s", macs)
292293

293-
ips = self._find_dhcp_lease(macs, 100)
294+
ips = self._find_dhcp_lease(macs, 200)
294295
logger.debug("Found IPs: %s", ips)
295296

297+
if wait_extra:
298+
logger.info("Waiting for extra %s seconds", wait_extra)
299+
time.sleep(wait_extra)
300+
logger.info("Done waiting, starting to find IPs")
301+
296302
if len(ips) > 1:
297303
logger.error("Found more than one IP: %s", ips)
298304
raise PytestNetworkError("Found more than one IP")

0 commit comments

Comments
 (0)