Skip to content

Commit 14396e7

Browse files
author
Yongxue Hong
committed
tests: Add unit and functional tests for qemu_utils module
This commit introduces both unit and functional tests for the qemu_utils module, providing thorough coverage of QEMU option detection and machine type querying functionality. Signed-off-by: Yongxue Hong <yhong@redhat.com>
1 parent 8a8934d commit 14396e7

4 files changed

Lines changed: 417 additions & 0 deletions

File tree

selftests/functional/test_vt_utils/__init__.py

Whitespace-only changes.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import os
2+
import subprocess
3+
import unittest
4+
5+
from virttest.vt_utils import qemu_utils as qemu
6+
7+
8+
class QemuFunctionalTest(unittest.TestCase):
9+
"""Functional tests for autils.hypervisor.qemu module"""
10+
11+
def setUp(self):
12+
super().setUp()
13+
self.available_qemu_paths = self._get_available_qemu_paths()
14+
15+
def _get_available_qemu_paths(self):
16+
"""Get the available qemu paths on the system"""
17+
qemu_paths = [
18+
"/usr/bin/qemu-kvm",
19+
"/usr/bin/qemu-system-x86_64",
20+
"/usr/libexec/qemu-kvm",
21+
"/usr/bin/qemu",
22+
]
23+
24+
available_qemu_paths = []
25+
26+
for path in qemu_paths:
27+
if os.path.exists(path):
28+
try:
29+
result = subprocess.run(
30+
[path, "-help"], capture_output=True, timeout=10, check=False
31+
)
32+
if not result.returncode:
33+
available_qemu_paths.append(path)
34+
except (subprocess.TimeoutExpired, FileNotFoundError):
35+
continue
36+
return available_qemu_paths
37+
38+
def test_has_option_with_real_qemu(self):
39+
"""Test has_option function with real QEMU binary"""
40+
if not self.available_qemu_paths:
41+
self.skipTest("QEMU not available on system")
42+
43+
default_qemu_path = "/usr/bin/qemu-kvm"
44+
if default_qemu_path not in self.available_qemu_paths:
45+
self.skipTest(
46+
f"The default qemu path: {default_qemu_path} not available on system"
47+
)
48+
49+
# Test common options that should exist in most QEMU versions
50+
self.assertTrue(qemu.has_option("h"))
51+
self.assertTrue(qemu.has_option("version"))
52+
53+
# Test option that should not exist
54+
self.assertFalse(qemu.has_option("nonexistent-option-12345"))
55+
56+
def test_has_option_with_custom_path(self):
57+
"""Test has_option with custom QEMU path"""
58+
# Find an available QEMU binary
59+
qemu_paths = [
60+
"/usr/bin/qemu-system-x86_64",
61+
"/usr/libexec/qemu-kvm",
62+
"/usr/bin/qemu",
63+
]
64+
65+
available_path = None
66+
for path in qemu_paths:
67+
if os.path.exists(path):
68+
available_path = path
69+
break
70+
71+
if not available_path:
72+
self.skipTest(
73+
f"The custom qemu paths: {qemu_paths} not available on system"
74+
)
75+
76+
if available_path:
77+
result = qemu.has_option("h", available_path)
78+
self.assertTrue(result)
79+
80+
def test_get_support_machine_type_with_real_qemu(self):
81+
"""Test get_support_machine_type function with real QEMU binary"""
82+
if not self.available_qemu_paths:
83+
self.skipTest("QEMU not available on system")
84+
85+
names, types, aliases = qemu.get_support_machine_type(
86+
self.available_qemu_paths[0]
87+
)
88+
89+
# Verify return types
90+
self.assertIsInstance(names, list)
91+
self.assertIsInstance(types, list)
92+
self.assertIsInstance(aliases, list)
93+
94+
# All lists should have the same length
95+
self.assertEqual(len(names), len(types))
96+
self.assertEqual(len(names), len(aliases))
97+
98+
# Should have at least one machine type
99+
self.assertGreater(len(names), 0)
100+
101+
# Each name should be a string
102+
for name in names:
103+
self.assertIsInstance(name, str)
104+
self.assertGreater(len(name), 0)
105+
106+
# Each type should be a string
107+
for machine_type in types:
108+
self.assertIsInstance(machine_type, str)
109+
110+
# Aliases can be None or strings
111+
for alias in aliases:
112+
self.assertTrue(alias is None or isinstance(alias, str))
113+
114+
def test_get_support_machine_type_remove_alias(self):
115+
"""Test get_support_machine_type with remove_alias=True"""
116+
if not self.available_qemu_paths:
117+
self.skipTest("QEMU not available on system")
118+
119+
_, _, aliases = qemu.get_support_machine_type(
120+
self.available_qemu_paths[0], remove_alias=True
121+
)
122+
123+
# With remove_alias=True, all aliases should be None
124+
for alias in aliases:
125+
self.assertIsNone(alias)
126+
127+
def test_get_support_machine_type_no_none_machines(self):
128+
"""Test that 'none' machines are filtered out"""
129+
if not self.available_qemu_paths:
130+
self.skipTest("QEMU not available on system")
131+
132+
names, _, _ = qemu.get_support_machine_type(self.available_qemu_paths[0])
133+
134+
# Should not contain 'none' in machine names
135+
self.assertNotIn("none", names)
136+
137+
def test_has_option_invalid_qemu_path(self):
138+
"""Test has_option with invalid QEMU path"""
139+
result = qemu.has_option("help", "/nonexistent/qemu/path")
140+
# Should handle the error gracefully and return False
141+
self.assertFalse(result)
142+
143+
def test_get_support_machine_type_invalid_path(self):
144+
"""Test get_support_machine_type with invalid QEMU path"""
145+
with self.assertRaises((subprocess.CalledProcessError, FileNotFoundError)):
146+
qemu.get_support_machine_type("/nonexistent/qemu/path")
147+
148+
149+
if __name__ == "__main__":
150+
unittest.main()

selftests/unit/test_vt_utils/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)