Pārlūkot izejas kodu

Fix the "spot" check to have a nicer error when there are no source paths to compare.

Dan Helfman 5 mēneši atpakaļ
vecāks
revīzija
3bda843139
3 mainītis faili ar 39 papildinājumiem un 1 dzēšanām
  1. 1 0
      NEWS
  2. 8 0
      borgmatic/actions/check.py
  3. 30 1
      tests/unit/actions/test_check.py

+ 1 - 0
NEWS

@@ -12,6 +12,7 @@
  * #965: Fix a borgmatic runtime directory error when running the "spot" check with a database hook
    enabled.
  * Fix the "spot" check to no longer consider pipe files within an archive for file comparisons.
+ * Fix the "spot" check to have a nicer error when there are no source paths to compare.
  * Fix auto-excluding of special files (when databases are configured) to support relative source
    directory paths.
  * Drop support for Python 3.8, which has been end-of-lifed.

+ 8 - 0
borgmatic/actions/check.py

@@ -629,6 +629,14 @@ def spot_check(
     )
     logger.debug(f'{log_prefix}: {len(archive_paths)} total archive paths for spot check')
 
+    if len(source_paths) == 0:
+        logger.debug(
+            f'{log_prefix}: Paths in latest archive but not source paths: {", ".join(set(archive_paths)) or "none"}'
+        )
+        raise ValueError(
+            f'Spot check failed: There are no source paths to compare against the archive'
+        )
+
     # Calculate the percentage delta between the source paths count and the archive paths count, and
     # compare that delta to the configured count tolerance percentage.
     count_delta_percentage = abs(len(source_paths) - len(archive_paths)) / len(source_paths) * 100

+ 30 - 1
tests/unit/actions/test_check.py

@@ -1251,7 +1251,7 @@ def test_spot_check_without_any_configuration_errors():
         )
 
 
-def test_spot_check_data_tolerance_percenatge_greater_than_data_sample_percentage_errors():
+def test_spot_check_data_tolerance_percentage_greater_than_data_sample_percentage_errors():
     with pytest.raises(ValueError):
         module.spot_check(
             repository={'path': 'repo'},
@@ -1369,6 +1369,35 @@ def test_spot_check_with_high_enough_tolerances_does_not_raise():
     )
 
 
+def test_spot_check_without_any_source_paths_errors():
+    flexmock(module).should_receive('collect_spot_check_source_paths').and_return(())
+    flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
+        'archive'
+    )
+    flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
+    flexmock(module).should_receive('compare_spot_check_hashes').never()
+
+    with pytest.raises(ValueError):
+        module.spot_check(
+            repository={'path': 'repo'},
+            config={
+                'checks': [
+                    {
+                        'name': 'spot',
+                        'count_tolerance_percentage': 10,
+                        'data_tolerance_percentage': 40,
+                        'data_sample_percentage': 50,
+                    },
+                ]
+            },
+            local_borg_version=flexmock(),
+            global_arguments=flexmock(),
+            local_path=flexmock(),
+            remote_path=flexmock(),
+            borgmatic_runtime_directory='/run/borgmatic',
+        )
+
+
 def test_run_check_checks_archives_for_configured_repository():
     flexmock(module.logger).answer = lambda message: None
     flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()