Skip to content

Commit

Permalink
Merge pull request #1984 from opentensor/feature/opendansor/add_e2e_f…
Browse files Browse the repository at this point in the history
…or_axon

Add e2e test for axon
  • Loading branch information
opendansor committed Jun 6, 2024
2 parents 9fd57e2 + fb527e3 commit 8a73421
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 22 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/e2e-subtensor-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ jobs:

- name: Setup subtensor repo
working-directory: ${{ github.workspace }}/subtensor
run: git checkout development
run: git checkout testnet

- name: Run tests
run: |
python3 -m pip install -e .[torch] pytest
LOCALNET_SH_PATH="./subtensor/scripts/localnet.sh" pytest tests/e2e_tests/ -s
python3 -m pip install -e .[dev] pytest
LOCALNET_SH_PATH="${{ github.workspace }}/subtensor/scripts/localnet.sh" pytest tests/e2e_tests/ -s
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
black==23.7.0
pytest==7.2.0
pytest-asyncio
pytest-asyncio==0.23.7
pytest-mock==3.12.0
pytest-split==0.8.0
pytest-xdist==3.0.2
Expand Down
27 changes: 22 additions & 5 deletions tests/e2e_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import logging
import os
import re
import shlex
import signal
from substrateinterface import SubstrateInterface
import pytest
import subprocess
import logging
import shlex
import re
import time

import pytest
from substrateinterface import SubstrateInterface

from tests.e2e_tests.utils import (
clone_or_update_templates,
install_templates,
uninstall_templates,
template_path,
)

logging.basicConfig(level=logging.INFO)


Expand All @@ -31,6 +39,11 @@ def local_chain():
# Pattern match indicates node is compiled and ready
pattern = re.compile(r"Successfully ran block step\.")

# install neuron templates
logging.info("downloading and installing neuron templates from github")
templates_dir = clone_or_update_templates()
install_templates(templates_dir)

def wait_for_node_start(process, pattern):
for line in process.stdout:
print(line.strip())
Expand All @@ -55,3 +68,7 @@ def wait_for_node_start(process, pattern):

# Ensure the process has terminated
process.wait()

# uninstall templates
logging.info("uninstalling neuron templates")
uninstall_templates(template_path)
103 changes: 103 additions & 0 deletions tests/e2e_tests/multistep/test_axon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import asyncio
import sys

import pytest

import bittensor
from bittensor.utils import networking
from bittensor.commands import (
RegisterCommand,
RegisterSubnetworkCommand,
)
from tests.e2e_tests.utils import (
setup_wallet,
template_path,
repo_name,
)


@pytest.mark.asyncio
async def test_axon(local_chain):
# Register root as Alice
alice_keypair, exec_command, wallet_path = setup_wallet("//Alice")
exec_command(RegisterSubnetworkCommand, ["s", "create"])

# Verify subnet 1 created successfully
assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize()

# Register a neuron to the subnet
exec_command(
RegisterCommand,
[
"s",
"register",
"--netuid",
"1",
"--wallet.name",
"default",
"--wallet.hotkey",
"default",
"--wallet.path",
wallet_path,
"--subtensor.network",
"local",
"--subtensor.chain_endpoint",
"ws://localhost:9945",
"--no_prompt",
],
)

metagraph = bittensor.metagraph(netuid=1, network="ws://localhost:9945")

# validate one miner with ip of none
old_axon = metagraph.axons[0]

assert len(metagraph.axons) == 1
assert old_axon.hotkey == alice_keypair.ss58_address
assert old_axon.coldkey == alice_keypair.ss58_address
assert old_axon.ip == "0.0.0.0"
assert old_axon.port == 0
assert old_axon.ip_type == 0

# register miner
# "python neurons/miner.py --netuid 1 --subtensor.chain_endpoint ws://localhost:9945 --wallet.name wallet.name --wallet.hotkey wallet.hotkey.ss58_address"
cmd = " ".join(
[
f"{sys.executable}",
f'"{template_path}{repo_name}/neurons/miner.py"',
"--no_prompt",
"--netuid",
"1",
"--subtensor.network",
"local",
"--subtensor.chain_endpoint",
"ws://localhost:9945",
"--wallet.path",
wallet_path,
"--wallet.name",
"default",
"--wallet.hotkey",
"default",
]
)

await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await asyncio.sleep(
5
) # wait for 5 seconds for the metagraph to refresh with latest data

# refresh metagraph
metagraph = bittensor.metagraph(netuid=1, network="ws://localhost:9945")
updated_axon = metagraph.axons[0]
external_ip = networking.get_external_ip()

assert len(metagraph.axons) == 1
assert updated_axon.ip == external_ip
assert updated_axon.ip_type == networking.ip_version(external_ip)
assert updated_axon.port == 8091
assert updated_axon.hotkey == alice_keypair.ss58_address
assert updated_axon.coldkey == alice_keypair.ss58_address
6 changes: 3 additions & 3 deletions tests/e2e_tests/multistep/test_last_tx_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# https://discord.com/channels/799672011265015819/1176889736636407808/1236057424134144152
def test_takes(local_chain):
# Register root as Alice
(keypair, exec_command) = setup_wallet("//Alice")
keypair, exec_command, wallet_path = setup_wallet("//Alice")
exec_command(RootRegisterCommand, ["root", "register"])

# Create subnet 1 and verify created successfully
Expand All @@ -21,7 +21,7 @@ def test_takes(local_chain):
assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize()

# Register and nominate Bob
(keypair, exec_command) = setup_wallet("//Bob")
keypair, exec_command, wallet_path = setup_wallet("//Bob")
assert (
local_chain.query(
"SubtensorModule", "LastTxBlock", [keypair.ss58_address]
Expand All @@ -35,7 +35,7 @@ def test_takes(local_chain):
).serialize()
== 0
)
exec_command(RegisterCommand, ["s", "register", "--neduid", "1"])
exec_command(RegisterCommand, ["s", "register", "--netuid", "1"])
exec_command(NominateCommand, ["root", "nominate"])
assert (
local_chain.query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def test_set_delegate_increase_take(local_chain):
# Register root as Alice
(keypair, exec_command) = setup_wallet("//Alice")
keypair, exec_command, wallet_path = setup_wallet("//Alice")
exec_command(RootRegisterCommand, ["root", "register"])

# Create subnet 1 and verify created successfully
Expand All @@ -20,7 +20,7 @@ def test_set_delegate_increase_take(local_chain):
assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize()

# Register and nominate Bob
(keypair, exec_command) = setup_wallet("//Bob")
keypair, exec_command, wallet_path = setup_wallet("//Bob")
assert (
local_chain.query(
"SubtensorModule", "LastTxBlock", [keypair.ss58_address]
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e_tests/subcommands/wallet/test_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Example test using the local_chain fixture
def test_transfer(local_chain):
(keypair, exec_command) = setup_wallet("//Alice")
keypair, exec_command, wallet_path = setup_wallet("//Alice")

acc_before = local_chain.query("System", "Account", [keypair.ss58_address])
exec_command(
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e_tests/subcommands/weights/test_commit_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

def test_commit_and_reveal_weights(local_chain):
# Register root as Alice
(alice_keypair, exec_command) = setup_wallet("//Alice")
keypair, exec_command, wallet_path = setup_wallet("//Alice")
exec_command(RegisterSubnetworkCommand, ["s", "create"])

# define values
Expand All @@ -36,9 +36,9 @@ def test_commit_and_reveal_weights(local_chain):

# Create a test wallet and set the coldkey, coldkeypub, and hotkey
wallet = bittensor.wallet(path="/tmp/btcli-wallet")
wallet.set_coldkey(keypair=alice_keypair, encrypt=False, overwrite=True)
wallet.set_coldkeypub(keypair=alice_keypair, encrypt=False, overwrite=True)
wallet.set_hotkey(keypair=alice_keypair, encrypt=False, overwrite=True)
wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=True)
wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=True)
wallet.set_hotkey(keypair=keypair, encrypt=False, overwrite=True)

# Stake to become to top neuron after the first epoch
exec_command(
Expand Down
45 changes: 44 additions & 1 deletion tests/e2e_tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import os
import shutil
import subprocess
import sys

import requests
from substrateinterface import Keypair
from typing import List
import bittensor

template_path = os.getcwd() + "/neurons/"
repo_name = "templates repository"


def setup_wallet(uri: str):
keypair = Keypair.create_from_uri(uri)
Expand Down Expand Up @@ -29,4 +38,38 @@ def exec_command(command, extra_args: List[str]):
cli_instance = bittensor.cli(config)
command.run(cli_instance)

return (keypair, exec_command)
return keypair, exec_command, wallet_path


def clone_or_update_templates():
install_dir = template_path
repo_mapping = {
repo_name: "https://github.com/opentensor/bittensor-subnet-template.git",
}
os.makedirs(install_dir, exist_ok=True)
os.chdir(install_dir)

for repo, git_link in repo_mapping.items():
if not os.path.exists(repo):
print(f"\033[94mCloning {repo}...\033[0m")
subprocess.run(["git", "clone", git_link, repo], check=True)
else:
print(f"\033[94mUpdating {repo}...\033[0m")
os.chdir(repo)
subprocess.run(["git", "pull"], check=True)
os.chdir("..")

return install_dir + repo_name + "/"


def install_templates(install_dir):
subprocess.check_call([sys.executable, "-m", "pip", "install", install_dir])


def uninstall_templates(install_dir):
# uninstall templates
subprocess.check_call(
[sys.executable, "-m", "pip", "uninstall", "bittensor_subnet_template", "-y"]
)
# delete everything in directory
shutil.rmtree(install_dir)
4 changes: 2 additions & 2 deletions tests/unit_tests/test_dendrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_close(dendrite_obj, setup_axon):
# Query the axon to open a session
dendrite_obj.query(axon, SynapseDummy(input=1))
# Session should be automatically closed after query
assert dendrite_obj._session == None
assert dendrite_obj._session is None


@pytest.mark.asyncio
Expand All @@ -103,7 +103,7 @@ async def test_aclose(dendrite_obj, setup_axon):
async with dendrite_obj:
resp = await dendrite_obj([axon], SynapseDummy(input=1), deserialize=False)
# Close should automatically be called on the session after context manager scope
assert dendrite_obj._session == None
assert dendrite_obj._session is None


class AsyncMock(Mock):
Expand Down

0 comments on commit 8a73421

Please sign in to comment.