Skip to content

Commit 71694ec

Browse files
Feature: Add option extra wait time (#31)
* Feature: Add option extra wait time * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix precommit * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 201e8f8 commit 71694ec

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/pytest_ansible_network_integration/__init__.py

Lines changed: 18 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,20 @@ 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+
if wait_extra_time:
365+
try:
366+
wait_seconds = int(wait_extra_time)
367+
except ValueError:
368+
logger.warning(
369+
"Invalid wait_extra value: '%s'. Expected an integer. Skipping extra wait.",
370+
wait_extra_time,
371+
)
372+
wait_seconds = 0
373+
360374
try:
361-
ip_address = virsh.get_dhcp_lease(lab_id)
375+
ip_address = virsh.get_dhcp_lease(lab_id, wait_seconds)
362376
except PytestNetworkError as exc:
363377
logger.error("Failed to get DHCP lease for the appliance")
364378
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)