Testing a Provider
Unit-test a Carina provider's operations against a fake API on the host target, then run acceptance tests that apply real configurations.
A provider is tested at two levels. Unit tests run its operations on the host target against a fake API and check the outcomes Carina will see. Acceptance tests build the component and run carina against a real account, which is the only way to check that a resource converges.
Unit tests
The widget provider takes its API client as a type parameter, so tests can give it a fake. They are ordinary Rust tests and run with cargo test on the host; you do not need the wasm32-wasip2 target for them.
There is one wrinkle. A CreateAttempt can only be created by the SDK, so a test cannot call create directly. Call carina_plugin_sdk::dispatch_create instead: it is the entry point the component’s generated code uses, so it mints the attempt and returns the protocol-shaped outcome with native opaque errors and diagnostics. Encoding into WIT happens afterward. dispatch_poll_create and dispatch_resume_create do the same for the other create calls. update, delete and read take no typestate, so tests call them directly.
For native assertions on provider errors, enable carina-core’s test-utils feature in your dev-dependency. NativeWireFixture encodes an error or partial diagnostic through the real ProviderBoundary; BindProviderText binds it through that boundary so tests can inspect host diagnostics. Use context to register secrets when checking redaction. These helpers are available only in test builds or with test-utils enabled.
use carina_core::provider::ProviderError;
use carina_core::test_utils::provider_text::{
BindProviderText, NativeWireFixture, context,
};
let error = ProviderError::api_error_caused_by(
"read error",
std::io::Error::other("ResourceNotFoundException"),
);
let wire = error.to_wire();
assert_eq!(wire.cause.as_deref(), Some("ResourceNotFoundException"));
let error = error.into_host_for_test();
assert!(error.to_string().contains("ResourceNotFoundException"));
assert_eq!(error.detail().context(), "read error");
let error = ProviderError::api_error("password rejected")
.with_secrets_for_test(&context(&["password"]));
assert_eq!(error.to_string(), "(secret) rejected");
source() advances one logical causal link, including through opaque and shared diagnostics. source_is::<T>() searches descendants; matches_for_test::<T>(fn(&T) -> bool) searches the current node and descendants. is and downcast_ref inspect only the current node, and downcasts return opaque views. Test predicates must be non-capturing. Binding a cloned diagnostic must leave the other handle’s secret inventory unchanged.
Check partial reasons before encoding as well as through the boundary:
use carina_core::provider::{NonEmptyMissingAttributes, PartialReadDiagnostic, ProviderError};
use carina_core::provider_text::{ProviderDiagnostic, Retryability};
use carina_core::test_utils::provider_text::{BindProviderText, NativeWireFixture, context};
let missing = NonEmptyMissingAttributes::try_from(vec!["name".to_owned()]).unwrap();
let reason = ProviderError::api_error_caused_by("read failed", std::io::Error::other("password"))
.with_retryability(Retryability::Transient);
let partial = PartialReadDiagnostic::new(ProviderDiagnostic::new(reason), missing);
assert!(partial.diagnostic().source_is::<std::io::Error>());
assert_eq!(partial.diagnostic().retryability(), Retryability::Transient);
let host = partial.with_secrets_for_test(&context(&["password"]));
assert_eq!(host.reason(), "read failed: (secret)");
assert!(host.diagnostic().source_is::<std::io::Error>());
Wire and persistence round trips retain textual reasons, not the original typed cause or retryability. Test empty wire partials separately: decoders deliberately normalize them to success.
#[cfg(test)]
mod tests {
use super::*;
use carina_plugin_sdk::protocol::types::{
CreateStep as WireCreateStep, CreateVerdict as WireCreateVerdict,
PollVerdict as WirePollVerdict,
};
use carina_plugin_sdk::{PollFinality, PollRequest};
use std::cell::RefCell;
use std::collections::HashMap;
/// An in-memory API that records what the provider asked for.
#[derive(Default)]
struct FakeApi {
widgets: RefCell<Vec<Widget>>,
/// Client tokens already used, with the operation each started.
operations: RefCell<HashMap<String, String>>,
/// Errors to return from the next start_create calls, in order.
start_errors: RefCell<Vec<ApiError>>,
size_calls: RefCell<Vec<(String, i64)>>,
}
impl Api for FakeApi {
fn new_client_token(&self) -> String {
"token-1".to_string()
}
fn start_create(
&self,
client_token: &str,
name: &str,
size: i64,
) -> Result<String, ApiError> {
if let Some(operation) = self.operations.borrow().get(client_token) {
return Ok(operation.clone());
}
if !self.start_errors.borrow().is_empty() {
return Err(self.start_errors.borrow_mut().remove(0));
}
self.widgets.borrow_mut().push(Widget {
id: format!("w-{name}"),
name: name.into(),
size,
});
let operation = format!("op-{name}");
self.operations
.borrow_mut()
.insert(client_token.into(), operation.clone());
Ok(operation)
}
fn create_status(&self, operation: &str) -> Result<CreateStatus, ApiError> {
let name = operation.trim_start_matches("op-");
Ok(CreateStatus::Done {
id: format!("w-{name}"),
})
}
fn get(&self, id: &str) -> Result<Widget, ApiError> {
self.widgets
.borrow()
.iter()
.find(|w| w.id == id)
.cloned()
.ok_or(ApiError::NotFound)
}
fn set_size(&self, id: &str, size: i64) -> Result<(), ApiError> {
self.size_calls.borrow_mut().push((id.to_string(), size));
Ok(())
}
fn delete(&self, _id: &str) -> Result<(), ApiError> {
Err(ApiError::NotFound)
}
}
fn api_failing_with(errors: Vec<ApiError>) -> FakeApi {
FakeApi {
start_errors: RefCell::new(errors),
..Default::default()
}
}
/// Poll a pending create once, the way the host does.
fn poll(
provider: &WidgetProvider<FakeApi>,
pending: carina_plugin_sdk::protocol::types::PendingCreate<CreateToken>,
) -> WireCreateStep<BTreeMap<String, Value>, PartialReadDiagnostic, CreateToken> {
carina_plugin_sdk::dispatch_poll_create(
provider,
&resource_id(),
create_request("a", 1),
pending.progress,
pending.sent,
pending.token,
PollRequest { finality: PollFinality::Continue },
)
.expect("poll should not fail")
}
fn resource_id() -> ResourceId {
ResourceId {
provider: "widget".into(),
resource_type: "widget".into(),
identity: "main".into(),
}
}
fn create_request(name: &str, size: i64) -> CreateRequest {
CreateRequest {
resource: Resource {
id: resource_id(),
attributes: BTreeMap::from([
("name".to_string(), Value::String(name.into())),
("size".to_string(), Value::Int(size)),
]),
directives: Default::default(),
},
}
}
#[test]
fn rejected_create_is_not_created() {
let provider = WidgetProvider {
api: api_failing_with(vec![ApiError::Rejected("size too large".into())]),
};
// `CreateAttempt` can only be minted by the SDK; dispatch_create
// goes through the same entry point the host uses.
let verdict =
carina_plugin_sdk::dispatch_create(&provider, &resource_id(), create_request("a", 99))
.expect_err("a rejected create must not succeed");
assert!(matches!(*verdict, WireCreateVerdict::NotCreated(_)));
}
#[test]
fn create_with_unknown_outcome_is_retried_with_the_same_client_token() {
let provider = WidgetProvider {
api: api_failing_with(vec![ApiError::Unknown("connection reset".into())]),
};
let step =
carina_plugin_sdk::dispatch_create(&provider, &resource_id(), create_request("a", 1))
.expect("an unknown outcome must not be reported as a failure");
let WireCreateStep::Pending(pending) = step else {
panic!("expected pending")
};
assert!(pending.identifier().is_none());
// The retry starts the create once; no second widget appears.
let WireCreateStep::Pending(pending) = poll(&provider, pending) else {
panic!("expected pending")
};
assert!(matches!(pending.token, CreateToken::Operation(_)));
assert_eq!(provider.api.widgets.borrow().len(), 1);
}
#[test]
fn rejected_retry_preserves_the_continuation() {
let provider = WidgetProvider {
api: api_failing_with(vec![
ApiError::Unknown("timeout".into()),
ApiError::Rejected("client token reused with different parameters".into()),
]),
};
let step =
carina_plugin_sdk::dispatch_create(&provider, &resource_id(), create_request("a", 1))
.expect("an unknown outcome must not be reported as a failure");
let WireCreateStep::Pending(pending) = step else {
panic!("expected pending")
};
let verdict = carina_plugin_sdk::dispatch_poll_create(
&provider,
&resource_id(),
create_request("a", 1),
pending.progress,
pending.sent,
pending.token,
PollRequest { finality: PollFinality::Continue },
)
.expect_err("a rejected retry must not succeed");
let WirePollVerdict::InFlight(in_flight) = *verdict else {
panic!("expected an in-flight verdict")
};
match in_flight.pending.token {
CreateToken::Operation(CreateOperation::Starting { client_token }) => {
assert_eq!(client_token, "token-1");
}
unexpected => {
panic!("expected the starting continuation, got {unexpected:?}");
}
}
}
#[test]
fn poll_records_the_identifier_then_reads_the_widget() {
let provider = WidgetProvider {
api: FakeApi::default(),
};
let step =
carina_plugin_sdk::dispatch_create(&provider, &resource_id(), create_request("a", 1))
.expect("create should start");
let WireCreateStep::Pending(pending) = step else {
panic!("expected pending")
};
let WireCreateStep::Pending(pending) = poll(&provider, pending) else {
panic!("expected pending")
};
assert_eq!(
pending.identifier().map(|id| id.as_str()),
Some("w-a")
);
let WireCreateStep::Success {
identifier,
attributes,
} = poll(&provider, pending)
else {
panic!("expected success")
};
assert_eq!(identifier.as_str(), "w-a");
assert!(matches!(attributes.get("size"), Some(Value::Int(1))));
}
#[test]
fn update_sends_only_patched_keys() {
let provider = WidgetProvider {
api: FakeApi::default(),
};
let identifier = AppliedIdentifier::new("w-a").unwrap();
let request = UpdateRequest {
from: PersistedState {
id: resource_id(),
identifier: Some("w-a".into()),
attributes: BTreeMap::from([
("name".to_string(), PersistedValue::String("a".into())),
("size".to_string(), PersistedValue::Int(1)),
]),
exists: true,
},
patch: UpdatePatch {
ops: vec![PatchOp::Replace {
key: "size".into(),
value: Value::Int(2),
}],
},
};
provider
.update(&resource_id(), &identifier, request)
.expect("update should succeed");
assert_eq!(
*provider.api.size_calls.borrow(),
vec![("w-a".to_string(), 2)]
);
}
#[test]
fn deleting_a_missing_widget_succeeds() {
let provider = WidgetProvider {
api: FakeApi::default(),
};
let identifier = AppliedIdentifier::new("w-gone").unwrap();
let outcome = provider.delete(&resource_id(), &identifier, DeleteRequest::default());
assert!(matches!(outcome, Ok(DeleteOutcome::Deleted)));
}
}
Test the classification as well as the happy path. Tests like create_with_unknown_outcome_is_retried_with_the_same_client_token and rejected_retry_preserves_the_continuation are what keep a later change from turning an ambiguous failure into not-created and losing track of a real resource.
Acceptance tests
Unit tests cannot tell you whether a resource converges: whether, after apply, the attributes the provider returns from read match the configuration so that the next plan is empty. That needs the real API.
The official providers keep acceptance tests in an acceptance-tests/ directory, one directory per resource type with one or more .crn configurations, and a script that runs each one through a full cycle:
carina initinstalls the component under test. Run it again after every rebuild; otherwise the test runs the copy installed earlier. On that init, an unchanged egress declaration carries consent forward without a prompt, while a changed declaration shows the diff and asks again.carina apply --auto-approvecreates the resources.carina plan --detailed-exitcodemust exit with0. It exits with2when the plan has changes, which means the resource does not converge, usually becausereadreturns a value in a different form than the configuration or leaves an attribute out.carina destroy --auto-approveremoves everything.
Updates, attribute removal and replacement need more than one configuration. Keep the steps outside the working directory, such as step1.crn and step2.crn, and copy each in turn over main.crn in one working directory, so the second apply updates what the first created. Do not put the step files in the working directory itself: Carina reads every .crn file in a directory, so it would declare the resources twice. Check the plan between steps for the change you expect, and run the converge check after each apply.
Run acceptance tests in an account that holds nothing else, and make the script destroy what it created even when a step fails or the run is interrupted. A test that leaves resources behind costs money and can break the next run. Point source at the build under test with a file:// URL, which step 1 installs, and use a release build of carina: loading a component in a debug build is slow.
A local build cannot link the host AWS credential import by default, and metadata is unavailable to every guest. For an AWS or AWSCC acceptance run, set CARINA_TRUST_LOCAL_PROVIDER=/absolute/path/to/provider.wasm=aws for every apply, plan and destroy command, or pass the equivalent repeatable --trust-local-provider flag to each command. The environment variable separates multiple entries with : on Unix and ; on Windows. Re-run carina init after rebuilding so the installed bytes still match the granted source file.
Next steps
Versioning and Compatibility explains which Carina releases can load your provider and what to do when the protocol changes. The protocol-level details behind these pages are in docs/protocol.md.