From fee0e7693b94b932f53d8f39f4879d49fcf30984 Mon Sep 17 00:00:00 2001 From: utam0k Date: Thu, 2 Sep 2021 23:04:12 +0900 Subject: [PATCH 1/2] remove unnecessary clone() in container.rs --- src/commands/list.rs | 2 +- src/container/container.rs | 8 ++++---- src/container/init_builder.rs | 2 +- src/container/state.rs | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/commands/list.rs b/src/commands/list.rs index 974344f49..a6294575e 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -55,7 +55,7 @@ impl List { container.id(), pid, container.status(), - container.bundle(), + container.bundle().to_string_lossy(), created, user_name.to_string_lossy() )); diff --git a/src/container/container.rs b/src/container/container.rs index e88c5b9f4..b4e33d87a 100644 --- a/src/container/container.rs +++ b/src/container/container.rs @@ -38,11 +38,11 @@ impl Container { container_id: &str, status: ContainerStatus, pid: Option, - bundle: &str, + bundle: &Path, container_root: &Path, ) -> Result { 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, @@ -154,8 +154,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 { diff --git a/src/container/init_builder.rs b/src/container/init_builder.rs index e91c13a6a..ab572a679 100644 --- a/src/container/init_builder.rs +++ b/src/container/init_builder.rs @@ -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()?; diff --git a/src/container/state.rs b/src/container/state.rs index 68c73e3e2..a6c6a7949 100644 --- a/src/container/state.rs +++ b/src/container/state.rs @@ -84,7 +84,7 @@ pub struct State { #[serde(skip_serializing_if = "Option::is_none")] pub pid: Option, // 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>, @@ -105,14 +105,14 @@ impl State { container_id: &str, status: ContainerStatus, pid: Option, - 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, From c84b5e9a36bbca70c1dbc9267de328331ce186af Mon Sep 17 00:00:00 2001 From: utam0k Date: Thu, 2 Sep 2021 23:04:35 +0900 Subject: [PATCH 2/2] add basic tests for container struct. --- src/container/container.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/container/container.rs b/src/container/container.rs index b4e33d87a..cb38abb6c 100644 --- a/src/container/container.rs +++ b/src/container/container.rs @@ -56,6 +56,7 @@ impl Container { pub fn status(&self) -> ContainerStatus { self.state.status } + pub fn refresh_status(&mut self) -> Result { let new_status = match self.pid() { Some(pid) => { @@ -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(()) + } +}