-
Notifications
You must be signed in to change notification settings - Fork 289
Spin up checks required variables for some Providers
#3265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
itowlson
merged 1 commit into
spinframework:main
from
seun-ja:spin-up-should-check-required-variables-with-dynamic-metadata
Sep 29, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use async_trait::async_trait; | ||
| use spin_expressions::{provider::ProviderVariableKind, Key, Provider, ProviderResolver}; | ||
| use spin_locked_app::Variable; | ||
|
|
||
| #[derive(Default)] | ||
| struct ResolverTester { | ||
| providers: Vec<Box<dyn Provider>>, | ||
| variables: HashMap<String, Variable>, | ||
| } | ||
|
|
||
| impl ResolverTester { | ||
| fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| fn with_provider(mut self, provider: impl Provider + 'static) -> Self { | ||
| self.providers.push(Box::new(provider)); | ||
| self | ||
| } | ||
|
|
||
| fn with_variable(mut self, key: &str, default: Option<&str>) -> Self { | ||
| self.variables.insert( | ||
| key.to_string(), | ||
| Variable { | ||
| description: None, | ||
| default: default.map(ToString::to_string), | ||
| secret: false, | ||
| }, | ||
| ); | ||
| self | ||
| } | ||
|
|
||
| fn make_resolver(self) -> anyhow::Result<ProviderResolver> { | ||
| let mut provider_resolver = ProviderResolver::new(self.variables)?; | ||
|
|
||
| for provider in self.providers { | ||
| provider_resolver.add_provider(provider); | ||
| } | ||
|
|
||
| Ok(provider_resolver) | ||
| } | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_single_static_provider_with_no_key_to_resolve_is_valid() -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(StaticProvider::with_variable( | ||
| "database_host", | ||
| Some("localhost"), | ||
| )) | ||
| .make_resolver()?; | ||
|
|
||
| resolver.ensure_required_variables_resolvable().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_single_static_provider_has_data_for_variable_key_to_resolve_it_succeeds( | ||
| ) -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(StaticProvider::with_variable( | ||
| "database_host", | ||
| Some("localhost"), | ||
| )) | ||
| .with_variable("database_host", None) | ||
| .make_resolver()?; | ||
|
|
||
| resolver.ensure_required_variables_resolvable().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_there_is_a_single_static_provider_and_it_does_not_contain_a_required_variable_then_validation_fails( | ||
| ) -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(StaticProvider::with_variable( | ||
| "database_host", | ||
| Some("localhost"), | ||
| )) | ||
| .with_variable("api_key", None) | ||
| .make_resolver()?; | ||
|
|
||
| assert!(resolver | ||
| .ensure_required_variables_resolvable() | ||
| .await | ||
| .is_err()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_there_is_a_dynamic_provider_then_validation_succeeds_even_without_default_value_in_play( | ||
| ) -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(DynamicProvider) | ||
| .with_variable("api_key", None) | ||
| .make_resolver()?; | ||
|
|
||
| resolver.ensure_required_variables_resolvable().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_there_is_a_dynamic_provider_and_static_provider_but_the_variable_to_be_resolved_is_not_in_play( | ||
| ) -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(DynamicProvider) | ||
| .with_provider(StaticProvider::with_variable( | ||
| "database_host", | ||
| Some("localhost"), | ||
| )) | ||
| .with_variable("api_key", None) | ||
| .make_resolver()?; | ||
|
|
||
| resolver.ensure_required_variables_resolvable().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_there_is_a_dynamic_provider_and_a_static_provider_then_validation_succeeds_even_if_there_is_a_variable_in_play( | ||
| ) -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(DynamicProvider) | ||
| .with_provider(StaticProvider::with_variable( | ||
| "database_host", | ||
| Some("localhost"), | ||
| )) | ||
| .with_variable("api_key", Some("super-secret-key")) | ||
| .make_resolver()?; | ||
|
|
||
| resolver.ensure_required_variables_resolvable().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_there_are_two_static_providers_where_one_has_data_is_valid() -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(StaticProvider::with_variable( | ||
| "database_host", | ||
| Some("localhost"), | ||
| )) | ||
| .with_provider(StaticProvider::with_variable( | ||
| "api_key", | ||
| Some("super-secret-key"), | ||
| )) | ||
| .with_variable("database_host", None) | ||
| .make_resolver()?; | ||
|
|
||
| resolver.ensure_required_variables_resolvable().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| // Ensure that if there are two or more static providers and the first one does not have data for the variable to be resolved, | ||
| // but the second or subsequent one does, then validation still succeeds. | ||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_there_are_two_static_providers_where_first_provider_does_not_have_data_while_second_provider_does( | ||
| ) -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(StaticProvider::with_variable( | ||
| "database_host", | ||
| Some("localhost"), | ||
| )) | ||
| .with_provider(StaticProvider::with_variable( | ||
| "api_key", | ||
| Some("super-secret-key"), | ||
| )) | ||
| .with_variable("api_key", None) | ||
| .make_resolver()?; | ||
|
|
||
| resolver.ensure_required_variables_resolvable().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn if_there_is_two_static_providers_neither_having_data_is_invalid() -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_provider(StaticProvider::with_variable( | ||
| "database_host", | ||
| Some("localhost"), | ||
| )) | ||
| .with_provider(StaticProvider::with_variable( | ||
| "api_key", | ||
| Some("super-secret-key"), | ||
| )) | ||
| .with_variable("hello", None) | ||
| .make_resolver()?; | ||
|
|
||
| assert!(resolver | ||
| .ensure_required_variables_resolvable() | ||
| .await | ||
| .is_err()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn no_provider_data_available_but_variable_default_value_needed_is_invalid( | ||
| ) -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_variable("api_key", None) | ||
| .make_resolver()?; | ||
|
|
||
| assert!(resolver | ||
| .ensure_required_variables_resolvable() | ||
| .await | ||
| .is_err()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread")] | ||
| async fn no_provider_data_available_but_variable_has_default_value_needed_is_valid( | ||
| ) -> anyhow::Result<()> { | ||
| let resolver = ResolverTester::new() | ||
| .with_variable("api_key", Some("super-secret-key")) | ||
| .make_resolver()?; | ||
|
|
||
| resolver.ensure_required_variables_resolvable().await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you previously had a test for "providers don't have the data but there is a default value"? That test is needed. |
||
|
|
||
| #[derive(Debug)] | ||
| struct StaticProvider { | ||
| variables: HashMap<String, Option<String>>, | ||
| } | ||
|
|
||
| impl StaticProvider { | ||
| fn with_variable(key: &str, value: Option<&str>) -> Self { | ||
| Self { | ||
| variables: HashMap::from([(key.into(), value.map(|v| v.into()))]), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl Provider for StaticProvider { | ||
| async fn get(&self, key: &Key) -> anyhow::Result<Option<String>> { | ||
| Ok(self.variables.get(key.as_str()).cloned().flatten()) | ||
| } | ||
|
|
||
| fn kind(&self) -> ProviderVariableKind { | ||
| ProviderVariableKind::Static | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct DynamicProvider; | ||
|
|
||
| #[async_trait] | ||
| impl Provider for DynamicProvider { | ||
| async fn get(&self, _key: &Key) -> anyhow::Result<Option<String>> { | ||
| panic!("validation should never call get for a dynamic provider") | ||
| } | ||
|
|
||
| fn kind(&self) -> ProviderVariableKind { | ||
| ProviderVariableKind::Dynamic | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already a
testsmodule inexpressions/lib.rs- maybe use that instead of creating a new file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually now I look at this I can't figure out how it's getting incorporated. Or where the conditional compilation directive is. Is this some Cargo convention I've not learned about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also just realised it's possible. There are a couple of tests in Spin that are similar. Apparently, if the folder is named "tests", Cargo automatically incorporates it.
The reason I created a new file for it was that I thought separating the tests would be better, as the tests in lib, while similar, test for different purposes. They also have a different boilerplate.