Skip to content

Commit

Permalink
Add debugging helpers to test_state_restore (#86)
Browse files Browse the repository at this point in the history
See this issue for context:
    #93
  • Loading branch information
robin-nitrokey authored Nov 17, 2023
1 parent ee7c7ca commit 96b86e8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ jobs:
run: |
. venv/bin/activate
make test
- name: Upload artifacts
uses: actions/upload-artifact@v3
if: ${{ failure() }}
with:
name: backup
path: backupNethsm.bin
- name: upload coverage
uses: codecov/codecov-action@v3
with:
Expand Down
30 changes: 22 additions & 8 deletions tests/test_nethsm_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
update,
)

from nethsm import NetHSM
from nethsm import NetHSM, NetHSMError
from nethsm.backup import Backup, EncryptedBackup

"""######################### Preparation for the Tests #########################
Expand Down Expand Up @@ -104,13 +104,27 @@ def test_state_restore(nethsm: NetHSM) -> None:
system_time = datetime.datetime.now(datetime.timezone.utc)
assert nethsm.get_state().value == "Unprovisioned"

try:
with open(C.FILENAME_BACKUP, "rb") as f:
nethsm.restore(f, C.BACKUP_PASSPHRASE, system_time)
nethsm.unlock(C.UNLOCK_PASSPHRASE)
except OSError as e:
print(e, type(e))
assert False
# We repeat the restore call to debug a problem with the restore endpoint in the CI.
# See this issue for more information:
# https://github.com/Nitrokey/nethsm-sdk-py/issues/93

successful_try = None
last_exception = None
for i in range(10):
try:
with open(C.FILENAME_BACKUP, "rb") as f:
nethsm.restore(f, C.BACKUP_PASSPHRASE, system_time)
successful_try = i
except NetHSMError as e:
last_exception = e
continue

if successful_try != 0:
print(f"successful try: {successful_try}")
assert last_exception
raise last_exception

nethsm.unlock(C.UNLOCK_PASSPHRASE)

assert nethsm.list_keys() == [C.KEY_ID_GENERATED]

Expand Down

0 comments on commit 96b86e8

Please sign in to comment.