mirror of
https://github.com/containers/youki
synced 2024-12-04 02:58:22 +01:00
Merge pull request #257 from utam0k/improvement/container-struct
Minor improvements to Container Struct
This commit is contained in:
commit
6dfad07c02
@ -55,7 +55,7 @@ impl List {
|
|||||||
container.id(),
|
container.id(),
|
||||||
pid,
|
pid,
|
||||||
container.status(),
|
container.status(),
|
||||||
container.bundle(),
|
container.bundle().to_string_lossy(),
|
||||||
created,
|
created,
|
||||||
user_name.to_string_lossy()
|
user_name.to_string_lossy()
|
||||||
));
|
));
|
||||||
|
@ -38,11 +38,11 @@ impl Container {
|
|||||||
container_id: &str,
|
container_id: &str,
|
||||||
status: ContainerStatus,
|
status: ContainerStatus,
|
||||||
pid: Option<i32>,
|
pid: Option<i32>,
|
||||||
bundle: &str,
|
bundle: &Path,
|
||||||
container_root: &Path,
|
container_root: &Path,
|
||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let container_root = fs::canonicalize(container_root)?;
|
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 {
|
Ok(Self {
|
||||||
state,
|
state,
|
||||||
root: container_root,
|
root: container_root,
|
||||||
@ -56,6 +56,7 @@ impl Container {
|
|||||||
pub fn status(&self) -> ContainerStatus {
|
pub fn status(&self) -> ContainerStatus {
|
||||||
self.state.status
|
self.state.status
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh_status(&mut self) -> Result<Self> {
|
pub fn refresh_status(&mut self) -> Result<Self> {
|
||||||
let new_status = match self.pid() {
|
let new_status = match self.pid() {
|
||||||
Some(pid) => {
|
Some(pid) => {
|
||||||
@ -154,8 +155,8 @@ impl Container {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bundle(&self) -> String {
|
pub fn bundle(&self) -> &PathBuf {
|
||||||
self.state.bundle.clone()
|
&self.state.bundle
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_systemd(mut self, should_use: bool) -> Self {
|
pub fn set_systemd(mut self, should_use: bool) -> Self {
|
||||||
@ -200,3 +201,35 @@ impl Container {
|
|||||||
Spec::load(self.root.join("config.json"))
|
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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -121,7 +121,7 @@ impl InitContainerBuilder {
|
|||||||
&self.base.container_id,
|
&self.base.container_id,
|
||||||
ContainerStatus::Creating,
|
ContainerStatus::Creating,
|
||||||
None,
|
None,
|
||||||
self.bundle.as_path().to_str().unwrap(),
|
self.bundle.as_path(),
|
||||||
container_dir,
|
container_dir,
|
||||||
)?;
|
)?;
|
||||||
container.save()?;
|
container.save()?;
|
||||||
|
@ -84,7 +84,7 @@ pub struct State {
|
|||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub pid: Option<i32>,
|
pub pid: Option<i32>,
|
||||||
// Bundle is the path to the container's bundle directory.
|
// Bundle is the path to the container's bundle directory.
|
||||||
pub bundle: String,
|
pub bundle: PathBuf,
|
||||||
// Annotations are key values associated with the container.
|
// Annotations are key values associated with the container.
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub annotations: Option<HashMap<String, String>>,
|
pub annotations: Option<HashMap<String, String>>,
|
||||||
@ -105,14 +105,14 @@ impl State {
|
|||||||
container_id: &str,
|
container_id: &str,
|
||||||
status: ContainerStatus,
|
status: ContainerStatus,
|
||||||
pid: Option<i32>,
|
pid: Option<i32>,
|
||||||
bundle: &str,
|
bundle: PathBuf,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
oci_version: "v1.0.2".to_string(),
|
oci_version: "v1.0.2".to_string(),
|
||||||
id: container_id.to_string(),
|
id: container_id.to_string(),
|
||||||
status,
|
status,
|
||||||
pid,
|
pid,
|
||||||
bundle: bundle.to_string(),
|
bundle,
|
||||||
annotations: Some(HashMap::default()),
|
annotations: Some(HashMap::default()),
|
||||||
created: None,
|
created: None,
|
||||||
creator: None,
|
creator: None,
|
||||||
|
Loading…
Reference in New Issue
Block a user