11import pytest
22
33import logging
4+ from dataclasses import dataclass
45
56from lib .common import exec_nofail , raise_errors , setup_formatted_and_mounted_disk , teardown_formatted_and_mounted_disk
67from lib .netutil import is_ipv6
78
9+ from typing import TYPE_CHECKING
10+
11+ if TYPE_CHECKING :
12+ from lib .host import Host
13+ from lib .pool import Pool
14+ from lib .sr import SR
15+
816# explicit import for package-scope fixtures
917from pkgfixtures import pool_with_saved_yum_state
1018
1119GLUSTERFS_PORTS = [('24007' , 'tcp' ), ('49152:49251' , 'tcp' )]
1220
21+ @dataclass
22+ class GlusterFsConfig :
23+ uninstall_glusterfs : bool = True
24+
1325def _setup_host_with_glusterfs (host ):
1426 for service in ['iptables' , 'ip6tables' ]:
1527 host .ssh (['cp' , '/etc/sysconfig/%s' % service , '/etc/sysconfig/%s.orig' % service ])
@@ -30,13 +42,18 @@ def _setup_host_with_glusterfs(host):
3042
3143 host .ssh (['systemctl' , 'enable' , '--now' , 'glusterd.service' ])
3244
33- def _teardown_host_with_glusterfs (host ):
45+ def _uninstall_host_glusterfs (host : 'Host' ):
3446 errors = []
3547 errors += exec_nofail (lambda : host .ssh (['systemctl' , 'disable' , '--now' , 'glusterd.service' ]))
3648
3749 # Remove any remaining gluster-related data to avoid issues in future test runs
3850 errors += exec_nofail (lambda : host .ssh (['rm' , '-rf' , '/var/lib/glusterd' ]))
3951
52+ raise_errors (errors )
53+
54+ def _restore_host_iptables (host : 'Host' ):
55+ errors = []
56+
4057 iptables = 'ip6tables' if is_ipv6 (host .hostname_or_ip ) else 'iptables'
4158 for h in host .pool .hosts :
4259 hostname_or_ip = h .hostname_or_ip
@@ -56,7 +73,7 @@ def _teardown_host_with_glusterfs(host):
5673 raise_errors (errors )
5774
5875@pytest .fixture (scope = 'package' )
59- def pool_without_glusterfs (host ):
76+ def pool_without_glusterfs (host : 'Host' ):
6077 for h in host .pool .hosts :
6178 if h .file_exists ('/usr/sbin/glusterd' ):
6279 raise Exception (
@@ -65,11 +82,30 @@ def pool_without_glusterfs(host):
6582 yield host .pool
6683
6784@pytest .fixture (scope = 'package' )
68- def pool_with_glusterfs (pool_without_glusterfs , pool_with_saved_yum_state ):
85+ def _glusterfs_config ():
86+ return GlusterFsConfig ()
87+
88+ @pytest .fixture (scope = 'package' )
89+ def pool_with_glusterfs (
90+ pool_without_glusterfs : 'Pool' ,
91+ pool_with_saved_yum_state : 'Pool' ,
92+ _glusterfs_config : GlusterFsConfig
93+ ):
94+
95+ def _host_rollback (host : 'Host' ):
96+ _uninstall_host_glusterfs (host )
97+ _restore_host_iptables (host )
98+ def _disable_yum_rollback (host : 'Host' ):
99+ host .saved_rollback_id = None
100+
69101 pool = pool_with_saved_yum_state
70- pool .exec_on_hosts_on_error_rollback (_setup_host_with_glusterfs , _teardown_host_with_glusterfs )
102+ pool .exec_on_hosts_on_error_rollback (_setup_host_with_glusterfs , _host_rollback )
71103 yield pool
72- pool .exec_on_hosts_on_error_continue (_teardown_host_with_glusterfs )
104+ if _glusterfs_config .uninstall_glusterfs :
105+ pool .exec_on_hosts_on_error_continue (_uninstall_host_glusterfs )
106+ else :
107+ pool .exec_on_hosts_on_error_continue (_disable_yum_rollback )
108+ pool .exec_on_hosts_on_error_continue (_restore_host_iptables )
73109
74110@pytest .fixture (scope = 'package' )
75111def gluster_disk (pool_with_unused_512B_disk , unused_512B_disks ):
@@ -184,13 +220,22 @@ def glusterfs_device_config(host):
184220 }
185221
186222@pytest .fixture (scope = 'package' )
187- def glusterfs_sr (host , pool_with_glusterfs , gluster_volume_started , glusterfs_device_config ):
223+ def glusterfs_sr (host : 'Host' ,
224+ pool_with_glusterfs : 'Pool' ,
225+ gluster_volume_started : None ,
226+ glusterfs_device_config ,
227+ _glusterfs_config : GlusterFsConfig
228+ ):
188229 """ A GlusterFS SR on first host. """
189230 # Create the SR
190231 sr = host .sr_create ('glusterfs' , "GlusterFS-SR-test" , glusterfs_device_config , shared = True )
191232 yield sr
192233 # teardown
193- sr .destroy ()
234+ try :
235+ sr .destroy ()
236+ except Exception as e :
237+ _glusterfs_config .uninstall_glusterfs = False
238+ raise e
194239
195240@pytest .fixture (scope = 'module' )
196241def vdi_on_glusterfs_sr (glusterfs_sr ):
0 commit comments