Skip to content

Commit 221d797

Browse files
tests/storage/linstor: Add test for forgetting and introducing Linstor SR
- Introduced `test_forget_and_introduce_sr` in `test_linstor_sr.py`: - Validates the ability to forget an SR and properly reintroduce it. - Ensures all PBD configurations are stored and restored correctly. - Checks that exceptions are raised when querying a forgotten SR. - Restores the SR and verifies successful reattachment of PBDs. - Enhanced `SR` class in `sr.py` with: - `param_get()`: Fetch a specific SR parameter. - `type()`: Retrieve SR type. - `forget()`: Remove SR from the pool. - `introduce()`: Introduce SR. - Improved `Host` command execution in `host.py`: - Added support for handling both dictionary and list-based arguments. - Fixed command formatting for `config:key=value` scenarios. Signed-off-by: Rushikesh Jadhav <[email protected]>
1 parent 7a712e6 commit 221d797

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

lib/host.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,14 @@ def stringify(key, value):
8686
return ret.rstrip()
8787
return "{}={}".format(key, shlex.quote(value))
8888

89-
command = ['xe', action] + maybe_param_minimal + maybe_param_force + \
90-
[stringify(key, value) for key, value in args.items()]
89+
# Handle scenarios like config:key=value
90+
# device-config:redundancy=2 device-config:provisioning=thin
91+
command = ['xe', action] + maybe_param_minimal + maybe_param_force
92+
if isinstance(args, dict):
93+
command += [stringify(key, value) for key, value in args.items()]
94+
elif isinstance(args, list):
95+
command += [stringify(key, value) for key, value in args]
96+
9197
result = self.ssh(
9298
command,
9399
check=check,

lib/sr.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def unplug_pbds(self, force=False):
4040

4141
def all_pbds_attached(self):
4242
all_attached = True
43+
# TBD: handle sr that does not have PBD, following will return all_attached=True
4344
for pbd_uuid in self.pbd_uuids():
4445
all_attached = all_attached and self.pool.master.xe('pbd-param-get', {'uuid': pbd_uuid,
4546
'param-name': 'currently-attached'})
@@ -145,6 +146,20 @@ def main_host(self):
145146
def content_type(self):
146147
return self.pool.master.xe('sr-param-get', {'uuid': self.uuid, 'param-name': 'content-type'})
147148

149+
def param_get(self, param):
150+
return self.pool.master.xe('sr-param-get', {'uuid': self.uuid, 'param-name': param})
151+
152+
def type(self):
153+
return self.pool.master.xe('sr-param-get', {'uuid': self.uuid, 'param-name': 'type'})
154+
155+
def forget(self):
156+
return self.pool.master.xe('sr-forget', {'uuid': self.uuid})
157+
158+
def introduce(self, type, shared, name_label, uuid):
159+
return self.pool.master.xe('sr-introduce', {'uuid': self.uuid, 'type': type,
160+
'shared': shared, 'content-type': 'user',
161+
'name-label': name_label, 'uuid': uuid})
162+
148163
def is_shared(self):
149164
if self._is_shared is None:
150165
self._is_shared = self.pool.master.xe('sr-param-get', {'uuid': self.uuid, 'param-name': 'shared'})

tests/storage/linstor/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def linstor_sr(pool_with_linstor, provisioning_type, storage_pool_name):
9393
'provisioning': provisioning_type
9494
}, shared=True)
9595
yield sr
96-
sr.destroy()
96+
sr.destroy(verify=True)
9797

9898
@pytest.fixture(scope='module')
9999
def vdi_on_linstor_sr(linstor_sr):

tests/storage/linstor/test_linstor_sr.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .conftest import LINSTOR_PACKAGE
66
from lib.commands import SSHCommandFailed
7-
from lib.common import wait_for, vm_image
7+
from lib.common import wait_for, vm_image, safe_split
88
from tests.storage import vdi_is_open
99

1010
# Requirements:
@@ -78,6 +78,57 @@ def test_snapshot(self, vm_on_linstor_sr):
7878
finally:
7979
vm.shutdown(verify=True)
8080

81+
def test_forget_and_introduce_sr(self, linstor_sr):
82+
from lib.sr import SR
83+
84+
sr = linstor_sr
85+
sr_name = sr.param_get('name-label')
86+
all_pbds = sr.pbd_uuids()
87+
pbd_config_hosts = []
88+
pbd_config_devices = []
89+
# TBD: Move the pbd-param-get to either sr.py or introduce pbd.py
90+
for pbd in all_pbds:
91+
pbd_config_hosts.append(
92+
safe_split(sr.pool.master.xe('pbd-param-get', {'uuid': pbd, 'param-name': 'host-uuid'})))
93+
pbd_config_devices.append(
94+
safe_split(sr.pool.master.xe('pbd-param-get', {'uuid': pbd, 'param-name': 'device-config'})))
95+
96+
if sr.all_pbds_attached():
97+
sr.unplug_pbds()
98+
99+
sr.forget()
100+
logging.info(f"Forgot SR {sr.uuid} successfully.")
101+
102+
with pytest.raises(Exception):
103+
sr_type = sr.param_get('type') # Expecting exception as sr should not exist
104+
sr.plug_pbds() # Plug back pbds and let teardown handle SR destroy
105+
pytest.fail(f"SR still exists; returned type: {sr_type}")
106+
107+
logging.info(f"Introducing SR {sr.uuid} back.")
108+
new_sr = sr.introduce(type='linstor', shared='true', name_label=sr_name, uuid=sr.uuid)
109+
110+
# Example pbd_config_device
111+
# {provisioning: thin; redundancy: 3; group-name: linstor_group/thin_device}
112+
for pbd_config_host, pbd_config_device in zip(pbd_config_hosts, pbd_config_devices):
113+
pbd_config_dict = dict(
114+
(kv.split(": ")[0].strip(), kv.split(": ")[1].strip())
115+
for kv in pbd_config_device[0].split(";") if ": " in kv # Ensure key-value pair
116+
)
117+
device_config_entries = [('device-config:' + k, v) for k, v in pbd_config_dict.items()]
118+
119+
sr.pool.master.xe(
120+
'pbd-create',
121+
[
122+
('sr-uuid', new_sr),
123+
('host-uuid', pbd_config_host[0]),
124+
('content-type', 'user'),
125+
] + device_config_entries
126+
)
127+
128+
restored_sr = SR(new_sr, sr.pool)
129+
restored_sr.plug_pbds(verify=True)
130+
logging.info(f"Introduced SR {sr.uuid} successfully.")
131+
81132
# *** tests with reboots (longer tests).
82133

83134
@pytest.mark.reboot

0 commit comments

Comments
 (0)