Skip to content

Commit fee384b

Browse files
committed
Separated resume tests into a separate module
- added the `test_modules` parameter to to specify which modules to include in test image. - the `test_resume` parameter now includes the extra hibernate/resume testing function - including swap in the test image now works off the `test_swap_uuid` parameter, so it's less dependent on the resume module. would still be ideal to fix up the code that builds the swap at some point.
1 parent 61e55fe commit fee384b

File tree

8 files changed

+42
-35
lines changed

8 files changed

+42
-35
lines changed

src/ugrd/base/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def make_test_image(self):
8686
"validate": False,
8787
"NO_BASE": True,
8888
"config": None,
89-
"modules": "ugrd.fs.test_image",
89+
"modules": ','.join(self["test_modules"]), # By default is only "ugrd.fs.test_image"
9090
"out_file": self["test_rootfs_name"],
9191
"build_dir": self["test_rootfs_build_dir"],
9292
"custom_parameters": get_copy_config_types(self),

src/ugrd/base/test.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ test_cpu = 'host'
1010
test_arch = 'x86_64'
1111
test_timeout = 30
1212
test_cmdline = 'console=ttyS0,115200 panic=0'
13+
test_modules = [ "ugrd.fs.test_image" ]
1314
qemu_bool_args = ['nographic', 'enable-kvm' ]
1415

1516
[imports.build_pre]
@@ -30,4 +31,5 @@ test_flag = "str" # Define the success flag for the test
3031
test_no_rootfs = "bool" # Toggle to run tests without mounting the root image
3132
test_rootfs_name = "str" # Define the name of the rootfs image
3233
test_rootfs_build_dir = "Path" # Define the build directory for the rootfs image
34+
test_modules = "NoDupFlatList" # List of modules to include in test image
3335
qemu_bool_args = "NoDupFlatList" # Define the qemu boolean arguments

src/ugrd/fs/resume.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ def handle_late_resume(self) -> None:
7171
# At the moment it's the same code but delayed, will change when more features are added
7272
return handle_early_resume(self)
7373

74-
7574
@contains("test_resume")
76-
def test_init_swap_uuid(self):
77-
if "test_cpu" in self:
75+
def test_resume_setup(self) -> None:
76+
if "ugrd.base.test" in self["modules"]:
77+
# Add resume to the list of test modules
78+
self["test_modules"] = "ugrd.fs.test_resume"
79+
80+
# Create a uuid for the swap partition in the test image
7881
from uuid import uuid4
79-
8082
if not self["test_swap_uuid"]:
8183
self["test_swap_uuid"] = swap_uuid = uuid4()
8284

83-
# append to test kernel cmdline and adjust planned image size to allow enough space
85+
# append to QEMU kernel cmdline
8486
self["test_cmdline"] = f"{self.get('test_cmdline')} resume=UUID={swap_uuid}"

src/ugrd/fs/resume.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ binaries = [ 'lsblk' ]
33
test_copy_config = [ "test_resume", "test_swap_uuid" ]
44

55
[imports.build_pre]
6-
"ugrd.fs.resume" = [ "test_init_swap_uuid" ]
6+
"ugrd.fs.resume" = [ "test_resume_setup" ]
77

88
[imports.build_pre]
99
"ugrd.fs.resume" = [ "test_init_swap_uuid" ]

src/ugrd/fs/test_image.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,6 @@ def init_banner(self):
1515
self["banner"] = "echo ugRD Test Image"
1616

1717

18-
@contains("test_resume")
19-
def resume_tests(self):
20-
return """
21-
echo "Begin resume testing."
22-
if [ "$(</sys/power/resume)" != "0:0" ] ; then
23-
echo reboot > /sys/power/disk
24-
echo 1 > /sys/power/pm_debug_messages
25-
26-
local SWAPDEV=/dev/$(source "/sys/dev/block/$(</sys/power/resume)/uevent" && echo $DEVNAME)
27-
echo "Activating swap device ${SWAPDEV}..."
28-
swapon ${SWAPDEV}
29-
30-
echo "Triggering test hibernation..."
31-
echo disk > /sys/power/state || (echo "Suspend to disk failed!" ; echo c > /proc/sysrq-trigger)
32-
33-
# Assume at this point system has hibernated then resumed again
34-
echo "Resume test completed without error."
35-
else
36-
echo "No resume device found! Resume test not possible!"
37-
echo c > /proc/sysrq-trigger
38-
fi
39-
"""
40-
41-
4218
@contains("test_flag", "A test flag must be set to create a test image", raise_exception=True)
4319
def complete_tests(self):
4420
return f"""
@@ -156,7 +132,7 @@ def make_test_image(self):
156132
_allocate_image(self, image_path)
157133

158134
loopback = None
159-
if self.get("test_resume"):
135+
if self.get("test_swap_uuid"):
160136
try:
161137
self._run(["sgdisk", "-og", "-n", "1:0:+128M", "-n", "2:0:0", image_path])
162138
except RuntimeError as e:
@@ -168,6 +144,7 @@ def make_test_image(self):
168144

169145
image_path = f"{loopback}p2"
170146
except RuntimeError as e:
147+
self._run(["losetup", "-d", loopback]) # Free loopback device on fail
171148
raise RuntimeError("Failed to allocate loopback device for disk creation: %s", e)
172149

173150
# sleep for 100ms, to give the loopback device time to scan for partitions
@@ -178,6 +155,7 @@ def make_test_image(self):
178155
try:
179156
self._run(["mkswap", "-U", self["test_swap_uuid"], f"{loopback}p1"])
180157
except RuntimeError as e:
158+
self._run(["losetup", "-d", loopback])
181159
raise RuntimeError("Failed to create swap partition on test disk: %s", e)
182160

183161
if rootfs_type == "ext4":

src/ugrd/fs/test_image.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ _cryptsetup_root = "root"
1010
[imports.build_pre]
1111
"ugrd.fs.test_image" = ["init_banner"]
1212

13-
[imports.init_main]
14-
"ugrd.fs.test_image" = ["resume_tests"]
15-
1613
[imports.init_final]
1714
"ugrd.fs.test_image" = ["complete_tests"]
1815

src/ugrd/fs/test_resume.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
__version__ = "2.0.0"
2+
3+
from zenlib.util import contains
4+
5+
@contains("test_resume")
6+
def resume_tests(self):
7+
return """
8+
echo "Begin resume testing."
9+
if [ "$(</sys/power/resume)" != "0:0" ] ; then
10+
echo reboot > /sys/power/disk
11+
echo 1 > /sys/power/pm_debug_messages
12+
13+
local SWAPDEV=/dev/$(source "/sys/dev/block/$(</sys/power/resume)/uevent" && echo $DEVNAME)
14+
echo "Activating swap device ${SWAPDEV}..."
15+
swapon ${SWAPDEV}
16+
17+
echo "Triggering test hibernation..."
18+
echo disk > /sys/power/state || (echo "Suspend to disk failed!" ; echo c > /proc/sysrq-trigger)
19+
20+
# Assume at this point system has hibernated then resumed again
21+
echo "Resume test completed without error."
22+
else
23+
echo "No resume device found! Resume test not possible!"
24+
echo c > /proc/sysrq-trigger
25+
fi
26+
"""

src/ugrd/fs/test_resume.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[imports.init_main]
2+
"ugrd.fs.test_resume" = ["resume_tests"]

0 commit comments

Comments
 (0)