Skip to content

Commit e88518b

Browse files
committed
a bunch of assorted fixes to make pre-commit happy
Signed-off-by: NilashishC <[email protected]>
1 parent 1c624d5 commit e88518b

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

.pre-commit-config.yaml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
---
22
default_language_version:
33
# ensures that we get same behavior on CI(s) as on local machines
4-
python: python3.10
4+
python: python3.11
55
repos:
66
- repo: https://github.com/asottile/add-trailing-comma.git
7-
rev: v2.2.2
7+
rev: v3.1.0
88
hooks:
99
- id: add-trailing-comma
1010
args:
1111
- --py36-plus
1212
stages: ["manual"]
1313

1414
- repo: https://github.com/PyCQA/isort
15-
rev: 5.10.1
15+
rev: 5.13.2
1616
hooks:
1717
- id: isort
1818
name: Sort import statements using isort
1919

2020
- repo: https://github.com/pre-commit/mirrors-prettier
21-
rev: "v2.6.2"
21+
rev: v4.0.0-alpha.8
2222
hooks:
2323
- id: prettier
2424
# Original hook implementation is flaky due to *several* bugs described
@@ -45,24 +45,24 @@ repos:
4545
- prettier-plugin-toml
4646

4747
- repo: https://github.com/psf/black.git
48-
rev: 22.3.0
48+
rev: 24.3.0
4949
hooks:
5050
- id: black
5151
language_version: python3
5252

5353
- repo: https://github.com/streetsidesoftware/cspell-cli
54-
rev: v5.9.1
54+
rev: v8.6.1
5555
hooks:
5656
- id: cspell
5757
name: Spell check with cspell
5858

5959
- repo: https://github.com/Lucas-C/pre-commit-hooks.git
60-
rev: v1.1.13
60+
rev: v1.5.5
6161
hooks:
6262
- id: remove-tabs
6363

6464
- repo: https://github.com/pre-commit/pre-commit-hooks.git
65-
rev: v4.2.0
65+
rev: v4.5.0
6666
hooks:
6767
# Side-effects:
6868
- id: trailing-whitespace
@@ -82,46 +82,46 @@ repos:
8282
- id: debug-statements
8383

8484
- repo: https://github.com/codespell-project/codespell
85-
rev: v2.1.0
85+
rev: v2.2.6
8686
hooks:
8787
- id: codespell
8888

8989
- repo: https://github.com/adrienverge/yamllint.git
90-
rev: v1.26.3
90+
rev: v1.35.1
9191
hooks:
9292
- id: yamllint
9393
args:
9494
- --strict
9595
types: [file, yaml]
9696

9797
- repo: https://github.com/PyCQA/flake8.git
98-
rev: 4.0.1
98+
rev: 5.0.4
9999
hooks:
100100
- id: flake8
101101
alias: flake8
102-
language_version: python3
102+
language_version: python311
103103
additional_dependencies:
104104
- flake8-2020 >= 1.6.0
105105
- flake8-isort >= 4.1.1
106106
- darglint
107107
- flake8-docstrings # uses pydocstyle
108108

109109
- repo: https://github.com/pre-commit/mirrors-mypy.git
110-
rev: v0.942
110+
rev: v1.9.0
111111
hooks:
112112
- id: mypy
113-
alias: mypy-py310
114-
name: MyPy, for Python 3.10
113+
alias: mypy-py311
114+
name: MyPy, for Python 3.11
115115
additional_dependencies:
116116
- ansible-pylibssh
117117
- pytest
118118
- xmltodict
119119
args:
120-
- --python-version=3.10
120+
- --python-version=3.11
121121
pass_filenames: false
122122

123123
- repo: https://github.com/pycqa/pylint.git
124-
rev: v2.13.5
124+
rev: v3.1.0
125125
hooks:
126126
- id: pylint
127127
args:

src/pytest_ansible_network_integration/__init__.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# cspell:ignore nodeid
22
"""Common fixtures for tests."""
3+
34
import json
45
import logging
56
import os
@@ -18,6 +19,7 @@
1819

1920
from .defs import AnsibleProject
2021
from .defs import CmlWrapper
22+
from .defs import PytestNetworkError
2123
from .defs import VirshWrapper
2224

2325

@@ -91,11 +93,11 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
9193
"""Generate tests.
9294
9395
:param metafunc: The pytest metafunc object
94-
:raises Exception: If the options have not been set
96+
:raises PytestNetworkError: If the options have not been set
9597
"""
9698
if "integration_test_path" in metafunc.fixturenames:
9799
if not OPTIONS:
98-
raise Exception("pytest_configure not called")
100+
raise PytestNetworkError("pytest_configure not called")
99101
rootdir = Path(OPTIONS.integration_tests_path)
100102
roles = [path for path in Path(rootdir).iterdir() if path.is_dir()]
101103
# test_ids = [role.name for role in roles]
@@ -179,7 +181,7 @@ def playbook(hosts: str, role: str) -> List[Dict[str, object]]:
179181
def required_environment_variables() -> Dict[str, str]:
180182
"""Return the required environment variables.
181183
182-
:raises Exception: If the environment variables are not set
184+
:raises PytestNetworkError: If the environment variables are not set
183185
:returns: The required environment variables
184186
"""
185187
variables = {
@@ -192,7 +194,7 @@ def required_environment_variables() -> Dict[str, str]:
192194
"network_os": os.environ.get("ANSIBLE_NETWORK_OS"),
193195
}
194196
if not all(variables.values()):
195-
raise Exception("CML environment variables not set")
197+
raise PytestNetworkError("CML environment variables not set")
196198

197199
return variables # type: ignore[return-value]
198200

@@ -221,20 +223,19 @@ def _appliance_dhcp_address(env_vars: Dict[str, str]) -> Generator[str, None, No
221223
"""Build the lab and collect the appliance DHCP address.
222224
223225
:param env_vars: The environment variables
224-
:raises Exception: Missing environment variables, lab, or appliance
226+
:raises PytestNetworkError: Missing environment variables, lab, or appliance
225227
:yields: The appliance DHCP address
226228
"""
227229
_github_action_log("::group::Starting lab provisioning")
228230

229231
_print("Starting lab provisioning")
230232

231233
try:
232-
233234
if not OPTIONS:
234-
raise Exception("Missing CML lab")
235+
raise PytestNetworkError("Missing CML lab")
235236
lab_file = OPTIONS.cml_lab
236237
if not os.path.exists(lab_file):
237-
raise Exception(f"Missing lab file '{lab_file}'")
238+
raise PytestNetworkError(f"Missing lab file '{lab_file}'")
238239

239240
start = time.time()
240241
cml = CmlWrapper(
@@ -254,18 +255,18 @@ def _appliance_dhcp_address(env_vars: Dict[str, str]) -> Generator[str, None, No
254255

255256
try:
256257
ip_address = virsh.get_dhcp_lease(lab_id)
257-
except Exception as exc:
258+
except PytestNetworkError as exc:
258259
virsh.close()
259260
cml.remove()
260-
raise Exception("Failed to get DHCP lease for the appliance") from exc
261+
raise PytestNetworkError("Failed to get DHCP lease for the appliance") from exc
261262

262263
end = time.time()
263264
_print(f"Elapsed time to provision {end - start} seconds")
264265

265-
except Exception as exc:
266+
except PytestNetworkError as exc:
266267
logger.error("Failed to provision lab")
267268
_github_action_log("::endgroup::")
268-
raise Exception("Failed to provision lab") from exc
269+
raise PytestNetworkError("Failed to provision lab") from exc
269270

270271
virsh.close()
271272
_github_action_log("::endgroup::")
@@ -344,10 +345,10 @@ def environment() -> Dict[str, Any]:
344345
return env
345346

346347

347-
@pytest.hookimpl(tryfirst=True, hookwrapper=True) # type: ignore[misc]
348+
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
348349
def pytest_runtest_makereport(
349350
item: pytest.Item, *_args: Any, **_kwargs: Any
350-
) -> Generator[None, pluggy_result, None]:
351+
) -> Generator[None, pluggy_result, None]: # type: ignore[type-arg]
351352
"""Add additional information to the test item.
352353
353354
:param item: The test item

src/pytest_ansible_network_integration/defs.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
"""Common objects."""
2+
3+
from __future__ import annotations
4+
25
import logging
36
import os
47
import re
@@ -123,7 +126,7 @@ def bring_up(self, file: str) -> None:
123126
"""Bring the lab up.
124127
125128
:param file: The file
126-
:raises Exception: If the lab fails to start
129+
:raises PytestNetworkError: If the lab fails to start
127130
"""
128131
logger.info("Check if lab is already provisioned")
129132
stdout, _stderr = self._run("id")
@@ -142,7 +145,7 @@ def bring_up(self, file: str) -> None:
142145
# Starting lab xxx (ID: 9fde5f)\n
143146
current_lab_match = re.match(r".*ID: (?P<id>\S+)\)\n", stdout, re.DOTALL)
144147
if not current_lab_match:
145-
raise Exception(f"Could not get lab ID: {stdout} {stderr}")
148+
raise PytestNetworkError(f"Could not get lab ID: {stdout} {stderr}")
146149
self.current_lab_id = current_lab_match.groupdict()["id"]
147150
logger.info("Started lab id '%s'", self.current_lab_id)
148151

@@ -222,7 +225,7 @@ def get_dhcp_lease(self, current_lab_id: str) -> str:
222225
"""Get the dhcp lease.
223226
224227
:param current_lab_id: The current lab id
225-
:raises Exception: If the dhcp lease cannot be found
228+
:raises PytestNetworkError: If the dhcp lease cannot be found
226229
:return: The ip address
227230
"""
228231
attempt = 0
@@ -248,7 +251,7 @@ def get_dhcp_lease(self, current_lab_id: str) -> str:
248251
break
249252
attempt += 1
250253
if attempt == 10:
251-
raise Exception("Could not find current lab")
254+
raise PytestNetworkError("Could not find current lab")
252255
time.sleep(5)
253256

254257
macs = [
@@ -272,16 +275,27 @@ def get_dhcp_lease(self, current_lab_id: str) -> str:
272275
ips = [leases[mac] for mac in macs if mac in leases]
273276
attempt += 1
274277
if attempt == 30:
275-
raise Exception("Could not find IPs")
278+
raise PytestNetworkError("Could not find IPs")
276279
time.sleep(10)
277280

278281
logger.debug("Found IPs: %s", ips)
279282

280283
if len(ips) > 1:
281-
raise Exception("Found more than one IP")
284+
raise PytestNetworkError("Found more than one IP")
282285

283286
return ips[0]
284287

285288
def close(self) -> None:
286289
"""Close the connection."""
287290
self.ssh.close()
291+
292+
293+
class PytestNetworkError(Exception):
294+
"""Class representing exceptions raised from the pytest plugin code."""
295+
296+
def __init__(self: PytestNetworkError, message: str) -> None:
297+
"""Instantiate an object of this class.
298+
299+
:param message: The exception message.
300+
"""
301+
super().__init__(message)

0 commit comments

Comments
 (0)