mirror of
https://github.com/containers/youki
synced 2024-12-03 18:48: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(),
|
||||
pid,
|
||||
container.status(),
|
||||
container.bundle(),
|
||||
container.bundle().to_string_lossy(),
|
||||
created,
|
||||
user_name.to_string_lossy()
|
||||
));
|
||||
|
@ -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,
|
||||
@ -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) => {
|
||||
@ -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 {
|
||||
@ -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(())
|
||||
}
|
||||
}
|
||||
|
@ -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()?;
|
||||
|
@ -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>>,
|
||||
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user