Skip to content

Commit

Permalink
simplify patch failure test
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisvang committed Feb 6, 2024
1 parent e987682 commit dcc4a63
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/tufup/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _apply_updates(
# write the patched new archive
self.new_archive_local_path.write_bytes(archive_bytes)
except Exception as e:
if target and file_path:
if target and file_path and file_path.exists():
renamed_path = file_path.replace(
file_path.with_suffix(file_path.suffix + SUFFIX_FAILED)
)
Expand Down
33 changes: 6 additions & 27 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import packaging.version
from requests.auth import HTTPBasicAuth
import tuf.api.exceptions
from tuf.api.exceptions import LengthOrHashMismatchError
from tuf.ngclient import TargetFile

from tests import TempDirTestCase, TEST_REPO_DIR
Expand Down Expand Up @@ -222,15 +223,13 @@ def test__download_updates(self):
def test__apply_updates(self):
client = self.get_refreshed_client()
# copy files from test data to temporary client cache
target_paths = []
client.downloaded_target_files = dict()
for target_meta in client.trusted_target_metas:
if target_meta.is_patch and str(target_meta.version) in ['2.0', '3.0rc0']:
src_path = TEST_REPO_DIR / 'targets' / target_meta.filename
dst_path = pathlib.Path(client.target_dir, target_meta.filename)
shutil.copy(src=src_path, dst=dst_path)
client.downloaded_target_files[target_meta] = dst_path
target_paths.append(dst_path)
# specify new archive (normally done in _check_updates)
archives = [
tp
Expand All @@ -252,37 +251,17 @@ def test__apply_updates(self):
mock_install = Mock()
client._apply_updates(install=mock_install, skip_confirmation=True)
mock_install.assert_called()
with self.subTest(msg='patch failure due to current archive read error'):
mock_install = Mock()
nonexistent_path = self.temp_dir_path / 'nonexistent.tar.gz'
with patch.object(client, 'current_archive_local_path', nonexistent_path):
client._apply_updates(install=mock_install, skip_confirmation=True)
mock_install.assert_not_called()
# verify failed suffix (then restore for next subtest)
suffix_failed_found = False
for path in pathlib.Path(client.target_dir).iterdir():
if path.suffix == SUFFIX_FAILED:
suffix_failed_found = True
path.rename(path.with_suffix(''))
self.assertTrue(suffix_failed_found)
with self.subTest('patch failure due to length or hash failure'):
with self.subTest(msg='patch failure due to mismatch'):
mock_install = Mock()
with patch.object(
client.new_archive_info,
'verify_length_and_hashes',
Mock(
side_effect=tuf.api.exceptions.LengthOrHashMismatchError(
'integrity check failed'
)
),
Mock(side_effect=LengthOrHashMismatchError()),
):
client._apply_updates(install=mock_install, skip_confirmation=True)
self.assertTrue(
any(
path.suffix == SUFFIX_FAILED
for path in pathlib.Path(client.target_dir).iterdir()
)
)
mock_install.assert_not_called()
target_paths = pathlib.Path(client.target_dir).iterdir()
self.assertTrue(any(path.suffix == SUFFIX_FAILED for path in target_paths))

def test__apply_updates_failed(self):
client = self.get_refreshed_client()

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (macos-latest, 3.8)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (macos-latest, 3.9)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (macos-latest, 3.10)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (macos-latest, 3.11)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (macos-latest, 3.12)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (windows-latest, 3.8)

Ruff (F841)

tests\test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (windows-latest, 3.9)

Ruff (F841)

tests\test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (windows-latest, 3.10)

Ruff (F841)

tests\test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (windows-latest, 3.11)

Ruff (F841)

tests\test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (windows-latest, 3.12)

Ruff (F841)

tests\test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 3.8)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 3.9)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 3.10)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 3.11)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used

Check failure on line 267 in tests/test_client.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 3.12)

Ruff (F841)

tests/test_client.py:267:9: F841 Local variable `client` is assigned to but never used
Expand Down

0 comments on commit dcc4a63

Please sign in to comment.