Skip to content

Commit

Permalink
test(jstzd): add container test
Browse files Browse the repository at this point in the history
  • Loading branch information
ryutamago committed Sep 18, 2024
1 parent 90fc52b commit 65c107e
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 3 deletions.
41 changes: 41 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ regex = "1"
reqwest = { version = "0.11.24", features = ["json"] }
reqwest-eventsource = "0.5.0"
rustyline = "14.0.0"
serial_test = "3.1.1"
serde = { version = "1.0.196", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
serde_json = "1.0.107"
Expand All @@ -103,6 +104,7 @@ wasm-bindgen = "0.2.92"
async-dropper = { version = "0.3.1", features = ["tokio", "simple"] }
async-trait = "0.1.82"


[workspace.dependencies.tezos-smart-rollup]
version = "0.2.2"
default-features = false
Expand Down
3 changes: 3 additions & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ log.workspace = true
name = "jstzd"
path = "src/main.rs"


[dev-dependencies]
serial_test.workspace = true
2 changes: 1 addition & 1 deletion crates/jstzd/src/docker/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Container {
self.id
));
}
return Err(e.into());
Err(e.into())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/jstzd/src/docker/runnable_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl<I: Image> RunnableImage<I> {
self.image.image_uri()
));
}
return Err(err.into());
Err(err.into())
}
}
}
Expand Down
137 changes: 137 additions & 0 deletions crates/jstzd/tests/container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use std::sync::Arc;

use anyhow::Result;
use bollard::{container::ListContainersOptions, Docker};
use jstzd::docker::{GenericImage, Image, RunnableImage};
use serial_test::serial;

#[tokio::test]
#[serial]
async fn creates_container() -> Result<()> {
let docker = docker();
let image = image(docker.clone()).await?;
let runnable_image = RunnableImage::new(image.clone(), "test_container1");
let container = runnable_image.create_container(docker.clone()).await?;
let containers = docker
.list_containers(Some(ListContainersOptions::<String> {
all: true,
..Default::default()
}))
.await?;
assert!(containers
.iter()
.any(|c| c.id.as_deref() == Some(&container.id)));
container.cleanup().await?;
Ok(())
}

#[tokio::test]
#[serial]
async fn runs_container() -> Result<()> {
let docker = docker();
let image = image(docker.clone()).await?;
let cmd = vec!["sh".to_string(), "-c".to_string(), "sleep 1".to_string()];
let runnable_image =
RunnableImage::new(image.clone(), "test_container2").with_overridden_cmd(cmd);
let container = runnable_image.create_container(docker.clone()).await?;
container.start().await?;
let containers = docker
.list_containers(Some(ListContainersOptions::<String> {
..Default::default()
}))
.await?;
assert!(containers
.iter()
.any(|c| c.id.as_deref() == Some(&container.id)));
container.cleanup().await?;
Ok(())
}

#[tokio::test]
#[serial]
async fn cleans_up_container() -> Result<()> {
let docker = docker();
let image = image(docker.clone()).await?;
let runnable_image = RunnableImage::new(image.clone(), "test_container3");
let container = runnable_image.create_container(docker.clone()).await?;
container.cleanup().await?;
let containers = docker
.list_containers(Some(ListContainersOptions::<String> {
all: true,
..Default::default()
}))
.await?;
assert!(containers
.iter()
.any(|c| c.id.as_deref() != Some(&container.id)));
Ok(())
}

#[tokio::test]
#[serial]
async fn removing_container_twice_fails() -> Result<()> {
let docker = docker();
let image = image(docker.clone()).await?;
let runnable_image = RunnableImage::new(image.clone(), "test_container4");
let container = runnable_image.create_container(docker.clone()).await?;
container.start().await?;
container.stop().await?;
container.remove().await?;
let result = container.remove().await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
format!("Failed to remove non existent container: {}", container.id)
);
Ok(())
}

#[tokio::test]
#[serial]
async fn removing_running_container_fails() -> Result<()> {
let docker = docker();
let image = image(docker.clone()).await?;
let cmd = vec!["sh".to_string(), "-c".to_string(), "sleep 1".to_string()];
let runnable_image =
RunnableImage::new(image.clone(), "test_container5").with_overridden_cmd(cmd);
let container = runnable_image.create_container(docker.clone()).await?;
container.start().await?;
let result = container.remove().await;
assert!(result.is_err());
assert!(result
.err()
.unwrap()
.to_string()
.contains("stop the container before removing"));
container.cleanup().await?;
Ok(())
}

#[tokio::test]
#[serial]
async fn running_container_with_no_image_fails() -> Result<()> {
let docker = docker();
let image = image(docker.clone()).await?;
docker.remove_image(&image.image_uri(), None, None).await?;
let runnable_image = RunnableImage::new(image.clone(), "test_container6");
let result = runnable_image.create_container(docker.clone()).await;
assert!(result.is_err());
assert_eq!(
result.err().unwrap().to_string(),
format!(
"Image: {} not found, please make sure the image is pulled",
image.image_uri()
)
);
Ok(())
}

fn docker() -> Arc<Docker> {
Arc::new(Docker::connect_with_socket_defaults().unwrap())
}

async fn image(client: Arc<Docker>) -> Result<GenericImage> {
let image = GenericImage::new("busybox").with_tag("stable");
image.pull_image(client).await?;
Ok(image)
}
3 changes: 2 additions & 1 deletion crates/jstzd/tests/pull_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::HashMap, sync::Arc};
use anyhow::Result;
use bollard::{image::ListImagesOptions, secret::ImageSummary, Docker};
use jstzd::docker::{GenericImage, Image};
use serial_test::serial;

// search image locally
async fn search_local_image(
Expand All @@ -24,8 +25,8 @@ async fn search_local_image(
Err(_) => Err(anyhow::anyhow!("Image not found")),
}
}

#[tokio::test]
#[serial]
async fn test_pull_image() -> Result<()> {
let docker = Docker::connect_with_socket_defaults().unwrap();
let docker = Arc::new(docker);
Expand Down

0 comments on commit 65c107e

Please sign in to comment.