Skip to content

Commit e6dba75

Browse files
authored
Merge pull request #400 from xcp-ng/gln/storage-typehints
2 parents f203beb + 63d20a9 commit e6dba75

59 files changed

Lines changed: 553 additions & 313 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/storage/cephfs/conftest.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,52 @@
44

55
from lib import config
66
from lib.common import exec_nofail, raise_errors
7+
from lib.host import Host
8+
from lib.pool import Pool
9+
from lib.sr import SR
10+
from lib.vdi import VDI
11+
from lib.vm import VM
712

813
# explicit import for package-scope fixtures
914
from pkgfixtures import pool_with_saved_yum_state
1015

16+
from typing import Generator
17+
1118
@pytest.fixture(scope='package')
12-
def pool_without_ceph(host):
19+
def pool_without_ceph(host: Host) -> Generator[Pool, None, None]:
1320
for h in host.pool.hosts:
1421
assert not host.file_exists('/usr/sbin/mount.ceph'), \
1522
"mount.ceph must not be installed on the host at the beginning of the tests"
1623
yield host.pool
1724

1825
@pytest.fixture(scope='package')
19-
def pool_with_ceph(pool_without_ceph, pool_with_saved_yum_state):
26+
def pool_with_ceph(pool_without_ceph: Pool, pool_with_saved_yum_state: Pool) -> Generator[Pool, None, None]:
2027
pool = pool_with_saved_yum_state
2128
for h in pool.hosts:
2229
h.yum_install(['centos-release-ceph-jewel'], enablerepo="base,extras")
2330
h.yum_install(['ceph-common'], enablerepo="base,extras")
2431
yield pool
2532

2633
@pytest.fixture(scope='package')
27-
def cephfs_device_config():
34+
def cephfs_device_config() -> dict[str, str]:
2835
return config.sr_device_config("CEPHFS_DEVICE_CONFIG")
2936

3037
@pytest.fixture(scope='package')
31-
def cephfs_sr(host, cephfs_device_config, pool_with_ceph):
38+
def cephfs_sr(host: Host, cephfs_device_config: dict[str, str], pool_with_ceph: Pool) -> Generator[SR, None, None]:
3239
""" A CephFS SR on first host. """
3340
sr = host.sr_create('cephfs', "CephFS-SR-test", cephfs_device_config, shared=True)
3441
yield sr
3542
# teardown
3643
sr.destroy()
3744

3845
@pytest.fixture(scope='module')
39-
def vdi_on_cephfs_sr(cephfs_sr):
46+
def vdi_on_cephfs_sr(cephfs_sr: SR) -> Generator[VDI, None, None]:
4047
vdi = cephfs_sr.create_vdi('CephFS-VDI-test')
4148
yield vdi
4249
vdi.destroy()
4350

4451
@pytest.fixture(scope='module')
45-
def vm_on_cephfs_sr(host, cephfs_sr, vm_ref):
52+
def vm_on_cephfs_sr(host: Host, cephfs_sr: SR, vm_ref: str) -> Generator[VM, None, None]:
4653
vm = host.import_vm(vm_ref, sr_uuid=cephfs_sr.uuid)
4754
yield vm
4855
# teardown

tests/storage/cephfs/test_cephfs_sr.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
from lib.commands import SSHCommandFailed
77
from lib.common import vm_image, wait_for
8+
from lib.host import Host
9+
from lib.pool import Pool
10+
from lib.sr import SR
11+
from lib.vdi import VDI
12+
from lib.vm import VM
813
from tests.storage import vdi_is_open
914

1015
# Requirements:
@@ -19,7 +24,7 @@ class TestCephFSSRCreateDestroy:
1924
and VM import.
2025
"""
2126

22-
def test_create_cephfs_sr_without_ceph(self, host, cephfs_device_config):
27+
def test_create_cephfs_sr_without_ceph(self, host: Host, cephfs_device_config: dict[str, str]) -> None:
2328
# This test must be the first in the series in this module
2429
assert not host.file_exists('/usr/sbin/mount.ceph'), \
2530
"mount.ceph must not be installed on the host at the beginning of the tests"
@@ -32,7 +37,9 @@ def test_create_cephfs_sr_without_ceph(self, host, cephfs_device_config):
3237
sr.destroy()
3338
assert False, "SR creation should not have succeeded!"
3439

35-
def test_create_and_destroy_sr(self, host, cephfs_device_config, pool_with_ceph):
40+
def test_create_and_destroy_sr(
41+
self, host: Host, cephfs_device_config: dict[str, str], pool_with_ceph: Pool
42+
) -> None:
3643
# Create and destroy tested in the same test to leave the host as unchanged as possible
3744
sr = host.sr_create('cephfs', "CephFS-SR-test", cephfs_device_config, shared=True, verify=True)
3845
# import a VM in order to detect vm import issues here rather than in the vm_on_xfs_fixture used in
@@ -44,23 +51,23 @@ def test_create_and_destroy_sr(self, host, cephfs_device_config, pool_with_ceph)
4451
@pytest.mark.usefixtures("cephfs_sr")
4552
class TestCephFSSR:
4653
@pytest.mark.quicktest
47-
def test_quicktest(self, cephfs_sr):
54+
def test_quicktest(self, cephfs_sr: SR) -> None:
4855
cephfs_sr.run_quicktest()
4956

50-
def test_vdi_is_not_open(self, vdi_on_cephfs_sr):
57+
def test_vdi_is_not_open(self, vdi_on_cephfs_sr: VDI) -> None:
5158
assert not vdi_is_open(vdi_on_cephfs_sr)
5259

5360
@pytest.mark.small_vm # run with a small VM to test the features
5461
@pytest.mark.big_vm # and ideally with a big VM to test it scales
55-
def test_start_and_shutdown_VM(self, vm_on_cephfs_sr):
62+
def test_start_and_shutdown_VM(self, vm_on_cephfs_sr: VM) -> None:
5663
vm = vm_on_cephfs_sr
5764
vm.start()
5865
vm.wait_for_os_booted()
5966
vm.shutdown(verify=True)
6067

6168
@pytest.mark.small_vm
6269
@pytest.mark.big_vm
63-
def test_snapshot(self, vm_on_cephfs_sr):
70+
def test_snapshot(self, vm_on_cephfs_sr: VM) -> None:
6471
vm = vm_on_cephfs_sr
6572
vm.start()
6673
try:
@@ -73,7 +80,7 @@ def test_snapshot(self, vm_on_cephfs_sr):
7380

7481
@pytest.mark.reboot
7582
@pytest.mark.small_vm
76-
def test_reboot(self, vm_on_cephfs_sr, host, cephfs_sr):
83+
def test_reboot(self, vm_on_cephfs_sr: VM, host: Host, cephfs_sr: SR) -> None:
7784
sr = cephfs_sr
7885
vm = vm_on_cephfs_sr
7986
host.reboot(verify=True)
@@ -84,7 +91,7 @@ def test_reboot(self, vm_on_cephfs_sr, host, cephfs_sr):
8491
vm.shutdown(verify=True)
8592

8693
@pytest.mark.reboot # reboots the host
87-
def test_ceph_missing(self, host, cephfs_sr):
94+
def test_ceph_missing(self, host: Host, cephfs_sr: SR) -> None:
8895
sr = cephfs_sr
8996
ceph_installed = True
9097
try:

tests/storage/cephfs/test_cephfs_sr_crosspool_migration.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import pytest
22

3+
from lib.host import Host
4+
from lib.sr import SR
5+
from lib.vm import VM
36
from tests.storage import cold_migration_then_come_back, live_storage_migration_then_come_back
47

58
# Requirements:
@@ -15,8 +18,12 @@
1518
@pytest.mark.big_vm # and ideally with a big VM to test it scales
1619
@pytest.mark.usefixtures("hostB1", "local_sr_on_hostB1")
1720
class Test:
18-
def test_cold_crosspool_migration(self, host, hostB1, vm_on_cephfs_sr, local_sr_on_hostB1):
21+
def test_cold_crosspool_migration(
22+
self, host: Host, hostB1: Host, vm_on_cephfs_sr: VM, local_sr_on_hostB1: SR
23+
) -> None:
1924
cold_migration_then_come_back(vm_on_cephfs_sr, host, hostB1, local_sr_on_hostB1)
2025

21-
def test_live_crosspool_migration(self, host, hostB1, vm_on_cephfs_sr, local_sr_on_hostB1):
26+
def test_live_crosspool_migration(
27+
self, host: Host, hostB1: Host, vm_on_cephfs_sr: VM, local_sr_on_hostB1: SR
28+
) -> None:
2229
live_storage_migration_then_come_back(vm_on_cephfs_sr, host, hostB1, local_sr_on_hostB1)

tests/storage/cephfs/test_cephfs_sr_intrapool_migration.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import pytest
22

3+
from lib.host import Host
4+
from lib.sr import SR
5+
from lib.vm import VM
36
from tests.storage import cold_migration_then_come_back, live_storage_migration_then_come_back
47

58
# Requirements:
@@ -15,12 +18,16 @@
1518
@pytest.mark.big_vm # and ideally with a big VM to test it scales
1619
@pytest.mark.usefixtures("hostA2", "local_sr_on_hostA2")
1720
class Test:
18-
def test_live_intrapool_shared_migration(self, host, hostA2, vm_on_cephfs_sr):
21+
def test_live_intrapool_shared_migration(self, host: Host, hostA2: Host, vm_on_cephfs_sr: VM) -> None:
1922
sr = vm_on_cephfs_sr.get_sr()
2023
live_storage_migration_then_come_back(vm_on_cephfs_sr, host, hostA2, sr)
2124

22-
def test_cold_intrapool_migration(self, host, hostA2, vm_on_cephfs_sr, local_sr_on_hostA2):
25+
def test_cold_intrapool_migration(
26+
self, host: Host, hostA2: Host, vm_on_cephfs_sr: VM, local_sr_on_hostA2: SR
27+
) -> None:
2328
cold_migration_then_come_back(vm_on_cephfs_sr, host, hostA2, local_sr_on_hostA2)
2429

25-
def test_live_intrapool_migration(self, host, hostA2, vm_on_cephfs_sr, local_sr_on_hostA2):
30+
def test_live_intrapool_migration(
31+
self, host: Host, hostA2: Host, vm_on_cephfs_sr: VM, local_sr_on_hostA2: SR
32+
) -> None:
2633
live_storage_migration_then_come_back(vm_on_cephfs_sr, host, hostA2, local_sr_on_hostA2)

tests/storage/conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from lib.vm import VM
66
from tests.storage import install_randstream
77

8-
def pytest_collection_modifyitems(config, items):
8+
from typing import Any, Generator
9+
10+
def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item]) -> None:
911
# modify ordering so that ext is always tested first,
1012
# before more complex storage drivers
1113
for item in reversed(list(items)):
@@ -14,6 +16,6 @@ def pytest_collection_modifyitems(config, items):
1416
items.insert(0, item)
1517

1618
@pytest.fixture(scope='module')
17-
def storage_test_vm(running_unix_vm: VM):
19+
def storage_test_vm(running_unix_vm: VM) -> Generator[VM, None, None]:
1820
install_randstream(running_unix_vm)
1921
yield running_unix_vm

tests/storage/ext/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
from lib.host import Host
88
from lib.sr import SR
9-
from lib.vdi import ImageFormat
9+
from lib.vdi import VDI, ImageFormat
10+
from lib.vm import VM
1011

11-
from typing import Generator
12+
from typing import Any, Generator
1213

1314
@pytest.fixture(scope='package')
1415
def ext_sr(host: Host,
1516
unused_512B_disks: dict[Host, list[Host.BlockDeviceInfo]],
1617
image_format: ImageFormat
17-
) -> Generator[SR]:
18+
) -> Generator[SR, None, None]:
1819
""" An EXT SR on first host. """
1920
sr_disk = unused_512B_disks[host][0]["name"]
2021
sr = host.sr_create('ext', "EXT-local-SR-test",
@@ -25,13 +26,13 @@ def ext_sr(host: Host,
2526
sr.destroy()
2627

2728
@pytest.fixture(scope='module')
28-
def vdi_on_ext_sr(ext_sr: SR):
29+
def vdi_on_ext_sr(ext_sr: SR) -> Generator[VDI, None, None]:
2930
vdi = ext_sr.create_vdi('EXT-local-VDI-test')
3031
yield vdi
3132
vdi.destroy()
3233

3334
@pytest.fixture(scope='module')
34-
def vm_on_ext_sr(host, ext_sr, vm_ref):
35+
def vm_on_ext_sr(host: Host, ext_sr: SR, vm_ref: str) -> Generator[VM, None, None]:
3536
vm = host.import_vm(vm_ref, sr_uuid=ext_sr.uuid)
3637
yield vm
3738
# teardown

tests/storage/ext/test_ext_sr.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TestEXTSRCreateDestroy:
3232
and VM import.
3333
"""
3434

35-
def test_create_sr_with_missing_device(self, host):
35+
def test_create_sr_with_missing_device(self, host: Host) -> None:
3636
try_to_create_sr_with_missing_device('ext', 'EXT-local-SR-test', host)
3737

3838
def test_create_and_destroy_sr(self, host: Host,
@@ -53,13 +53,13 @@ def test_create_and_destroy_sr(self, host: Host,
5353
@pytest.mark.usefixtures("ext_sr")
5454
class TestEXTSR:
5555
@pytest.mark.quicktest
56-
def test_quicktest(self, ext_sr):
56+
def test_quicktest(self, ext_sr: SR) -> None:
5757
ext_sr.run_quicktest()
5858

59-
def test_vdi_is_not_open(self, vdi_on_ext_sr):
59+
def test_vdi_is_not_open(self, vdi_on_ext_sr: VDI) -> None:
6060
assert not vdi_is_open(vdi_on_ext_sr)
6161

62-
def test_vdi_image_format(self, vdi_on_ext_sr: VDI, image_format: ImageFormat):
62+
def test_vdi_image_format(self, vdi_on_ext_sr: VDI, image_format: ImageFormat) -> None:
6363
fmt = vdi_on_ext_sr.get_image_format()
6464
# feature-detect: if the SM doesn't report image-format, skip this check
6565
if not fmt:
@@ -68,15 +68,15 @@ def test_vdi_image_format(self, vdi_on_ext_sr: VDI, image_format: ImageFormat):
6868

6969
@pytest.mark.small_vm # run with a small VM to test the features
7070
@pytest.mark.big_vm # and ideally with a big VM to test it scales
71-
def test_start_and_shutdown_VM(self, vm_on_ext_sr):
71+
def test_start_and_shutdown_VM(self, vm_on_ext_sr: VM) -> None:
7272
vm = vm_on_ext_sr
7373
vm.start()
7474
vm.wait_for_os_booted()
7575
vm.shutdown(verify=True)
7676

7777
@pytest.mark.small_vm
7878
@pytest.mark.big_vm
79-
def test_snapshot(self, vm_on_ext_sr):
79+
def test_snapshot(self, vm_on_ext_sr: VM) -> None:
8080
vm = vm_on_ext_sr
8181
vm.start()
8282
try:
@@ -87,23 +87,23 @@ def test_snapshot(self, vm_on_ext_sr):
8787

8888
@pytest.mark.small_vm
8989
@pytest.mark.parametrize("vdi_op", ["snapshot", "clone"])
90-
def test_coalesce(self, storage_test_vm: VM, vdi_on_ext_sr: VDI, vdi_op: CoalesceOperation):
90+
def test_coalesce(self, storage_test_vm: VM, vdi_on_ext_sr: VDI, vdi_op: CoalesceOperation) -> None:
9191
coalesce_integrity(storage_test_vm, vdi_on_ext_sr, vdi_op)
9292

9393
@pytest.mark.small_vm
9494
@pytest.mark.parametrize("compression", ["none", "gzip", "zstd"])
95-
def test_xva_export_import(self, vm_on_ext_sr: VM, compression: XVACompression):
95+
def test_xva_export_import(self, vm_on_ext_sr: VM, compression: XVACompression) -> None:
9696
xva_export_import(vm_on_ext_sr, compression)
9797

9898
@pytest.mark.small_vm
99-
def test_vdi_export_import(self, storage_test_vm: VM, ext_sr: SR, image_format: ImageFormat):
99+
def test_vdi_export_import(self, storage_test_vm: VM, ext_sr: SR, image_format: ImageFormat) -> None:
100100
vdi_export_import(storage_test_vm, ext_sr, image_format)
101101

102102
# *** tests with reboots (longer tests).
103103

104104
@pytest.mark.small_vm
105105
@pytest.mark.big_vm
106-
def test_blktap_activate_failure(self, vm_on_ext_sr):
106+
def test_blktap_activate_failure(self, vm_on_ext_sr: VM) -> None:
107107
from lib.fistpoint import FistPoint
108108
vm = vm_on_ext_sr
109109
with FistPoint(vm.host, "blktap_activate_inject_failure"), pytest.raises(SSHCommandFailed):
@@ -112,7 +112,7 @@ def test_blktap_activate_failure(self, vm_on_ext_sr):
112112

113113
@pytest.mark.small_vm
114114
@pytest.mark.big_vm
115-
def test_resize(self, vm_on_ext_sr):
115+
def test_resize(self, vm_on_ext_sr: VM) -> None:
116116
vm = vm_on_ext_sr
117117
vdi = VDI(vm.vdi_uuids()[0], host=vm.host)
118118
old_size = vdi.get_virtual_size()
@@ -124,7 +124,7 @@ def test_resize(self, vm_on_ext_sr):
124124

125125
@pytest.mark.small_vm
126126
@pytest.mark.big_vm
127-
def test_failing_resize(self, host, ext_sr, vm_on_ext_sr, exit_on_fistpoint):
127+
def test_failing_resize(self, host: Host, ext_sr: SR, vm_on_ext_sr: VM, exit_on_fistpoint: None) -> None:
128128
vm = vm_on_ext_sr
129129
vdi = VDI(vm.vdi_uuids()[0], host=vm.host)
130130
old_size = vdi.get_virtual_size()
@@ -135,13 +135,13 @@ def test_failing_resize(self, host, ext_sr, vm_on_ext_sr, exit_on_fistpoint):
135135
vdi.resize(new_size)
136136
except SSHCommandFailed:
137137
logging.info(f"Launching SR scan for {ext_sr} after failure")
138-
host.xe("sr-scan", {"uuid": ext_sr})
138+
host.xe("sr-scan", {"uuid": ext_sr.uuid})
139139

140140
assert vdi.get_virtual_size() == new_size
141141

142142
@pytest.mark.reboot
143143
@pytest.mark.small_vm
144-
def test_reboot(self, host, ext_sr, vm_on_ext_sr):
144+
def test_reboot(self, host: Host, ext_sr: SR, vm_on_ext_sr: VM) -> None:
145145
sr = ext_sr
146146
vm = vm_on_ext_sr
147147
host.reboot(verify=True)

tests/storage/ext/test_ext_sr_crosspool_migration.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
from __future__ import annotations
2+
13
import pytest
24

5+
from lib.host import Host
6+
from lib.sr import SR
7+
from lib.vm import VM
38
from tests.storage import cold_migration_then_come_back, live_storage_migration_then_come_back
49

510
# Requirements:
@@ -13,8 +18,8 @@
1318
@pytest.mark.big_vm # and ideally with a big VM to test it scales
1419
@pytest.mark.usefixtures("hostB1", "local_sr_on_hostB1")
1520
class Test:
16-
def test_cold_crosspool_migration(self, host, hostB1, vm_on_ext_sr, local_sr_on_hostB1):
21+
def test_cold_crosspool_migration(self, host: Host, hostB1: Host, vm_on_ext_sr: VM, local_sr_on_hostB1: SR) -> None:
1722
cold_migration_then_come_back(vm_on_ext_sr, host, hostB1, local_sr_on_hostB1)
1823

19-
def test_live_crosspool_migration(self, host, hostB1, vm_on_ext_sr, local_sr_on_hostB1):
24+
def test_live_crosspool_migration(self, host: Host, hostB1: Host, vm_on_ext_sr: VM, local_sr_on_hostB1: SR) -> None:
2025
live_storage_migration_then_come_back(vm_on_ext_sr, host, hostB1, local_sr_on_hostB1)

tests/storage/ext/test_ext_sr_intrapool_migration.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
from __future__ import annotations
2+
13
import pytest
24

5+
from lib.host import Host
6+
from lib.sr import SR
7+
from lib.vm import VM
38
from tests.storage import cold_migration_then_come_back, live_storage_migration_then_come_back
49

510
# Requirements:
@@ -13,8 +18,8 @@
1318
@pytest.mark.big_vm # and ideally with a big VM to test it scales
1419
@pytest.mark.usefixtures("hostA2", "local_sr_on_hostA2")
1520
class Test:
16-
def test_cold_intrapool_migration(self, host, hostA2, vm_on_ext_sr, local_sr_on_hostA2):
21+
def test_cold_intrapool_migration(self, host: Host, hostA2: Host, vm_on_ext_sr: VM, local_sr_on_hostA2: SR) -> None:
1722
cold_migration_then_come_back(vm_on_ext_sr, host, hostA2, local_sr_on_hostA2)
1823

19-
def test_live_intrapool_migration(self, host, hostA2, vm_on_ext_sr, local_sr_on_hostA2):
24+
def test_live_intrapool_migration(self, host: Host, hostA2: Host, vm_on_ext_sr: VM, local_sr_on_hostA2: SR) -> None:
2025
live_storage_migration_then_come_back(vm_on_ext_sr, host, hostA2, local_sr_on_hostA2)

0 commit comments

Comments
 (0)