Use pathlib in test mixed configuration cli test

pull/1973/head
Kieran R. Prasch 2020-05-08 14:38:00 -07:00 committed by Kieran Prasch
parent 31e3a1afbe
commit 83bfefc9a1
No known key found for this signature in database
GPG Key ID: 199AB839D4125A62
2 changed files with 41 additions and 40 deletions

View File

@ -17,15 +17,13 @@ along with nucypher. If not, see <https://www.gnu.org/licenses/>.
import contextlib
from pathlib import Path
from datetime import datetime
import json
import os
import shutil
import pytest
import shutil
from click.testing import CliRunner
from datetime import datetime
from nucypher.blockchain.eth.registry import InMemoryContractRegistry, LocalContractRegistry
from nucypher.config.characters import UrsulaConfiguration, StakeHolderConfiguration
@ -85,7 +83,7 @@ def new_local_registry():
@pytest.fixture(scope='module')
def custom_filepath():
_custom_filepath = Path(MOCK_CUSTOM_INSTALLATION_PATH)
_custom_filepath = MOCK_CUSTOM_INSTALLATION_PATH
with contextlib.suppress(FileNotFoundError):
shutil.rmtree(_custom_filepath, ignore_errors=True)
yield _custom_filepath
@ -95,14 +93,14 @@ def custom_filepath():
@pytest.fixture(scope='module')
def custom_filepath_2():
_custom_filepath = Path(MOCK_CUSTOM_INSTALLATION_PATH_2)
_custom_filepath = MOCK_CUSTOM_INSTALLATION_PATH_2
with contextlib.suppress(FileNotFoundError):
shutil.rmtree(_custom_filepath, ignore_errors=True)
try:
yield _custom_filepath
finally:
with contextlib.suppress(FileNotFoundError):
shutil.rmtree(_custom_filepath / '*', ignore_errors=True)
shutil.rmtree(_custom_filepath, ignore_errors=True)
@pytest.fixture(scope='module')

View File

@ -1,10 +1,7 @@
from pathlib import Path
import shutil
import os
import pytest
import shutil
from pathlib import Path
from nucypher.blockchain.eth.actors import Worker
from nucypher.cli.main import nucypher_cli
@ -17,21 +14,29 @@ from nucypher.utilities.sandbox.constants import (
INSECURE_DEVELOPMENT_PASSWORD,
TEST_PROVIDER_URI,
MOCK_IP_ADDRESS,
MOCK_IP_ADDRESS_2,
MOCK_IP_ADDRESS_2, MOCK_CUSTOM_INSTALLATION_PATH,
)
@pytest.fixture(scope='function')
def custom_filepath():
_path = MOCK_CUSTOM_INSTALLATION_PATH
shutil.rmtree(_path, ignore_errors=True)
assert not Path(_path).exists()
yield Path(_path)
shutil.rmtree(_path, ignore_errors=True)
def test_destroy_with_no_configurations(click_runner, custom_filepath):
"""Provide useful error messages when attempting to destroy when there is nothing to destroy"""
shutil.rmtree(custom_filepath, ignore_errors=True)
assert not Path(custom_filepath).exists()
ursula_file_location = os.path.join(custom_filepath, 'ursula.json')
destruction_args = ('ursula', 'destroy', '--config-file', ursula_file_location)
assert not custom_filepath.exists()
ursula_file_location = custom_filepath / 'ursula.json'
destruction_args = ('ursula', 'destroy', '--config-file', str(ursula_file_location))
result = click_runner.invoke(nucypher_cli, destruction_args, catch_exceptions=False)
assert result.exit_code == 2
assert "Error: Invalid value for '--config-file':" in result.output
assert ursula_file_location in result.output
assert not Path(custom_filepath).exists()
assert str(ursula_file_location) in result.output
assert not custom_filepath.exists()
def test_coexisting_configurations(click_runner,
@ -42,8 +47,9 @@ def test_coexisting_configurations(click_runner,
# Setup
#
shutil.rmtree(custom_filepath, ignore_errors=True)
assert not Path(custom_filepath).exists()
if custom_filepath.exists():
shutil.rmtree(custom_filepath, ignore_errors=True)
assert not custom_filepath.exists()
# Parse node addresses
alice, ursula, another_ursula, felix, staker, *all_yall = testerchain.unassigned_accounts
@ -52,33 +58,32 @@ def test_coexisting_configurations(click_runner,
'NUCYPHER_FELIX_DB_SECRET': INSECURE_DEVELOPMENT_PASSWORD}
# Future configuration filepaths for assertions...
public_keys_dir = os.path.join(custom_filepath, 'keyring', 'public')
known_nodes_dir = os.path.join(custom_filepath, 'known_nodes')
public_keys_dir = custom_filepath / 'keyring' / 'public'
known_nodes_dir = custom_filepath / 'known_nodes'
# ... Ensure they do not exist to begin with.
assert not os.path.isdir(public_keys_dir)
assert not os.path.isfile(known_nodes_dir)
# No keys have been generated...
with pytest.raises(FileNotFoundError):
assert len(os.listdir(public_keys_dir)) == 0
assert not public_keys_dir.exists()
# No known nodes exist...
with pytest.raises(FileNotFoundError):
assert len(os.listdir(known_nodes_dir)) == 0
assert not known_nodes_dir.exists()
# Not the configuration root...
assert not os.path.isdir(custom_filepath)
# ... nothing
None
#
# Create
#
# Expected config files
felix_file_location = os.path.join(custom_filepath, FelixConfiguration.generate_filename())
alice_file_location = os.path.join(custom_filepath, AliceConfiguration.generate_filename())
ursula_file_location = os.path.join(custom_filepath, UrsulaConfiguration.generate_filename())
another_ursula_configuration_file_location = os.path.join(custom_filepath, UrsulaConfiguration.generate_filename(modifier=another_ursula))
felix_file_location = custom_filepath / FelixConfiguration.generate_filename()
alice_file_location = custom_filepath / AliceConfiguration.generate_filename()
ursula_file_location = custom_filepath / UrsulaConfiguration.generate_filename()
another_ursula_configuration_file_location = custom_filepath / UrsulaConfiguration.generate_filename(modifier=another_ursula)
# Felix creates a system configuration
felix_init_args = ('felix', 'init',
@ -185,7 +190,8 @@ def test_coexisting_configurations(click_runner,
# Destroy
#
another_ursula_destruction_args = ('ursula', 'destroy', '--force',
another_ursula_destruction_args = ('ursula', 'destroy',
'--force',
'--config-file', another_ursula_configuration_file_location)
result = click_runner.invoke(nucypher_cli, another_ursula_destruction_args, catch_exceptions=False, env=envvars)
assert result.exit_code == 0
@ -218,9 +224,6 @@ def test_corrupted_configuration(click_runner,
agency_local_registry):
alice, ursula, another_ursula, felix, staker, *all_yall = testerchain.unassigned_accounts
shutil.rmtree(custom_filepath, ignore_errors=True)
assert not Path(custom_filepath).exists()
init_args = ('ursula', 'init',
'--provider', TEST_PROVIDER_URI,
'--worker-address', another_ursula,
@ -266,7 +269,7 @@ def test_corrupted_configuration(click_runner,
# Ensure configuration creation
top_level_config_root = os.listdir(custom_filepath)
assert default_filename in top_level_config_root, "JSON configuration file was not created"
assert len(os.listdir(os.path.join(custom_filepath, 'keyring', 'private'))) == 4 # keys were created
assert len(os.listdir(custom_filepath / 'keyring' / 'private')) == 4 # keys were created
for field in ['known_nodes', 'keyring', default_filename]:
assert field in top_level_config_root
@ -274,7 +277,7 @@ def test_corrupted_configuration(click_runner,
os.remove(agency_local_registry.filepath)
# Attempt destruction with invalid configuration (missing registry)
ursula_file_location = os.path.join(custom_filepath, default_filename)
ursula_file_location = custom_filepath / default_filename
destruction_args = ('ursula', 'destroy', '--debug', '--config-file', ursula_file_location)
result = click_runner.invoke(nucypher_cli, destruction_args, input='Y\n', catch_exceptions=False, env=envvars)
assert result.exit_code == 0
@ -282,4 +285,4 @@ def test_corrupted_configuration(click_runner,
# Ensure character destruction
top_level_config_root = os.listdir(custom_filepath)
assert default_filename not in top_level_config_root # config file was destroyed
assert len(os.listdir(os.path.join(custom_filepath, 'keyring', 'private'))) == 0 # keys were destroyed
assert len(os.listdir(custom_filepath / 'keyring' / 'private')) == 0 # keys were destroyed