Crates.io | wasmcloud-test-util |
lib.rs | wasmcloud-test-util |
version | 0.15.0 |
source | src |
created_at | 2021-08-12 21:57:36.299321 |
updated_at | 2024-11-08 23:45:38.315835 |
description | Utilities for testing wasmCloud hosts, providers, and components. |
homepage | |
repository | https://github.com/wasmCloud/wasmCloud |
max_upload_size | |
id | 435433 |
size | 53,032 |
wasmcloud-test-util
This repository contains utilities for writing tests for the wasmCloud ecosystem.
This crate provides utilities for:
This crate is meant to be used by programs, utilities and infrastructure targeting the wasmCloud platform.
To use wasmcloud-test-util
in your project, you can add it via cargo add
as a test (development) dependency:
cargo add --dev wasmcloud-test-util
Or include the following in your Cargo.toml
:
wasmcloud-test-util = "0.13.0"
wasmcloud-test-util
comes with the following features:
Feature | Default? | Description |
---|---|---|
http | no | Enable HTTP related test utilities |
os | no | Enable OS-level test utilities |
testcontainers | no | Enable testcontainers -related extensions |
wasmcloud-test-util
wasmcloud-test-util
does not provide a prelude
, but instead exports types as needed under appropriate modules.
use tokio::time::Duration;
use wasmcloud_test_util::control_interface::ClientBuilder;
use wasmcloud_test_util::lattice::link::{assert_advertise_link, assert_remove_link};
use wasmcloud_test_util::nats::wait_for_nats_connection;
use wasmcloud_test_util::provider::StartProviderArgs;
use wasmcloud_test_util::testcontainers::{AsyncRunner as _, ImageExt, NatsServer};
use wasmcloud_test_util::{
assert_config_put, assert_scale_component, assert_start_provider, WasmCloudTestHost,
};
#[tokio::test]
async fn example_test() -> anyhow::Result<()> {
// Start NATS
let nats_container = NatsServer::default()
.with_cmd(["--jetstream"])
.start()
.await
.expect("failed to start nats-server container");
let nats_port = nats_container
.get_host_port_ipv4(4222)
.await
.expect("should be able to find the NATS port");
let nats_url = format!("nats://127.0.0.1:{nats_port}");
// Build a wasmCloud host (assuming you have a local NATS server running)
let lattice = "default";
let host = WasmCloudTestHost::start(nats_url, lattice).await?;
// Once you have a host (AKA a single-member wasmCloud lattice), you'll want a NATS client
// which you can use to control the host and the lattice:
let nats_client = async_nats::connect(nats_url).await?;
let ctl_client = ClientBuilder::new(nats_client)
.lattice(host.lattice_name().to_string())
.build();
// Now that you have a control client, you can use the `assert_*` functions to perform actions on your host:
assert_config_put(
&ctl_client,
"test-config",
[("EXAMPLE_KEY".to_string(), "EXAMPLE_VALUE".to_string())],
)
.await?;
assert_scale_component(
&ctl_client,
&host.host_key(),
"ghcr.io/wasmcloud/components/http-jsonify-rust:0.1.1",
"example-component",
None,
1,
Vec::new(),
Duration::from_secs(10),
)
.await?;
// ... your test logic goes here ...
Ok(())
}
Have a change that belongs be in wasmcloud-test-util
? Please feel free to file an issue and/or join us on the wasmCloud slack!