Skip to content

Commit 0b433dc

Browse files
committed
tests: add GuestDistro to Microvm
Add a new member of the Microvm class, GuestDisrto, which contains distro-specific information. The currently included fields are hostname (set in rootfs build), ssh_service (Ubuntu uses ssh.service vs AL2023's sshd.service), os_release_token (displayed as part of /etc/os-release), and shell_prompt. These fields are used a few different tests. The GuestDistro class makes it easier to extend if new fields are required. Signed-off-by: James Curtis <jxcurtis@amazon.co.uk>
1 parent e8da8cf commit 0b433dc

5 files changed

Lines changed: 39 additions & 7 deletions

File tree

tests/framework/guest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
"""Guest distro detection and distro-specific properties."""
5+
6+
from pathlib import Path
7+
8+
9+
class GuestDistro:
10+
"""Distro-specific guest properties, inferred from rootfs filename."""
11+
12+
def __init__(self, rootfs_path: Path):
13+
name = rootfs_path.stem.lower()
14+
if "ubuntu" in name:
15+
self.hostname = "ubuntu-fc-uvm"
16+
self.ssh_service = "ssh.service"
17+
self.os_release_token = "ID=ubuntu"
18+
self.shell_prompt = f"{self.hostname}:~#"
19+
elif "amazon" in name or "al2023" in name:
20+
self.hostname = "al2023-fc-uvm"
21+
self.ssh_service = "sshd.service"
22+
self.os_release_token = 'ID="amzn"'
23+
self.shell_prompt = f"[root@{self.hostname} ~]#"
24+
else:
25+
raise ValueError(f"Unknown guest distro for rootfs: {rootfs_path}")

tests/framework/microvm.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import host_tools.network as net_tools
3535
from framework import utils
3636
from framework.defs import DEFAULT_BINARY_DIR, MAX_API_CALL_DURATION_MS
37+
from framework.guest import GuestDistro
3738
from framework.http_api import Api
3839
from framework.jailer import JailerContext
3940
from framework.microvm_helpers import MicrovmHelpers
@@ -221,6 +222,7 @@ def __init__(
221222

222223
self.kernel_file = None
223224
self.rootfs_file = None
225+
self.distro = None
224226
self.ssh_key = None
225227
self.initrd_file = None
226228
self.boot_args = None
@@ -1062,6 +1064,7 @@ def make_snapshot(
10621064
snapshot_type=snapshot_type,
10631065
meta={
10641066
"kernel_file": str(self.kernel_file),
1067+
"rootfs_file": str(self.rootfs_file) if self.rootfs_file else None,
10651068
"vcpus_count": self.vcpus_count,
10661069
},
10671070
)
@@ -1125,6 +1128,9 @@ def restore_from_snapshot(
11251128
setattr(self, key, value)
11261129
# Adjust things just in case
11271130
self.kernel_file = Path(self.kernel_file)
1131+
if self.rootfs_file:
1132+
self.rootfs_file = Path(self.rootfs_file)
1133+
self.distro = GuestDistro(self.rootfs_file)
11281134

11291135
iface_overrides = []
11301136
if rename_interfaces is not None:
@@ -1306,6 +1312,7 @@ def build(self, kernel=None, rootfs=None, **kwargs):
13061312
rootfs_path = Path(vm.path) / rootfs.name
13071313
shutil.copyfile(rootfs, rootfs_path)
13081314
vm.rootfs_file = rootfs_path
1315+
vm.distro = GuestDistro(rootfs_path)
13091316
vm.ssh_key = ssh_key
13101317
return vm
13111318

tests/framework/utils_vsock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def check_guest_connections(vm, server_port_path, blob_path, blob_hash):
163163
# Needed to execute the bash script that tests for concurrent
164164
# vsock guest initiated connections.
165165
vm.ssh.check_output(
166-
"echo 1024 > /sys/fs/cgroup/system.slice/ssh.service/pids.max"
166+
f"echo 1024 > /sys/fs/cgroup/system.slice/{vm.distro.ssh_service}/pids.max"
167167
)
168168

169169
# Build the guest worker sub-command.

tests/integration_tests/functional/test_kernel_cmdline.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ def test_init_params(uvm_plain):
1818
vm.memory_monitor = None
1919

2020
# We will override the init with /bin/cat so that we try to read the
21-
# Ubuntu version from the /etc/issue file.
21+
# distro info from the /etc/os-release file.
2222
vm.basic_config(
2323
vcpu_count=1,
24-
boot_args="console=ttyS0 reboot=k panic=1 swiotlb=noforce init=/bin/cat -- /etc/issue",
24+
boot_args="console=ttyS0 reboot=k panic=1 swiotlb=noforce init=/bin/cat -- /etc/os-release",
2525
)
2626

2727
vm.start()
2828
serial = Serial(vm)
2929
serial.open()
3030
# If the string does not show up, the test will fail.
31-
serial.rx(token="Ubuntu 24.04")
31+
serial.rx(token=vm.distro.os_release_token)

tests/integration_tests/functional/test_serial_io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_serial_after_snapshot(uvm_plain, microvm_factory):
3131
microvm.start()
3232

3333
# looking for the # prompt at the end
34-
serial.rx("ubuntu-fc-uvm:~#")
34+
serial.rx(microvm.distro.shell_prompt)
3535

3636
# Create snapshot.
3737
snapshot = microvm.snapshot_full()
@@ -99,7 +99,7 @@ def test_serial_active_tx_snapshot(uvm_plain, microvm_factory):
9999
# Send Ctrl-C to the guest to stop the ongoing transmission and regain the shell
100100
serial.tx("\x03", end="")
101101
# looking for the # prompt at the end
102-
serial.rx("ubuntu-fc-uvm:~#")
102+
serial.rx(vm.distro.shell_prompt)
103103
serial.tx("pwd")
104104
res = serial.rx("#")
105105
assert "/root" in res
@@ -124,7 +124,7 @@ def test_serial_console_login(uvm_plain_any):
124124

125125
serial = Serial(microvm)
126126
serial.open()
127-
serial.rx("ubuntu-fc-uvm:")
127+
serial.rx(microvm.distro.shell_prompt)
128128
serial.tx("id")
129129
serial.rx("uid=0(root) gid=0(root) groups=0(root)")
130130

0 commit comments

Comments
 (0)