mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-11 04:26:38 +00:00
main: add tests for argh compatibility code
This was previously untested, so split it into its own function and add a few basic tests. BUG=None TEST=cargo test Change-Id: I36ae7876d22684dfae1ec3f0edf1d06d3fc04cdd Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3749940 Reviewed-by: Alexandre Courbot <acourbot@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Daniel Verkamp <dverkamp@chromium.org> Reviewed-by: Dennis Kempin <denniskempin@google.com>
This commit is contained in:
parent
a44e8a03e3
commit
4a764d2c83
1 changed files with 156 additions and 5 deletions
161
src/main.rs
161
src/main.rs
|
@ -405,13 +405,11 @@ fn pkg_version() -> std::result::Result<(), ()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn crosvm_main() -> Result<CommandStatus> {
|
// Perform transformations on `args_iter` to produce arguments suitable for parsing by `argh`.
|
||||||
#[cfg(not(feature = "crash-report"))]
|
fn prepare_argh_args<I: IntoIterator<Item = String>>(args_iter: I) -> Vec<String> {
|
||||||
sys::set_panic_hook();
|
|
||||||
|
|
||||||
let mut args: Vec<String> = Vec::default();
|
let mut args: Vec<String> = Vec::default();
|
||||||
// http://b/235882579
|
// http://b/235882579
|
||||||
for arg in std::env::args() {
|
for arg in args_iter {
|
||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"--host_ip" => {
|
"--host_ip" => {
|
||||||
eprintln!("`--host_ip` option is deprecated!");
|
eprintln!("`--host_ip` option is deprecated!");
|
||||||
|
@ -453,6 +451,14 @@ fn crosvm_main() -> Result<CommandStatus> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
args
|
||||||
|
}
|
||||||
|
|
||||||
|
fn crosvm_main() -> Result<CommandStatus> {
|
||||||
|
#[cfg(not(feature = "crash-report"))]
|
||||||
|
sys::set_panic_hook();
|
||||||
|
|
||||||
|
let args = prepare_argh_args(std::env::args());
|
||||||
let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
|
let args = args.iter().map(|s| s.as_str()).collect::<Vec<_>>();
|
||||||
let args = match crosvm::cmdline::CrosvmCmdlineArgs::from_args(&args[..1], &args[1..]) {
|
let args = match crosvm::cmdline::CrosvmCmdlineArgs::from_args(&args[..1], &args[1..]) {
|
||||||
Ok(args) => args,
|
Ok(args) => args,
|
||||||
|
@ -580,3 +586,148 @@ fn main() {
|
||||||
};
|
};
|
||||||
std::process::exit(exit_code);
|
std::process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn args_host_ip() {
|
||||||
|
assert_eq!(
|
||||||
|
prepare_argh_args(
|
||||||
|
["crosvm", "run", "--host_ip", "1.2.3.4", "vm_kernel"].map(|x| x.to_string())
|
||||||
|
),
|
||||||
|
["crosvm", "run", "--host-ip", "1.2.3.4", "vm_kernel"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn args_balloon_bias_mib() {
|
||||||
|
assert_eq!(
|
||||||
|
prepare_argh_args(
|
||||||
|
["crosvm", "run", "--balloon_bias_mib", "1234", "vm_kernel"].map(|x| x.to_string())
|
||||||
|
),
|
||||||
|
["crosvm", "run", "--balloon-bias-mib", "1234", "vm_kernel"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn args_h() {
|
||||||
|
assert_eq!(
|
||||||
|
prepare_argh_args(["crosvm", "run", "-h"].map(|x| x.to_string())),
|
||||||
|
["crosvm", "run", "--help"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn args_battery_switch() {
|
||||||
|
assert_eq!(
|
||||||
|
prepare_argh_args(
|
||||||
|
["crosvm", "run", "--battery", "--other-args", "vm_kernel"].map(|x| x.to_string())
|
||||||
|
),
|
||||||
|
[
|
||||||
|
"crosvm",
|
||||||
|
"run",
|
||||||
|
"--battery",
|
||||||
|
"",
|
||||||
|
"--other-args",
|
||||||
|
"vm_kernel"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn args_battery_switch_last_arg() {
|
||||||
|
assert_eq!(
|
||||||
|
prepare_argh_args(["crosvm", "run", "--battery", "vm_kernel"].map(|x| x.to_string())),
|
||||||
|
["crosvm", "run", "--battery", "", "vm_kernel"]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn args_battery_switch_short_arg() {
|
||||||
|
assert_eq!(
|
||||||
|
prepare_argh_args(
|
||||||
|
[
|
||||||
|
"crosvm",
|
||||||
|
"run",
|
||||||
|
"--battery",
|
||||||
|
"-p",
|
||||||
|
"init=/bin/bash",
|
||||||
|
"vm_kernel"
|
||||||
|
]
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
),
|
||||||
|
[
|
||||||
|
"crosvm",
|
||||||
|
"run",
|
||||||
|
"--battery",
|
||||||
|
"",
|
||||||
|
"-p",
|
||||||
|
"init=/bin/bash",
|
||||||
|
"vm_kernel"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn args_battery_option() {
|
||||||
|
assert_eq!(
|
||||||
|
prepare_argh_args(
|
||||||
|
[
|
||||||
|
"crosvm",
|
||||||
|
"run",
|
||||||
|
"--battery",
|
||||||
|
"type=goldfish",
|
||||||
|
"-p",
|
||||||
|
"init=/bin/bash",
|
||||||
|
"vm_kernel"
|
||||||
|
]
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
),
|
||||||
|
[
|
||||||
|
"crosvm",
|
||||||
|
"run",
|
||||||
|
"--battery",
|
||||||
|
"type=goldfish",
|
||||||
|
"-p",
|
||||||
|
"init=/bin/bash",
|
||||||
|
"vm_kernel"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn args_switch_multi() {
|
||||||
|
assert_eq!(
|
||||||
|
prepare_argh_args(
|
||||||
|
[
|
||||||
|
"crosvm",
|
||||||
|
"run",
|
||||||
|
"--gpu",
|
||||||
|
"test",
|
||||||
|
"--video-decoder",
|
||||||
|
"--video-encoder",
|
||||||
|
"--battery",
|
||||||
|
"--other-switch",
|
||||||
|
"vm_kernel"
|
||||||
|
]
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
),
|
||||||
|
[
|
||||||
|
"crosvm",
|
||||||
|
"run",
|
||||||
|
"--gpu",
|
||||||
|
"test",
|
||||||
|
"--video-decoder",
|
||||||
|
"",
|
||||||
|
"--video-encoder",
|
||||||
|
"",
|
||||||
|
"--battery",
|
||||||
|
"",
|
||||||
|
"--other-switch",
|
||||||
|
"vm_kernel"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue