Skip to content

Commit add8109

Browse files
committed
fix(coalesce): Refactor function to deduplicate
Signed-off-by: Damien Thenot <[email protected]>
1 parent 2e9f430 commit add8109

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

tests/storage/coalesce/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def vdi_with_vbd_on_dom0(host, vdi_on_local_sr):
2424

2525
dom0.disconnect_vdi(vdi_on_local_sr)
2626

27-
@pytest.fixture(scope="class")
27+
@pytest.fixture(scope="function")
2828
def data_file_on_host(host):
2929
filename = "/root/data.bin"
3030
logging.info(f">> Creating data file {filename} on host")

tests/storage/coalesce/test_coalesce.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,40 @@
1-
import logging
1+
import pytest
22

3-
from .utils import compare_data, copy_data_to_tapdev, snapshot_vdi, wait_for_vdi_coalesce
3+
import logging
44

5-
class Test:
6-
def test_write_data(self, host, tapdev, data_file_on_host):
7-
length = 1 * 1024 * 1024
8-
offset = 0
5+
from .utils import compare_data, copy_data_to_tapdev, operation_on_vdi, wait_for_vdi_coalesce
96

10-
logging.info("Copying data to tapdev")
11-
copy_data_to_tapdev(host, data_file_on_host, tapdev, offset, length)
7+
from typing import TYPE_CHECKING
128

13-
logging.info("Comparing data to tapdev")
14-
assert compare_data(host, tapdev, data_file_on_host, offset, length)
9+
if TYPE_CHECKING:
10+
from lib.host import Host
11+
from lib.vdi import VDI
1512

16-
def test_coalesce(self, host, tapdev, vdi_with_vbd_on_dom0, data_file_on_host):
17-
vdi = vdi_with_vbd_on_dom0
18-
vdi_uuid = vdi.uuid
13+
class Test:
14+
def test_write_data(self, host: "Host", tapdev: str, data_file_on_host: str):
1915
length = 1 * 1024 * 1024
2016
offset = 0
2117

22-
vdi_snap = snapshot_vdi(host, vdi_uuid)
23-
2418
logging.info("Copying data to tapdev")
2519
copy_data_to_tapdev(host, data_file_on_host, tapdev, offset, length)
2620

27-
logging.info("Removing VDI snapshot")
28-
host.xe("vdi-destroy", {"uuid": vdi_snap})
29-
30-
wait_for_vdi_coalesce(vdi)
31-
3221
logging.info("Comparing data to tapdev")
3322
assert compare_data(host, tapdev, data_file_on_host, offset, length)
3423

35-
def test_clone_coalesce(self, host, tapdev, vdi_with_vbd_on_dom0, data_file_on_host):
24+
@pytest.mark.parametrize("vdi_op", ["snapshot", "clone"])
25+
def test_coalesce(self, host: "Host", tapdev: str, vdi_with_vbd_on_dom0: "VDI", data_file_on_host: str, vdi_op):
3626
vdi = vdi_with_vbd_on_dom0
3727
vdi_uuid = vdi.uuid
3828
length = 1 * 1024 * 1024
3929
offset = 0
4030

41-
clone_uuid = host.xe("vdi-clone", {"uuid": vdi_uuid})
42-
logging.info(f"Clone VDI {vdi_uuid}: {clone_uuid}")
31+
new_vdi = operation_on_vdi(host, vdi_uuid, vdi_op)
4332

4433
logging.info("Copying data to tapdev")
4534
copy_data_to_tapdev(host, data_file_on_host, tapdev, offset, length)
4635

47-
logging.info("Removing VDI clone")
48-
host.xe("vdi-destroy", {"uuid": clone_uuid})
36+
logging.info(f"Removing VDI {vdi_op}")
37+
host.xe("vdi-destroy", {"uuid": new_vdi})
4938

5039
wait_for_vdi_coalesce(vdi)
5140

tests/storage/coalesce/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from lib.host import Host
55
from lib.vdi import VDI
66

7+
from typing import Literal
8+
79
def wait_for_vdi_coalesce(vdi: VDI):
810
wait_for_not(lambda: vdi.get_parent(), msg="Waiting for coalesce")
911
logging.info("Coalesce done")
@@ -31,10 +33,10 @@ def get_data(host: Host, file: str, offset: int, length: int, checksum: bool = F
3133
def get_hashed_data(host: Host, file: str, offset: int, length: int):
3234
return get_data(host, file, offset, length, True).split()[0]
3335

34-
def snapshot_vdi(host: Host, vdi_uuid: str):
35-
vdi_snap = host.xe("vdi-snapshot", {"uuid": vdi_uuid})
36-
logging.info(f"Snapshot VDI {vdi_uuid}: {vdi_snap}")
37-
return vdi_snap
36+
def operation_on_vdi(host: Host, vdi_uuid: str, vdi_op: Literal["snapshot", "clone"]) -> str:
37+
new_vdi = host.xe(f"vdi-{vdi_op}", {"uuid": vdi_uuid})
38+
logging.info(f"{vdi_op.capitalize()} VDI {vdi_uuid}: {new_vdi}")
39+
return new_vdi
3840

3941
def compare_data(host: Host, tapdev: str, data_file: str, offset: int, length: int) -> bool:
4042
logging.info("Getting data from VDI and file")

0 commit comments

Comments
 (0)