diff --git a/CHANGELOG.md b/CHANGELOG.md index 30bad5914a..df118b6ed0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ # UNRELEASED +### feat!: `dfx info pocketic-config-port` + +Due to the incompatibility between the APIs on the replica port and the PocketIC port, `dfx info replica-port` +no longer works with PocketIC, and the PocketIC port is provided by a new command, `dfx info pocketic-config-port`. + ### test: adds playwright test for svelte `dfx new` project The first of a suite of baseline tests to automate testing starter projects. Makes sure they are compatible with other dfx or asset canister changes. diff --git a/docs/cli-reference/dfx-info.mdx b/docs/cli-reference/dfx-info.mdx index 6ff0b43215..10ce99290b 100644 --- a/docs/cli-reference/dfx-info.mdx +++ b/docs/cli-reference/dfx-info.mdx @@ -14,14 +14,15 @@ dfx info [type] [flag] These are the types of information that the `dfx info` command can display. -| Information | Description | -|--------------------|--------------------------------------------------------------------------------------------------------------------| -| candid-ui-url | The URL of the Candid UI canister. | -| networks-json-path | Path to network definition file networks.json. | -| replica-port | The listening port of the replica. | -| replica-rev | The revision of the bundled replica. | -| security-policy | Show the headers that gets applied to assets in .ic-assets.json5 if "security_policy" is "standard" or "hardened". | -| webserver-port | The local webserver port. | +| Information | Description | +|----------------------|--------------------------------------------------------------------------------------------------------------------| +| candid-ui-url | The URL of the Candid UI canister. | +| networks-json-path | Path to network definition file networks.json. | +| replica-port | The listening port of the replica. | +| pocketic-config-port | The listening port of PocketIC. | +| replica-rev | The revision of the bundled replica. | +| security-policy | Show the headers that gets applied to assets in .ic-assets.json5 if "security_policy" is "standard" or "hardened". | +| webserver-port | The local webserver port. | ## Options diff --git a/e2e/tests-dfx/create.bash b/e2e/tests-dfx/create.bash index fbbe866d4d..30976902e7 100644 --- a/e2e/tests-dfx/create.bash +++ b/e2e/tests-dfx/create.bash @@ -416,7 +416,7 @@ function textual_decode() { # base64 encode the actual canister id CANISTER_ID_BASE64="$(textual_decode "${CANISTER_ID}" | xxd -r -p | base64)" # fetch topology from PocketIC server - TOPOLOGY="$(curl "http://127.0.0.1:$(dfx info replica-port)/instances/0/read/topology")" + TOPOLOGY="$(curl "http://127.0.0.1:$(dfx info pocketic-config-port)/instances/0/read/topology")" echo "${TOPOLOGY}" # find application subnet id in the topology for subnet_id in $(echo "${TOPOLOGY}" | jq '.subnet_configs | keys[]') diff --git a/e2e/tests-dfx/info.bash b/e2e/tests-dfx/info.bash index cd60d1ed08..d6307dddf3 100644 --- a/e2e/tests-dfx/info.bash +++ b/e2e/tests-dfx/info.bash @@ -15,15 +15,22 @@ teardown() { } @test "displays the replica port" { - assert_command_fail dfx info replica-port - assert_contains "No replica port found" - - dfx_start - assert_command dfx info replica-port if [[ "$USE_POCKETIC" ]] then + assert_command_fail dfx info pocketic-config-port + assert_contains "No PocketIC port found" + dfx_start + assert_command_fail dfx info replica-port + assert_contains "The running server is PocketIC" + assert_command dfx info pocketic-config-port assert_eq "$(get_pocketic_port)" else + assert_command_fail dfx info replica-port + assert_contains "No replica port found" + dfx_start + assert_command_fail dfx info pocketic-config-port + assert_contains "The running server is a native replica" + assert_command dfx info replica-port assert_eq "$(get_replica_port)" fi } diff --git a/e2e/tests-dfx/start.bash b/e2e/tests-dfx/start.bash index 9874305fd1..c86a949cb5 100644 --- a/e2e/tests-dfx/start.bash +++ b/e2e/tests-dfx/start.bash @@ -283,8 +283,11 @@ teardown() { jq ".local.replica.port=$replica_port" "$E2E_NETWORKS_JSON" | sponge "$E2E_NETWORKS_JSON" dfx_start - - assert_command dfx info replica-port + if [[ "$USE_POCKETIC" ]]; then + assert_command dfx info pocketic-config-port + else + assert_command dfx info replica-port + fi assert_eq "$replica_port" } diff --git a/src/dfx-core/src/config/model/local_server_descriptor.rs b/src/dfx-core/src/config/model/local_server_descriptor.rs index e2a6f9af62..f43a83400a 100644 --- a/src/dfx-core/src/config/model/local_server_descriptor.rs +++ b/src/dfx-core/src/config/model/local_server_descriptor.rs @@ -18,6 +18,8 @@ use std::net::SocketAddr; use std::path::{Path, PathBuf}; use time::OffsetDateTime; +use super::replica_config::CachedReplicaConfig; + #[derive(Deserialize, Serialize, Debug)] pub struct NetworkMetadata { pub created: OffsetDateTime, @@ -394,6 +396,12 @@ impl LocalServerDescriptor { None => Ok(None), } } + + pub fn is_pocketic(&self) -> Result, StructuredFileError> { + Ok(self + .effective_config()? + .map(|cfg| matches!(cfg.config, CachedReplicaConfig::PocketIc { .. }))) + } } /// Reads a port number from a file. diff --git a/src/dfx/src/commands/info/mod.rs b/src/dfx/src/commands/info/mod.rs index 02ddbdff38..092f4e0397 100644 --- a/src/dfx/src/commands/info/mod.rs +++ b/src/dfx/src/commands/info/mod.rs @@ -1,3 +1,4 @@ +mod pocketic_config_port; mod replica_port; mod webserver_port; use crate::commands::info::{replica_port::get_replica_port, webserver_port::get_webserver_port}; @@ -10,6 +11,7 @@ use crate::Environment; use anyhow::{bail, Context}; use clap::{Parser, Subcommand}; use dfx_core::config::model::dfinity::NetworksConfig; +use pocketic_config_port::get_pocketic_config_port; #[derive(Subcommand, Clone, Debug)] enum InfoType { @@ -17,14 +19,16 @@ enum InfoType { CandidUiUrl, /// Show the headers that gets applied to assets in .ic-assets.json5 if "security_policy" is "standard" or "hardened". SecurityPolicy, - /// Show the port of the local replica - ReplicaPort, + /// Show the port of the local IC API/HTTP gateway + WebserverPort, /// Show the revision of the replica shipped with this dfx binary ReplicaRev, - /// Show the port of the webserver - WebserverPort, /// Show the path to network configuration file NetworksJsonPath, + /// Show the port the replica is using, if it is running + ReplicaPort, + /// Show the port that PocketIC is using, if it is running + PocketicConfigPort, } #[derive(Parser)] @@ -54,6 +58,7 @@ pub fn exec(env: &dyn Environment, opts: InfoOpts) -> DfxResult { ic_asset::security_policy::SecurityPolicy::Standard.to_json5_str() } InfoType::ReplicaPort => get_replica_port(env)?, + InfoType::PocketicConfigPort => get_pocketic_config_port(env)?, InfoType::ReplicaRev => info::replica_rev().to_string(), InfoType::WebserverPort => get_webserver_port(env)?, InfoType::NetworksJsonPath => NetworksConfig::new()? diff --git a/src/dfx/src/commands/info/pocketic_config_port.rs b/src/dfx/src/commands/info/pocketic_config_port.rs new file mode 100644 index 0000000000..74322a46ef --- /dev/null +++ b/src/dfx/src/commands/info/pocketic_config_port.rs @@ -0,0 +1,26 @@ +use anyhow::bail; +use dfx_core::network::provider::{create_network_descriptor, LocalBindDetermination}; + +use crate::lib::{environment::Environment, error::DfxResult}; + +pub(crate) fn get_pocketic_config_port(env: &dyn Environment) -> DfxResult { + let network_descriptor = create_network_descriptor( + env.get_config()?, + env.get_networks_config(), + None, + None, + LocalBindDetermination::AsConfigured, + )?; + let local = network_descriptor.local_server_descriptor()?; + match local.is_pocketic()? { + Some(true) => {} + Some(false) => bail!("The running server is a native replica, not PocketIC"), + None => bail!("No PocketIC port found"), + } + let logger = None; + if let Some(port) = local.get_running_pocketic_port(logger)? { + Ok(format!("{}", port)) + } else { + bail!("No PocketIC port found"); + } +} diff --git a/src/dfx/src/commands/info/replica_port.rs b/src/dfx/src/commands/info/replica_port.rs index 8dc4ebd149..2e441e0c30 100644 --- a/src/dfx/src/commands/info/replica_port.rs +++ b/src/dfx/src/commands/info/replica_port.rs @@ -11,12 +11,14 @@ pub(crate) fn get_replica_port(env: &dyn Environment) -> DfxResult { None, LocalBindDetermination::AsConfigured, )?; - + let local = network_descriptor.local_server_descriptor()?; + match local.is_pocketic()? { + Some(false) => {} + Some(true) => bail!("The running server is PocketIC, not a native replica"), + None => bail!("No replica port found"), + } let logger = None; - if let Some(port) = network_descriptor - .local_server_descriptor()? - .get_running_replica_port(logger)? - { + if let Some(port) = local.get_running_replica_port(logger)? { Ok(format!("{}", port)) } else { bail!("No replica port found");