Skip to content

Commit

Permalink
Add checkpoint testing
Browse files Browse the repository at this point in the history
This still uses the subcommand 'checkpointt' until it works in
combination with Podman.

Signed-off-by: Adrian Reber <areber@redhat.com>
  • Loading branch information
adrianreber committed Jan 25, 2022
1 parent d3186d5 commit e62e325
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 1 deletion.
167 changes: 167 additions & 0 deletions crates/integration_test/src/tests/lifecycle/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
use super::get_result_from_output;
use crate::utils::get_runtime_path;
use crate::utils::test_utils::State;
use crate::utils::{create_temp_dir, generate_uuid};
use anyhow::anyhow;
use std::path::Path;
use std::process::{Command, Stdio};
use test_framework::TestResult;

// Simple function to figure out the PID of the first container process
fn get_container_pid(project_path: &Path, id: &str) -> Result<i32, TestResult> {
let res_state = match Command::new(get_runtime_path())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--root")
.arg(project_path.join("runtime"))
.arg("state")
.arg(id)
.spawn()
.expect("failed to execute state command")
.wait_with_output()
{
Ok(o) => o,
Err(e) => {
return Err(TestResult::Failed(anyhow!(
"error getting container state {}",
e
)))
}
};
let stdout = match String::from_utf8(res_state.stdout) {
Ok(s) => s,
Err(e) => {
return Err(TestResult::Failed(anyhow!(
"failed to parse container stdout {}",
e
)))
}
};
let state: State = match serde_json::from_str(&stdout) {
Ok(v) => v,
Err(e) => {
return Err(TestResult::Failed(anyhow!(
"error in parsing state of container: stdout : {}, parse error : {}",
stdout,
e
)))
}
};

Ok(match state.pid {
Some(p) => p,
_ => -1,
})
}

// CRIU requires a minimal network setup in the network namespace
fn setup_network_namespace(project_path: &Path, id: &str) -> Result<(), TestResult> {
let pid = get_container_pid(project_path, id)?;

if let Err(e) = Command::new("nsenter")
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("-t")
.arg(format!("{}", pid))
.arg("-a")
.args(vec!["/bin/ip", "link", "set", "up", "dev", "lo"])
.spawn()
.expect("failed to exec ip")
.wait_with_output()
{
return Err(TestResult::Failed(anyhow!(
"error setting up network namespace {}",
e
)));
}

Ok(())
}

fn checkpoint(
project_path: &Path,
id: &str,
args: Vec<&str>,
work_path: Option<&str>,
) -> TestResult {
if let Err(e) = setup_network_namespace(project_path, id) {
return e;
}

let temp_dir = match create_temp_dir(&generate_uuid()) {
Ok(td) => td,
Err(e) => {
return TestResult::Failed(anyhow::anyhow!(
"Failed creating temporary directory {:?}",
e
))
}
};
let checkpoint_dir = temp_dir.as_ref().join("checkpoint");
if let Err(e) = std::fs::create_dir(&checkpoint_dir) {
return TestResult::Failed(anyhow::anyhow!(
"Failed creating checkpoint directory ({:?}): {}",
&checkpoint_dir,
e
));
}

let additional_args = match work_path {
Some(wp) => vec!["--work-path", wp],
_ => Vec::new(),
};

let runtime_path = get_runtime_path();

let res = Command::new(runtime_path)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--root")
.arg(project_path.join("runtime"))
.arg(match runtime_path {
_ if runtime_path.ends_with("youki") => "checkpointt",
_ => "checkpoint",
})
.arg("--image-path")
.arg(&checkpoint_dir)
.args(additional_args)
.args(args)
.arg(id)
.spawn()
.expect("failed to execute checkpoint command")
.wait_with_output();

if let Err(e) = res {
return TestResult::Failed(anyhow::Error::new(e));
}

// Check for complete checkpoint
if !Path::new(&checkpoint_dir.join("inventory.img")).exists() {
return TestResult::Failed(anyhow::anyhow!(
"Resulting checkpoint does not seem to be complete. {:?}/inventory.img is missing",
&checkpoint_dir,
));
}

let dump_log = match work_path {
Some(wp) => Path::new(wp).join("dump.log"),
_ => checkpoint_dir.join("dump.log"),
};

if !dump_log.exists() {
return TestResult::Failed(anyhow::anyhow!(
"Resulting checkpoint log file {:?} not found.",
&dump_log,
));
}

get_result_from_output(res)
}

pub fn checkpoint_leave_running_work_path_tmp(project_path: &Path, id: &str) -> TestResult {
checkpoint(project_path, id, vec!["--leave-running"], Some("/tmp/"))
}

pub fn checkpoint_leave_running(project_path: &Path, id: &str) -> TestResult {
checkpoint(project_path, id, vec!["--leave-running"], None)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::thread::sleep;
use std::time::Duration;
use test_framework::{TestResult, TestableGroup};

use super::{create, delete, kill, start, state};
use super::{checkpoint, create, delete, kill, start, state};

// By experimenting, somewhere around 50 is enough for youki process
// to get the kill signal and shut down
Expand Down Expand Up @@ -54,6 +54,14 @@ impl ContainerLifecycle {
pub fn delete(&self) -> TestResult {
delete::delete(&self.project_path, &self.container_id)
}

pub fn checkpoint_leave_running(&self) -> TestResult {
checkpoint::checkpoint_leave_running(&self.project_path, &self.container_id)
}

pub fn checkpoint_leave_running_work_path_tmp(&self) -> TestResult {
checkpoint::checkpoint_leave_running_work_path_tmp(&self.project_path, &self.container_id)
}
}

impl<'a> TestableGroup<'a> for ContainerLifecycle {
Expand All @@ -64,6 +72,14 @@ impl<'a> TestableGroup<'a> for ContainerLifecycle {
vec![
("create", self.create()),
("start", self.start()),
(
"checkpoint and leave running with --work-path /tmp",
self.checkpoint_leave_running_work_path_tmp(),
),
(
"checkpoint and leave running",
self.checkpoint_leave_running(),
),
("kill", self.kill()),
("state", self.state()),
("delete", self.delete()),
Expand All @@ -75,6 +91,14 @@ impl<'a> TestableGroup<'a> for ContainerLifecycle {
match *name {
"create" => ret.push(("create", self.create())),
"start" => ret.push(("start", self.start())),
"checkpoint_leave_running_work_path_tmp" => ret.push((
"checkpoint and leave running with --work-path /tmp",
self.checkpoint_leave_running_work_path_tmp(),
)),
"checkpoint_leave_running" => ret.push((
"checkpoint and leave running",
self.checkpoint_leave_running(),
)),
"kill" => ret.push(("kill", self.kill())),
"state" => ret.push(("state", self.state())),
"delete" => ret.push(("delete", self.delete())),
Expand Down
1 change: 1 addition & 0 deletions crates/integration_test/src/tests/lifecycle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod checkpoint;
mod container_create;
mod container_lifecycle;
mod create;
Expand Down

0 comments on commit e62e325

Please sign in to comment.