diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a44485491..66c19452bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [E2E] Rename `.call` to `.call_builder` ‒ [#2078](https://github.com/paritytech/ink/pull/2078) - Improve syntax for ink! e2e `runtime_only` attribute argument - [#2083](https://github.com/paritytech/ink/pull/2083) - [E2E] Remove `additional_contracts` parameter [#2098](https://github.com/paritytech/ink/pull/2098) +- [E2E] change node url backend config - [#2101](https://github.com/paritytech/ink/pull/2101) ### Fixed - Fix the `StorageVec` type by excluding the `len_cached` field from its type info - [#2052](https://github.com/paritytech/ink/pull/2052) diff --git a/crates/e2e/macro/src/codegen.rs b/crates/e2e/macro/src/codegen.rs index ce128a1281..c0a9bac678 100644 --- a/crates/e2e/macro/src/codegen.rs +++ b/crates/e2e/macro/src/codegen.rs @@ -13,7 +13,10 @@ // limitations under the License. use crate::{ - config::Backend, + config::{ + Backend, + Node, + }, ir, }; use derive_more::From; @@ -56,11 +59,9 @@ impl InkE2ETest { ::ink_e2e::build_root_and_contract_dependencies() }; - let node_url = self.test.config.node_url(); - let client_building = match self.test.config.backend() { - Backend::Full => { - build_full_client(&environment, exec_build_contracts, node_url) + Backend::Node(node_config) => { + build_full_client(&environment, exec_build_contracts, node_config) } #[cfg(any(test, feature = "drink"))] Backend::RuntimeOnly(runtime) => { @@ -109,9 +110,9 @@ impl InkE2ETest { fn build_full_client( environment: &syn::Path, contracts: TokenStream2, - node_url: Option, + node_config: Node, ) -> TokenStream2 { - match node_url { + match node_config.url() { Some(url) => { quote! { let rpc = ::ink_e2e::RpcClient::from_url(#url) diff --git a/crates/e2e/macro/src/config.rs b/crates/e2e/macro/src/config.rs index 2216ec7d69..3e17eb1f16 100644 --- a/crates/e2e/macro/src/config.rs +++ b/crates/e2e/macro/src/config.rs @@ -13,13 +13,12 @@ // limitations under the License. /// The type of the architecture that should be used to run test. -#[derive(Clone, Eq, PartialEq, Debug, Default, darling::FromMeta)] +#[derive(Clone, Eq, PartialEq, Debug, darling::FromMeta)] #[darling(rename_all = "snake_case")] pub enum Backend { /// The standard approach with running dedicated single-node blockchain in a /// background process. - #[default] - Full, + Node(Node), /// The lightweight approach skipping node layer. /// @@ -29,6 +28,38 @@ pub enum Backend { RuntimeOnly(RuntimeOnly), } +impl Default for Backend { + fn default() -> Self { + Backend::Node(Node::Auto) + } +} + +/// Configure whether to automatically spawn a node instance for the test or to use +/// an already running node at the supplied URL. +#[derive(Clone, Eq, PartialEq, Debug, darling::FromMeta)] +pub enum Node { + /// A fresh node instance will be spawned for the lifetime of the test. + #[darling(word)] + Auto, + /// The test will run against an already running node at the supplied URL. + Url(String), +} + +impl Node { + /// The URL to the running node, default value can be overridden with + /// `CONTRACTS_NODE_URL`. + /// + /// Returns `None` if [`Self::Auto`] and `CONTRACTS_NODE_URL` not specified. + pub fn url(&self) -> Option { + std::env::var("CONTRACTS_NODE_URL").ok().or_else(|| { + match self { + Node::Auto => None, + Node::Url(url) => Some(url.clone()), + } + }) + } +} + /// The runtime emulator that should be used within `TestExternalities` (using drink! /// library). #[cfg(any(test, feature = "drink"))] @@ -63,8 +94,6 @@ pub struct E2EConfig { /// The type of the architecture that should be used to run test. #[darling(default)] backend: Backend, - /// The URL to the running node. See [`Self::node_url()`] for more details. - node_url: Option, } impl E2EConfig { @@ -77,15 +106,6 @@ impl E2EConfig { pub fn backend(&self) -> Backend { self.backend.clone() } - - /// The URL to the running node, default value can be overridden with - /// `CONTRACTS_NODE_URL`. If no URL is provided, then a default node instance will - /// be spawned per test. - pub fn node_url(&self) -> Option { - std::env::var("CONTRACTS_NODE_URL") - .ok() - .or_else(|| self.node_url.clone()) - } } #[cfg(test)] @@ -98,11 +118,10 @@ mod tests { use quote::quote; #[test] - fn config_works() { + fn config_works_backend_runtime_only() { let input = quote! { environment = crate::CustomEnvironment, backend(runtime_only), - node_url = "ws://127.0.0.1:8000" }; let config = E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap(); @@ -113,14 +132,10 @@ mod tests { ); assert_eq!(config.backend(), Backend::RuntimeOnly(RuntimeOnly::Default)); - assert_eq!(config.node_url(), Some(String::from("ws://127.0.0.1:8000"))); - - std::env::set_var("CONTRACTS_NODE_URL", "ws://127.0.0.1:9000"); - assert_eq!(config.node_url(), Some(String::from("ws://127.0.0.1:9000"))) } #[test] - fn config_works_with_custom_backend() { + fn config_works_runtime_only_with_custom_backend() { let input = quote! { backend(runtime_only(runtime = ::ink_e2e::MinimalRuntime)), }; @@ -133,6 +148,60 @@ mod tests { syn::parse_quote! { ::ink_e2e::MinimalRuntime } )) ); - assert_eq!(config.node_url(), None) + } + + #[test] + fn config_works_backend_node_default_auto() { + let input = quote! { + backend(node), + }; + let config = + E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap(); + + assert_eq!(config.backend(), Backend::Node(Node::Auto)); + } + + #[test] + fn config_works_backend_node_auto() { + let input = quote! { + backend(node(auto)), + }; + let config = + E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap(); + + match config.backend() { + Backend::Node(node_config) => { + assert_eq!(node_config, Node::Auto); + + std::env::remove_var("CONTRACTS_NODE_URL"); + assert_eq!(node_config.url(), None); + + std::env::set_var("CONTRACTS_NODE_URL", "ws://127.0.0.1:9000"); + assert_eq!(node_config.url(), Some(String::from("ws://127.0.0.1:9000"))); + } + _ => panic!("Expected Backend::Node"), + } + } + + #[test] + fn config_works_backend_node_url() { + let input = quote! { + backend(node(url = "ws://0.0.0.0:9999")), + }; + let config = + E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap(); + + match config.backend() { + Backend::Node(node_config) => { + assert_eq!(node_config, Node::Url("ws://0.0.0.0:9999".to_owned())); + + std::env::remove_var("CONTRACTS_NODE_URL"); + assert_eq!(node_config.url(), Some("ws://0.0.0.0:9999".to_owned())); + + std::env::set_var("CONTRACTS_NODE_URL", "ws://127.0.0.1:9000"); + assert_eq!(node_config.url(), Some(String::from("ws://127.0.0.1:9000"))); + } + _ => panic!("Expected Backend::Node"), + } } }