Alternera.PluginWasmFramework.Test (alternera_plugin_wasm_framework v0.9.0)

Copy Markdown View Source

The contract a language plugin keeps, as ExUnit tests, the way the SDK's plugin_contract/2 checks a plugin. The plugin gives, in its own language, the source for each named scenario; the kit runs each through the step with the SDK's test host (Plugin.Test) and checks the result.

defmodule Alternera.Plugins.JavascriptContractTest do
  use ExUnit.Case, async: true
  use Alternera.PluginWasmFramework.Test

  language_contract(Alternera.Plugins.Javascript,
    sources: %{
      body_unchanged: "return req.body.read()",
      ...
    }
  )
end

A scenario with no source fails, so the list is the contract; one a plugin cannot keep goes under skip: with the reason, and the test says so. Grow this list rather than writing per-language one-offs.

The scenarios

Each names what the script does and what the kit then checks. A request step runs on the body "kit body" unless the scenario says otherwise; "returns" means the script's returned value.

ScenarioThe scriptThe kit checks
body_unchangedreturns its request body as it got itthe body stays, nothing is written
body_changedreturns its request body with " changed" appendedthe body is "kit body changed"
nothingreturns nothingthe step skips
read_headerreturns the request header x-kit"from header", written to the variable got
read_queryreturns the query parameter kit"from query", to got
read_varreturns the variable kit"from var", to got
read_inputreturns the input kit"from input", to got
content_typea response step: returns {"kit":true} with the content type application/jsonthe body and its Content-Type
two_outputssets the outputs a to 1 and b to "two", returns nothingthe outputs output
store_put_getputs "stored" under k in the store kit, gets it back and returns it"stored"
secret_store_scrubgets k from the secret store kitsecret and fails with the type kit_failure and the message "saw " followed by the valuethe message is scrubbed
tenant_varreturns the tenant variable kit_var"plain value"
tenant_secretreturns the tenant secret kit_secretits reference, {{secret.kit_secret}}, never the value
pipeline_callcalls kit_callee with the input n = 1 and returns its resultthe call and "callee result"
pipeline_call_failurecalls kit_missing and does not handle the failurethe step fails with the callee's type, pipeline_not_found
failfails with the type kit_failure, the status 418 and the message "on purpose"that error, on purpose
syntax_errora source whose line 2 does not compilethe save is refused, naming line 2
runtime_errorraises an error on line 3script_error, naming line 3
endless_looploops forevertimeout
allocation_loopallocates until memory runs out (Flat 8 MiB)memory_limit
allocation_loop_caughtthe same, catching the error it gets and returning "survived"still memory_limit
global_freshreturns "reused" when a global it sets is already set, else sets it and returns "fresh""fresh" on two runs in a row

Summary

Functions

Runs one scenario's source and checks it: :ok or {:fail, why}.

One test per scenario for plugin. sources: maps a scenario to its source; skip: maps a scenario the plugin cannot keep to the reason.

The scenarios, in order.

Functions

check(plugin, scenario, source)

@spec check(module(), atom(), String.t() | nil) :: :ok | {:fail, String.t()}

Runs one scenario's source and checks it: :ok or {:fail, why}.

language_contract(plugin, opts)

(macro)

One test per scenario for plugin. sources: maps a scenario to its source; skip: maps a scenario the plugin cannot keep to the reason.

scenarios()

@spec scenarios() :: [atom()]

The scenarios, in order.