Docs

Testing

You test an extension on your machine before it reaches a customer. One command runs the extension's tests and the importer check that alien release repeats:

alien extensions test

It runs your workspace's tests, runs your hooks' tests again inside the WebAssembly module Alien loads, runs each importer natively and in the module, and loads every importer fixture into its controller or, for a type with no controller, checks that it reports outputs.

Test hooks

Hooks are plain Rust, so cargo test runs their unit tests natively. This test checks the interceptor from the interceptors page:

hooks/src/postgres_iam_auth.rs
#[test]
fn adds_iam_auth() {
    let mut request = AwsRequest::new(
        "rds",
        "CreateDBCluster",
        json!({ "DBClusterIdentifier": "db" }),
    );
    Rds.modify_request(&mut request).unwrap();
    assert_eq!(request.param("EnableIAMDatabaseAuthentication"), Some("true"));
}
cargo test

A hook runs sandboxed in production, with no network and no files. A test that passes natively can still fail there, which is why alien extensions test runs the same tests again inside the module.

Test the whole extension

alien extensions test runs from the extension's workspace. It checks what cargo test can't:

  • Your tests. It runs cargo test --workspace first.
  • Hooks in the module. It runs the hooks crate's tests natively and inside the WebAssembly module, and fails if the two give different results.
  • Importers in the module. It runs each importer on its fixtures natively and in the module alien extensions build builds, and fails unless the two outputs are the same bytes.
  • Importer fixtures. It loads each fixture's import into the controller, which must read it back unchanged: the same fields, status and outputs. For a type with no controller, it checks that the importer reports outputs, as below.

alien release runs the importer check again, so a release never ships an importer its controller can't load.

Importer fixtures

An importer fixture is a sample of what the setup template returns for one resource. Put one or more per importer under tests/import/<resource-type>/<platform>/:

tests/import/acme/dns-zone/aws/basic.json
{
  "resource": { "id": "zone", "domain": "internal.acme.dev" },
  "importData": { "zoneId": "Z123" }
}

resource is the resource's config in alien.ts, and importData is what the template returns. For each fixture, Alien runs the importer in the WebAssembly module and loads its state into the controller. The check fails when:

  • the controller can't load the state, for example a state name it doesn't know;
  • the importer writes a field the controller doesn't read, or leaves out one it does;
  • the controller changes a value the importer wrote;
  • the status or outputs the importer reports differ from the controller's.

For a resource with no controller, such as the budget alert, the check runs the importer on each fixture and checks that it reports outputs.

Every importer needs at least one fixture. alien extensions test, and alien release from the extension's workspace, fail on an importer without one. Without the check, a field the importer and the controller name differently reaches your customer, and the controller runs with that field empty and no error.

Test controllers

The controller crate's library also builds natively, so cargo test steps its controllers with alien_extension_sdk::controller_module::testing::answer. The test takes Alien's place: it sends one step and answers each call the handler makes.

A step is the JSON request of the step protocol. This helper builds the first step of a create:

controller/tests/native_steps.rs
use alien_extension_sdk::controller_module::testing::answer;
use serde_json::{json, Value};
use std::sync::{Arc, Mutex};

fn create(platform: &str, cloud: Value) -> Value {
    json!({
        "protocol": 1, "stepId": "step-1", "resourceType": "acme/dns-zone",
        "platform": platform, "resourcePrefix": "q6aa0b26",
        "resource": { "type": "acme/dns-zone", "id": "zone", "domain": "internal.acme.dev" },
        "previousResource": null, "dependencies": {}, "state": null, "transitions": [],
        "cloud": cloud, "deadline": "2099-01-01T00:00:00Z"
    })
}

The test passes the library's register function, the step, and a closure that answers the handler's calls:

controller/tests/native_steps.rs
#[test]
fn the_google_cloud_controller_creates_the_zone_it_names() {
    let sent = Arc::new(Mutex::new(Vec::new()));
    let seen = sent.clone();
    let reply = answer(
        acme_controller::register,
        &create(
            "gcp",
            json!({ "platform": "gcp", "projectId": "the-project", "region": "us-central1" }),
        ),
        move |request| {
            seen.lock().unwrap().push(request.clone());
            Ok(json!({ "status": 200, "headers": [], "body": "{}" }))
        },
    )
    .unwrap();

    assert_eq!(reply["result"], "stepped", "{reply}");
    assert_eq!(reply["controller"]["status"], "running");
    assert_eq!(reply["controller"]["outputs"]["zoneId"], "q6aa0b26-zone");
    let sent = sent.lock().unwrap();
    assert_eq!(sent.len(), 1);
    assert_eq!(sent[0]["cloud"], "gcp");
    assert_eq!(sent[0]["service"], "dns");
    assert_eq!(sent[0]["method"], "POST");
    assert_eq!(sent[0]["path"], "/dns/v1/projects/the-project/managedZones");
}
  • The closure gets each request as Alien would: service, method, path, query, headers, and body, or url for a declared origin. An AWS SDK client's requests reach it too.
  • It returns the response, { "status": 200, "headers": [], "body": "..." }. To stand in for a call Alien didn't answer, it returns Err with a line that starts refused: or failed:.
  • answer returns the step's reply, with the controller's state, status, and outputs. Pass the reply's controller.state as the next step's state to step again.
  • register is pub in the library so the test can name it.

The test runs the controller natively, outside the sandbox. It covers the controller's states, requests, and outputs, not the limits Alien puts on a module's calls. Controllers covers the rules a handler follows in production, such as being safe to run twice.

The local controller in the crate's binary is Alien's own kind of controller. SingleControllerExecutor, from local_controller_prelude, steps it the way Alien's own controller tests do.

Rendered templates

alien render prints a setup template with your extensions applied, on your machine. Build the extension first, then render from the folder with alien.ts:

alien extensions build
alien render --format cloudformation --stack alien.ts
alien render --format terraform --target aws --stack alien.ts
alien render --format helm --stack alien.ts

The output is the template your customer's security team reviews. alien render lists which extension changed which resource, and fails with the extension's name if a modifier removes a resource Alien emits or changes its type. Template modifiers covers what a modifier may change.

Local deployment

alien dev runs your stack on your machine with the extension loaded:

alien dev

It builds the extension for your machine and starts each custom resource's local controller, the one registered for Platform::Local, as a process. Modifiers and interceptors don't run there, because alien dev generates no setup template and makes no cloud calls. Local development covers the session, and Testing covers deploying the stack from a test suite.

Next

On this page