Skip to content

rust testcontainer framework #41

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

Merged
merged 12 commits into from
Feb 20, 2025
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ syn = "2.0"
typify = { version = "0.1.0" }

[workspace]
members = ["macros", "test-services"]
members = ["macros", "test-services", "test-env"]
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,58 @@ async fn main() {
The SDK uses tokio's [`tracing`](https://docs.rs/tracing/latest/tracing/) crate to generate logs.
Just configure it as usual through [`tracing_subscriber`](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/) to get your logs.

### Testing

The SDK uses [Testcontainers](https://rust.testcontainers.org/) to support integration testing using a Docker-deployed restate server.
The `restate-sdk-test-env` crate provides a framework for initializing the test environment, and an integration test example in `test-env/tests/test_container.rs`.

```rust
#[tokio::test]
async fn test_container() {
tracing_subscriber::fmt::fmt()
.with_max_level(tracing::Level::INFO) // Set the maximum log level
.init();

let endpoint = Endpoint::builder().bind(MyServiceImpl.serve()).build();

// simple test container intialization with default configuration
//let test_container = TestContainer::default().start(endpoint).await.unwrap();

// custom test container initialization with builder
let test_container = TestContainer::builder()
// optional passthrough logging from the resstate server testcontainer
// prints container logs to tracing::info level
.with_container_logging()
.with_container(
"docker.io/restatedev/restate".to_string(),
"latest".to_string(),
)
.build()
.start(endpoint)
.await
.unwrap();

let ingress_url = test_container.ingress_url();

// call container ingress url for /MyService/my_handler
let response = reqwest::Client::new()
.post(format!("{}/MyService/my_handler", ingress_url))
.header("Accept", "application/json")
.header("Content-Type", "*/*")
.header("idempotency-key", "abc")
.send()
.await
.unwrap();

assert_eq!(response.status(), StatusCode::OK);

info!(
"/MyService/my_handler response: {:?}",
response.text().await.unwrap()
);
}
```

## Versions

The Rust SDK is currently in active development, and might break across releases.
Expand Down
21 changes: 21 additions & 0 deletions test-env/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "restate-sdk-test-env"
version = "0.3.2"
edition = "2021"
description = "Test Utilities for Restate SDK for Rust"
license = "MIT"
repository = "https://github.com/restatedev/sdk-rust"
rust-version = "1.76.0"


[dependencies]
anyhow = "1.0.95"
nu-ansi-term = "0.50.1"
reqwest = {version= "0.12.12", features = ["json"]}
restate-sdk = {path = "../"}
serde = "1.0.217"
serde_json = "1.0.138"
testcontainers = "0.23.1"
tokio = "1.43.0"
tracing = "0.1.41"
tracing-subscriber = "0.3.19"
Loading
Loading