Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argument parsing: add size to memory argument #492

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 154 additions & 4 deletions src/common/commands_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use libc::VMADDR_CID_HOST;
use libc::VMADDR_CID_LOCAL;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::str::FromStr;

use crate::common::{NitroCliErrorEnum, NitroCliFailure, NitroCliResult, VMADDR_CID_PARENT};
use crate::get_id_by_name;
Expand Down Expand Up @@ -290,21 +291,74 @@ fn parse_file_path(args: &ArgMatches, val_name: &str) -> NitroCliResult<String>
Ok(path.to_string())
}

#[derive(Debug)]
enum MemoryUnit {
Mebibytes,
Gibibytes,
Tebibytes,
}

#[derive(Debug)]
struct UnknownMemoryUnitErr;

impl MemoryUnit {
fn to_mebibytes(&self) -> u64 {
match self {
MemoryUnit::Mebibytes => 1,
MemoryUnit::Gibibytes => 1024,
MemoryUnit::Tebibytes => 1024 * 1024,
}
}
}

impl FromStr for MemoryUnit {
type Err = UnknownMemoryUnitErr;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"M" | "m" | "" => Ok(MemoryUnit::Mebibytes),
"G" | "g" => Ok(MemoryUnit::Gibibytes),
"T" | "t" => Ok(MemoryUnit::Tebibytes),
_ => Err(UnknownMemoryUnitErr),
}
}
}

/// Parse the requested amount of enclave memory from the command-line arguments.
fn parse_memory(args: &ArgMatches) -> NitroCliResult<u64> {
/// It can be just a number like 123, or it can end in a size indicator like 100M or 10G.
/// If the size indicator is missing, it defaults to M.
/// If the size indicator is not M, G or T, it returns an error.
///
/// # Arguments
/// * `args` - The command-line arguments.
pub fn parse_memory(args: &ArgMatches) -> NitroCliResult<u64> {
let memory = args.value_of("memory").ok_or_else(|| {
new_nitro_cli_failure!(
"`memory` argument not found",
NitroCliErrorEnum::MissingArgument
)
})?;
memory.parse().map_err(|_| {

let (num_str, size_str) = match memory.find(|c: char| !c.is_numeric()) {
Some(index) => memory.split_at(index),
None => (memory, ""),
};
let num = num_str.parse::<u64>().map_err(|_| {
new_nitro_cli_failure!(
"`memory` argument does not contain a number",
NitroCliErrorEnum::InvalidArgument
)
.add_info(vec!["memory", memory])
})?;

let unit = size_str.parse::<MemoryUnit>().map_err(|_| {
new_nitro_cli_failure!(
"`memory` is not a number",
"`memory` argument does not contain a valid size indicator",
NitroCliErrorEnum::InvalidArgument
)
.add_info(vec!["memory", memory])
})
})?;
Ok(num * unit.to_mebibytes())
}

/// Parse the Docker tag from the command-line arguments.
Expand Down Expand Up @@ -565,6 +619,102 @@ mod tests {
let err_str = construct_error_message(&err_info);
assert!(err_str.contains("Invalid argument provided"))
}

let app = create_app!();
let args = vec![
"nitro-cli",
"run-enclave",
"--memory",
"256",
"--cpu-count",
"2",
"--eif-path",
"non_existing_eif.eif",
];

let matches = app.get_matches_from_safe(args);
assert!(matches.is_ok());

let result = parse_memory(
matches
.as_ref()
.unwrap()
.subcommand_matches("run-enclave")
.unwrap(),
);
assert_eq!(result, Ok(256));

let app = create_app!();
let args = vec![
"nitro-cli",
"run-enclave",
"--memory",
"100M",
"--cpu-count",
"2",
"--eif-path",
"non_existing_eif.eif",
];

let matches = app.get_matches_from_safe(args);
assert!(matches.is_ok());

let result = parse_memory(
matches
.as_ref()
.unwrap()
.subcommand_matches("run-enclave")
.unwrap(),
);
assert_eq!(result, Ok(100));

let app = create_app!();
let args = vec![
"nitro-cli",
"run-enclave",
"--memory",
"10G",
"--cpu-count",
"2",
"--eif-path",
"non_existing_eif.eif",
];

let matches = app.get_matches_from_safe(args);
assert!(matches.is_ok());

let result = parse_memory(
matches
.as_ref()
.unwrap()
.subcommand_matches("run-enclave")
.unwrap(),
);
assert_eq!(result, Ok(10_240));

let app = create_app!();
let args = vec![
"nitro-cli",
"run-enclave",
"--memory",
"2T",
"--cpu-count",
"2",
"--eif-path",
"non_existing_eif.eif",
];

let matches = app.get_matches_from_safe(args);
assert!(matches.is_ok());

let result = parse_memory(
matches
.as_ref()
.unwrap()
.subcommand_matches("run-enclave")
.unwrap(),
);
assert_eq!(result, Ok(2 * 1024 * 1024));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub enum EnclaveProcessReply {
}

/// Struct that is passed along the backtrace and accumulates error messages.
#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq)]
pub struct NitroCliFailure {
/// Main action which was attempted and failed.
pub action: String,
Expand Down