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 Feb 24, 2022
1 parent 44ae6d7 commit 0547fc2
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/integration_tests_validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ jobs:
uses: Swatinem/rust-cache@v1
- run: sudo apt-get -y update
- run: sudo apt-get install -y pkg-config libsystemd-dev libdbus-glib-1-dev libelf-dev libseccomp-dev
- name: Install runc 1.1.0
run: |
wget -q https://github.com/opencontainers/runc/releases/download/v1.1.0/runc.amd64
sudo mv runc.amd64 /usr/bin/runc
sudo chmod 755 /usr/bin/runc
- name: Build
run: ./build.sh
- name: Validate tests on runc
run: cd ./crates/integration_test && sudo ./tests.sh run runc
run: runc --version && cd ./crates/integration_test && sudo ./tests.sh run runc
- name: Validate tests on youki
run: cd ./crates/integration_test && sudo ./tests.sh run ../../youki
168 changes: 168 additions & 0 deletions crates/integration_test/src/tests/lifecycle/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
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 checkpoint = 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();

let result = get_result_from_output(checkpoint);
if let TestResult::Failed(_) = result {
return result;
}

// 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,
));
}

TestResult::Passed
}

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, exec, kill, start, state};
use super::{checkpoint, create, delete, exec, 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 @@ -59,6 +59,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 @@ -71,6 +79,14 @@ impl<'a> TestableGroup<'a> for ContainerLifecycle {
("create", self.create()),
("start", self.start()),
// ("exec", self.exec(vec!["echo", "Hello"], Some("Hello\n"))),
(
"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 @@ -83,6 +99,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 0547fc2

Please sign in to comment.