Skip to content

Commit

Permalink
[tools/shoestring]: fix more linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayonb committed Nov 22, 2024
1 parent ae7afef commit 7c731cb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
74 changes: 41 additions & 33 deletions tools/shoestring/tests/commands/test_import_bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import tempfile
from collections import namedtuple
from pathlib import Path

import pytest
Expand All @@ -9,8 +10,19 @@

from ..test.ConfigurationTestUtils import prepare_shoestring_configuration

EnabledImports = namedtuple('EnabledImports', ['harvester', 'voter', 'node_key'])
ExpectedImports = namedtuple('ExpectedImports', ['harvester', 'voter', 'node_key'])

async def _run_test(is_harvester, is_voter, is_node_key, expected_imports_harvester, expected_imports_voter, expected_imports_node_key):

def build_expected_imports(enabled_imports):
return ExpectedImports(
'{bootstrap_root}/nodes/node/server-config/resources/config-harvesting.properties' if enabled_imports.harvester else '',
'{bootstrap_root}/nodes/node/votingkeys' if enabled_imports.voter else '',
'{bootstrap_root}/nodes/node/cert/node.key.pem' if enabled_imports.node_key else ''
)


async def _run_test(enabled_imports, expected_imports):
# Arrange:
with tempfile.TemporaryDirectory() as output_directory:
config_filepath = prepare_shoestring_configuration(output_directory, NodeFeatures.PEER)
Expand All @@ -19,17 +31,17 @@ async def _run_test(is_harvester, is_voter, is_node_key, expected_imports_harves
bootstrap_node_path = Path(bootstrap_directory) / 'nodes/node'
bootstrap_node_path.mkdir(parents=True)

if is_harvester:
if enabled_imports.harvester:
bootstrap_resources_path = bootstrap_node_path / 'server-config/resources'
bootstrap_resources_path.mkdir(parents=True)
with open(bootstrap_resources_path / 'config-harvesting.properties', 'wt', encoding='utf8') as outfile:
outfile.write('lorem ipsum')

if is_voter:
if enabled_imports.voter:
bootstrap_voting_keys_path = bootstrap_node_path / 'votingkeys'
bootstrap_voting_keys_path.mkdir(parents=True)

if is_node_key:
if enabled_imports.node_key:
bootstrap_node_key_path = bootstrap_node_path / 'cert/node.key.pem'
bootstrap_node_key_path.mkdir(parents=True)

Expand All @@ -38,7 +50,7 @@ async def _run_test(is_harvester, is_voter, is_node_key, expected_imports_harves
'import-bootstrap',
'--config', str(config_filepath),
'--bootstrap', str(bootstrap_directory),
*(['--node-key'] if is_node_key else [])
*(['--node-key'] if enabled_imports.node_key else [])
])

# Assert:
Expand All @@ -49,9 +61,9 @@ async def _run_test(is_harvester, is_voter, is_node_key, expected_imports_harves
])

assert [
expected_imports_harvester.format(bootstrap_root=bootstrap_directory),
expected_imports_voter.format(bootstrap_root=bootstrap_directory),
expected_imports_node_key.format(bootstrap_root=bootstrap_directory)
expected_imports.harvester.format(bootstrap_root=bootstrap_directory),
expected_imports.voter.format(bootstrap_root=bootstrap_directory),
expected_imports.node_key.format(bootstrap_root=bootstrap_directory)
] == imports


Expand All @@ -60,49 +72,45 @@ async def _run_test(is_harvester, is_voter, is_node_key, expected_imports_harves
# region success

async def test_can_import_with_neither_harvester_nor_voter_nor_node_key():
await _run_test(False, False, False, '', '', '')
actual_imports = EnabledImports(False, False, False)
expected_imports = build_expected_imports(actual_imports)
await _run_test(actual_imports, expected_imports)


async def test_can_import_with_harvester_but_not_voter_or_node_key():
await _run_test(True, False, False, '{bootstrap_root}/nodes/node/server-config/resources/config-harvesting.properties', '', '')
actual_imports = EnabledImports(True, False, False)
expected_imports = build_expected_imports(actual_imports)
await _run_test(actual_imports, expected_imports)


async def test_can_import_with_voter_but_not_harvester_or_node_key():
await _run_test(False, True, False, '', '{bootstrap_root}/nodes/node/votingkeys', '')
actual_imports = EnabledImports(False, True, False)
expected_imports = build_expected_imports(actual_imports)
await _run_test(actual_imports, expected_imports)


async def test_can_import_with_node_key_but_not_harvester_or_voter():
await _run_test(False, False, True, '', '', '{bootstrap_root}/nodes/node/cert/node.key.pem')
actual_imports = EnabledImports(False, False, True)
expected_imports = build_expected_imports(actual_imports)
await _run_test(actual_imports, expected_imports)


async def test_can_import_with_harvester_and_voter_but_not_node_key():
await _run_test(
True,
True,
False,
'{bootstrap_root}/nodes/node/server-config/resources/config-harvesting.properties',
'{bootstrap_root}/nodes/node/votingkeys',
'')
actual_imports = EnabledImports(True, True, False)
expected_imports = build_expected_imports(actual_imports)
await _run_test(actual_imports, expected_imports)


async def test_can_import_with_harvester_and_node_key_but_not_voter():
await _run_test(
True,
False,
True,
'{bootstrap_root}/nodes/node/server-config/resources/config-harvesting.properties',
'',
'{bootstrap_root}/nodes/node/cert/node.key.pem')
actual_imports = EnabledImports(True, False, True)
expected_imports = build_expected_imports(actual_imports)
await _run_test(actual_imports, expected_imports)


async def test_can_import_with_both_harvester_and_voter_and_node_key():
await _run_test(
True,
True,
True,
'{bootstrap_root}/nodes/node/server-config/resources/config-harvesting.properties',
'{bootstrap_root}/nodes/node/votingkeys',
'{bootstrap_root}/nodes/node/cert/node.key.pem')
actual_imports = EnabledImports(True, True, True)
expected_imports = build_expected_imports(actual_imports)
await _run_test(actual_imports, expected_imports)

# endregion

Expand Down
2 changes: 2 additions & 0 deletions tools/shoestring/tests/commands/test_renew_certificates.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def _assert_node_full_certificate(ca_certificate_filepath, node_certificate_file


async def _assert_can_renew_node_certificate(ca_password=None, retain_key=False):
# pylint: disable=too-many-locals

# Arrange:
with tempfile.TemporaryDirectory() as output_directory:
config_filepath_1 = _create_configuration(output_directory, ca_password, 'ORIGINAL CA CN', 'ORIGINAL NODE CN', '1.shoestring.ini')
Expand Down

0 comments on commit 7c731cb

Please sign in to comment.