Skip to content

Commit d21b505

Browse files
feat(tests): integrate with xcp-ng fixtures
Signed-off-by: Mathieu Labourier <[email protected]>
1 parent 500f788 commit d21b505

File tree

2 files changed

+120
-47
lines changed

2 files changed

+120
-47
lines changed

tests/storage/benchmarks/conftest.py

Lines changed: 82 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,99 @@
33
import os
44
import logging
55

6-
from helpers import load_results_from_csv
6+
from lib.commands import SSHCommandFailed
7+
from .helpers import load_results_from_csv
78

8-
@pytest.fixture(scope='package')
9-
def ext_sr(host, sr_disk):
10-
sr = host.sr_create('ext', "EXT-local-SR-test", {'device': '/dev/' + sr_disk})
11-
yield sr
12-
# teardown
13-
sr.destroy()
14-
15-
@pytest.fixture(scope='module', params=['raw', 'vhd', 'qcow2'])
16-
def disk_on_ext_sr(request, ext_sr):
17-
disk_type = request.param
18-
disk = {}
19-
if disk_type == 'raw':
20-
...
21-
elif disk_type == 'vhd':
22-
...
23-
elif disk_type == 'qcow2':
24-
...
9+
MAX_LENGTH = 64*(1024**3) # 64GiB
10+
11+
# use vhd, qcow2, raw... when image_format support will be available
12+
@pytest.fixture(scope="module", params=['vdi'])
13+
def image_format(request):
14+
return request.param
15+
16+
@pytest.fixture(scope="module")
17+
def running_unix_vm_with_fio(running_unix_vm):
18+
vm = running_unix_vm
19+
install_cmds = (
20+
("command -v apt", "apt update && apt install -y fio", "apt remove -y fio"),
21+
("command -v dnf", "dnf install -y fio", "dnf remove -y fio"),
22+
("command -v yum", "yum install -y fio", "yum remove -y fio"),
23+
("command -v apk", "apk add fio", "apk del fio")
24+
)
25+
26+
for check_cmd, install_cmd, remove in install_cmds:
27+
try:
28+
vm.ssh(check_cmd, check=True)
29+
logging.info(f">> Installing fio with {install_cmd}")
30+
vm.ssh(install_cmd, check=True)
31+
remove_cmd = remove
32+
break
33+
except SSHCommandFailed: ...
2534
else:
26-
raise ValueError(f"Unsupported disk type: {disk_type}")
35+
raise RuntimeError("Unsupported package manager: could not install fio")
2736

28-
yield disk
37+
yield vm
2938

3039
# teardown
31-
...
40+
logging.info(f"<< Removing fio with {remove_cmd}")
41+
vm.ssh(remove_cmd, check=False)
42+
43+
44+
@pytest.fixture(scope="module")
45+
def vdi_on_local_sr(host, local_sr_on_hostA1, image_format):
46+
sr = local_sr_on_hostA1
47+
vdi = sr.create_vdi("testVDI", MAX_LENGTH)
48+
vdi.image_format = image_format
49+
logging.info(f">> Created VDI {vdi.uuid} of type {image_format}")
50+
51+
yield vdi
52+
53+
# teardown
54+
logging.info(f"<< Destroying VDI {vdi.uuid}")
55+
vdi.destroy()
56+
57+
@pytest.fixture(scope="module")
58+
def running_unix_vm_and_vbd(host, vdi_on_local_sr, running_unix_vm_with_fio):
59+
vm = running_unix_vm_with_fio
60+
vdi = vdi_on_local_sr
61+
vbd = vm.create_vbd("autodetect", vdi.uuid)
62+
63+
logging.info(f">> Plugging VDI {vdi.uuid} on VM {vm.uuid}")
64+
vbd.plug()
65+
66+
yield vm, vbd
3267

33-
@pytest.fixture(scope='module')
34-
def vm_on_ext_sr(host, ext_sr, vm_ref):
35-
vm = host.import_vm(vm_ref, sr_uuid=ext_sr.uuid)
36-
yield vm
3768
# teardown
38-
logging.info("<< Destroy VM")
39-
vm.destroy(verify=True)
69+
logging.info(f"<< Unplugging VDI {vdi.uuid} from VM {vm.uuid}")
70+
vbd.unplug()
71+
vbd.destroy()
72+
73+
@pytest.fixture(scope="module")
74+
def vm_with_vbd(running_unix_vm_and_vbd):
75+
vm, _vbd = running_unix_vm_and_vbd
76+
return vm
77+
78+
@pytest.fixture(scope="module")
79+
def plugged_vbd(running_unix_vm_and_vbd):
80+
_vm, vbd = running_unix_vm_and_vbd
81+
return vbd
4082

41-
@pytest.fixture
42-
def temp_dir():
83+
@pytest.fixture(scope="module")
84+
def local_temp_dir():
4385
with tempfile.TemporaryDirectory() as tmpdir:
4486
yield tmpdir
4587

88+
@pytest.fixture(scope="module")
89+
def temp_dir(vm_with_vbd):
90+
vm = vm_with_vbd
91+
tempdir = vm.ssh("mktemp -d")
92+
93+
yield tempdir
94+
95+
# teardown
96+
vm.ssh(f"rm -r {tempdir}")
97+
98+
4699
def pytest_addoption(parser):
47100
parser.addoption(
48101
"--prev-csv",

tests/storage/benchmarks/test_disk_perf.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import os
33
import json
44
import statistics
5-
import subprocess
65
import pytest
76
import logging
87
from datetime import datetime
98

10-
from helpers import load_results_from_csv, log_result_csv, mean
9+
from lib.commands import SSHCommandFailed
10+
from .helpers import load_results_from_csv, log_result_csv, mean
1111

1212
### Tests default settings ###
1313

@@ -32,25 +32,21 @@
3232
"randwrite"
3333
)
3434

35-
test_types = {
36-
"read": "seq_read",
37-
"randread": "rand_read",
38-
"write": "seq_write",
39-
"randwrite": "rand_write"
40-
}
41-
4235
### End of tests parameters ###
4336

4437
def run_fio(
38+
vm,
4539
test_name,
4640
rw_mode,
4741
temp_dir,
42+
local_temp_dir,
4843
bs=DEFAULT_BS,
4944
iodepth=DEFAULT_IODEPTH,
5045
size=DEFAULT_SIZE,
5146
file_path="",
5247
):
5348
json_output_path = os.path.join(temp_dir, f"{test_name}.json")
49+
local_json_path = os.path.join(local_temp_dir, f"{test_name}.json")
5450
if not file_path:
5551
file_path = os.path.join(temp_dir, DEFAULT_FILE)
5652
fio_cmd = [
@@ -69,12 +65,14 @@ def run_fio(
6965
"--output-format=json",
7066
f"--output={json_output_path}"
7167
]
72-
73-
result = subprocess.run(fio_cmd, capture_output=True, text=True)
74-
if result.returncode != 0:
75-
raise RuntimeError(f"fio failed for {test_name}:\n{result.stderr}")
76-
77-
with open(json_output_path) as f:
68+
logging.debug(f"Running {fio_cmd}")
69+
try:
70+
vm.ssh(fio_cmd, check=True)
71+
except SSHCommandFailed as e:
72+
raise RuntimeError(f"fio failed for {test_name}:{e}")
73+
vm.scp(json_output_path, local_json_path, local_dest=True)
74+
logging.debug(f"Stored json at {local_json_path}")
75+
with open(local_json_path) as f:
7876
return json.load(f)
7977

8078
def assert_performance_not_degraded(current, previous, threshold=10):
@@ -107,14 +105,36 @@ class TestDiskPerf:
107105
def test_disk_benchmark(
108106
self,
109107
temp_dir,
108+
local_temp_dir,
110109
prev_results,
111110
block_size,
112111
file_size,
113-
rw_mode
112+
rw_mode,
113+
vm_with_vbd,
114+
plugged_vbd,
115+
image_format
114116
):
115-
test_type = test_types[rw_mode]
117+
vm = vm_with_vbd
118+
vbd = plugged_vbd
119+
device = f"/dev/{vbd.param_get(param_name="device")}"
120+
test_type = "{}-{}-{}-{}".format(
121+
block_size,
122+
file_size,
123+
rw_mode,
124+
image_format
125+
)
126+
116127
for i in range(DEFAULT_SAMPLES_NUM):
117-
result = run_fio(test_type, rw_mode, temp_dir)
128+
result = run_fio(
129+
vm,
130+
test_type,
131+
rw_mode,
132+
temp_dir,
133+
local_temp_dir,
134+
file_path=device,
135+
bs=block_size,
136+
size=file_size
137+
)
118138
summary = log_result_csv(test_type, rw_mode, result, CSV_FILE)
119139
assert summary["IOPS"] > 0
120140
results = load_results_from_csv(CSV_FILE)

0 commit comments

Comments
 (0)