Skip to content

Commit

Permalink
Merge pull request #17 from fpco/testing
Browse files Browse the repository at this point in the history
Integration testing for pid1-rs
  • Loading branch information
snoyberg authored Feb 17, 2024
2 parents 0306ae8 + 010cc13 commit 213b79e
Show file tree
Hide file tree
Showing 9 changed files with 373 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Unreleased

- Add integration testing
- Add examples/dumb_shell.rs code for easy testing.

# v0.1.3

- Support options for running with specific user id and group id.
Expand Down
56 changes: 55 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ used for testing this library.
- zombie.rs: Creates zombie process
- sigterm_handler.rs: Program which has SIGTERM handler and exits on receiving it.
- sigterm_loop.rs: A buggy program which doesn't exit on SIGTERM

- dumb_shell.rs: Alternative shell that you can use to test your
program since bash does reaping of process.

## Environment setup

Expand Down
3 changes: 3 additions & 0 deletions pid1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ categories = ["command-line-utilities"]
nix = { version = "0.27.1", features = ["process", "signal"] }
signal-hook = "0.3.17"
thiserror = "1"

[dev-dependencies]
rand = "0.8.5"
2 changes: 2 additions & 0 deletions pid1/etc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ COPY sigterm_handler /usr/bin/sigterm_handler

COPY sigterm_loop /usr/bin/sigterm_loop

COPY dumb_shell /usr/bin/dumb_shell

CMD ["/simple"]
29 changes: 29 additions & 0 deletions pid1/examples/dumb_shell.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::{
ffi::OsString,
io::{self, BufRead, Write},
process::Command,
str::FromStr,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let stdin = io::stdin();
loop {
print!("$ ");
io::stdout().flush().unwrap();
for line in stdin.lock().lines() {
let args: Vec<OsString> = line
.unwrap()
.split(' ')
.map(|item| OsString::from_str(item).unwrap())
.collect();
let exe = args.get(0).unwrap();
if exe == "exit" {
std::process::exit(0);
}
let output = Command::new(exe).args(&args[1..]).output().unwrap();
print!("{}", String::from_utf8(output.stdout).unwrap());
print!("$ ");
io::stdout().flush().unwrap();
}
}
}
6 changes: 4 additions & 2 deletions pid1/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

if args.len() > 1 {
println!("Going to sleep 500 seconds");
std::thread::sleep(std::time::Duration::from_secs(500));
let duration = &args[2];
let duration = duration.parse().expect("Expected int value");
println!("Going to sleep {duration} seconds");
std::thread::sleep(std::time::Duration::from_secs(duration));
}

Ok(())
Expand Down
5 changes: 4 additions & 1 deletion pid1/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ build-image:
cargo build --target x86_64-unknown-linux-musl --example zombie
cargo build --target x86_64-unknown-linux-musl --example sigterm_handler
cargo build --target x86_64-unknown-linux-musl --example sigterm_loop
cargo build --target x86_64-unknown-linux-musl --example dumb_shell

cp ../target/x86_64-unknown-linux-musl/debug/examples/simple etc
cp ../target/x86_64-unknown-linux-musl/debug/examples/zombie etc
cp ../target/x86_64-unknown-linux-musl/debug/examples/sigterm_handler etc
cp ../target/x86_64-unknown-linux-musl/debug/examples/sigterm_loop etc
cp ../target/x86_64-unknown-linux-musl/debug/examples/dumb_shell etc
docker build etc -f etc/Dockerfile --tag pid1rstest

# Run test image
run-image:
docker rm pid1rs || exit 0
docker run --name pid1rs -t pid1rstest /simple --sleep
docker run --name pid1rs -t pid1rstest /simple --sleep 20

# Run zombie in the container
run-zombie:
Expand All @@ -28,6 +30,7 @@ run-zombie:
test: build-image
docker rm pid1rs || exit 0
docker run --name pid1rs -t pid1rstest
cargo test

# Run SIGTERM test
sigterm-test:
Expand Down
Loading

0 comments on commit 213b79e

Please sign in to comment.