Browse Source

move debug cmd tests to own module

Thomas Waldmann 2 years ago
parent
commit
d762833cd6
3 changed files with 124 additions and 108 deletions
  1. 1 0
      setup.cfg
  2. 3 108
      src/borg/testsuite/archiver/__init__.py
  3. 120 0
      src/borg/testsuite/archiver/debug_cmds.py

+ 1 - 0
setup.cfg

@@ -158,6 +158,7 @@ per_file_ignores =
     src/borg/testsuite/__init__.py:E501,F401
     src/borg/testsuite/__init__.py:E501,F401
     src/borg/testsuite/archive.py:E128,W504
     src/borg/testsuite/archive.py:E128,W504
     src/borg/testsuite/archiver/__init__.py:E128,E501,E722,F401,F405,F811
     src/borg/testsuite/archiver/__init__.py:E128,E501,E722,F401,F405,F811
+    src/borg/testsuite/archiver/debug_cmds.py:E501
     src/borg/testsuite/benchmark.py:F401,F811
     src/borg/testsuite/benchmark.py:F401,F811
     src/borg/testsuite/chunker.py:E501,F405
     src/borg/testsuite/chunker.py:E501,F405
     src/borg/testsuite/chunker_pytest.py:F401
     src/borg/testsuite/chunker_pytest.py:F401

+ 3 - 108
src/borg/testsuite/archiver/__init__.py

@@ -2509,20 +2509,6 @@ class ArchiverTestCase(ArchiverTestCaseBase):
         assert log_message["levelname"] == "DEBUG"  # there should only be DEBUG messages
         assert log_message["levelname"] == "DEBUG"  # there should only be DEBUG messages
         assert isinstance(log_message["message"], str)
         assert isinstance(log_message["message"], str)
 
 
-    def test_debug_profile(self):
-        self.create_test_files()
-        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
-        self.cmd(f"--repo={self.repository_location}", "create", "test", "input", "--debug-profile=create.prof")
-        self.cmd("debug", "convert-profile", "create.prof", "create.pyprof")
-        stats = pstats.Stats("create.pyprof")
-        stats.strip_dirs()
-        stats.sort_stats("cumtime")
-
-        self.cmd(f"--repo={self.repository_location}", "create", "test2", "input", "--debug-profile=create.pyprof")
-        stats = pstats.Stats("create.pyprof")  # Only do this on trusted data!
-        stats.strip_dirs()
-        stats.sort_stats("cumtime")
-
     def test_common_options(self):
     def test_common_options(self):
         self.create_test_files()
         self.create_test_files()
         self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
         self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
@@ -2870,45 +2856,6 @@ class ArchiverTestCase(ArchiverTestCaseBase):
             # Undecorate
             # Undecorate
             borg.locking.Lock.migrate_lock = borg.locking.Lock.migrate_lock.__wrapped__
             borg.locking.Lock.migrate_lock = borg.locking.Lock.migrate_lock.__wrapped__
 
 
-    def test_debug_dump_archive_items(self):
-        self.create_test_files()
-        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
-        self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
-        with changedir("output"):
-            output = self.cmd(f"--repo={self.repository_location}", "debug", "dump-archive-items", "test")
-        output_dir = sorted(os.listdir("output"))
-        assert len(output_dir) > 0 and output_dir[0].startswith("000000_")
-        assert "Done." in output
-
-    def test_debug_dump_repo_objs(self):
-        self.create_test_files()
-        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
-        self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
-        with changedir("output"):
-            output = self.cmd(f"--repo={self.repository_location}", "debug", "dump-repo-objs")
-        output_dir = sorted(os.listdir("output"))
-        assert len(output_dir) > 0 and output_dir[0].startswith("00000000_")
-        assert "Done." in output
-
-    def test_debug_put_get_delete_obj(self):
-        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
-        data = b"some data"
-        hexkey = sha256(data).hexdigest()
-        self.create_regular_file("file", contents=data)
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "put-obj", "input/file")
-        assert hexkey in output
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "get-obj", hexkey, "output/file")
-        assert hexkey in output
-        with open("output/file", "rb") as f:
-            data_read = f.read()
-        assert data == data_read
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "delete-obj", hexkey)
-        assert "deleted" in output
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "delete-obj", hexkey)
-        assert "not found" in output
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "delete-obj", "invalid")
-        assert "is invalid" in output
-
     def test_init_interrupt(self):
     def test_init_interrupt(self):
         def raise_eof(*args, **kwargs):
         def raise_eof(*args, **kwargs):
             raise EOFError
             raise EOFError
@@ -3364,56 +3311,6 @@ id: 2 / e29442 3506da 4e1ea7 / 25f62a 5a3d41 - 02
         typed_input = b"2 / e29442 3506da 4e1ea7 / 25f62a 5a3d41 - 02\n\ny\n"
         typed_input = b"2 / e29442 3506da 4e1ea7 / 25f62a 5a3d41 - 02\n\ny\n"
         self.cmd(f"--repo={self.repository_location}", "key", "import", "--paper", input=typed_input)
         self.cmd(f"--repo={self.repository_location}", "key", "import", "--paper", input=typed_input)
 
 
-    def test_debug_dump_manifest(self):
-        self.create_regular_file("file1", size=1024 * 80)
-        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
-        self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
-        dump_file = self.output_path + "/dump"
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "dump-manifest", dump_file)
-        assert output == ""
-        with open(dump_file) as f:
-            result = json.load(f)
-        assert "archives" in result
-        assert "config" in result
-        assert "item_keys" in result
-        assert "timestamp" in result
-        assert "version" in result
-
-    def test_debug_dump_archive(self):
-        self.create_regular_file("file1", size=1024 * 80)
-        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
-        self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
-        dump_file = self.output_path + "/dump"
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "dump-archive", "test", dump_file)
-        assert output == ""
-        with open(dump_file) as f:
-            result = json.load(f)
-        assert "_name" in result
-        assert "_manifest_entry" in result
-        assert "_meta" in result
-        assert "_items" in result
-
-    def test_debug_refcount_obj(self):
-        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "refcount-obj", "0" * 64).strip()
-        assert (
-            output
-            == "object 0000000000000000000000000000000000000000000000000000000000000000 not found [info from chunks cache]."
-        )
-
-        create_json = json.loads(self.cmd(f"--repo={self.repository_location}", "create", "--json", "test", "input"))
-        archive_id = create_json["archive"]["id"]
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "refcount-obj", archive_id).strip()
-        assert output == "object " + archive_id + " has 1 referrers [info from chunks cache]."
-
-        # Invalid IDs do not abort or return an error
-        output = self.cmd(f"--repo={self.repository_location}", "debug", "refcount-obj", "124", "xyza").strip()
-        assert output == "object id 124 is invalid.\nobject id xyza is invalid."
-
-    def test_debug_info(self):
-        output = self.cmd("debug", "info")
-        assert "Python" in output
-
     def test_benchmark_crud(self):
     def test_benchmark_crud(self):
         self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
         self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
         with environment_variable(_BORG_BENCHMARK_CRUD_TEST="YES"):
         with environment_variable(_BORG_BENCHMARK_CRUD_TEST="YES"):
@@ -3892,12 +3789,14 @@ class ManifestAuthenticationTest(ArchiverTestCaseBase):
             self.cmd(f"--repo={self.repository_location}", "rlist")
             self.cmd(f"--repo={self.repository_location}", "rlist")
 
 
 
 
-class RemoteArchiverTestCase(ArchiverTestCase):
+class RemoteArchiverTestCaseBase:
     prefix = "ssh://__testsuite__"
     prefix = "ssh://__testsuite__"
 
 
     def open_repository(self):
     def open_repository(self):
         return RemoteRepository(Location(self.repository_location))
         return RemoteRepository(Location(self.repository_location))
 
 
+
+class RemoteArchiverTestCase(RemoteArchiverTestCaseBase, ArchiverTestCase):
     def test_remote_repo_restrict_to_path(self):
     def test_remote_repo_restrict_to_path(self):
         # restricted to repo directory itself:
         # restricted to repo directory itself:
         with patch.object(RemoteRepository, "extra_test_args", ["--restrict-to-path", self.repository_path]):
         with patch.object(RemoteRepository, "extra_test_args", ["--restrict-to-path", self.repository_path]):
@@ -3930,10 +3829,6 @@ class RemoteArchiverTestCase(ArchiverTestCase):
             with pytest.raises(PathNotAllowed):
             with pytest.raises(PathNotAllowed):
                 self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
                 self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
 
 
-    @unittest.skip("only works locally")
-    def test_debug_put_get_delete_obj(self):
-        pass
-
     @unittest.skip("only works locally")
     @unittest.skip("only works locally")
     def test_config(self):
     def test_config(self):
         pass
         pass

+ 120 - 0
src/borg/testsuite/archiver/debug_cmds.py

@@ -0,0 +1,120 @@
+import json
+import os
+import pstats
+import unittest
+from hashlib import sha256
+
+from ...constants import *  # NOQA
+from .. import changedir
+from . import ArchiverTestCaseBase, RemoteArchiverTestCaseBase, RK_ENCRYPTION
+
+
+class ArchiverTestCase(ArchiverTestCaseBase):
+    def test_debug_profile(self):
+        self.create_test_files()
+        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
+        self.cmd(f"--repo={self.repository_location}", "create", "test", "input", "--debug-profile=create.prof")
+        self.cmd("debug", "convert-profile", "create.prof", "create.pyprof")
+        stats = pstats.Stats("create.pyprof")
+        stats.strip_dirs()
+        stats.sort_stats("cumtime")
+
+        self.cmd(f"--repo={self.repository_location}", "create", "test2", "input", "--debug-profile=create.pyprof")
+        stats = pstats.Stats("create.pyprof")  # Only do this on trusted data!
+        stats.strip_dirs()
+        stats.sort_stats("cumtime")
+
+    def test_debug_dump_archive_items(self):
+        self.create_test_files()
+        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
+        self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
+        with changedir("output"):
+            output = self.cmd(f"--repo={self.repository_location}", "debug", "dump-archive-items", "test")
+        output_dir = sorted(os.listdir("output"))
+        assert len(output_dir) > 0 and output_dir[0].startswith("000000_")
+        assert "Done." in output
+
+    def test_debug_dump_repo_objs(self):
+        self.create_test_files()
+        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
+        self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
+        with changedir("output"):
+            output = self.cmd(f"--repo={self.repository_location}", "debug", "dump-repo-objs")
+        output_dir = sorted(os.listdir("output"))
+        assert len(output_dir) > 0 and output_dir[0].startswith("00000000_")
+        assert "Done." in output
+
+    def test_debug_put_get_delete_obj(self):
+        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
+        data = b"some data"
+        hexkey = sha256(data).hexdigest()
+        self.create_regular_file("file", contents=data)
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "put-obj", "input/file")
+        assert hexkey in output
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "get-obj", hexkey, "output/file")
+        assert hexkey in output
+        with open("output/file", "rb") as f:
+            data_read = f.read()
+        assert data == data_read
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "delete-obj", hexkey)
+        assert "deleted" in output
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "delete-obj", hexkey)
+        assert "not found" in output
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "delete-obj", "invalid")
+        assert "is invalid" in output
+
+    def test_debug_dump_manifest(self):
+        self.create_regular_file("file1", size=1024 * 80)
+        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
+        self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
+        dump_file = self.output_path + "/dump"
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "dump-manifest", dump_file)
+        assert output == ""
+        with open(dump_file) as f:
+            result = json.load(f)
+        assert "archives" in result
+        assert "config" in result
+        assert "item_keys" in result
+        assert "timestamp" in result
+        assert "version" in result
+
+    def test_debug_dump_archive(self):
+        self.create_regular_file("file1", size=1024 * 80)
+        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
+        self.cmd(f"--repo={self.repository_location}", "create", "test", "input")
+        dump_file = self.output_path + "/dump"
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "dump-archive", "test", dump_file)
+        assert output == ""
+        with open(dump_file) as f:
+            result = json.load(f)
+        assert "_name" in result
+        assert "_manifest_entry" in result
+        assert "_meta" in result
+        assert "_items" in result
+
+    def test_debug_refcount_obj(self):
+        self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "refcount-obj", "0" * 64).strip()
+        assert (
+            output
+            == "object 0000000000000000000000000000000000000000000000000000000000000000 not found [info from chunks cache]."
+        )
+
+        create_json = json.loads(self.cmd(f"--repo={self.repository_location}", "create", "--json", "test", "input"))
+        archive_id = create_json["archive"]["id"]
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "refcount-obj", archive_id).strip()
+        assert output == "object " + archive_id + " has 1 referrers [info from chunks cache]."
+
+        # Invalid IDs do not abort or return an error
+        output = self.cmd(f"--repo={self.repository_location}", "debug", "refcount-obj", "124", "xyza").strip()
+        assert output == "object id 124 is invalid.\nobject id xyza is invalid."
+
+    def test_debug_info(self):
+        output = self.cmd("debug", "info")
+        assert "Python" in output
+
+
+class RemoteArchiverTestCase(RemoteArchiverTestCaseBase, ArchiverTestCase):
+    @unittest.skip("only works locally")
+    def test_debug_put_get_delete_obj(self):
+        pass