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

Enhances tests according to changes #45

Merged
merged 10 commits into from
Sep 4, 2024
4 changes: 2 additions & 2 deletions .github/workflows/e2e-subtensor-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ jobs:
run: git checkout testnet

- name: Install Python dependencies
run: python3 -m pip install -r requirements.txt pytest
run: python3 -m pip install -e . pytest

- name: Run all tests
run: |
LOCALNET_SH_PATH="${{ github.workspace }}/subtensor/scripts/localnet.sh" pytest src/tests/e2e_tests -s
LOCALNET_SH_PATH="${{ github.workspace }}/subtensor/scripts/localnet.sh" pytest tests/e2e_tests -s
27 changes: 14 additions & 13 deletions tests/e2e_tests/test_root.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import logging
import time

from bittensor_cli.src.bittensor.balances import Balance

from tests.e2e_tests.utils import setup_wallet
from tests.e2e_tests.utils import setup_wallet, remove_wallets

"""
Verify commands:
Expand Down Expand Up @@ -33,7 +32,7 @@ def test_root_commands(local_chain):
Raises:
AssertionError: If any of the checks or verifications fail
"""
logging.info("Testing Root commands 🧪")
print("Testing Root commands 🧪")

wallet_path_alice = "//Alice"
wallet_path_bob = "//Bob"
Expand Down Expand Up @@ -80,16 +79,16 @@ def test_root_commands(local_chain):

# Capture root information and assert correct values
# First two rows are labels, entries start from the third row
bob_root_info = check_root_list.stdout.splitlines()[2].split()
bob_root_info = check_root_list.stdout.splitlines()[3].split()

# UID: First uid is always 0
assert bob_root_info[0] == "0"

# ADDRESS: Assert correct hotkey is registered
assert bob_root_info[1] == wallet_bob.hotkey.ss58_address
assert bob_root_info[3] == wallet_bob.hotkey.ss58_address

# SENATOR: Since there are senator slots empty, Bob is assigned senator status
assert bob_root_info[3] == "Yes"
assert bob_root_info[7] == "Yes"

# List all root delegates in the network
check_delegates = exec_command_alice(
Expand All @@ -105,7 +104,7 @@ def test_root_commands(local_chain):

# Capture delegate information and assert correct values
# First row are labels, entries start from the second row
bob_delegate_info = check_delegates.stdout.splitlines()[1].split()
bob_delegate_info = check_delegates.stdout.splitlines()[3].split()

# INDEX: First uid is always 0
assert bob_delegate_info[0] == "0"
Expand Down Expand Up @@ -166,7 +165,7 @@ def test_root_commands(local_chain):
],
)
# Capture delegate information after setting take
bob_delegate_info = check_delegates.stdout.splitlines()[1].split()
bob_delegate_info = check_delegates.stdout.splitlines()[3].split()

# Take percentage: This should be 18% by default
take_percentage = float(bob_delegate_info[7].strip("%")) / 100
Expand Down Expand Up @@ -210,15 +209,13 @@ def test_root_commands(local_chain):
],
)
# First row are headers, records start from second row
alice_delegates_info = alice_delegates.stdout.splitlines()[1].split()
alice_delegates_info = alice_delegates.stdout.splitlines()[4].split()

# WALLET: Wallet name of Alice
assert alice_delegates_info[0] == wallet_alice.name

# SS58: address of the Bob's hotkey (Alice has staked to Bob)
assert wallet_bob.hotkey.ss58_address.startswith(
alice_delegates_info[1].replace("...", "")
)
assert wallet_bob.hotkey.ss58_address == alice_delegates_info[1]

# Delegation: This should be 10 as Alice delegated 10 TAO to Bob
delegate_stake = Balance.from_tao(float(alice_delegates_info[2].strip("τ")))
Expand All @@ -231,7 +228,7 @@ def test_root_commands(local_chain):
# Total delegated Tao: This is listed at the bottom of the information
# Since Alice has only delegated to Bob, total should be 10 TAO
total_delegated_tao = Balance.from_tao(
float(alice_delegates.stdout.splitlines()[3].split()[3].strip("τ"))
float(alice_delegates.stdout.splitlines()[7].split()[3].strip("τ"))
)
assert total_delegated_tao == Balance.from_tao(10)

Expand Down Expand Up @@ -261,3 +258,7 @@ def test_root_commands(local_chain):
],
)
assert "✅ Finalized" in undelegate_alice.stdout

print("✅ Passed Root commands")
remove_wallets(wallet_path_alice)
remove_wallets(wallet_path_bob)
13 changes: 7 additions & 6 deletions tests/e2e_tests/test_staking_sudo.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import re
import logging
import time

from bittensor_cli.src.bittensor.balances import Balance
from tests.e2e_tests.utils import setup_wallet
from tests.e2e_tests.utils import setup_wallet, remove_wallets

"""
Verify commands:
Expand Down Expand Up @@ -33,7 +32,7 @@ def test_staking(local_chain):
Raises:
AssertionError: If any of the checks or verifications fail
"""
logging.info("Testing staking and sudo commands🧪")
print("Testing staking and sudo commands🧪")
netuid = 1
wallet_path_alice = "//Alice"

Expand Down Expand Up @@ -123,7 +122,7 @@ def test_staking(local_chain):
cleaned_stake = [
re.sub(r"\s+", " ", line) for line in show_stake.stdout.splitlines()
]
stake_added = cleaned_stake[3].split()[4].strip("τ")
stake_added = cleaned_stake[4].split()[4].strip("τ")
assert Balance.from_tao(100) == Balance.from_tao(float(stake_added))

# TODO: Ask nucleus the rate limit and wait epoch
Expand Down Expand Up @@ -169,7 +168,7 @@ def test_staking(local_chain):

# Parse all hyperparameters and single out max_burn in TAO
all_hyperparams = hyperparams.stdout.splitlines()
max_burn_tao = all_hyperparams[18].split()[2]
max_burn_tao = all_hyperparams[23].split()[2]

# Assert max_burn is 100 TAO from default
assert Balance.from_tao(float(max_burn_tao.strip("τ"))) == Balance.from_tao(100)
Expand Down Expand Up @@ -218,9 +217,11 @@ def test_staking(local_chain):

# Parse updated hyperparameters
all_updated_hyperparams = updated_hyperparams.stdout.splitlines()
updated_max_burn_tao = all_updated_hyperparams[18].split()[2]
updated_max_burn_tao = all_updated_hyperparams[23].split()[2]

# Assert max_burn is now 10 TAO
assert Balance.from_tao(float(updated_max_burn_tao.strip("τ"))) == Balance.from_tao(
10
)
print("✅ Passed staking and sudo commands")
remove_wallets(wallet_path_alice)
39 changes: 17 additions & 22 deletions tests/e2e_tests/test_wallet_creations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logging
import os
import re
import time
from typing import Dict, Optional, Tuple

from tests.e2e_tests.utils import setup_wallet
from tests.e2e_tests.utils import setup_wallet, remove_wallets, get_path_from_uri

"""
Verify commands:
Expand Down Expand Up @@ -194,7 +193,7 @@ def test_wallet_creations():
# Command 1: <btcli w create>
# -----------------------------

logging.info("Testing wallet create command 🧪")
print("Testing wallet create command 🧪")
# Create a new wallet (coldkey + hotkey)
exec_command(
command="wallet",
Expand All @@ -207,8 +206,6 @@ def test_wallet_creations():
"--hotkey",
"new_hotkey",
"--no-use-password",
"--overwrite-coldkey",
"--overwrite-hotkey",
"--n-words",
"12",
],
Expand All @@ -235,14 +232,13 @@ def test_wallet_creations():
# Command 2: <btcli w new_coldkey>
# -----------------------------

logging.info("Testing wallet new_coldkey command 🧪")
print("Testing wallet new_coldkey command 🧪")

# Create a new wallet (coldkey)
exec_command(
"wallet",
sub_command="new-coldkey",
extra_args=[
"--overwrite-coldkey",
"--wallet-name",
"new_coldkey",
"--wallet-path",
Expand All @@ -269,7 +265,7 @@ def test_wallet_creations():
# Command 3: <btcli w new_hotkey>
# -----------------------------

logging.info("Testing wallet new_hotkey command 🧪")
print("Testing wallet new_hotkey command 🧪")
# Create a new hotkey for new_coldkey wallet
result = exec_command(
"wallet",
Expand All @@ -279,7 +275,6 @@ def test_wallet_creations():
"new_coldkey",
"--hotkey",
"new_hotkey",
"--overwrite-hotkey",
"--wallet-path",
wallet_path,
"--n-words",
Expand All @@ -301,6 +296,7 @@ def test_wallet_creations():
wallet_path, "new_coldkey", hotkey_name="new_hotkey"
)
assert wallet_status, message
remove_wallets(wallet_path)


def test_wallet_regen():
Expand Down Expand Up @@ -331,8 +327,6 @@ def test_wallet_regen():
"--hotkey",
"new_hotkey",
"--no-use-password",
"--overwrite-coldkey",
"--overwrite-hotkey",
"--n-words",
"12",
],
Expand All @@ -351,7 +345,7 @@ def test_wallet_regen():
# -----------------------------
# Command 1: <btcli w regen_coldkey>
# -----------------------------
logging.info("Testing wallet regen_coldkey command 🧪")
print("Testing wallet regen_coldkey command 🧪")
coldkey_path = os.path.join(wallet_path, "new_wallet", "coldkey")
initial_coldkey_mod_time = os.path.getmtime(coldkey_path)

Expand All @@ -365,11 +359,11 @@ def test_wallet_regen():
"new_hotkey",
"--wallet-path",
wallet_path,
"--overwrite-coldkey",
"--mnemonic",
mnemonics["coldkey"],
"--no-use-password",
],
inputs=["y", "y"],
)

# Wait a bit to ensure file system updates modification time
Expand All @@ -380,13 +374,13 @@ def test_wallet_regen():
assert (
initial_coldkey_mod_time != new_coldkey_mod_time
), "Coldkey file was not regenerated as expected"
logging.info("Passed wallet regen_coldkey command ✅")
print("Passed wallet regen_coldkey command ✅")

# -----------------------------
# Command 2: <btcli w regen_coldkeypub>
# -----------------------------

logging.info("Testing wallet regen_coldkeypub command 🧪")
print("Testing wallet regen_coldkeypub command 🧪")
coldkeypub_path = os.path.join(wallet_path, "new_wallet", "coldkeypub.txt")
initial_coldkeypub_mod_time = os.path.getmtime(coldkeypub_path)

Expand All @@ -408,8 +402,8 @@ def test_wallet_regen():
wallet_path,
"--ss58-address",
ss58_address,
"--overwrite-coldkeypub",
],
inputs=["y"],
)

# Wait a bit to ensure file system updates modification time
Expand All @@ -420,17 +414,17 @@ def test_wallet_regen():
assert (
initial_coldkeypub_mod_time != new_coldkeypub_mod_time
), "Coldkeypub file was not regenerated as expected"
logging.info("Passed wallet regen_coldkeypub command ✅")
print("Passed wallet regen_coldkeypub command ✅")

# -----------------------------
# Command 3: <btcli w regen_hotkey>
# -----------------------------

logging.info("Testing wallet regen_hotkey command 🧪")
print("Testing wallet regen_hotkey command 🧪")
hotkey_path = os.path.join(wallet_path, "new_wallet", "hotkeys", "new_hotkey")
initial_hotkey_mod_time = os.path.getmtime(hotkey_path)

exec_command(
result = exec_command(
command="wallet",
sub_command="regen-hotkey",
extra_args=[
Expand All @@ -442,17 +436,18 @@ def test_wallet_regen():
wallet_path,
"--mnemonic",
mnemonics["hotkey"],
"--overwrite-hotkey",
"--no-use-password",
],
inputs=["y"],
)

# Wait a bit to ensure file system updates modification time
time.sleep(1)
time.sleep(2)

new_hotkey_mod_time = os.path.getmtime(hotkey_path)

assert (
initial_hotkey_mod_time != new_hotkey_mod_time
), "Hotkey file was not regenerated as expected"
logging.info("Passed wallet regen_hotkey command ✅")
print("Passed wallet regen_hotkey command ✅")
remove_wallets(wallet_path)
14 changes: 10 additions & 4 deletions tests/e2e_tests/test_wallet_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
validate_wallet_inspect,
validate_wallet_overview,
verify_subnet_entry,
remove_wallets,
)

"""
Expand All @@ -34,7 +35,7 @@ def test_wallet_overview_inspect(local_chain):
Raises:
AssertionError: If any of the checks or verifications fail
"""
logging.info("Testing wallet overview, inspect command 🧪")
print("Testing wallet overview, inspect command 🧪")
netuid = 1
wallet_path_name = "//Alice"

Expand Down Expand Up @@ -174,7 +175,8 @@ def test_wallet_overview_inspect(local_chain):
(1, f"default-{wallet.hotkey.ss58_address}", 0, False)
], # (netuid, hotkey-display, stake, check_emissions)
)
logging.info("Passed wallet overview, inspect command ✅")
print("Passed wallet overview, inspect command ✅")
remove_wallets(wallet_path)


"""
Expand All @@ -199,7 +201,7 @@ def test_wallet_transfer(local_chain):
Raises:
AssertionError: If any of the checks or verifications fail
"""
logging.info("Testing wallet transfer, balance command 🧪")
print("Testing wallet transfer, balance command 🧪")
wallet_path_alice = "//Alice"
wallet_path_bob = "//Bob"

Expand Down Expand Up @@ -356,4 +358,8 @@ def test_wallet_transfer(local_chain):

# This transfer is expected to fail due to low balance
assert "❌ Not enough balance" in result.stdout
logging.info("Testing wallet transfer, balance command ✅")
print("Testing wallet transfer, balance command ✅")

remove_wallets(wallet_path_alice)
remove_wallets(wallet_path_bob)
remove_wallets(wallet_path_anakin)
Loading
Loading