Skip to content
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
25 changes: 20 additions & 5 deletions tests/e2e_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from collections.abc import Iterator, Callable
import logging
import os
import re
Expand All @@ -8,11 +9,13 @@
import subprocess
import sys
import time
from typing import Generator

import bittensor_wallet.keypair
import pytest
from async_substrate_interface.async_substrate import AsyncSubstrateInterface

from .utils import setup_wallet
from .utils import setup_wallet, ExecCommand

LOCALNET_IMAGE_NAME = "ghcr.io/opentensor/subtensor-localnet:devnet-ready"

Expand All @@ -31,7 +34,7 @@ def wait_for_node_start(process, pattern, timestamp: int = None):

# Fixture for setting up and tearing down a localnet.sh chain between tests
@pytest.fixture(scope="function")
def local_chain(request):
def local_chain(request) -> Iterator[AsyncSubstrateInterface]:
"""Determines whether to run the localnet.sh script in a subprocess or a Docker container."""
args = request.param if hasattr(request, "param") else None
params = "" if args is None else f"{args}"
Expand All @@ -58,7 +61,7 @@ def local_chain(request):
yield from legacy_runner(request)


def legacy_runner(request):
def legacy_runner(request) -> Iterator[AsyncSubstrateInterface]:
param = request.param if hasattr(request, "param") else None
# Get the environment variable for the script path
script_path = os.getenv("LOCALNET_SH_PATH")
Expand Down Expand Up @@ -103,7 +106,7 @@ def legacy_runner(request):
process.wait()


def docker_runner(params):
def docker_runner(params) -> Iterator[AsyncSubstrateInterface]:
"""Starts a Docker container before tests and gracefully terminates it after."""

def is_docker_running():
Expand Down Expand Up @@ -211,7 +214,19 @@ def try_start_docker():


@pytest.fixture(scope="function")
def wallet_setup():
def wallet_setup() -> Generator[
Callable[
[str],
tuple[
bittensor_wallet.Keypair,
bittensor_wallet.Wallet,
str,
ExecCommand,
],
],
None,
None,
]:
wallet_paths = []

def _setup_wallet(uri: str):
Expand Down
9 changes: 8 additions & 1 deletion tests/e2e_tests/test_hyperparams_setting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import json

from bittensor_cli.src import HYPERPARAMS
from .utils import turn_off_hyperparam_freeze_window

"""
Verify commands:
Expand All @@ -18,7 +20,12 @@ def test_hyperparams_setting(local_chain, wallet_setup):
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
wallet_path_alice
)

try:
asyncio.run(turn_off_hyperparam_freeze_window(local_chain, wallet_alice))
except ValueError:
print(
"Skipping turning off hyperparams freeze window. This indicates the call does not exist on the chain you are testing."
)
# Register a subnet with sudo as Alice
result = exec_command_alice(
command="subnets",
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e_tests/test_liquidity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
import json
import re

from bittensor_cli.src.bittensor.balances import Balance
from .utils import turn_off_hyperparam_freeze_window

"""
Verify commands:
Expand Down Expand Up @@ -40,6 +42,12 @@ def liquidity_list():
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
wallet_path_alice
)
try:
asyncio.run(turn_off_hyperparam_freeze_window(local_chain, wallet_alice))
except ValueError:
print(
"Skipping turning off hyperparams freeze window. This indicates the call does not exist on the chain you are testing."
)

# Register a subnet with sudo as Alice
result = exec_command_alice(
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e_tests/test_staking_sudo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
import json
import re

from bittensor_cli.src.bittensor.balances import Balance
from .utils import turn_off_hyperparam_freeze_window

"""
Verify commands:
Expand Down Expand Up @@ -45,6 +47,12 @@ def test_staking(local_chain, wallet_setup):
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
wallet_path_alice
)
try:
asyncio.run(turn_off_hyperparam_freeze_window(local_chain, wallet_alice))
except ValueError:
print(
"Skipping turning off hyperparams freeze window. This indicates the call does not exist on the chain you are testing."
)

burn_cost = exec_command_alice(
"subnets",
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e_tests/test_unstaking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from bittensor_cli.src.bittensor.balances import Balance

from btcli.tests.e2e_tests.utils import set_storage_extrinsic
from .utils import set_storage_extrinsic


def test_unstaking(local_chain, wallet_setup):
Expand Down
45 changes: 42 additions & 3 deletions tests/e2e_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import shutil
import subprocess
import sys
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING, Optional, Protocol

from bittensor_wallet import Keypair, Wallet
from click.testing import Result
from packaging.version import parse as parse_version, Version
from typer.testing import CliRunner

Expand All @@ -20,7 +21,19 @@
templates_repo = "templates repository"


def setup_wallet(uri: str):
class ExecCommand(Protocol):
"""Type Protocol for setup_wallet's exec_command fn"""

def __call__(
self,
command: str,
sub_command: str,
extra_args: Optional[list[str]],
inputs: Optional[list[str]],
) -> Result: ...


def setup_wallet(uri: str) -> tuple[Keypair, Wallet, str, ExecCommand]:
keypair = Keypair.create_from_uri(uri)
wallet_path = f"/tmp/btcli-e2e-wallet-{uri.strip('/')}"
wallet = Wallet(path=wallet_path)
Expand All @@ -32,7 +45,7 @@ def exec_command(
command: str,
sub_command: str,
extra_args: Optional[list[str]] = None,
inputs: list[str] = None,
inputs: Optional[list[str]] = None,
):
extra_args = extra_args or []
cli_manager = CLIManager()
Expand Down Expand Up @@ -370,3 +383,29 @@ async def set_storage_extrinsic(
print(":white_heavy_check_mark: [dark_sea_green_3]Success[/dark_sea_green_3]")

return response


async def turn_off_hyperparam_freeze_window(
substrate: "AsyncSubstrateInterface", wallet: Wallet
):
call = await substrate.compose_call(
call_module="Sudo",
call_function="sudo",
call_params={
"call": await substrate.compose_call(
call_module="AdminUtils",
call_function="sudo_set_admin_freeze_window",
call_params={"window": 0},
)
},
)
extrinsic = await substrate.create_signed_extrinsic(
call=call, keypair=wallet.coldkey
)
response = await substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=True,
wait_for_finalization=True,
)

return await response.is_success, await response.error_message
Loading