Skip to content

Commit

Permalink
feat: make dfx info replica-port pocketic-incompatible (#4056)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity authored Jan 8, 2025
1 parent 90d18e9 commit 72f0cde
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 9 additions & 8 deletions docs/cli-reference/dfx-info.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/create.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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[]')
Expand Down
17 changes: 12 additions & 5 deletions e2e/tests-dfx/info.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions e2e/tests-dfx/start.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
8 changes: 8 additions & 0 deletions src/dfx-core/src/config/model/local_server_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -394,6 +396,12 @@ impl LocalServerDescriptor {
None => Ok(None),
}
}

pub fn is_pocketic(&self) -> Result<Option<bool>, StructuredFileError> {
Ok(self
.effective_config()?
.map(|cfg| matches!(cfg.config, CachedReplicaConfig::PocketIc { .. })))
}
}

/// Reads a port number from a file.
Expand Down
13 changes: 9 additions & 4 deletions src/dfx/src/commands/info/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -10,21 +11,24 @@ 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 {
/// Show the URL of the Candid UI canister
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)]
Expand Down Expand Up @@ -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()?
Expand Down
26 changes: 26 additions & 0 deletions src/dfx/src/commands/info/pocketic_config_port.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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");
}
}
12 changes: 7 additions & 5 deletions src/dfx/src/commands/info/replica_port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ pub(crate) fn get_replica_port(env: &dyn Environment) -> DfxResult<String> {
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");
Expand Down

0 comments on commit 72f0cde

Please sign in to comment.