Skip to content

Commit a656187

Browse files
authored
rust testcontainer framework (#41)
Introduced new crate `restate-sdk-test-env` to help people test their restate services.
1 parent 69723f9 commit a656187

File tree

5 files changed

+473
-1
lines changed

5 files changed

+473
-1
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ syn = "2.0"
4747
typify = { version = "0.1.0" }
4848

4949
[workspace]
50-
members = ["macros", "test-services"]
50+
members = ["macros", "test-services", "test-env"]

README.md

+52
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,58 @@ async fn main() {
6363
The SDK uses tokio's [`tracing`](https://docs.rs/tracing/latest/tracing/) crate to generate logs.
6464
Just configure it as usual through [`tracing_subscriber`](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/) to get your logs.
6565

66+
### Testing
67+
68+
The SDK uses [Testcontainers](https://rust.testcontainers.org/) to support integration testing using a Docker-deployed restate server.
69+
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`.
70+
71+
```rust
72+
#[tokio::test]
73+
async fn test_container() {
74+
tracing_subscriber::fmt::fmt()
75+
.with_max_level(tracing::Level::INFO) // Set the maximum log level
76+
.init();
77+
78+
let endpoint = Endpoint::builder().bind(MyServiceImpl.serve()).build();
79+
80+
// simple test container intialization with default configuration
81+
//let test_container = TestContainer::default().start(endpoint).await.unwrap();
82+
83+
// custom test container initialization with builder
84+
let test_container = TestContainer::builder()
85+
// optional passthrough logging from the resstate server testcontainer
86+
// prints container logs to tracing::info level
87+
.with_container_logging()
88+
.with_container(
89+
"docker.io/restatedev/restate".to_string(),
90+
"latest".to_string(),
91+
)
92+
.build()
93+
.start(endpoint)
94+
.await
95+
.unwrap();
96+
97+
let ingress_url = test_container.ingress_url();
98+
99+
// call container ingress url for /MyService/my_handler
100+
let response = reqwest::Client::new()
101+
.post(format!("{}/MyService/my_handler", ingress_url))
102+
.header("Accept", "application/json")
103+
.header("Content-Type", "*/*")
104+
.header("idempotency-key", "abc")
105+
.send()
106+
.await
107+
.unwrap();
108+
109+
assert_eq!(response.status(), StatusCode::OK);
110+
111+
info!(
112+
"/MyService/my_handler response: {:?}",
113+
response.text().await.unwrap()
114+
);
115+
}
116+
```
117+
66118
## Versions
67119

68120
The Rust SDK is currently in active development, and might break across releases.

test-env/Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "restate-sdk-test-env"
3+
version = "0.3.2"
4+
edition = "2021"
5+
description = "Test Utilities for Restate SDK for Rust"
6+
license = "MIT"
7+
repository = "https://github.com/restatedev/sdk-rust"
8+
rust-version = "1.76.0"
9+
10+
11+
[dependencies]
12+
anyhow = "1.0.95"
13+
nu-ansi-term = "0.50.1"
14+
reqwest = {version= "0.12.12", features = ["json"]}
15+
restate-sdk = {path = "../"}
16+
serde = "1.0.217"
17+
serde_json = "1.0.138"
18+
testcontainers = "0.23.1"
19+
tokio = "1.43.0"
20+
tracing = "0.1.41"
21+
tracing-subscriber = "0.3.19"

0 commit comments

Comments
 (0)