Skip to content

[sled-agent] Add tests for service management, fix prefix-matching bug #886

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

Merged
merged 2 commits into from
Apr 6, 2022
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
2 changes: 1 addition & 1 deletion sled-agent/src/illumos/dladm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum Error {
}

/// The name of a physical datalink.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct PhysicalLink(pub String);

/// Wraps commands for interacting with data links.
Expand Down
45 changes: 31 additions & 14 deletions sled-agent/src/illumos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

//! Wrappers around illumos-specific commands.

use cfg_if::cfg_if;

pub mod addrobj;
pub mod dladm;
pub mod running_zone;
Expand All @@ -26,20 +28,35 @@ pub enum ExecutionError {
CommandFailure { status: std::process::ExitStatus, stderr: String },
}

// Helper function for starting the process and checking the
// exit code result.
fn execute(
command: &mut std::process::Command,
) -> Result<std::process::Output, ExecutionError> {
let output =
command.output().map_err(|e| ExecutionError::ExecutionStart(e))?;

if !output.status.success() {
return Err(ExecutionError::CommandFailure {
status: output.status,
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
});
// We wrap this method in an inner module to make it possible to mock
// these free functions.
#[cfg_attr(test, mockall::automock, allow(dead_code))]
mod inner {
use super::*;

// Helper function for starting the process and checking the
// exit code result.
pub fn execute(
command: &mut std::process::Command,
) -> Result<std::process::Output, ExecutionError> {
let output =
command.output().map_err(|e| ExecutionError::ExecutionStart(e))?;

if !output.status.success() {
return Err(ExecutionError::CommandFailure {
status: output.status,
stderr: String::from_utf8_lossy(&output.stderr).to_string(),
});
}

Ok(output)
}
}

Ok(output)
cfg_if! {
if #[cfg(test)] {
pub use mock_inner::*;
} else {
pub use inner::*;
}
}
34 changes: 22 additions & 12 deletions sled-agent/src/illumos/running_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@ pub struct InstalledZone {
}

impl InstalledZone {
/// Returns the name of a zone, based on the service name plus any unique
/// identifying info.
///
/// The zone name is based on:
/// - A unique Oxide prefix ("oxz_")
/// - The name of the service being hosted (e.g., "nexus")
/// - An optional, service-unique identifier (typically a UUID).
///
/// This results in a zone name which is distinct across different zpools,
/// but stable and predictable across reboots.
pub fn get_zone_name(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is simply a refactor to make accessing this information easier from elsewhere

service_name: &str,
unique_name: Option<&str>,
) -> String {
let mut zone_name = format!("{}{}", ZONE_PREFIX, service_name);
if let Some(suffix) = unique_name {
zone_name.push_str(&format!("_{}", suffix));
}
zone_name
}

pub async fn install(
log: &Logger,
vnic_allocator: &VnicAllocator,
Expand All @@ -197,18 +218,7 @@ impl InstalledZone {
) -> Result<InstalledZone, Error> {
let control_vnic = vnic_allocator.new_control(None)?;

// The zone name is based on:
// - A unique Oxide prefix ("oxz_")
// - The name of the service being hosted (e.g., "nexus")
// - An optional, service-unique identifier (typically a UUID).
//
// This results in a zone name which is distinct across different zpools,
// but stable and predictable across reboots.
let mut zone_name = format!("{}{}", ZONE_PREFIX, service_name);
if let Some(suffix) = unique_name {
zone_name.push_str(&format!("_{}", suffix));
}

let zone_name = Self::get_zone_name(service_name, unique_name);
let zone_image_path =
PathBuf::from(&format!("/opt/oxide/{}.tar.gz", service_name));

Expand Down
Loading