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

Minor improvements to Container Struct #257

Merged
merged 2 commits into from
Sep 2, 2021
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 src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl List {
container.id(),
pid,
container.status(),
container.bundle(),
container.bundle().to_string_lossy(),
created,
user_name.to_string_lossy()
));
Expand Down
41 changes: 37 additions & 4 deletions src/container/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ impl Container {
container_id: &str,
status: ContainerStatus,
pid: Option<i32>,
bundle: &str,
bundle: &Path,
container_root: &Path,
) -> Result<Self> {
let container_root = fs::canonicalize(container_root)?;
let state = State::new(container_id, status, pid, bundle);
let state = State::new(container_id, status, pid, bundle.to_path_buf());
Ok(Self {
state,
root: container_root,
Expand All @@ -56,6 +56,7 @@ impl Container {
pub fn status(&self) -> ContainerStatus {
self.state.status
}

pub fn refresh_status(&mut self) -> Result<Self> {
let new_status = match self.pid() {
Some(pid) => {
Expand Down Expand Up @@ -154,8 +155,8 @@ impl Container {
None
}

pub fn bundle(&self) -> String {
self.state.bundle.clone()
pub fn bundle(&self) -> &PathBuf {
&self.state.bundle
}

pub fn set_systemd(mut self, should_use: bool) -> Self {
Expand Down Expand Up @@ -200,3 +201,35 @@ impl Container {
Spec::load(self.root.join("config.json"))
}
}

#[cfg(test)]
mod tests {
use std::env;

use super::*;
use anyhow::Result;

#[test]
fn test_set_id() -> Result<()> {
let dir = env::temp_dir();
let container = Container::new("container_id", ContainerStatus::Created, None, &dir, &dir)?;
let container = container.set_pid(1);
assert_eq!(container.pid(), Some(Pid::from_raw(1)));
Ok(())
}

#[test]
fn test_basic_getter() -> Result<()> {
let container = Container::new(
"container_id",
ContainerStatus::Created,
None,
&PathBuf::from("."),
&PathBuf::from("."),
)?;

assert_eq!(container.bundle(), &PathBuf::from("."));
assert_eq!(container.root, fs::canonicalize(PathBuf::from("."))?);
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/container/init_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl InitContainerBuilder {
&self.base.container_id,
ContainerStatus::Creating,
None,
self.bundle.as_path().to_str().unwrap(),
self.bundle.as_path(),
container_dir,
)?;
container.save()?;
Expand Down
6 changes: 3 additions & 3 deletions src/container/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct State {
#[serde(skip_serializing_if = "Option::is_none")]
pub pid: Option<i32>,
// Bundle is the path to the container's bundle directory.
pub bundle: String,
pub bundle: PathBuf,
// Annotations are key values associated with the container.
#[serde(skip_serializing_if = "Option::is_none")]
pub annotations: Option<HashMap<String, String>>,
Expand All @@ -105,14 +105,14 @@ impl State {
container_id: &str,
status: ContainerStatus,
pid: Option<i32>,
bundle: &str,
bundle: PathBuf,
) -> Self {
Self {
oci_version: "v1.0.2".to_string(),
id: container_id.to_string(),
status,
pid,
bundle: bundle.to_string(),
bundle,
annotations: Some(HashMap::default()),
created: None,
creator: None,
Expand Down