Skip to content

Commit

Permalink
fix: use thiserror::Error in vmm_config
Browse files Browse the repository at this point in the history
vmm: use thiserror::Error

Signed-off-by: czybjtu <smartczy@outlook.com>
  • Loading branch information
nayihz authored and wearyzen committed Jun 2, 2023
1 parent 03262b2 commit 4a1cbbd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 103 deletions.
23 changes: 4 additions & 19 deletions src/vmm/src/vmm_config/boot_source.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use std::fmt::{Display, Formatter, Result};
use std::fs::File;
use std::io;

Expand Down Expand Up @@ -38,33 +37,19 @@ pub struct BootSourceConfig {
}

/// Errors associated with actions on `BootSourceConfig`.
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum BootSourceConfigError {
/// The kernel file cannot be opened.
#[error("The kernel file cannot be opened: {0}")]
InvalidKernelPath(io::Error),
/// The initrd file cannot be opened.
#[error("The initrd file cannot be opened due to invalid path or invalid permissions. {0}")]
InvalidInitrdPath(io::Error),
/// The kernel command line is invalid.
#[error("The kernel command line is invalid: {0}")]
InvalidKernelCommandLine(String),
}

impl Display for BootSourceConfigError {
fn fmt(&self, f: &mut Formatter) -> Result {
use self::BootSourceConfigError::*;
match *self {
InvalidKernelPath(ref err) => write!(f, "The kernel file cannot be opened: {}", err),
InvalidInitrdPath(ref err) => write!(
f,
"The initrd file cannot be opened due to invalid path or invalid permissions. {}",
err,
),
InvalidKernelCommandLine(ref err) => {
write!(f, "The kernel command line is invalid: {}", err.as_str())
}
}
}
}

/// Holds the kernel specification (both configuration as well as runtime details).
#[derive(Default)]
pub struct BootSource {
Expand Down
26 changes: 2 additions & 24 deletions src/vmm/src/vmm_config/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

//! Auxiliary module for configuring the logger.
use std::fmt::{Display, Formatter};
use std::path::PathBuf;

use logger::{LevelFilter, LOGGER};
Expand Down Expand Up @@ -106,21 +105,13 @@ impl LoggerConfig {
}

/// Errors associated with actions on the `LoggerConfig`.
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum LoggerConfigError {
/// Cannot initialize the logger due to bad user input.
#[error("{}", format!("{:?}", .0).replace('\"', ""))]
InitializationFailure(String),
}

impl Display for LoggerConfigError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
use self::LoggerConfigError::*;
match *self {
InitializationFailure(ref err_msg) => write!(f, "{}", err_msg.replace('\"', "")),
}
}
}

/// Configures the logger as described in `logger_cfg`.
pub fn init_logger(
logger_cfg: LoggerConfig,
Expand Down Expand Up @@ -215,19 +206,6 @@ mod tests {
}
}

#[test]
fn test_error_display() {
assert_eq!(
format!(
"{}",
LoggerConfigError::InitializationFailure(String::from(
"Failed to initialize logger"
))
),
"Failed to initialize logger"
);
}

#[test]
fn test_new_logger_config() {
let logger_config =
Expand Down
26 changes: 2 additions & 24 deletions src/vmm/src/vmm_config/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

//! Auxiliary module for configuring the metrics system.
use std::fmt::{Display, Formatter};
use std::path::PathBuf;

use logger::METRICS;
Expand All @@ -18,21 +17,13 @@ pub struct MetricsConfig {
}

/// Errors associated with actions on the `MetricsConfig`.
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum MetricsConfigError {
/// Cannot initialize the metrics system due to bad user input.
#[error("{}", format!("{:?}", .0).replace('\"', ""))]
InitializationFailure(String),
}

impl Display for MetricsConfigError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
use self::MetricsConfigError::*;
match *self {
InitializationFailure(ref err_msg) => write!(f, "{}", err_msg.replace('\"', "")),
}
}
}

/// Configures the metrics as described in `metrics_cfg`.
pub fn init_metrics(metrics_cfg: MetricsConfig) -> std::result::Result<(), MetricsConfigError> {
let writer = FcLineWriter::new(
Expand Down Expand Up @@ -67,17 +58,4 @@ mod tests {
assert!(init_metrics(desc.clone()).is_ok());
assert!(init_metrics(desc).is_err());
}

#[test]
fn test_error_display() {
assert_eq!(
format!(
"{}",
MetricsConfigError::InitializationFailure(String::from(
"Failed to initialize metrics"
))
),
"Failed to initialize metrics"
);
}
}
42 changes: 8 additions & 34 deletions src/vmm/src/vmm_config/mmds.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use std::fmt::{Display, Formatter, Result};
use std::net::Ipv4Addr;

use mmds::data_store;
Expand Down Expand Up @@ -40,46 +38,22 @@ impl MmdsConfig {
}

/// MMDS configuration related errors.
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum MmdsConfigError {
/// The network interfaces list provided is empty.
#[error("The list of network interface IDs that allow forwarding MMDS requests is empty.")]
EmptyNetworkIfaceList,
/// The provided IPv4 address is not link-local valid.
#[error("The MMDS IPv4 address is not link local.")]
InvalidIpv4Addr,
/// The network interfaces list provided contains IDs that
/// does not correspond to any existing network interface.
#[error(
"The list of network interface IDs provided contains at least one ID that does not \
correspond to any existing network interface."
)]
InvalidNetworkInterfaceId,
/// MMDS version could not be configured.
#[error("The MMDS could not be configured to version {0}: {1}")]
MmdsVersion(MmdsVersion, data_store::Error),
}

impl Display for MmdsConfigError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
MmdsConfigError::EmptyNetworkIfaceList => {
write!(
f,
"The list of network interface IDs that allow forwarding MMDS requests is \
empty."
)
}
MmdsConfigError::InvalidIpv4Addr => {
write!(f, "The MMDS IPv4 address is not link local.")
}
MmdsConfigError::InvalidNetworkInterfaceId => {
write!(
f,
"The list of network interface IDs provided contains at least one ID that \
does not correspond to any existing network interface."
)
}
MmdsConfigError::MmdsVersion(version, err) => {
write!(
f,
"The MMDS could not be configured to version {}: {}",
version, err
)
}
}
}
}
4 changes: 2 additions & 2 deletions tests/integration_tests/build/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def is_on_skylake():
# Checkout the cpuid crate. In the future other
# differences may appear.
if utils.is_io_uring_supported():
COVERAGE_DICT = {"Intel": 83.66, "AMD": 83.24, "ARM": 83.06}
COVERAGE_DICT = {"Intel": 83.76, "AMD": 83.34, "ARM": 83.12}
else:
COVERAGE_DICT = {"Intel": 80.90, "AMD": 80.45, "ARM": 80.06}
COVERAGE_DICT = {"Intel": 81.02, "AMD": 80.55, "ARM": 80.12}

PROC_MODEL = proc.proc_type()

Expand Down

0 comments on commit 4a1cbbd

Please sign in to comment.