vmm: config: Validate that serial/console file mode has a path

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-04-07 15:28:20 +01:00
parent 31928fb103
commit 12edb24678

View File

@ -62,8 +62,6 @@ pub enum Error {
ParsePersistentMemory(OptionParserError), ParsePersistentMemory(OptionParserError),
/// Failed parsing console /// Failed parsing console
ParseConsole(OptionParserError), ParseConsole(OptionParserError),
/// Missing file value for console
ParseConsoleFileMissing,
/// No mode given for console /// No mode given for console
ParseConsoleInvalidModeGiven, ParseConsoleInvalidModeGiven,
/// Failed parsing device parameters /// Failed parsing device parameters
@ -84,6 +82,8 @@ pub enum ValidationError {
DoubleTtyMode, DoubleTtyMode,
/// No kernel specified /// No kernel specified
KernelMissing, KernelMissing,
/// Missing file value for console
ConsoleFileMissing,
} }
type ValidationResult<T> = std::result::Result<T, ValidationError>; type ValidationResult<T> = std::result::Result<T, ValidationError>;
@ -94,6 +94,7 @@ impl fmt::Display for ValidationError {
match self { match self {
DoubleTtyMode => write!(f, "Console mode tty specified for both serial and console"), DoubleTtyMode => write!(f, "Console mode tty specified for both serial and console"),
KernelMissing => write!(f, "No kernel specified"), KernelMissing => write!(f, "No kernel specified"),
ConsoleFileMissing => write!(f, "Path missing when using file console mode"),
} }
} }
} }
@ -103,9 +104,6 @@ impl fmt::Display for Error {
use self::Error::*; use self::Error::*;
match self { match self {
ParseConsole(o) => write!(f, "Error parsing --console: {}", o), ParseConsole(o) => write!(f, "Error parsing --console: {}", o),
ParseConsoleFileMissing => {
write!(f, "Error parsing --console: path missing when using file")
}
ParseConsoleInvalidModeGiven => { ParseConsoleInvalidModeGiven => {
write!(f, "Error parsing --console: invalid console mode given") write!(f, "Error parsing --console: invalid console mode given")
} }
@ -1037,9 +1035,10 @@ impl ConsoleConfig {
mode = ConsoleOutputMode::Null mode = ConsoleOutputMode::Null
} else if parser.is_set("file") { } else if parser.is_set("file") {
mode = ConsoleOutputMode::File; mode = ConsoleOutputMode::File;
file = Some(PathBuf::from( file =
parser.get("file").ok_or(Error::ParseConsoleFileMissing)?, Some(PathBuf::from(parser.get("file").ok_or(
)); Error::Validation(ValidationError::ConsoleFileMissing),
)?));
} else { } else {
return Err(Error::ParseConsoleInvalidModeGiven); return Err(Error::ParseConsoleInvalidModeGiven);
} }
@ -1201,6 +1200,14 @@ impl VmConfig {
return Err(ValidationError::DoubleTtyMode); return Err(ValidationError::DoubleTtyMode);
} }
if self.console.mode == ConsoleOutputMode::File && self.console.file.is_none() {
return Err(ValidationError::ConsoleFileMissing);
}
if self.serial.mode == ConsoleOutputMode::File && self.serial.file.is_none() {
return Err(ValidationError::ConsoleFileMissing);
}
Ok(()) Ok(())
} }
@ -1857,6 +1864,11 @@ mod tests {
invalid_config.kernel = None; invalid_config.kernel = None;
assert!(invalid_config.validate().is_err()); assert!(invalid_config.validate().is_err());
let mut invalid_config = valid_config.clone();
invalid_config.serial.mode = ConsoleOutputMode::File;
invalid_config.serial.file = None;
assert!(invalid_config.validate().is_err());
Ok(()) Ok(())
} }
} }