Skip to content

Commit edaeb98

Browse files
committed
Updates
1 parent 464dcc0 commit edaeb98

File tree

3 files changed

+108
-29
lines changed

3 files changed

+108
-29
lines changed

.config/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ testpaths
2424
virl
2525
virsh
2626
xmltodict
27+
pluggy

mypy.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ strict_optional = true
1616
# No type hints as of version 0.4.0
1717
ignore_missing_imports = true
1818

19-
19+
[mypy-pluggy.*]
20+
# No type hints as of version 1.0.0
21+
ignore_missing_imports = true
2022

2123
[mypy-xmltodict]
2224
# No type hints as of version 0.12.0

src/pytest_ansible_network_integration/__init__.py

Lines changed: 104 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
import json
55
import logging
66
import os
7+
import re
78
import time
89

910
from pathlib import Path
1011
from typing import Any
12+
from typing import Callable
1113
from typing import Dict
1214
from typing import Generator
1315
from typing import List
1416

1517
import pytest
1618

19+
from pluggy._result import _Result as pluggy_result
20+
1721
from .defs import AnsibleProject
1822
from .defs import CmlWrapper
1923
from .defs import VirshWrapper
@@ -185,6 +189,24 @@ def required_environment_variables() -> Dict[str, str]:
185189
return variables # type: ignore[return-value]
186190

187191

192+
def _github_action_log(message: str) -> None:
193+
"""Log a message to GitHub Actions.
194+
195+
:param message: The message
196+
"""
197+
if os.environ.get("GITHUB_ACTIONS"):
198+
print(f"\n{message}", flush=True)
199+
200+
201+
@pytest.fixture(scope="session", autouse=True)
202+
def github_action_log() -> Callable[[str], None]:
203+
"""Log a message to GitHub Actions.
204+
205+
:returns: The log function
206+
"""
207+
return _github_action_log
208+
209+
188210
@pytest.fixture(scope="session", name="appliance_dhcp_address")
189211
def _appliance_dhcp_address(env_vars: Dict[str, str]) -> Generator[str, None, None]:
190212
"""Build the lab and collect the appliance DHCP address.
@@ -193,45 +215,57 @@ def _appliance_dhcp_address(env_vars: Dict[str, str]) -> Generator[str, None, No
193215
:raises Exception: Missing environment variables, lab, or appliance
194216
:yields: The appliance DHCP address
195217
"""
196-
logger.info("Starting lab provisioning")
218+
_github_action_log("::group::Starting lab provisioning")
197219

198-
if not OPTIONS:
199-
raise Exception("Missing CML lab")
200-
lab_file = OPTIONS.cml_lab
201-
if not os.path.exists(lab_file):
202-
raise Exception(f"Missing lab file '{lab_file}'")
220+
logger.info("Starting lab provisioning")
203221

204-
start = time.time()
205-
cml = CmlWrapper(
206-
host=env_vars["cml_host"],
207-
username=env_vars["cml_ui_user"],
208-
password=env_vars["cml_ui_password"],
209-
)
210-
cml.bring_up(file=lab_file)
211-
lab_id = cml.current_lab_id
222+
try:
212223

213-
virsh = VirshWrapper(
214-
host=env_vars["cml_host"],
215-
user=env_vars["cml_ssh_user"],
216-
password=env_vars["cml_ssh_password"],
217-
port=int(env_vars["cml_ssh_port"]),
218-
)
224+
if not OPTIONS:
225+
raise Exception("Missing CML lab")
226+
lab_file = OPTIONS.cml_lab
227+
if not os.path.exists(lab_file):
228+
raise Exception(f"Missing lab file '{lab_file}'")
229+
230+
start = time.time()
231+
cml = CmlWrapper(
232+
host=env_vars["cml_host"],
233+
username=env_vars["cml_ui_user"],
234+
password=env_vars["cml_ui_password"],
235+
)
236+
cml.bring_up(file=lab_file)
237+
lab_id = cml.current_lab_id
238+
239+
virsh = VirshWrapper(
240+
host=env_vars["cml_host"],
241+
user=env_vars["cml_ssh_user"],
242+
password=env_vars["cml_ssh_password"],
243+
port=int(env_vars["cml_ssh_port"]),
244+
)
245+
246+
try:
247+
ip_address = virsh.get_dhcp_lease(lab_id)
248+
except Exception as exc:
249+
virsh.close()
250+
cml.remove()
251+
raise Exception("Failed to get DHCP lease for the appliance") from exc
252+
253+
end = time.time()
254+
logger.info("Elapsed time to provision %s seconds", end - start)
219255

220-
try:
221-
ip_address = virsh.get_dhcp_lease(lab_id)
222256
except Exception as exc:
223-
virsh.close()
224-
cml.remove()
225-
raise Exception("Failed to get DHCP lease for the appliance") from exc
226-
227-
end = time.time()
228-
logger.info("Elapsed time to provision %s seconds", end - start)
257+
logger.error("Failed to provision lab")
258+
_github_action_log("::endgroup::")
259+
raise Exception("Failed to provision lab") from exc
229260

230261
virsh.close()
262+
_github_action_log("::endgroup::")
231263

232264
yield ip_address
233265

266+
_github_action_log("::group::Removing lab")
234267
cml.remove()
268+
_github_action_log("::endgroup::")
235269

236270

237271
@pytest.fixture
@@ -293,3 +327,45 @@ def environment() -> Dict[str, Any]:
293327
if "VIRTUAL_ENV" in os.environ:
294328
env["PATH"] = os.path.join(os.environ["VIRTUAL_ENV"], "bin") + os.pathsep + env["PATH"]
295329
return env
330+
331+
332+
@pytest.hookimpl(tryfirst=True, hookwrapper=True) # type: ignore[misc]
333+
def pytest_runtest_makereport(
334+
item: pytest.Item, *_args: Any, **_kwargs: Any
335+
) -> Generator[None, pluggy_result, None]:
336+
"""Add additional information to the test item.
337+
338+
:param item: The test item
339+
:param _args: The positional arguments
340+
:param _kwargs: The keyword arguments
341+
:yields: To all other hooks
342+
"""
343+
# execute all other hooks to obtain the report object
344+
outcome = yield
345+
rep = outcome.get_result()
346+
347+
# set a report attribute for each phase of a call, which can
348+
# be "setup", "call", "teardown"
349+
350+
setattr(item, "rep_" + rep.when, rep)
351+
352+
353+
@pytest.fixture(autouse=True)
354+
def github_log(request: pytest.FixtureRequest) -> Generator[None, None, None]:
355+
"""Log a message to GitHub Actions.
356+
357+
:param request: The request
358+
:yields: To the test
359+
"""
360+
if not os.environ.get("GITHUB_ACTIONS"):
361+
return
362+
363+
name = request.node.name
364+
365+
_github_action_log(f"::group::Run integration test: '{name}'")
366+
yield
367+
368+
if request.node.rep_setup.passed and request.node.rep_call.failed:
369+
_github_action_log(f"::error title=Integration test failure::{name}")
370+
371+
_github_action_log("::endgroup::")

0 commit comments

Comments
 (0)