Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
review comments - fixed redunant if and print
Browse files Browse the repository at this point in the history
  • Loading branch information
maaktweluit committed Jun 10, 2019
1 parent 8456a78 commit beadb18
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
51 changes: 33 additions & 18 deletions scripts/node_integration_tests/key_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,29 @@
_logging = False


class NodeKeyReuseBase():
def begin_test(self, datadirs: Dict[NodeId, Path]) -> None:
raise NotImplementedError()

def end_test(self) -> None:
raise NotImplementedError()


class NodeKeyReuse:
instance = None
provider = None
enabled = True
granary_hostname = None
instance: 'Optional[NodeKeyReuse]' = None
provider: Optional[NodeKeyReuseBase] = None
enabled: bool = True
granary_hostname: Optional[str] = None

@classmethod
def get(cls, test_dir: Path):
if not cls.instance:
if _logging:
print("NodeKeyReuse.get() called, no instance. "
"dir= ", test_dir)
cls.instance = cls()
if cls.granary_hostname:
print("key_reuse - granary selected:", cls.granary_hostname)
cls.provider = NodeKeyReuseGranary(cls.granary_hostname)
else:
print("key_reuse - local folder selected:", test_dir)
cls.provider = NodeKeyReuseLocalFolder(test_dir)
return cls.instance

Expand All @@ -60,12 +67,16 @@ def disable(cls):
def enable(cls):
cls.enabled = True

@classmethod
def reset(cls):
cls.instance = None

@classmethod
def set_granary(cls, hostname):
cls.granary_hostname = hostname


class NodeKeyReuseLocalFolder():
class NodeKeyReuseLocalFolder(NodeKeyReuseBase):
def __init__(self, test_dir: Path):
self.dir: Path = test_dir / 'key_reuse'
self.datadirs: Dict[NodeId, Path] = {}
Expand All @@ -81,7 +92,8 @@ def begin_test(self, datadirs: Dict[NodeId, Path]) -> None:
self._recycle_keys()

def end_test(self) -> None:
print("NodeKeyReuseLocalFolder.end_test() called.")
if _logging:
print("NodeKeyReuseLocalFolder.end_test() called.")
try:
if _logging:
print("Moving keys from data-dirs to reuse-dirs")
Expand Down Expand Up @@ -143,7 +155,7 @@ def _copy_keystore(datadir: Path, reuse_dir: Path) -> None:
shutil.copyfile(src, dst)


class NodeKeyReuseGranary():
class NodeKeyReuseGranary(NodeKeyReuseBase):
def __init__(self, hostname: str):
self.datadirs: Dict[NodeId, Path] = {}
self.granary = Granary(hostname)
Expand All @@ -152,12 +164,12 @@ def begin_test(self, datadirs: Dict[NodeId, Path]) -> None:
self.datadirs = datadirs
if _logging:
print("NodeKeyReuseGranary.begin_test() called.")
if _logging:
print("Moving keys from granary to data-dirs")
self._recycle_keys()
self._recycle_keys()

def end_test(self) -> None:
print("NodeKeyReuseGranary.end_test() called.")
if _logging:
print("NodeKeyReuseGranary.end_test() called.")
try:
if _logging:
print("Moving keys from data-dirs to granary")
Expand All @@ -167,7 +179,8 @@ def end_test(self) -> None:
return

def _recycle_keys(self) -> None:
print("Recycle keys")
if _logging:
print("Recycle keys")
# this is run before running second and later tests
for i, node_id in enumerate(self.datadirs):
account = self.granary.request_account()
Expand Down Expand Up @@ -207,12 +220,14 @@ def _copy_keystore(datadir: Path) -> Optional[Account]:
with open(src_ts_file, 'r') as f:
ts = f.read()
except FileNotFoundError:
print('No tx.json, continue')
if _logging:
print('No tx.json, continue')
try: # read keystore.json
with open(src_key_file, 'r') as f:
keystore = f.read()
except FileNotFoundError:
print('No File, no key')
if _logging:
print('No File, no key')
return None
keystore = json.loads(keystore)

Expand All @@ -225,8 +240,8 @@ def _copy_keystore(datadir: Path) -> Optional[Account]:

@staticmethod
def _save_private_key(key, key_path: Path, password: str) -> None:
print("_save_private_key")
print(password)
if _logging:
print("_save_private_key() password=", password)
keystore = create_keyfile_json(
key,
password.encode('utf-8'),
Expand Down
12 changes: 11 additions & 1 deletion scripts/node_integration_tests/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Callable,
Dict,
List,
Optional,
TYPE_CHECKING
)

Expand All @@ -33,7 +34,15 @@ def wrap(*args, **kwargs) -> None:


class NodeTestBase(unittest.TestCase):
reuse_keys = NodeKeyReuse.get(get_testdir())
reuse_keys: Optional[NodeKeyReuse] = None

@classmethod
def setUpClass(cls):
cls.reuse_keys = NodeKeyReuse.get(get_testdir())

@classmethod
def tearDownClass(cls):
NodeKeyReuse.reset()

def setUp(self):
self.test_dir = get_testdir() / self._relative_id()
Expand Down Expand Up @@ -76,6 +85,7 @@ def _run_test(self, test_path: str, *args, **kwargs):
if conftest.DumpOutput.enabled_on_crash():
test_args.append('--dump-output-on-crash')

assert self.reuse_keys is not None, "reuse_keys is not configured"
self.reuse_keys.begin_test(self.datadirs)
exit_code = subprocess.call(args=test_args)
self.reuse_keys.end_test()
Expand Down
3 changes: 3 additions & 0 deletions scripts/node_integration_tests/tests/test_fails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class TestFails:
def test_fails(self):
self.fail()
34 changes: 13 additions & 21 deletions tests/factories/granary.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
class Account:
def __init__(self, raw_key, ts):
self.key = ECCx(raw_key)
random.seed()
assert self.key.raw_privkey == raw_key
self.raw_key = self.key.raw_privkey
if _logging:
Expand All @@ -39,35 +38,28 @@ def request_account(self) -> Optional[Account]:
universal_newlines=True)
if _logging:
print('returncode:', completed.returncode)

if _logging:
if completed.stderr:
print('stderr:', completed.stderr)
else:
print('stderr: EMPTY')

if completed.stdout:
if _logging:
print('stdout:', completed.stdout)
out_lines = completed.stdout.split('\n')
raw_key = out_lines[0].strip()

if _logging:
print('raw_key:', raw_key)
key = decode_hex(raw_key)
if _logging:
print('key:', key)
return Account(key, out_lines[1] or None)
elif _logging:
if not completed.stdout:
print('stdout: EMPTY')
return None
return None
elif _logging:
print('stdout:', completed.stdout)

out_lines = completed.stdout.split('\n')
raw_key = out_lines[0].strip()

key = decode_hex(raw_key)
if _logging:
print('raw_key:', raw_key, 'key:', key)
return Account(key, out_lines[1] or None)

def return_account(self, account):
if _logging:
print("Granary called, account returned")
print(account)
print(account.raw_key)
print(account.transaction_store)
print("Granary called, account returned. account=", account)

key_pub_addr = encode_hex(sha3(account.key.raw_pubkey)[12:])

Expand Down

0 comments on commit beadb18

Please sign in to comment.