vmm/test_utils/mock_resources/
mod.rs

1// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3#![allow(missing_docs)]
4
5use std::path::PathBuf;
6
7use crate::cpu_config::templates::CustomCpuTemplate;
8use crate::resources::VmResources;
9use crate::vmm_config::boot_source::BootSourceConfig;
10use crate::vmm_config::machine_config::{MachineConfig, MachineConfigUpdate};
11
12pub const DEFAULT_BOOT_ARGS: &str = "reboot=k panic=1 pci=off";
13#[cfg(target_arch = "x86_64")]
14pub const DEFAULT_KERNEL_IMAGE: &str = "test_elf.bin";
15#[cfg(target_arch = "aarch64")]
16pub const DEFAULT_KERNEL_IMAGE: &str = "test_pe.bin";
17#[cfg(target_arch = "x86_64")]
18pub const NOISY_KERNEL_IMAGE: &str = "test_noisy_elf.bin";
19#[cfg(target_arch = "aarch64")]
20pub const NOISY_KERNEL_IMAGE: &str = "test_pe.bin";
21
22pub fn kernel_image_path(kernel_image: Option<&str>) -> String {
23    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
24    path.push("src/test_utils/mock_resources");
25    path.push(kernel_image.unwrap_or(DEFAULT_KERNEL_IMAGE));
26    path.as_os_str().to_str().unwrap().to_string()
27}
28
29macro_rules! generate_from {
30    ($src_type: ty, $dst_type: ty) => {
31        impl From<$src_type> for $dst_type {
32            fn from(src: $src_type) -> $dst_type {
33                src.0
34            }
35        }
36    };
37}
38
39#[derive(Debug)]
40pub struct MockBootSourceConfig(BootSourceConfig);
41
42impl MockBootSourceConfig {
43    pub fn new() -> MockBootSourceConfig {
44        MockBootSourceConfig(BootSourceConfig {
45            kernel_image_path: kernel_image_path(None),
46            initrd_path: None,
47            boot_args: None,
48        })
49    }
50
51    pub fn with_default_boot_args(mut self) -> Self {
52        self.0.boot_args = Some(DEFAULT_BOOT_ARGS.to_string());
53        self
54    }
55
56    #[cfg(target_arch = "x86_64")]
57    pub fn with_kernel(mut self, kernel_image: &str) -> Self {
58        self.0.kernel_image_path = kernel_image_path(Some(kernel_image));
59        self
60    }
61}
62
63impl Default for MockBootSourceConfig {
64    fn default() -> Self {
65        Self::new()
66    }
67}
68
69#[derive(Debug, Default)]
70pub struct MockVmResources(VmResources);
71
72impl MockVmResources {
73    pub fn new() -> MockVmResources {
74        MockVmResources::default()
75    }
76
77    pub fn with_boot_source(mut self, boot_source_cfg: BootSourceConfig) -> Self {
78        self.0.build_boot_source(boot_source_cfg).unwrap();
79        self
80    }
81
82    pub fn with_vm_config(mut self, vm_config: MachineConfig) -> Self {
83        let machine_config = MachineConfigUpdate::from(vm_config);
84        self.0.update_machine_config(&machine_config).unwrap();
85        self
86    }
87
88    pub fn set_cpu_template(&mut self, cpu_template: CustomCpuTemplate) {
89        self.0.machine_config.set_custom_cpu_template(cpu_template);
90    }
91}
92
93#[derive(Debug, Default)]
94pub struct MockVmConfig(MachineConfig);
95
96impl MockVmConfig {
97    pub fn new() -> MockVmConfig {
98        MockVmConfig::default()
99    }
100
101    pub fn with_dirty_page_tracking(mut self) -> Self {
102        self.0.track_dirty_pages = true;
103        self
104    }
105}
106
107generate_from!(MockBootSourceConfig, BootSourceConfig);
108generate_from!(MockVmResources, VmResources);
109generate_from!(MockVmConfig, MachineConfig);