|
5 | 5 | import os |
6 | 6 | import platform |
7 | 7 | from pathlib import Path |
8 | | -from typing import List, Tuple |
| 8 | +from typing import Any, Dict, List, Tuple |
9 | 9 |
|
10 | 10 | import libvirt # type: ignore |
11 | 11 | import pytest |
|
14 | 14 | from ..utils.closeable import Closeable |
15 | 15 | from ..utils.host_utils import get_host_distro |
16 | 16 | from ..utils.imagecreator import run_image_creator, run_image_customizer_binary |
17 | | -from ..utils.imagecustomizer import add_ssh_to_config |
| 17 | +from ..utils.imagecustomizer import add_preview_features_to_config, add_ssh_to_config |
18 | 18 | from ..utils.libvirt_utils import VmSpec, create_libvirt_domain_xml |
19 | 19 | from ..utils.libvirt_vm import LibvirtVm |
20 | 20 | from ..utils.ssh_client import SshClient |
|
25 | 25 | "toolkit/tools/pkg/imagecreatorlib/testdata" |
26 | 26 | ) |
27 | 27 |
|
| 28 | +# Common packages that should be present in all distributions |
| 29 | +COMMON_PACKAGES = ["kernel", "systemd", "bash"] |
| 30 | + |
| 31 | +# Distribution-specific configuration |
| 32 | +DISTRO_CONFIGS: Dict[str, Dict[str, Any]] = { |
| 33 | + "fedora": { |
| 34 | + "os_release": { |
| 35 | + "ID": "fedora", |
| 36 | + "VERSION_ID": "42", |
| 37 | + }, |
| 38 | + "packages": COMMON_PACKAGES + ["grub2-efi-x64"], |
| 39 | + }, |
| 40 | + "azurelinux": { |
| 41 | + "os_release": { |
| 42 | + "ID": "azurelinux", |
| 43 | + "VERSION_ID": "3.0", |
| 44 | + }, |
| 45 | + "packages": COMMON_PACKAGES + ["grub2"], |
| 46 | + }, |
| 47 | +} |
| 48 | + |
28 | 49 |
|
29 | 50 | def run_image_creator_test( |
30 | 51 | image_creator_binary_path: Path, |
@@ -89,6 +110,10 @@ def run_image_creator_test( |
89 | 110 | base_ssh_config_path = IMAGECREATOR_TEST_CONFIGS_DIR.joinpath("ssh-base-config.yaml") |
90 | 111 | customizer_config_path_obj = add_ssh_to_config(base_ssh_config_path, username, ssh_public_key, close_list) |
91 | 112 |
|
| 113 | + # Add Fedora preview features if needed |
| 114 | + if distro.lower() == "fedora": |
| 115 | + customizer_config_path_obj = add_preview_features_to_config(customizer_config_path_obj, "fedora-42", close_list) |
| 116 | + |
92 | 117 | run_image_customizer_binary( |
93 | 118 | image_customizer_binary_path, |
94 | 119 | initial_output_image_path, |
@@ -189,32 +214,65 @@ def run_image_creator_test( |
189 | 214 | logging.info(f"SSH connection established successfully!") |
190 | 215 | # Run the test |
191 | 216 | logging.info(f"Running basic checks on the VM") |
192 | | - run_basic_checks(ssh_client, test_temp_dir) |
| 217 | + run_basic_checks(ssh_client, test_temp_dir, distro) |
193 | 218 | logging.info(f"Basic checks completed successfully!") |
194 | 219 |
|
195 | 220 |
|
| 221 | +def verify_packages(ssh_client: SshClient, packages_to_check: List[str]) -> None: |
| 222 | + """Verify that specified packages are present in the system. |
| 223 | +
|
| 224 | + Args: |
| 225 | + ssh_client: SSH client connected to the target system |
| 226 | + packages_to_check: List of package names to verify |
| 227 | + """ |
| 228 | + for package in packages_to_check: |
| 229 | + ssh_client.run(f"rpm -q {package}").check_exit_code() |
| 230 | + |
| 231 | + |
| 232 | +def verify_os_release(os_release_text: str, expected_values: Dict[str, str]) -> None: |
| 233 | + """Verify os-release content matches expected values. |
| 234 | +
|
| 235 | + Args: |
| 236 | + os_release_text: Content of os-release file |
| 237 | + expected_values: Dictionary of key-value pairs to verify |
| 238 | + """ |
| 239 | + for key, value in expected_values.items(): |
| 240 | + expected = f"{key}={value}" |
| 241 | + assert expected in os_release_text, f"Expected '{expected}' in os-release, but not found" |
| 242 | + |
| 243 | + |
196 | 244 | def run_basic_checks( |
197 | 245 | ssh_client: SshClient, |
198 | 246 | test_temp_dir: Path, |
| 247 | + distro: str, |
199 | 248 | ) -> None: |
| 249 | + """Run basic checks for the specified distribution. |
200 | 250 |
|
| 251 | + Args: |
| 252 | + ssh_client: SSH client for running commands |
| 253 | + test_temp_dir: Temporary directory for test artifacts |
| 254 | + distro: Distribution name (must be a key in DISTRO_CONFIGS) |
| 255 | + """ |
| 256 | + if distro not in DISTRO_CONFIGS: |
| 257 | + raise ValueError(f"Unsupported distribution: {distro}") |
| 258 | + |
| 259 | + config = DISTRO_CONFIGS[distro] |
| 260 | + |
| 261 | + # Check kernel cmdline |
201 | 262 | ssh_client.run("cat /proc/cmdline").check_exit_code() |
202 | 263 |
|
| 264 | + # Get and verify os-release |
203 | 265 | os_release_path = test_temp_dir.joinpath("os-release") |
204 | 266 | ssh_client.get_file(Path("/etc/os-release"), os_release_path) |
205 | 267 |
|
206 | 268 | with open(os_release_path, "r") as os_release_fd: |
207 | 269 | os_release_text = os_release_fd.read() |
| 270 | + os_release_config = {str(k): str(v) for k, v in config["os_release"].items()} |
| 271 | + verify_os_release(os_release_text, os_release_config) |
208 | 272 |
|
209 | | - # Since imagecreator creates new images, we expect Azure Linux 3.0 |
210 | | - assert "ID=azurelinux" in os_release_text |
211 | | - assert 'VERSION_ID="3.0"' in os_release_text |
212 | | - |
213 | | - # Check that essential packages are installed |
214 | | - ssh_client.run("rpm -q kernel").check_exit_code() |
215 | | - ssh_client.run("rpm -q systemd").check_exit_code() |
216 | | - ssh_client.run("rpm -q grub2").check_exit_code() |
217 | | - ssh_client.run("rpm -q bash").check_exit_code() |
| 273 | + # Check required packages |
| 274 | + package_list = [str(pkg) for pkg in config["packages"]] |
| 275 | + verify_packages(ssh_client, package_list) |
218 | 276 |
|
219 | 277 |
|
220 | 278 | @pytest.mark.skipif(platform.machine() != "x86_64", reason="arm64 is not supported for this combination") |
|
0 commit comments