Skip to content

Commit 0abe5d1

Browse files
wallet_setup fixture + fresh wallets
1 parent 55b1dd1 commit 0abe5d1

7 files changed

+37
-48
lines changed

tests/e2e_tests/__init__.py

Whitespace-only changes.

tests/e2e_tests/conftest.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
import os
33
import re
44
import shlex
5+
import shutil
56
import signal
67
import subprocess
78
import time
89

910
import pytest
10-
1111
from bittensor_cli.src.bittensor.async_substrate_interface import (
1212
AsyncSubstrateInterface,
1313
)
1414

15+
from .utils import setup_wallet
16+
1517

1618
# Fixture for setting up and tearing down a localnet.sh chain between tests
1719
@pytest.fixture(scope="function")
@@ -70,3 +72,19 @@ def wait_for_node_start(process, pattern):
7072

7173
# Ensure the process has terminated
7274
process.wait()
75+
76+
77+
@pytest.fixture(scope="function")
78+
def wallet_setup():
79+
wallet_paths = []
80+
81+
def _setup_wallet(uri: str):
82+
keypair, wallet, wallet_path, exec_command = setup_wallet(uri)
83+
wallet_paths.append(wallet_path)
84+
return keypair, wallet, wallet_path, exec_command
85+
86+
yield _setup_wallet
87+
88+
# Cleanup after the test
89+
for path in wallet_paths:
90+
shutil.rmtree(path, ignore_errors=True)

tests/e2e_tests/test_root.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from bittensor_cli.src.bittensor.balances import Balance
44

5-
from tests.e2e_tests.utils import setup_wallet, remove_wallets
6-
75
"""
86
Verify commands:
97
@@ -17,7 +15,7 @@
1715
"""
1816

1917

20-
def test_root_commands(local_chain):
18+
def test_root_commands(local_chain, wallet_setup):
2119
"""
2220
Test the root commands and inspects their output
2321
@@ -38,10 +36,10 @@ def test_root_commands(local_chain):
3836
wallet_path_bob = "//Bob"
3937

4038
# Create wallet for Alice
41-
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = setup_wallet(
39+
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
4240
wallet_path_alice
4341
)
44-
keypair_bob, wallet_bob, wallet_path_bob, exec_command_bob = setup_wallet(
42+
keypair_bob, wallet_bob, wallet_path_bob, exec_command_bob = wallet_setup(
4543
wallet_path_bob
4644
)
4745

@@ -260,5 +258,3 @@ def test_root_commands(local_chain):
260258
assert "✅ Finalized" in undelegate_alice.stdout
261259

262260
print("✅ Passed Root commands")
263-
remove_wallets(wallet_path_alice)
264-
remove_wallets(wallet_path_bob)

tests/e2e_tests/test_staking_sudo.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import time
33

44
from bittensor_cli.src.bittensor.balances import Balance
5-
from tests.e2e_tests.utils import setup_wallet, remove_wallets
65

76
"""
87
Verify commands:
@@ -17,7 +16,7 @@
1716
"""
1817

1918

20-
def test_staking(local_chain):
19+
def test_staking(local_chain, wallet_setup):
2120
"""
2221
Test staking & sudo commands and inspect their output
2322
@@ -37,7 +36,7 @@ def test_staking(local_chain):
3736
wallet_path_alice = "//Alice"
3837

3938
# Create wallet for Alice
40-
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = setup_wallet(
39+
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
4140
wallet_path_alice
4241
)
4342

@@ -224,4 +223,3 @@ def test_staking(local_chain):
224223
10
225224
)
226225
print("✅ Passed staking and sudo commands")
227-
remove_wallets(wallet_path_alice)

tests/e2e_tests/test_wallet_creations.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import time
44
from typing import Dict, Optional, Tuple
55

6-
from tests.e2e_tests.utils import setup_wallet, remove_wallets, get_path_from_uri
7-
86
"""
97
Verify commands:
108
@@ -158,7 +156,7 @@ def extract_mnemonics_from_commands(output: str) -> Dict[str, Optional[str]]:
158156
return mnemonics
159157

160158

161-
def test_wallet_creations():
159+
def test_wallet_creations(wallet_setup):
162160
"""
163161
Test the creation and verification of wallet keys and directories in the Bittensor network.
164162
@@ -175,7 +173,7 @@ def test_wallet_creations():
175173
"""
176174

177175
wallet_path_name = "//Alice"
178-
keypair, wallet, wallet_path, exec_command = setup_wallet(wallet_path_name)
176+
keypair, wallet, wallet_path, exec_command = wallet_setup(wallet_path_name)
179177

180178
result = exec_command(
181179
command="wallet", sub_command="list", extra_args=["--wallet-path", wallet_path]
@@ -296,10 +294,9 @@ def test_wallet_creations():
296294
wallet_path, "new_coldkey", hotkey_name="new_hotkey"
297295
)
298296
assert wallet_status, message
299-
remove_wallets(wallet_path)
300297

301298

302-
def test_wallet_regen():
299+
def test_wallet_regen(wallet_setup):
303300
"""
304301
Test the regeneration of coldkeys, hotkeys, and coldkeypub files using mnemonics or ss58 address.
305302
@@ -313,7 +310,7 @@ def test_wallet_regen():
313310
AssertionError: If any of the checks or verifications fail
314311
"""
315312
wallet_path_name = "//Bob"
316-
keypair, wallet, wallet_path, exec_command = setup_wallet(wallet_path_name)
313+
keypair, wallet, wallet_path, exec_command = wallet_setup(wallet_path_name)
317314

318315
# Create a new wallet (coldkey + hotkey)
319316
result = exec_command(
@@ -450,4 +447,3 @@ def test_wallet_regen():
450447
initial_hotkey_mod_time != new_hotkey_mod_time
451448
), "Hotkey file was not regenerated as expected"
452449
print("Passed wallet regen_hotkey command ✅")
453-
remove_wallets(wallet_path)

tests/e2e_tests/test_wallet_interactions.py

+7-17
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import logging
2-
31
from bittensor_cli.src.bittensor.balances import Balance
4-
52
from tests.e2e_tests.utils import (
63
extract_coldkey_balance,
7-
setup_wallet,
84
validate_wallet_inspect,
95
validate_wallet_overview,
106
verify_subnet_entry,
11-
remove_wallets,
127
)
138

149
"""
@@ -22,7 +17,7 @@
2217
"""
2318

2419

25-
def test_wallet_overview_inspect(local_chain):
20+
def test_wallet_overview_inspect(local_chain, wallet_setup):
2621
"""
2722
Test the overview and inspect commands of the wallet by interaction with subnets
2823
@@ -40,7 +35,7 @@ def test_wallet_overview_inspect(local_chain):
4035
wallet_path_name = "//Alice"
4136

4237
# Create wallet for Alice
43-
keypair, wallet, wallet_path, exec_command = setup_wallet(wallet_path_name)
38+
keypair, wallet, wallet_path, exec_command = wallet_setup(wallet_path_name)
4439

4540
# Register a subnet with sudo as Alice
4641
result = exec_command(
@@ -176,7 +171,6 @@ def test_wallet_overview_inspect(local_chain):
176171
], # (netuid, hotkey-display, stake, check_emissions)
177172
)
178173
print("Passed wallet overview, inspect command ✅")
179-
remove_wallets(wallet_path)
180174

181175

182176
"""
@@ -187,7 +181,7 @@ def test_wallet_overview_inspect(local_chain):
187181
"""
188182

189183

190-
def test_wallet_transfer(local_chain):
184+
def test_wallet_transfer(local_chain, wallet_setup):
191185
"""
192186
Test the transfer and balance functionality in the Bittensor network.
193187
@@ -206,10 +200,10 @@ def test_wallet_transfer(local_chain):
206200
wallet_path_bob = "//Bob"
207201

208202
# Create wallets for Alice and Bob
209-
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = setup_wallet(
203+
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
210204
wallet_path_alice
211205
)
212-
keypair_bob, wallet_bob, wallet_path_bob, exec_command_bob = setup_wallet(
206+
keypair_bob, wallet_bob, wallet_path_bob, exec_command_bob = wallet_setup(
213207
wallet_path_bob
214208
)
215209

@@ -332,7 +326,7 @@ def test_wallet_transfer(local_chain):
332326

333327
wallet_path_anakin = "//Anakin"
334328
keypair_anakin, wallet_anakin, wallet_path_anakin, exec_command_anakin = (
335-
setup_wallet(wallet_path_anakin)
329+
wallet_setup(wallet_path_anakin)
336330
)
337331

338332
# Attempt transferring to Alice
@@ -358,8 +352,4 @@ def test_wallet_transfer(local_chain):
358352

359353
# This transfer is expected to fail due to low balance
360354
assert "❌ Not enough balance" in result.stdout
361-
print("Testing wallet transfer, balance command ✅")
362-
363-
remove_wallets(wallet_path_alice)
364-
remove_wallets(wallet_path_bob)
365-
remove_wallets(wallet_path_anakin)
355+
print("✅Passed wallet transfer, balance command ")

tests/e2e_tests/utils.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@
55
import sys
66
from typing import List, Tuple
77

8+
from bittensor_cli.cli import CLIManager
89
from bittensor_wallet import Wallet
910
from substrateinterface import Keypair
1011
from typer.testing import CliRunner
1112

12-
from bittensor_cli.cli import CLIManager
13-
1413
template_path = os.getcwd() + "/neurons/"
1514
templates_repo = "templates repository"
1615

1716

18-
def get_path_from_uri(uri: str):
19-
return f"/tmp/btcli-e2e-wallet-{uri.strip('/')}"
20-
21-
2217
def setup_wallet(uri: str):
2318
keypair = Keypair.create_from_uri(uri)
24-
wallet_path = get_path_from_uri(uri)
19+
wallet_path = f"/tmp/btcli-e2e-wallet-{uri.strip('/')}"
2520
wallet = Wallet(path=wallet_path)
2621
wallet.set_coldkey(keypair=keypair, encrypt=False, overwrite=True)
2722
wallet.set_coldkeypub(keypair=keypair, encrypt=False, overwrite=True)
@@ -287,7 +282,3 @@ def uninstall_templates(install_dir):
287282
)
288283
# Delete everything in directory
289284
shutil.rmtree(install_dir)
290-
291-
292-
def remove_wallets(wallets_dir):
293-
shutil.rmtree(wallets_dir)

0 commit comments

Comments
 (0)