Skip to content
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

[E2E] change node url backend config #2101

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions crates/e2e/macro/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// limitations under the License.

use crate::{
config::Backend,
config::{
Backend,
Node,
},
ir,
};
use derive_more::From;
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -109,9 +110,9 @@ impl InkE2ETest {
fn build_full_client(
environment: &syn::Path,
contracts: TokenStream2,
node_url: Option<String>,
node_config: Node,
) -> TokenStream2 {
match node_url {
match node_config.url() {
Some(url) => {
quote! {
let rpc = ::ink_e2e::RpcClient::from_url(#url)
Expand Down
113 changes: 91 additions & 22 deletions crates/e2e/macro/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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<String> {
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"))]
Expand Down Expand Up @@ -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<String>,
}

impl E2EConfig {
Expand All @@ -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<String> {
std::env::var("CONTRACTS_NODE_URL")
.ok()
.or_else(|| self.node_url.clone())
}
}

#[cfg(test)]
Expand All @@ -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();
Expand All @@ -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)),
};
Expand All @@ -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"),
}
}
}
Loading