Skip to content

Commit 356366d

Browse files
committed
Add CPU and memory config options to test framework
1 parent 8501ec3 commit 356366d

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

test/test-manager/src/config/vm.rs

+10
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ pub struct VmConfig {
6060
#[serde(default)]
6161
#[arg(long, requires("tpm"))]
6262
pub ovmf_code_path: Option<String>,
63+
64+
/// Number of vCPUs
65+
#[serde(default)]
66+
#[arg(long)]
67+
pub vcpus: Option<usize>,
68+
69+
/// Amount of memory, in MBs
70+
#[serde(default)]
71+
#[arg(long)]
72+
pub memory: Option<usize>,
6373
}
6474

6575
impl VmConfig {

test/test-manager/src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct Args {
3131
}
3232

3333
#[derive(clap::Subcommand, Debug)]
34+
#[allow(clippy::large_enum_variant)]
3435
enum Commands {
3536
/// Manage configuration for tests and VMs
3637
#[clap(subcommand)]
@@ -148,6 +149,7 @@ enum Commands {
148149
}
149150

150151
#[derive(clap::Subcommand, Debug)]
152+
#[allow(clippy::large_enum_variant)]
151153
enum ConfigArg {
152154
/// Print the current config
153155
Get,
@@ -159,6 +161,7 @@ enum ConfigArg {
159161
}
160162

161163
#[derive(clap::Subcommand, Debug)]
164+
#[allow(clippy::large_enum_variant)]
162165
enum VmConfig {
163166
/// Create or edit a VM config
164167
Set {

test/test-manager/src/vm/qemu.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ const STDERR_LOG_LEVEL: log::Level = log::Level::Error;
2525
const STDOUT_LOG_LEVEL: log::Level = log::Level::Debug;
2626
const OBTAIN_IP_TIMEOUT: Duration = Duration::from_secs(180);
2727

28+
/// Default number of VCPU cores (passed to -smp)
29+
const DEFAULT_NUM_VCPUS: usize = 2;
30+
31+
/// Default amount of memory, in MBs (passed to -m)
32+
const DEFAULT_AMOUNT_MEMORY: usize = 2048;
33+
2834
#[derive(thiserror::Error, Debug)]
2935
pub enum Error {
3036
#[error("Failed to set up network")]
@@ -83,15 +89,21 @@ pub async fn run(config: &Config, vm_config: &VmConfig) -> Result<QemuInstance>
8389
.map_err(Error::Network)?;
8490

8591
let mut qemu_cmd = Command::new("qemu-system-x86_64");
92+
let vcpus = vm_config.vcpus.unwrap_or(DEFAULT_NUM_VCPUS);
93+
let memory = vm_config.memory.unwrap_or(DEFAULT_AMOUNT_MEMORY);
94+
95+
log::debug!("CPU count: {vcpus}");
96+
log::debug!("Memory: {memory}M");
97+
8698
qemu_cmd.args([
8799
"-cpu",
88100
"host",
89101
"-accel",
90102
"kvm",
91103
"-m",
92-
"4096",
104+
&memory.to_string(),
93105
"-smp",
94-
"2",
106+
&vcpus.to_string(),
95107
"-drive",
96108
&format!("file={}", vm_config.image_path),
97109
"-device",

test/test-manager/src/vm/tart.rs

+26
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ pub async fn run(config: &Config, vm_config: &VmConfig) -> Result<TartInstance>
4949
MachineCopy::clone_vm(&vm_config.image_path).await?
5050
};
5151

52+
if let Err(err) = machine_copy.configure(vm_config).await {
53+
log::error!("Failed to configure tart vm: {err}");
54+
}
55+
5256
// Start VM
5357
let mut tart_cmd = Command::new("tart");
5458
tart_cmd.args(["run", &machine_copy.name, "--serial"]);
@@ -167,6 +171,28 @@ impl MachineCopy {
167171
})
168172
}
169173

174+
pub async fn configure(&self, vm_config: &VmConfig) -> Result<()> {
175+
let mut args = vec![];
176+
if let Some(cpu) = vm_config.vcpus {
177+
args.extend(["--cpu".to_owned(), cpu.to_string()]);
178+
log::info!("vCPUs: {cpu}");
179+
}
180+
if let Some(mem) = vm_config.memory {
181+
args.extend(["--memory".to_owned(), mem.to_string()]);
182+
log::info!("Memory: {mem} MB");
183+
}
184+
if !args.is_empty() {
185+
let mut tart_cmd = Command::new("tart");
186+
tart_cmd.args(["set", &self.name]);
187+
tart_cmd.args(args);
188+
tart_cmd
189+
.status()
190+
.await
191+
.context("failed to update tart config")?;
192+
}
193+
Ok(())
194+
}
195+
170196
pub async fn cleanup(mut self) {
171197
let _ = tokio::task::spawn_blocking(move || self.try_destroy()).await;
172198
}

0 commit comments

Comments
 (0)