Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build libcontainer's container on Instance::new to be spec compliant #340

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = [
"crates/containerd-shim-wasm",
"crates/containerd-shim-wasm-test",
"crates/containerd-shim-wasm-test-modules",
"crates/wasi-demo-app",
"crates/oci-tar-builder",
"crates/containerd-shim-wasmedge",
Expand All @@ -24,7 +24,7 @@ cap-std = "1.0"
chrono = { version = "0.4", default-features = false, features = ["clock"] }
containerd-shim = "0.5.0"
containerd-shim-wasm = { path = "crates/containerd-shim-wasm" }
containerd-shim-wasm-test = { path = "crates/containerd-shim-wasm-test" }
containerd-shim-wasm-test-modules = { path = "crates/containerd-shim-wasm-test-modules" }
crossbeam = { version = "0.8.2", default-features = false }
env_logger = "0.10"
libc = "0.2.147"
Expand All @@ -36,6 +36,7 @@ serde = "1.0"
serde_json = "1.0"
sha256 = "1.4.0"
tar = "0.4"
tempfile = "3.8"
thiserror = "1.0"
ttrpc = "0.8.0"
wat = "*" # Use whatever version wasmtime will make us pull
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ check: check-wasm $(RUNTIMES:%=check-%);

check-common: check-wasm;
check-wasm:
$(CARGO) +nightly fmt $(TARGET_FLAG) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- --check
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- $(WARNINGS)
$(CARGO) +nightly fmt $(TARGET_FLAG) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- --check
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- $(WARNINGS)

check-%:
$(CARGO) +nightly fmt $(TARGET_FLAG) -p containerd-shim-$* -- --check
Expand All @@ -59,8 +59,8 @@ fix: fix-wasm $(RUNTIMES:%=fix-%);

fix-common: fix-wasm;
fix-wasm:
$(CARGO) +nightly fmt $(TARGET_FLAG) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) --fix -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- $(WARNINGS)
$(CARGO) +nightly fmt $(TARGET_FLAG) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) --fix -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test-modules -- $(WARNINGS)

fix-%:
$(CARGO) +nightly fmt $(TARGET_FLAG) -p containerd-shim-$*
Expand All @@ -72,7 +72,7 @@ test: test-wasm $(RUNTIMES:%=test-%);
test-common: test-wasm;
test-wasm:
# oci-tar-builder and wasi-demo-app have no tests
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasm $(FEATURES_wasm) --verbose -- --nocapture
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasm $(FEATURES_wasm) --verbose -- --nocapture --test-threads=1

test-wasmedge:
# run tests in one thread to prevent paralellism
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[package]
name = "containerd-shim-wasm-test"
description = "Library for testing containerd shims for wasm"
name = "containerd-shim-wasm-test-modules"
description = "Set of WebAssembly modules for testing containerd shims for wasm"
version.workspace = true
edition.workspace = true
license.workspace = true

[dependencies]
anyhow = { workspace = true }
containerd-shim-wasm = { workspace = true }
env_logger = "0.10"
libc = { workspace = true }
log = { workspace = true }
Expand Down
10 changes: 8 additions & 2 deletions crates/containerd-shim-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ doctest = false
[dependencies]
anyhow = { workspace = true }
chrono = { workspace = true }
git-version = "0.3.5"
containerd-shim = { workspace = true }
containerd-shim-wasm-test-modules = { workspace = true, optional = true }
crossbeam = { workspace = true }
env_logger = { workspace = true, optional = true }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since these are under dev-dependencies are they needed here?

Copy link
Collaborator Author

@jprendes jprendes Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consumers of the testing feature of the library we need to specify it as a normal dependencies as well.
All 3 dev-dependencies are also specified as optional normal dependencies that get enabled by the testing feature.
I'm open to suggestions 😄

git-version = "0.3.5"
libc = { workspace = true }
log = { workspace = true }
oci-spec = { workspace = true }
protobuf = "3.2"
serde = { workspace = true }
serde_json = { workspace = true }
tempfile = { workspace = true, optional = true }
thiserror = { workspace = true }
ttrpc = { workspace = true }
wat = { workspace = true }
Expand All @@ -40,8 +43,11 @@ windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Storage
ttrpc-codegen = { version = "0.4.2", optional = true }

[dev-dependencies]
tempfile = "3.8"
containerd-shim-wasm-test-modules = { workspace = true }
env_logger = { workspace = true }
tempfile = { workspace = true }

[features]
testing = ["dep:containerd-shim-wasm-test-modules", "dep:env_logger", "dep:tempfile"]
generate_bindings = ["ttrpc-codegen"]
generate_doc = []
3 changes: 3 additions & 0 deletions crates/containerd-shim-wasm/src/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ pub use path::PathResolve;

pub use crate::sandbox::stdio::Stdio;
use crate::sys::container::instance;

#[cfg(test)]
mod tests;
38 changes: 38 additions & 0 deletions crates/containerd-shim-wasm/src/container/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use anyhow::bail;

use crate::container::{Engine, RuntimeContext, Stdio};
use crate::sys::container::instance::Instance;
use crate::testing::WasiTest;

#[derive(Clone, Default)]
struct EngineFailingValidation;

impl Engine for EngineFailingValidation {
fn name() -> &'static str {
"wasi_instance"
}
fn can_handle(&self, _ctx: &impl RuntimeContext) -> anyhow::Result<()> {
bail!("can't handle");
}
fn run_wasi(&self, _ctx: &impl RuntimeContext, _stdio: Stdio) -> anyhow::Result<i32> {
Ok(0)
}
}

type InstanceFailingValidation = Instance<EngineFailingValidation>;

#[test]
#[cfg(unix)] // not yet implemented on Windows
fn test_validation_error() -> anyhow::Result<()> {
// A validation error should fail when creating the container
// as opposed to failing when starting it.

let result = WasiTest::<InstanceFailingValidation>::builder()?
.with_start_fn("foo")?
.with_wasm("/invalid_entrypoint.wasm")?
.build();

assert!(result.is_err());

Ok(())
}
3 changes: 3 additions & 0 deletions crates/containerd-shim-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ pub mod services;
#[cfg_attr(unix, path = "sys/unix/mod.rs")]
#[cfg_attr(windows, path = "sys/windows/mod.rs")]
pub(crate) mod sys;

#[cfg(any(test, feature = "testing"))]
pub mod testing;
23 changes: 13 additions & 10 deletions crates/containerd-shim-wasm/src/sandbox/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ pub trait Instance {
type Engine: Send + Sync + Clone;

/// Create a new instance
fn new(id: String, cfg: Option<&InstanceConfig<Self::Engine>>) -> Self;
fn new(id: String, cfg: Option<&InstanceConfig<Self::Engine>>) -> Result<Self, Error>
where
Self: Sized;

/// Start the instance
/// The returned value should be a unique ID (such as a PID) for the instance.
Expand Down Expand Up @@ -178,10 +180,10 @@ pub struct Nop {

impl Instance for Nop {
type Engine = ();
fn new(_id: String, _cfg: Option<&InstanceConfig<Self::Engine>>) -> Self {
Nop {
fn new(_id: String, _cfg: Option<&InstanceConfig<Self::Engine>>) -> Result<Self, Error> {
Ok(Nop {
exit_code: Arc::new((Mutex::new(None), Condvar::new())),
}
})
}
fn start(&self) -> Result<u32, Error> {
Ok(std::process::id())
Expand Down Expand Up @@ -220,7 +222,7 @@ mod noptests {

#[test]
fn test_nop_kill_sigkill() -> Result<(), Error> {
let nop = Nop::new("".to_string(), None);
let nop = Nop::new("".to_string(), None)?;
let (tx, rx) = channel();
let waiter = Wait::new(tx);

Expand All @@ -233,7 +235,7 @@ mod noptests {

#[test]
fn test_nop_kill_sigterm() -> Result<(), Error> {
let nop = Nop::new("".to_string(), None);
let nop = Nop::new("".to_string(), None)?;
let (tx, rx) = channel();
let waiter = Wait::new(tx);

Expand All @@ -246,7 +248,7 @@ mod noptests {

#[test]
fn test_nop_kill_sigint() -> Result<(), Error> {
let nop = Nop::new("".to_string(), None);
let nop = Nop::new("".to_string(), None)?;
let (tx, rx) = channel();
let waiter = Wait::new(tx);

Expand All @@ -258,8 +260,9 @@ mod noptests {
}

#[test]
fn test_nop_delete_after_create() {
let nop = Nop::new("".to_string(), None);
nop.delete().unwrap();
fn test_nop_delete_after_create() -> Result<(), Error> {
let nop = Nop::new("".to_string(), None)?;
nop.delete()?;
Ok(())
}
}
20 changes: 6 additions & 14 deletions crates/containerd-shim-wasm/src/sandbox/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,10 @@ impl<T: Instance + Send + Sync> Local<T> {
);
InstanceData {
instance: None,
base: Some(Nop::new(id, None)),
base: Some(Nop::new(id, None).unwrap()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is safe since Nop::new() always returns Ok. might be work a comment incase things change in future?

cfg,
pid: RwLock::new(None),
status: Arc::new((Mutex::new(None), Condvar::new())),
status: Arc::default(),
state: Arc::new(RwLock::new(TaskStateWrapper::Created(
TaskState::<Created> {
s: std::marker::PhantomData,
Expand Down Expand Up @@ -954,11 +954,11 @@ impl<T: Instance + Send + Sync> Local<T> {
self.instances.write().unwrap().insert(
req.id().to_string(),
Arc::new(InstanceData {
instance: Some(T::new(req.id().to_string(), Some(&builder))),
instance: Some(T::new(req.id().to_string(), Some(&builder))?),
base: None,
cfg: builder,
pid: RwLock::new(None),
status: Arc::new((Mutex::new(None), Condvar::new())),
status: Arc::default(),
state: Arc::new(RwLock::new(TaskStateWrapper::Created(
TaskState::<Created> {
s: std::marker::PhantomData,
Expand Down Expand Up @@ -1097,9 +1097,7 @@ impl<T: Instance + Send + Sync> Local<T> {
..Default::default()
};

let status = i.status.0.lock().unwrap();
if status.is_some() {
let ec = status.unwrap();
if let Some(ec) = *i.status.0.lock().unwrap() {
event.exit_status = ec.0;
resp.exit_status = ec.0;

Expand All @@ -1111,7 +1109,6 @@ impl<T: Instance + Send + Sync> Local<T> {
event.set_exited_at(timestamp.clone());
resp.set_exited_at(timestamp);
}
drop(status);

self.instances.write().unwrap().remove(req.id());

Expand Down Expand Up @@ -1175,12 +1172,7 @@ impl<T: Instance + Send + Sync> Local<T> {

state.set_pid(pid.unwrap());

let status = i.status.0.lock().unwrap();

let code = *status;
drop(status);

if let Some(c) = code {
if let Some(c) = *i.status.0.lock().unwrap() {
state.set_status(Status::STOPPED);
let ec = c;
state.exit_status = ec.0;
Expand Down
Loading