Skip to content

Commit

Permalink
Merge pull request #2141 from fermyon/key-value-runtime-tests
Browse files Browse the repository at this point in the history
Key/Value runtime tests
  • Loading branch information
rylev authored Dec 11, 2023
2 parents 4847ced + 053c025 commit 45987c0
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 748 deletions.
7 changes: 7 additions & 0 deletions test-components/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test-components/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["sqlite", "helper", "outbound-mysql", "outbound-redis"]
members = ["sqlite", "helper", "outbound-mysql", "outbound-redis", "key-value"]
resolver = "2"
10 changes: 10 additions & 0 deletions test-components/key-value/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "key-value"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
helper = { path = "../helper" }
10 changes: 10 additions & 0 deletions test-components/key-value/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Key Value

Tests the key/value interface.

## Expectations

This test component expects the following to be true:
* It is given permission to open a connection to the "default" store.
* It does not have permission to access a store named "forbidden".
* It is empty
Binary file added test-components/key-value/component.wasm
Binary file not shown.
45 changes: 45 additions & 0 deletions test-components/key-value/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use helper::{ensure_matches, ensure_ok};

use bindings::fermyon::spin2_0_0::key_value::{Error, Store};

helper::define_component!(Component);

impl Component {
fn main() -> Result<(), String> {
ensure_matches!(Store::open("forbidden"), Err(Error::AccessDenied));

let store = ensure_ok!(Store::open("default"));

// Ensure nothing set in `bar` key
ensure_ok!(store.delete("bar"));
ensure_matches!(store.exists("bar"), Ok(false));
ensure_matches!(store.get("bar"), Ok(None));
ensure_matches!(store.get_keys().as_deref(), Ok(&[]));

// Set `bar` key
ensure_ok!(store.set("bar", b"baz"));
ensure_matches!(store.exists("bar"), Ok(true));
ensure_matches!(store.get("bar"), Ok(Some(baz)) if baz == b"baz");
ensure_matches!(store.get_keys().as_deref(), Ok(&[ref bar]) if bar == "bar");

// Override `bar` key
ensure_ok!(store.set("bar", b"wow"));
ensure_matches!(store.exists("bar"), Ok(true));
ensure_matches!(store.get("bar"), Ok(Some(wow)) if wow == b"wow");
ensure_matches!(store.get_keys().as_deref(), Ok(&[ref bar]) if bar == "bar");

// Set another key
ensure_ok!(store.set("qux", b"yay"));
ensure_matches!(store.get_keys().as_deref(), Ok(c) if c.len() == 2 && c.contains(&"bar".into()) && c.contains(&"qux".into()));

// Delete everything
ensure_ok!(store.delete("bar"));
ensure_ok!(store.delete("bar"));
ensure_ok!(store.delete("qux"));
ensure_matches!(store.exists("bar"), Ok(false));
ensure_matches!(store.get("qux"), Ok(None));
ensure_matches!(store.get_keys().as_deref(), Ok(&[]));

Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error::AccessDenied
13 changes: 13 additions & 0 deletions tests/runtime-tests/tests/key-value-no-permission/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
spin_manifest_version = 2

[application]
name = "key-value"
authors = ["Fermyon Engineering <[email protected]>"]
version = "0.1.0"

[[trigger.http]]
route = "/"
component = "test"

[component.test]
source = "{{key-value}}"
14 changes: 14 additions & 0 deletions tests/runtime-tests/tests/key-value/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spin_manifest_version = 2

[application]
name = "key-value"
authors = ["Fermyon Engineering <[email protected]>"]
version = "0.1.0"

[[trigger.http]]
route = "/"
component = "test"

[component.test]
source = "{{key-value}}"
key_value_stores = ["default"]
2 changes: 2 additions & 0 deletions tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ mod runtime_tests {
test!(outbound_redis_no_permission, "outbound-redis-no-permission");
test!(sqlite, "sqlite");
test!(sqlite_no_permission, "sqlite-no-permission");
test!(key_value, "key-value");
test!(key_value_no_permission, "key-value-no-permission");

fn run(name: &str) {
let spin_binary_path = env!("CARGO_BIN_EXE_spin").into();
Expand Down
5 changes: 0 additions & 5 deletions tests/spinup_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ mod spinup_tests {
testcases::key_value_works(CONTROLLER).await
}

#[tokio::test]
async fn key_value_validation_works() {
testcases::key_value_validation_works(CONTROLLER).await
}

#[tokio::test]
async fn http_go_works() {
testcases::http_go_works(CONTROLLER).await
Expand Down
2 changes: 0 additions & 2 deletions tests/testcases/key-value-undefined-store/.cargo/config.toml

This file was deleted.

Loading

0 comments on commit 45987c0

Please sign in to comment.