Skip to content

Commit 3c7a6ce

Browse files
committed
coalesce: move function in a utils.py file
It will allow to re-use in other tests Signed-off-by: Damien Thenot <[email protected]>
1 parent 453f339 commit 3c7a6ce

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

tests/storage/coalesce/test_coalesce.py

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,6 @@
11
import logging
22

3-
from lib.common import wait_for_not
4-
from lib.host import Host
5-
from lib.vdi import VDI
6-
7-
def wait_for_vdi_coalesce(vdi: VDI):
8-
wait_for_not(lambda: vdi.get_parent(), msg="Waiting for coalesce")
9-
logging.info("Coalesce done")
10-
11-
def copy_data_to_tapdev(host: Host, data_file: str, tapdev: str, offset: int, length: int):
12-
"""
13-
if offset == 0:
14-
off = "0"
15-
else:
16-
off = f"{offset}B" # Doesn't work with `dd` version of XCP-ng 8.3
17-
"""
18-
bs = 1
19-
off = int(offset / bs)
20-
count = length / bs
21-
count += length % bs
22-
count = int(count)
23-
cmd = ["dd", f"if={data_file}", f"of={tapdev}", f"bs={bs}", f"seek={off}", f"count={count}"]
24-
host.ssh(cmd)
25-
26-
def get_data(host: Host, file: str, offset: int, length: int, checksum: bool = False) -> str:
27-
cmd = ["xxd", "-p", "-seek", str(offset), "-len", str(length), file]
28-
if checksum:
29-
cmd = cmd + ["|", "sha256sum"]
30-
return host.ssh(cmd)
31-
32-
def get_hashed_data(host: Host, file: str, offset: int, length: int):
33-
return get_data(host, file, offset, length, True).split()[0]
34-
35-
def snapshot_vdi(host: Host, vdi_uuid: str):
36-
vdi_snap = host.xe("vdi-snapshot", {"uuid": vdi_uuid})
37-
logging.info(f"Snapshot VDI {vdi_uuid}: {vdi_snap}")
38-
return vdi_snap
39-
40-
def compare_data(host: Host, tapdev: str, data_file: str, offset: int, length: int) -> bool:
41-
logging.info("Getting data from VDI and file")
42-
vdi_checksum = get_hashed_data(host, tapdev, offset, length)
43-
file_checksum = get_hashed_data(host, data_file, 0, length)
44-
logging.info(f"VDI: {vdi_checksum}")
45-
logging.info(f"FILE: {file_checksum}")
46-
47-
return vdi_checksum == file_checksum
3+
from utils import wait_for_vdi_coalesce, copy_data_to_tapdev, snapshot_vdi, compare_data
484

495
class Test:
506
def test_write_data(self, host, tapdev, data_file_on_host):

tests/storage/coalesce/utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import logging
2+
3+
from lib.common import wait_for_not
4+
from lib.host import Host
5+
from lib.vdi import VDI
6+
7+
def wait_for_vdi_coalesce(vdi: VDI):
8+
wait_for_not(lambda: vdi.get_parent(), msg="Waiting for coalesce")
9+
logging.info("Coalesce done")
10+
11+
def copy_data_to_tapdev(host: Host, data_file: str, tapdev: str, offset: int, length: int):
12+
# if offset == 0:
13+
# off = "0"
14+
# else:
15+
# off = f"{offset}B" # Doesn't work with `dd` version of XCP-ng 8.3
16+
17+
bs = 1
18+
off = int(offset / bs)
19+
count = length / bs
20+
count += length % bs
21+
count = int(count)
22+
cmd = ["dd", f"if={data_file}", f"of={tapdev}", f"bs={bs}", f"seek={off}", f"count={count}"]
23+
host.ssh(cmd)
24+
25+
def get_data(host: Host, file: str, offset: int, length: int, checksum: bool = False) -> str:
26+
cmd = ["xxd", "-p", "-seek", str(offset), "-len", str(length), file]
27+
if checksum:
28+
cmd = cmd + ["|", "sha256sum"]
29+
return host.ssh(cmd)
30+
31+
def get_hashed_data(host: Host, file: str, offset: int, length: int):
32+
return get_data(host, file, offset, length, True).split()[0]
33+
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
38+
39+
def compare_data(host: Host, tapdev: str, data_file: str, offset: int, length: int) -> bool:
40+
logging.info("Getting data from VDI and file")
41+
vdi_checksum = get_hashed_data(host, tapdev, offset, length)
42+
file_checksum = get_hashed_data(host, data_file, 0, length)
43+
logging.info(f"VDI: {vdi_checksum}")
44+
logging.info(f"FILE: {file_checksum}")
45+
46+
return vdi_checksum == file_checksum

0 commit comments

Comments
 (0)