Skip to content

Commit 1358ffc

Browse files
authored
Merge pull request #402 from xcp-ng/gln/xapi-network-typehints
2 parents 5b696ad + 1468f65 commit 1358ffc

13 files changed

Lines changed: 61 additions & 35 deletions

File tree

tests/network/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import pytest
22

3+
from lib.host import Host
4+
35
@pytest.fixture(scope='package')
4-
def host_no_sdn_controller(host):
6+
def host_no_sdn_controller(host: Host) -> None:
57
""" An XCP-ng with no SDN controller. """
68
if host.xe('sdn-controller-list', minimal=True):
79
pytest.skip("This test requires an XCP-ng with no SDN controller")

tests/network/test_management_disable_address_type.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from lib.host import Host
2+
13
# Requirements:
24
# - one XCP-ng host (--hosts) (>= 8.3 for IPv6 test)
35

4-
def test_management_disable_address_type(host):
6+
def test_management_disable_address_type(host: Host) -> None:
57
management_pif = host.management_pif()
68
type = management_pif.param_get("primary-address-type").lower()
79

tests/network/test_vif_allowed_ip.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
# - one XCP-ng host (--hosts) >= 8.2 (>= 8.3 for the CIDR tests) with no SDN controller configured
1010
# - a VM (--vm)
1111

12-
def ip_responsive(ip):
12+
def ip_responsive(ip: str) -> bool:
1313
return not os.system(f"ping -c 3 -W 10 {ip} > /dev/null 2>&1")
1414

1515
@pytest.mark.small_vm
1616
@pytest.mark.usefixtures("host_no_sdn_controller")
1717
class TestAllowedIP:
18-
def test_unallowed_ip(self, running_vm: VM):
18+
def test_unallowed_ip(self, running_vm: VM) -> None:
1919
vm = running_vm
2020
vif = vm.vifs()[0]
2121
ip = vm.ip
@@ -32,7 +32,7 @@ def test_unallowed_ip(self, running_vm: VM):
3232
vif.param_clear(f"ipv{ip_family}-allowed")
3333
vif.param_set("locking-mode", "unlocked")
3434

35-
def test_allowed_ip(self, running_vm):
35+
def test_allowed_ip(self, running_vm: VM) -> None:
3636
vm = running_vm
3737
vif = vm.vifs()[0]
3838
ip = vm.ip
@@ -55,7 +55,7 @@ def test_allowed_ip(self, running_vm):
5555
@pytest.mark.small_vm
5656
@pytest.mark.usefixtures("host_at_least_8_3", "host_no_sdn_controller")
5757
class TestAllowedCIDR:
58-
def test_unallowed_cidr(self, running_vm: VM):
58+
def test_unallowed_cidr(self, running_vm: VM) -> None:
5959
vm = running_vm
6060
vif = vm.vifs()[0]
6161
ip = vm.ip
@@ -73,7 +73,7 @@ def test_unallowed_cidr(self, running_vm: VM):
7373
vif.param_clear(f"ipv{ip_family}-allowed")
7474
vif.param_set("locking-mode", "unlocked")
7575

76-
def test_allowed_cidr(self, running_vm: VM):
76+
def test_allowed_cidr(self, running_vm: VM) -> None:
7777
vm = running_vm
7878
vif = vm.vifs()[0]
7979
ip = vm.ip

tests/network/test_vif_management.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def count_interfaces(vm: VM) -> int:
2020

2121
@pytest.mark.small_vm
2222
class TestVIFManagement:
23-
def test_vif_management(self, running_unix_vm: VM):
23+
def test_vif_management(self, running_unix_vm: VM) -> None:
2424
vm = running_unix_vm
2525
host = vm.host
2626
network_uuid = host.management_network()

tests/xapi/tls_verification/test_tls_verification.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from lib.common import strtobool
77
from lib.host import Host
88

9+
from typing import Generator
10+
911
# Requirements:
1012
# From --hosts parameter:
1113
# - A XCP-ng >= 8.3 pool with at least two hosts
@@ -21,18 +23,19 @@
2123
XAPI_POOL_PEM_FILEPATH = f'/etc/xensource/{XAPI_POOL_PEM_FILENAME}'
2224

2325
@pytest.fixture(scope="module")
24-
def host_with_tls_verification_enabled(hostA1):
26+
def host_with_tls_verification_enabled(hostA1: Host) -> Generator[Host, None, None]:
2527
for h in hostA1.pool.hosts:
2628
logging.info(f"Check that TLS verification is enabled on host {h}")
2729
assert strtobool(h.param_get("tls-verification-enabled")), f"TLS verification must be enabled on host {h}"
2830
logging.info(f"Check that the host certificate exists on host {h}")
2931
cert_uuid = hostA1.xe('certificate-list', {'host': h.uuid, 'type': 'host_internal'}, minimal=True)
3032
assert len(cert_uuid) > 0, f"A host_internal certificate must exist on host {h}"
33+
yield hostA1
3134

3235

3336
@pytest.mark.usefixtures("host_at_least_8_3", "host_with_tls_verification_enabled")
3437
class TestTLSVerification:
35-
def _test_tls_verification(self, hostA1: Host, with_toolstack_restart=False):
38+
def _test_tls_verification(self, hostA1: Host, with_toolstack_restart: bool = False) -> None:
3639
for h in hostA1.pool.hosts[1:]:
3740
logging.info(f"Establish a connexion from host {hostA1} to host {h} by running 'xe host-dmesg'")
3841
hostA1.xe('host-dmesg', {'host': h.uuid})
@@ -44,10 +47,10 @@ def _test_tls_verification(self, hostA1: Host, with_toolstack_restart=False):
4447
logging.info(f"Test connexion from host {h} to host {hostA1} by running 'xe host-dmesg'")
4548
h.xe('host-dmesg', {'host': hostA1.uuid})
4649

47-
def test_tls_verification(self, hostA1):
50+
def test_tls_verification(self, hostA1: Host) -> None:
4851
self._test_tls_verification(hostA1)
4952

50-
def test_refresh_certificate(self, hostA1):
53+
def test_refresh_certificate(self, hostA1: Host) -> None:
5154
logging.info("Refresh the xapi:pool certificate on every pool member")
5255
for h in hostA1.pool.hosts:
5356
old_checksum = h.ssh(f'md5sum {XAPI_POOL_PEM_FILEPATH}').split()[0]
@@ -58,7 +61,7 @@ def test_refresh_certificate(self, hostA1):
5861
self._test_tls_verification(hostA1, with_toolstack_restart=True)
5962

6063
@pytest.fixture(scope="function")
61-
def hostA2_with_saved_cert(self, hostA2: Host):
64+
def hostA2_with_saved_cert(self, hostA2: Host) -> Generator[Host, None, None]:
6265
tmp_dir = hostA2.ssh('mktemp -d')
6366
logging.info(f"Save {XAPI_POOL_PEM_FILEPATH} on {hostA2}")
6467
hostA2.ssh(f'cp {XAPI_POOL_PEM_FILEPATH} {tmp_dir}')
@@ -68,7 +71,7 @@ def hostA2_with_saved_cert(self, hostA2: Host):
6871
hostA2.ssh(f'rm -r {tmp_dir}')
6972
hostA2.ssh('systemctl reload-or-restart stunnel@xapi')
7073

71-
def test_break_cert(self, hostA1: Host, hostA2_with_saved_cert: Host):
74+
def test_break_cert(self, hostA1: Host, hostA2_with_saved_cert: Host) -> None:
7275
hostA2 = hostA2_with_saved_cert
7376
logging.info(f"Replace the certificate on host {hostA2}")
7477
hostA2.ssh(f'rm {XAPI_POOL_PEM_FILEPATH}')
@@ -88,7 +91,7 @@ def test_break_cert(self, hostA1: Host, hostA2_with_saved_cert: Host):
8891
The server may be switched off or there may be network connectivity problems." in excinfo.value.stdout
8992
)
9093

91-
def test_toolstack_restart(self, hostA1: Host, hostA2: Host):
94+
def test_toolstack_restart(self, hostA1: Host, hostA2: Host) -> None:
9295
"""
9396
Same test as the previous one, but we don't break the cert.
9497
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from lib.host import Host
2+
13
# Requirements:
24
# From --hosts parameter:
35
# - host(A1): first XCP-ng host > 8.2.
46

5-
def test_get_hyperthreading(host):
7+
def test_get_hyperthreading(host: Host) -> None:
68
host.call_plugin('hyperthreading.py', 'get_hyperthreading')
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from lib.host import Host
2+
13
# Requirements:
24
# From --hosts parameter:
35
# - host(A1): first XCP-ng host > 8.2.
46

5-
def test_list_block_devices(host):
7+
def test_list_block_devices(host: Host) -> None:
68
host.call_plugin('lsblk.py', 'list_block_devices')

tests/xapi_plugins/plugin_netdata/test_netdata.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import pytest
22

33
from lib.common import strtobool
4+
from lib.host import Host
5+
6+
from typing import Generator
47

58
# Requirements:
69
# From --hosts parameter:
@@ -9,15 +12,15 @@
912
# - access to XCP-ng RPM repository from hostA1
1013

1114
@pytest.fixture(scope='module')
12-
def host_without_netdata(host):
15+
def host_without_netdata(host: Host) -> Generator[Host, None, None]:
1316
assert not strtobool(host.call_plugin('netdata.py', 'is_netdata_installed'))
1417
yield host
1518

1619
class TestInstall:
17-
def test_is_netdata_installed(self, host):
20+
def test_is_netdata_installed(self, host: Host) -> None:
1821
host.call_plugin('netdata.py', 'is_netdata_installed')
1922

20-
def test_install_netdata(self, host_without_netdata):
23+
def test_install_netdata(self, host_without_netdata: Host) -> None:
2124
host = host_without_netdata
2225
host.yum_save_state()
2326
host.call_plugin('netdata.py', 'install_netdata', {
@@ -29,7 +32,7 @@ def test_install_netdata(self, host_without_netdata):
2932
host.yum_restore_saved_state()
3033

3134
class TestApiKey:
32-
def test_get_netdata_api_key(self, host_without_netdata):
35+
def test_get_netdata_api_key(self, host_without_netdata: Host) -> None:
3336
host = host_without_netdata
3437
host.yum_save_state()
3538
host.call_plugin('netdata.py', 'install_netdata', {

tests/xapi_plugins/plugin_raid/test_raid.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
from lib.host import Host
66

7+
from typing import Generator
8+
79
# Requirements:
810
# From --hosts parameter:
911
# - host(A1): first XCP-ng host > 8.2.
1012

1113
@pytest.fixture(scope='module')
12-
def host_with_raid(host: Host):
14+
def host_with_raid(host: Host) -> Generator[Host, None, None]:
1315
dummy_raid = False
1416
if not host.file_exists('/dev/md127', regular_file=False):
1517
logging.info("> Host has no raids, creating one for tests")
@@ -29,6 +31,6 @@ def host_with_raid(host: Host):
2931
host.ssh('losetup -d /dev/loop1')
3032
host.ssh('rm -rf raid-1 raid-0')
3133

32-
def test_check_raid_pool(host_with_raid):
34+
def test_check_raid_pool(host_with_raid: Host) -> None:
3335
host = host_with_raid
3436
host.call_plugin('raid.py', 'check_raid_pool')

tests/xapi_plugins/plugin_smartctl/test_smartctl.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
import json
44

5+
from lib.host import Host
6+
57
# Requirements:
68
# From --hosts parameter:
79
# - host(A1): first XCP-ng host >= 8.3.
810

9-
def _call_plugin(host, fn):
11+
def _call_plugin(host: Host, fn: str) -> None:
1012
ret = host.call_plugin("smartctl.py", fn)
1113
try:
1214
json.loads(ret)
1315
except ValueError:
1416
pytest.fail("JSON string was expected but ValueError was raised")
1517

1618
@pytest.mark.usefixtures("host_at_least_8_3")
17-
def test_smartctl_information(host):
19+
def test_smartctl_information(host: Host) -> None:
1820
_call_plugin(host, "information")
1921

2022
@pytest.mark.usefixtures("host_at_least_8_3")
21-
def test_smartctl_health(host):
23+
def test_smartctl_health(host: Host) -> None:
2224
_call_plugin(host, "health")

0 commit comments

Comments
 (0)