Skip to content

Commit

Permalink
All the user to set variables
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Jul 31, 2024
1 parent 4dc2fdb commit f7bffd5
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 335 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ on:
env:
CARGO_TERM_COLOR: always
CARGO_COMPONENT_VERSION: 0.14.0
RUST_VERSION: 1.79
RUST_VERSION: "1.80"
WASI_SDK_VERSION: "22.0"
WASI_SDK_RELEASE: wasi-sdk-22

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ concurrency: ${{ github.workflow }}
env:
CARGO_TERM_COLOR: always
CARGO_COMPONENT_VERSION: 0.14.0
RUST_VERSION: 1.79
RUST_VERSION: "1.80"
WASI_SDK_VERSION: "22.0"
WASI_SDK_RELEASE: wasi-sdk-22

Expand Down
697 changes: 371 additions & 326 deletions crates/spin-test-virt/src/bindings.rs

Large diffs are not rendered by default.

29 changes: 22 additions & 7 deletions crates/spin-test-virt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod manifest;
mod wasi;

use std::{
cell::LazyCell,
cell::{LazyCell, RefCell},
collections::{HashMap, HashSet},
sync::{Arc, Mutex, OnceLock, RwLock},
};
Expand Down Expand Up @@ -702,20 +702,35 @@ thread_local! {
.map(|(k, v)| (k.to_string(), v)),
)
.unwrap();
resolver.add_provider(Box::new(TestProvider));
resolver.add_provider(Box::new(UserGivenProvider));
Ok(resolver)

});

/// The user-defined variables.
///
/// These are the application variables the user is supposed to provide.
static USER_DEFINED_VARIABLES: LazyCell<RefCell<HashMap<String, String>>> = LazyCell::new(|| {
RefCell::new(HashMap::new())
});
}

/// A variable provider for testing.
/// A variable provider populated through the `fermyon:spin-test-virt/variable` interface.
#[derive(Debug)]
struct TestProvider;
struct UserGivenProvider;

#[async_trait::async_trait]
impl spin_expressions::Provider for TestProvider {
async fn get(&self, _key: &spin_expressions::Key) -> anyhow::Result<Option<String>> {
Ok(Some("test".to_string()))
impl spin_expressions::Provider for UserGivenProvider {
async fn get(&self, key: &spin_expressions::Key) -> anyhow::Result<Option<String>> {
Ok(USER_DEFINED_VARIABLES.with(|vars| vars.borrow().get(key.as_str()).cloned()))
}
}

impl spin_test_virt::variables::Guest for Component {
fn set(key: String, value: String) {
USER_DEFINED_VARIABLES.with(|vars| {
vars.borrow_mut().insert(key, value);
});
}
}

Expand Down
6 changes: 6 additions & 0 deletions host-wit/deps/spin-test-virt/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ world env {
// Handles for configuring the environment
export key-value;
export sqlite;
export variables;

// Helper functions
export set-component-id: func(component-id: string);
import get-manifest: func() -> string;
}

/// Interface for configuring variables
interface variables {
set: func(key: string, value: string);
}

/// Interface for configuring the behavior of `fermyon:spin/sqlite` interface
interface sqlite {
use fermyon:spin/sqlite@2.0.0.{value, query-result, error};
Expand Down
1 change: 1 addition & 0 deletions host-wit/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package fermyon:spin-test;
world test-imports {
import fermyon:spin-test-virt/key-value;
import fermyon:spin-test-virt/sqlite;
import fermyon:spin-test-virt/variables;
import fermyon:spin-wasi-virt/http-handler;
import wasi:http/incoming-handler@0.2.0;
import http-helper;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ fn instantiate_test(
"fermyon:spin/[email protected]",
"fermyon:spin-test-virt/sqlite",
"fermyon:spin-test-virt/key-value",
"fermyon:spin-test-virt/variables",
"fermyon:spin/[email protected]",
"wasi:io/[email protected]",
"wasi:io/[email protected]",
Expand Down

0 comments on commit f7bffd5

Please sign in to comment.