Skip to content

Commit

Permalink
Merge pull request #2686 from lann/update-test-env
Browse files Browse the repository at this point in the history
factors: Update runtime config and tests
  • Loading branch information
rylev authored Jul 29, 2024
2 parents b7af6d9 + a10730c commit de120b6
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 226 deletions.
2 changes: 0 additions & 2 deletions 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 crates/core/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async fn run_test(
}]
}))?;
let app = App::new("test-app", locked);
let configured_app = factors.configure_app(app, ())?;
let configured_app = factors.configure_app(app, Default::default())?;
let mut builders = factors.prepare(&configured_app, "test-component")?;
// FIXME: it is unfortunate that we have to unwrap here...
builders.wasi.as_mut().unwrap().args(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ async fn default_key_value_works() -> anyhow::Result<()> {
let factors = TestFactors {
key_value: KeyValueFactor::new(test_resolver),
};
let env = TestEnvironment::default_manifest_extend(toml! {
let env = TestEnvironment::new(factors).extend_manifest(toml! {
[component.test-component]
source = "does-not-exist.wasm"
key_value_stores = ["default"]
});
let state = env.build_instance_state(factors, ()).await?;
let state = env.build_instance_state().await?;

assert_eq!(
state.key_value.allowed_stores(),
Expand All @@ -67,14 +67,14 @@ async fn run_test_with_config_and_stores_for_label(
key_value: KeyValueFactor::new(test_resolver),
};
let labels_clone = labels.clone();
let env = TestEnvironment::default_manifest_extend(toml! {
[component.test-component]
source = "does-not-exist.wasm"
key_value_stores = labels_clone
});
let state = env
.build_instance_state(factors, TomlConfig(runtime_config))
.await?;
let env = TestEnvironment::new(factors)
.extend_manifest(toml! {
[component.test-component]
source = "does-not-exist.wasm"
key_value_stores = labels_clone
})
.runtime_config(TomlConfig(runtime_config))?;
let state = env.build_instance_state().await?;
assert_eq!(
labels,
state.key_value.allowed_stores().iter().collect::<Vec<_>>()
Expand Down Expand Up @@ -192,29 +192,28 @@ async fn misconfigured_spin_key_value_fails() -> anyhow::Result<()> {
#[tokio::test]
async fn multiple_custom_key_value_uses_first_store() -> anyhow::Result<()> {
let tmp_dir = tempdir::TempDir::new("example")?;
let runtime_config = toml::toml! {
[key_value_store.custom]
type = "spin"
path = "custom.db"

[key_value_store.custom]
type = "redis"
url = "redis://localhost:6379"
};
let mut test_resolver = DelegatingRuntimeConfigResolver::new();
test_resolver.add_store_type(RedisKeyValueStore)?;
test_resolver.add_store_type(SpinKeyValueStore::new(tmp_dir.path().to_owned()))?;
let factors = TestFactors {
key_value: KeyValueFactor::new(test_resolver),
};
let env = TestEnvironment::default_manifest_extend(toml! {
[component.test-component]
source = "does-not-exist.wasm"
key_value_stores = ["custom"]
});
let state = env
.build_instance_state(factors, TomlConfig(Some(runtime_config)))
.await?;
let env = TestEnvironment::new(factors)
.extend_manifest(toml! {
[component.test-component]
source = "does-not-exist.wasm"
key_value_stores = ["custom"]
})
.runtime_config(TomlConfig(Some(toml::toml! {
[key_value_store.custom]
type = "spin"
path = "custom.db"

[key_value_store.custom]
type = "redis"
url = "redis://localhost:6379"
})))?;
let state = env.build_instance_state().await?;

assert_eq!(
state.key_value.allowed_stores(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ async fn llm_works() -> anyhow::Result<()> {
}) as _
}),
};

let env = TestEnvironment::default_manifest_extend(toml! {
let env = TestEnvironment::new(factors).extend_manifest(toml! {
[component.test-component]
source = "does-not-exist.wasm"
ai_models = ["llama2-chat"]
});
let mut state = env.build_instance_state(factors, ()).await?;
let mut state = env.build_instance_state().await?;

assert_eq!(
&*state.llm.allowed_models,
&["llama2-chat".to_owned()]
Expand Down
1 change: 0 additions & 1 deletion crates/factor-outbound-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = { workspace = true }
anyhow = "1.0"
http = "1.1.0"
spin-factor-outbound-networking = { path = "../factor-outbound-networking" }
spin-factor-wasi = { path = "../factor-wasi" }
spin-factors = { path = "../factors" }
spin-world = { path = "../world" }
tracing = { workspace = true }
Expand Down
19 changes: 6 additions & 13 deletions crates/factor-outbound-http/tests/factor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use http::Request;
use spin_factor_outbound_http::OutboundHttpFactor;
use spin_factor_outbound_networking::OutboundNetworkingFactor;
use spin_factor_variables::spin_cli::VariablesFactor;
use spin_factor_wasi::{DummyFilesMounter, WasiFactor};
use spin_factors::{anyhow, RuntimeFactors};
use spin_factors_test::{toml, TestEnvironment};
use wasmtime_wasi_http::{
Expand All @@ -14,30 +13,24 @@ use wasmtime_wasi_http::{

#[derive(RuntimeFactors)]
struct TestFactors {
wasi: WasiFactor,
variables: VariablesFactor,
networking: OutboundNetworkingFactor,
http: OutboundHttpFactor,
}

fn test_env() -> TestEnvironment {
TestEnvironment::default_manifest_extend(toml! {
[component.test-component]
source = "does-not-exist.wasm"
allowed_outbound_hosts = ["http://allowed.test"]
})
}

#[tokio::test]
async fn disallowed_host_fails() -> anyhow::Result<()> {
let factors = TestFactors {
wasi: WasiFactor::new(DummyFilesMounter),
variables: VariablesFactor::default(),
networking: OutboundNetworkingFactor,
http: OutboundHttpFactor,
};
let env = test_env();
let mut state = env.build_instance_state(factors, ()).await?;
let env = TestEnvironment::new(factors).extend_manifest(toml! {
[component.test-component]
source = "does-not-exist.wasm"
allowed_outbound_hosts = ["http://allowed.test"]
});
let mut state = env.build_instance_state().await?;
let mut wasi_http = OutboundHttpFactor::get_wasi_http_impl(&mut state).unwrap();

let req = Request::get("https://denied.test").body(Default::default())?;
Expand Down
32 changes: 12 additions & 20 deletions crates/factor-outbound-networking/tests/factor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,19 @@ struct TestFactors {
networking: OutboundNetworkingFactor,
}

fn test_env() -> TestEnvironment {
TestEnvironment::default_manifest_extend(toml! {
[component.test-component]
source = "does-not-exist.wasm"
allowed_outbound_hosts = ["*://192.0.2.1:12345"]
})
}

#[tokio::test]
async fn configures_wasi_socket_addr_check() -> anyhow::Result<()> {
let factors = TestFactors {
wasi: WasiFactor::new(DummyFilesMounter),
variables: VariablesFactor::default(),
networking: OutboundNetworkingFactor,
};

let env = test_env();
let mut state = env.build_instance_state(factors, ()).await?;
let env = TestEnvironment::new(factors).extend_manifest(toml! {
[component.test-component]
source = "does-not-exist.wasm"
allowed_outbound_hosts = ["*://192.0.2.1:12345"]
});
let mut state = env.build_instance_state().await?;
let mut wasi = WasiFactor::get_wasi_impl(&mut state).unwrap();

let network_resource = wasi.instance_network()?;
Expand Down Expand Up @@ -61,14 +56,11 @@ async fn wasi_factor_is_optional() -> anyhow::Result<()> {
variables: VariablesFactor,
networking: OutboundNetworkingFactor,
}
TestEnvironment::default()
.build_instance_state(
WithoutWasi {
variables: VariablesFactor::default(),
networking: OutboundNetworkingFactor,
},
(),
)
.await?;
TestEnvironment::new(WithoutWasi {
variables: VariablesFactor::default(),
networking: OutboundNetworkingFactor,
})
.build_instance_state()
.await?;
Ok(())
}
31 changes: 11 additions & 20 deletions crates/factor-outbound-pg/tests/factor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{bail, Result};
use spin_factor_outbound_networking::OutboundNetworkingFactor;
use spin_factor_outbound_pg::client::Client;
use spin_factor_outbound_pg::OutboundPgFactor;
use spin_factor_variables::spin_cli::{StaticVariables, VariablesFactor};
use spin_factor_variables::spin_cli::VariablesFactor;
use spin_factors::{anyhow, RuntimeFactors};
use spin_factors_test::{toml, TestEnvironment};
use spin_world::async_trait;
Expand All @@ -18,18 +18,16 @@ struct TestFactors {
pg: OutboundPgFactor<MockClient>,
}

fn factors() -> Result<TestFactors> {
let mut f = TestFactors {
fn factors() -> TestFactors {
TestFactors {
variables: VariablesFactor::default(),
networking: OutboundNetworkingFactor,
pg: OutboundPgFactor::<MockClient>::new(),
};
f.variables.add_provider_resolver(StaticVariables)?;
Ok(f)
}
}

fn test_env() -> TestEnvironment {
TestEnvironment::default_manifest_extend(toml! {
fn test_env() -> TestEnvironment<TestFactors> {
TestEnvironment::new(factors()).extend_manifest(toml! {
[component.test-component]
source = "does-not-exist.wasm"
allowed_outbound_hosts = ["postgres://*:*"]
Expand All @@ -38,12 +36,11 @@ fn test_env() -> TestEnvironment {

#[tokio::test]
async fn disallowed_host_fails() -> anyhow::Result<()> {
let factors = factors()?;
let env = TestEnvironment::default_manifest_extend(toml! {
let env = TestEnvironment::new(factors()).extend_manifest(toml! {
[component.test-component]
source = "does-not-exist.wasm"
});
let mut state = env.build_instance_state(factors, ()).await?;
let mut state = env.build_instance_state().await?;

let res = state
.pg
Expand All @@ -59,9 +56,7 @@ async fn disallowed_host_fails() -> anyhow::Result<()> {

#[tokio::test]
async fn allowed_host_succeeds() -> anyhow::Result<()> {
let factors = factors()?;
let env = test_env();
let mut state = env.build_instance_state(factors, ()).await?;
let mut state = test_env().build_instance_state().await?;

let res = state
.pg
Expand All @@ -76,9 +71,7 @@ async fn allowed_host_succeeds() -> anyhow::Result<()> {

#[tokio::test]
async fn exercise_execute() -> anyhow::Result<()> {
let factors = factors()?;
let env = test_env();
let mut state = env.build_instance_state(factors, ()).await?;
let mut state = test_env().build_instance_state().await?;

let connection = state
.pg
Expand All @@ -95,9 +88,7 @@ async fn exercise_execute() -> anyhow::Result<()> {

#[tokio::test]
async fn exercise_query() -> anyhow::Result<()> {
let factors = factors()?;
let env = test_env();
let mut state = env.build_instance_state(factors, ()).await?;
let mut state = test_env().build_instance_state().await?;

let connection = state
.pg
Expand Down
1 change: 0 additions & 1 deletion crates/factor-outbound-redis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ redis = { version = "0.21", features = ["tokio-comp", "tokio-native-tls-comp", "
spin-factors-test = { path = "../factors-test" }
tokio = { version = "1", features = ["macros", "rt"] }
spin-factor-variables = { path = "../factor-variables" }
spin-factor-wasi = { path = "../factor-wasi" }

# wasmtime-wasi-http = { workspace = true }
[lints]
Expand Down
39 changes: 13 additions & 26 deletions crates/factor-outbound-redis/tests/factor_test.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
use anyhow::bail;
use spin_factor_outbound_networking::OutboundNetworkingFactor;
use spin_factor_outbound_redis::OutboundRedisFactor;
use spin_factor_variables::spin_cli::{StaticVariables, VariablesFactor};
use spin_factor_wasi::{DummyFilesMounter, WasiFactor};
use spin_factor_variables::spin_cli::VariablesFactor;
use spin_factors::{anyhow, RuntimeFactors};
use spin_factors_test::{toml, TestEnvironment};
use spin_world::v2::redis::{Error, HostConnection};

#[derive(RuntimeFactors)]
struct TestFactors {
wasi: WasiFactor,
variables: VariablesFactor,
networking: OutboundNetworkingFactor,
redis: OutboundRedisFactor,
}

fn get_test_factors() -> TestFactors {
TestFactors {
wasi: WasiFactor::new(DummyFilesMounter),
#[tokio::test]
async fn no_outbound_hosts_fails() -> anyhow::Result<()> {
let factors = TestFactors {
variables: VariablesFactor::default(),
networking: OutboundNetworkingFactor,
redis: OutboundRedisFactor,
}
}

#[tokio::test]
async fn no_outbound_hosts_fails() -> anyhow::Result<()> {
let mut factors = get_test_factors();

factors.variables.add_provider_resolver(StaticVariables)?;

let env = TestEnvironment {
manifest: toml! {
spin_manifest_version = 2
application.name = "test-app"
[[trigger.test]]

[component.test-component]
source = "does-not-exist.wasm"
},
..Default::default()
};
let mut state = env.build_instance_state(factors, ()).await?;
let env = TestEnvironment::new(factors).extend_manifest(toml! {
spin_manifest_version = 2
application.name = "test-app"
[[trigger.test]]

[component.test-component]
source = "does-not-exist.wasm"
});
let mut state = env.build_instance_state().await?;
let connection = state
.redis
.open("redis://redis.test:8080".to_string())
Expand Down
Loading

0 comments on commit de120b6

Please sign in to comment.