Skip to content

Commit 7fd7312

Browse files
committed
Define package-scoped fixtures and add missing __init__.py files
* Fixtures defined in a test package's conftest.py should not be session-scoped. Change the scope to "package". * Due to a bug in pytest [1] regarding when package-scoped fixtures are setup and teared down, move shared package-scoped fixtures to pkgfixtures.py and import them explicitly in the test packages' conftest.py. * Add __init__.py to all test packages and to lib/, to make them proper python packages. [1] pytest-dev/pytest#8189 Signed-off-by: Samuel Verschelde <[email protected]>
1 parent c34f321 commit 7fd7312

32 files changed

+68
-43
lines changed

conftest.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
from lib.pool import Pool
1212
from lib.vm import VM
1313

14+
# import package-scoped fixtures. Although we need to define them in a separate file so that we can
15+
# then import them in individual packages to fix the buggy package scope handling by pytest, we also
16+
# need to import them in the global conftest.py so that they are recognized as fixtures.
17+
from pkgfixtures import formatted_and_mounted_ext4_disk, sr_disk_wiped
18+
1419
# *** Support for incremental tests in test classes ***
1520
# From https://stackoverflow.com/questions/12411431/how-to-skip-the-rest-of-tests-in-the-class-if-one-has-failed
1621
def pytest_runtest_makereport(item, call):
@@ -190,12 +195,6 @@ def sr_disk(request, host):
190195
f"disk or block device {disk} is either not present or already used on master host"
191196
yield disk
192197

193-
@pytest.fixture(scope='session')
194-
def sr_disk_wiped(host, sr_disk):
195-
logging.info(">> wipe disk %s" % sr_disk)
196-
host.ssh(['wipefs', '-a', '/dev/' + sr_disk])
197-
yield sr_disk
198-
199198
@pytest.fixture(scope='session')
200199
def sr_disk_for_all_hosts(request, host):
201200
disk = request.param
@@ -225,13 +224,6 @@ def sr_disk_for_all_hosts(request, host):
225224
logging.info(f">> Disk or block device {disk} is present and free on all pool members")
226225
yield candidates[0]
227226

228-
@pytest.fixture(scope='session')
229-
def formatted_and_mounted_ext4_disk(host, sr_disk):
230-
mountpoint = '/var/tmp/sr_disk_mountpoint'
231-
setup_formatted_and_mounted_disk(host, sr_disk, 'ext4', mountpoint)
232-
yield mountpoint
233-
teardown_formatted_and_mounted_disk(host, mountpoint)
234-
235227
@pytest.fixture(scope='module')
236228
def vm_ref(request):
237229
ref = request.param

lib/__init__.py

Whitespace-only changes.

pkgfixtures.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import logging
2+
import pytest
3+
4+
from lib.common import setup_formatted_and_mounted_disk, teardown_formatted_and_mounted_disk
5+
6+
# Due to a bug in the way pytest handles the setup and teardown of package-scoped fixtures,
7+
# we moved the following fixtures out of the main conftest.py.
8+
# To workaround the bug, the fixture must be imported either in a package's own conftest.py,
9+
# or directly in a test module. Then the fixtures will truly be handled as package-scoped.
10+
# Reference: https://github.com/pytest-dev/pytest/issues/8189
11+
12+
# package scope because previous test packages may have used the disk
13+
@pytest.fixture(scope='package')
14+
def sr_disk_wiped(host, sr_disk):
15+
logging.info(">> wipe disk %s" % sr_disk)
16+
host.ssh(['wipefs', '-a', '/dev/' + sr_disk])
17+
yield sr_disk
18+
19+
# package scope so that the device is unmounted before tests from the next package is executed.
20+
@pytest.fixture(scope='package')
21+
def formatted_and_mounted_ext4_disk(host, sr_disk):
22+
mountpoint = '/var/tmp/sr_disk_mountpoint'
23+
setup_formatted_and_mounted_disk(host, sr_disk, 'ext4', mountpoint)
24+
yield mountpoint
25+
teardown_formatted_and_mounted_disk(host, mountpoint)

tests/__init__.py

Whitespace-only changes.

tests/guest-tools/__init__.py

Whitespace-only changes.

tests/guest-tools/unix/__init__.py

Whitespace-only changes.

tests/migration/__init__.py

Whitespace-only changes.

tests/misc/__init__.py

Whitespace-only changes.

tests/misc/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# explicitly import package-scoped fixtures (see explanation in pkgfixtures.py)
2+
from pkgfixtures import formatted_and_mounted_ext4_disk

tests/quicktest/__init__.py

Whitespace-only changes.

tests/snapshot/__init__.py

Whitespace-only changes.

tests/storage/cephfs/__init__.py

Whitespace-only changes.

tests/storage/cephfs/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def _setup_host_with_ceph(host):
88
host.yum_install(['centos-release-ceph-jewel'], enablerepo="base,extras")
99
host.yum_install(['ceph-common'], enablerepo="base,extras")
1010

11-
@pytest.fixture(scope='session')
11+
@pytest.fixture(scope='package')
1212
def pool_with_ceph(host):
1313
for h in host.pool.hosts:
1414
_setup_host_with_ceph(h)
@@ -18,7 +18,7 @@ def pool_with_ceph(host):
1818
for h in host.pool.hosts:
1919
h.yum_restore_saved_state()
2020

21-
@pytest.fixture(scope='session')
21+
@pytest.fixture(scope='package')
2222
def cephfs_device_config(sr_device_config):
2323
if sr_device_config is not None:
2424
# SR device config from CLI param
@@ -35,7 +35,7 @@ def cephfs_device_config(sr_device_config):
3535
raise Exception("No default CephFS device-config found, neither in CLI nor in data.py defaults")
3636
return config
3737

38-
@pytest.fixture(scope='session')
38+
@pytest.fixture(scope='package')
3939
def cephfs_sr(host, cephfs_device_config, pool_with_ceph):
4040
""" A CephFS SR on first host. """
4141
sr = host.sr_create('cephfs', "CephFS-SR-test", cephfs_device_config, shared=True)

tests/storage/ext/__init__.py

Whitespace-only changes.

tests/storage/ext/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import pytest
33

4-
@pytest.fixture(scope='session')
4+
@pytest.fixture(scope='package')
55
def ext_sr(host, sr_disk):
66
""" An EXT SR on first host. """
77
sr = host.sr_create('ext', "EXT-local-SR-test", {'device': '/dev/' + sr_disk})

tests/storage/glusterfs/__init__.py

Whitespace-only changes.

tests/storage/glusterfs/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _teardown_host_with_glusterfs(host):
6262
if len(errors) > 0:
6363
raise Exception("\n".join(repr(e) for e in errors))
6464

65-
@pytest.fixture(scope='session')
65+
@pytest.fixture(scope='package')
6666
def pool_with_glusterfs(host):
6767
setup_errors = []
6868
for h in host.pool.hosts:
@@ -86,7 +86,7 @@ def pool_with_glusterfs(host):
8686
if len(teardown_errors) > 0:
8787
raise Exception("\n".join(repr(e) for e in teardown_errors))
8888

89-
@pytest.fixture(scope='session')
89+
@pytest.fixture(scope='package')
9090
def gluster_disk(host, sr_disk_for_all_hosts):
9191
sr_disk = sr_disk_for_all_hosts
9292
hosts = host.pool.hosts
@@ -136,7 +136,7 @@ def teardown_for_host(h):
136136
logging.error("< Fallback teardown failed on host: %s with error: %s" % (h, e))
137137
pass
138138

139-
@pytest.fixture(scope='session')
139+
@pytest.fixture(scope='package')
140140
def gluster_volume_started(host, hostA2, gluster_disk):
141141
hosts = host.pool.hosts
142142

@@ -174,7 +174,7 @@ def gluster_volume_started(host, hostA2, gluster_disk):
174174
h.ssh(['rm', '-rf', '/mnt/sr_disk/vol0'])
175175

176176

177-
@pytest.fixture(scope='session')
177+
@pytest.fixture(scope='package')
178178
def glusterfs_device_config(host):
179179
backup_servers = []
180180
for h in host.pool.hosts[1:]:
@@ -185,7 +185,7 @@ def glusterfs_device_config(host):
185185
'backupservers': ':'.join(backup_servers)
186186
}
187187

188-
@pytest.fixture(scope='session')
188+
@pytest.fixture(scope='package')
189189
def glusterfs_sr(host, pool_with_glusterfs, gluster_volume_started, glusterfs_device_config):
190190
""" A GlusterFS SR on first host. """
191191
# Create the SR

tests/storage/iso/conftest.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import time
33
import os
44

5+
# explicitly import package-scoped fixtures (see explanation in pkgfixtures.py)
6+
from pkgfixtures import formatted_and_mounted_ext4_disk
7+
58
def create_local_iso_sr(host, location):
69
host.ssh(['mkdir', '-p', location])
710
device_config = {
@@ -10,7 +13,7 @@ def create_local_iso_sr(host, location):
1013
}
1114
return host.sr_create('iso', "ISO-local-SR-test", device_config, verify=True)
1215

13-
@pytest.fixture(scope='session')
16+
@pytest.fixture(scope='package')
1417
def local_iso_sr(host, formatted_and_mounted_ext4_disk):
1518
""" An ISO SR on first host. """
1619
location = formatted_and_mounted_ext4_disk + '/iso_sr'
@@ -19,7 +22,7 @@ def local_iso_sr(host, formatted_and_mounted_ext4_disk):
1922
# teardown
2023
sr.destroy()
2124

22-
@pytest.fixture(scope='session')
25+
@pytest.fixture(scope='package')
2326
def nfs_iso_device_config(sr_device_config):
2427
if sr_device_config is not None:
2528
# SR device config from CLI param
@@ -36,7 +39,7 @@ def nfs_iso_device_config(sr_device_config):
3639
raise Exception("No default NFS ISO device-config found, neither in CLI nor in data.py defaults")
3740
return config
3841

39-
@pytest.fixture(scope='session')
42+
@pytest.fixture(scope='package')
4043
def cifs_iso_device_config(sr_device_config):
4144
if sr_device_config is not None:
4245
# SR device config from CLI param
@@ -53,15 +56,15 @@ def cifs_iso_device_config(sr_device_config):
5356
raise Exception("No default CIFS ISO device-config found, neither in CLI nor in data.py defaults")
5457
return config
5558

56-
@pytest.fixture(scope='session')
59+
@pytest.fixture(scope='package')
5760
def nfs_iso_sr(host, nfs_iso_device_config):
5861
""" A NFS ISO SR. """
5962
sr = host.sr_create('iso', "ISO-NFS-SR-test", nfs_iso_device_config, shared=True, verify=True)
6063
yield sr
6164
# teardown
6265
sr.forget()
6366

64-
@pytest.fixture(scope='session')
67+
@pytest.fixture(scope='package')
6568
def cifs_iso_sr(host, cifs_iso_device_config):
6669
""" A Samba/CIFS SR. """
6770
sr = host.sr_create('iso', "ISO-CIFS-SR-test", cifs_iso_device_config, shared=True, verify=True)

tests/storage/linstor/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
GROUP_NAME = 'linstor_group'
77
LINSTOR_PACKAGES = ['drbd', 'kmod-drbd', 'linstor-client', 'linstor-controller', 'linstor-satellite', 'python-linstor']
88

9-
@pytest.fixture(scope='session')
9+
@pytest.fixture(scope='package')
1010
def lvm_disks(host):
1111
disks = []
1212
hosts = host.pool.hosts
@@ -30,7 +30,7 @@ def check_linstor_packages(host):
3030
if not host.check_packages_available(LINSTOR_PACKAGES):
3131
raise Exception('Unable to find LINSTOR packages in the yum repositories of {}'.format(host))
3232

33-
@pytest.fixture(scope='session')
33+
@pytest.fixture(scope='package')
3434
def hosts_with_linstor(host, additional_repos):
3535
master = host
3636
hosts = master.pool.hosts
@@ -102,7 +102,7 @@ def destroy_linstor_sr(hosts_with_linstor, sr):
102102
sr.destroy(verify=True, force=True)
103103
delete_linstor_nodes(hosts_with_linstor)
104104

105-
@pytest.fixture(scope='session')
105+
@pytest.fixture(scope='package')
106106
def linstor_sr(hosts_with_linstor, lvm_disks):
107107
sr = create_linstor_sr(hosts_with_linstor)
108108
yield sr

tests/storage/lvm/__init__.py

Whitespace-only changes.

tests/storage/lvm/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import pytest
33

4-
@pytest.fixture(scope='session')
4+
@pytest.fixture(scope='package')
55
def lvm_sr(host, sr_disk):
66
""" An LVM SR on first host. """
77
sr = host.sr_create('lvm', "LVM-local-SR-test", {'device': '/dev/' + sr_disk})

tests/storage/lvmoiscsi/__init__.py

Whitespace-only changes.

tests/storage/lvmoiscsi/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import pytest
33

4-
@pytest.fixture(scope='session')
4+
@pytest.fixture(scope='package')
55
def lvmoiscsi_device_config(sr_device_config):
66
if sr_device_config is not None:
77
# SR device config from CLI param
@@ -18,7 +18,7 @@ def lvmoiscsi_device_config(sr_device_config):
1818
raise Exception("No default lvmoiscsi device-config found, neither in CLI nor in data.py defaults")
1919
return config
2020

21-
@pytest.fixture(scope='session')
21+
@pytest.fixture(scope='package')
2222
def lvmoiscsi_sr(host, lvmoiscsi_device_config):
2323
""" A lvmoiscsi SR on first host. """
2424
sr = host.sr_create('lvmoiscsi', "lvmoiscsi-SR-test", lvmoiscsi_device_config, shared=True)

tests/storage/moosefs/__init__.py

Whitespace-only changes.

tests/storage/moosefs/conftest.py

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

4-
@pytest.fixture(scope='session')
4+
@pytest.fixture(scope='package')
55
def host_with_moosefs(host):
66
assert not host.file_exists('/usr/sbin/mount.moosefs'), \
77
"MooseFS client should not be installed on the host before all tests"
@@ -16,7 +16,7 @@ def host_with_moosefs(host):
1616
host.ssh(['sh', '-c', '"rm -f /etc/pki/rpm-gpg/RPM-GPG-KEY-MooseFS"'])
1717
host.ssh(['sh', '-c', '"rm -f /etc/yum.repos.d/MooseFS.repo"'])
1818

19-
@pytest.fixture(scope='session')
19+
@pytest.fixture(scope='package')
2020
def moosefs_device_config(sr_device_config):
2121
if sr_device_config is not None:
2222
# SR device config from CLI param
@@ -33,7 +33,7 @@ def moosefs_device_config(sr_device_config):
3333
raise Exception("No default MooseFS device-config found, neither in CLI nor in data.py defaults")
3434
return config
3535

36-
@pytest.fixture(scope='session')
36+
@pytest.fixture(scope='package')
3737
def moosefs_sr(moosefs_device_config, host_with_moosefs):
3838
""" MooseFS SR on a specific host. """
3939
sr = host_with_moosefs.sr_create('moosefs', "MooseFS-SR-test", moosefs_device_config, shared=True)

tests/storage/nfs/__init__.py

Whitespace-only changes.

tests/storage/nfs/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import pytest
33

4-
@pytest.fixture(scope='session')
4+
@pytest.fixture(scope='package')
55
def nfs_device_config(sr_device_config):
66
if sr_device_config is not None:
77
# SR device config from CLI param
@@ -18,7 +18,7 @@ def nfs_device_config(sr_device_config):
1818
raise Exception("No default NFS device-config found, neither in CLI nor in data.py defaults")
1919
return config
2020

21-
@pytest.fixture(scope='session')
21+
@pytest.fixture(scope='package')
2222
def nfs_sr(host, nfs_device_config):
2323
""" A NFS SR on first host. """
2424
sr = host.sr_create('nfs', "NFS-SR-test", nfs_device_config, shared=True)

tests/storage/xfs/__init__.py

Whitespace-only changes.

tests/storage/xfs/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import pytest
33

4-
@pytest.fixture(scope='session')
4+
@pytest.fixture(scope='package')
55
def host_with_xfsprogs(host):
66
assert not host.file_exists('/usr/sbin/mkfs.xfs'), \
77
"xfsprogs must not be installed on the host at the beginning of the tests"
@@ -11,7 +11,7 @@ def host_with_xfsprogs(host):
1111
# teardown
1212
host.yum_restore_saved_state()
1313

14-
@pytest.fixture(scope='session')
14+
@pytest.fixture(scope='package')
1515
def xfs_sr(sr_disk, host_with_xfsprogs):
1616
""" A XFS SR on first host. """
1717
sr = host_with_xfsprogs.sr_create('xfs', "XFS-local-SR-test", {'device': '/dev/' + sr_disk})

tests/storage/zfs/conftest.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import logging
22
import pytest
33

4+
# explicitly import package-scoped fixtures (see explanation in pkgfixtures.py)
5+
from pkgfixtures import sr_disk_wiped
6+
47
VOLUME_NAME = 'vol0'
58
VOLUME_PATH = '/' + VOLUME_NAME
69

7-
@pytest.fixture(scope='session')
10+
@pytest.fixture(scope='package')
811
def host_with_zfs(host):
912
assert not host.file_exists('/usr/sbin/zpool'), \
1013
"zfs must not be installed on the host at the beginning of the tests"
@@ -15,14 +18,14 @@ def host_with_zfs(host):
1518
# teardown
1619
host.yum_restore_saved_state()
1720

18-
@pytest.fixture(scope='session')
21+
@pytest.fixture(scope='package')
1922
def zpool_vol0(sr_disk_wiped, host_with_zfs):
2023
host_with_zfs.ssh(['zpool', 'create', '-f', VOLUME_NAME, '/dev/' + sr_disk_wiped])
2124
yield
2225
# teardown
2326
host_with_zfs.ssh(['zpool', 'destroy', VOLUME_NAME])
2427

25-
@pytest.fixture(scope='session')
28+
@pytest.fixture(scope='package')
2629
def zfs_sr(host, zpool_vol0):
2730
""" A ZFS SR on first host. """
2831
sr = host.sr_create('zfs', "ZFS-local-SR-test", {'location': VOLUME_PATH})

tests/system/__init__.py

Whitespace-only changes.

tests/xapi-plugins/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)