]> git.ipfire.org Git - pakfire.git/commitdiff
python: Add tests for the Archive class
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 Jan 2024 18:11:40 +0000 (18:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 26 Jan 2024 18:11:40 +0000 (18:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
tests/python/archive.py [new file with mode: 0755]
tests/python/tests.py

index 0eae4f2cea36668ed53d45b8bbd5bb52dd0c924b..d064a6d20281f752ef55416b5a8dd1eb527aca20 100644 (file)
@@ -1136,6 +1136,7 @@ TESTS_ENVIRONMENT = \
        topdir="$(shell pwd)"
 
 dist_check_SCRIPTS = \
+       tests/python/archive.py \
        tests/python/ctx.py \
        tests/python/keys.py \
        tests/python/jail.py \
diff --git a/tests/python/archive.py b/tests/python/archive.py
new file mode 100755 (executable)
index 0000000..64229a0
--- /dev/null
@@ -0,0 +1,155 @@
+#!/usr/bin/python3
+
+import datetime
+import os.path
+import pakfire
+import stat
+
+import tests
+
+class ArchiveTest(tests.TestCase):
+       """
+               This tests the Package class
+       """
+       def setUp(self):
+               self.pakfire = self.setup_pakfire()
+
+       def test_open(self):
+               """
+                       Test if we can open an archive
+               """
+               path = self.path("beep-1.3-2.ip3.x86_64.pfm")
+
+               # Open the archive
+               archive = self.pakfire.open(path)
+
+               # Read back the path
+               self.assertEqual(archive.path, path)
+
+               # The format should be 6
+               self.assertEqual(archive.format, 6)
+
+       def test_filelist(self):
+               """
+                       Test parsing the filelist that is stored in the archive
+               """
+               path = self.path("beep-1.3-2.ip3.x86_64.pfm")
+
+               # Open the archive
+               archive = self.pakfire.open(path)
+
+               # Check that we read the entire list of 7 files
+               self.assertEqual(len(archive.filelist), 7)
+
+               # Check that we got files
+               for file in archive.filelist:
+                       self.assertIsInstance(file, pakfire.File)
+
+               # Check attributes of a few files
+               for file in archive.filelist:
+                       # Check attributes of /usr/bin/beep
+                       if file.path == "/usr/bin/beep":
+                               self.assertEqual(file.type, stat.S_IFREG)
+
+                               # Size
+                               self.assertEqual(file.size, 17192)
+
+                               # Mode
+                               self.assertEqual(file.mode, 0o100755)
+
+                               # Ownership
+                               self.assertEqual(file.uname, "root")
+                               self.assertEqual(file.gname, "root")
+
+                               # Creation/Modification Time
+                               self.assertEqual(file.ctime, datetime.datetime(2023, 2, 28, 18, 23, 48))
+                               self.assertEqual(file.mtime, datetime.datetime(2023, 2, 28, 18, 23, 48))
+
+                               # This file has no capabilities
+                               self.assertIsNone(file.capabilities)
+
+                               # This file has no mimetype
+                               self.assertIsNone(file.mimetype)
+
+       def test_read(self):
+               """
+                       Test whether we can read a file from the archive
+               """
+               # XXX needs tests for reading symlinks
+
+               path = self.path("beep-1.3-2.ip3.x86_64.pfm")
+
+               # Open the archive
+               archive = self.pakfire.open(path)
+
+               # Try opening /usr/bin/beep
+               f = archive.read("/usr/bin/beep")
+
+               # Check if we got the correct type
+               self.assertIsInstance(f, pakfire.ArchiveFile)
+
+               # Read the entire payload
+               payload = f.readall()
+
+               # Check type
+               self.assertIsInstance(payload, bytes)
+
+               # Make sure we read the entire file
+               self.assertEqual(len(payload), 17192)
+
+               # Check that the payload starts like an ELF file
+               self.assertEqual(payload[:4], b"\x7fELF")
+
+               # Try to read more after we have read everything
+               self.assertEqual(b"", f.read())
+
+               # Try reading a file which does not exist
+               with self.assertRaises(FileNotFoundError):
+                       archive.read("/usr/bin/does-not-exist")
+
+               # Try reading a directory
+               with self.assertRaises(FileNotFoundError):
+                       archive.read("/usr/bin")
+
+       def test_extract(self):
+               """
+                       Tests extracting an archive
+               """
+               path = self.path("beep-1.3-2.ip3.x86_64.pfm")
+
+               # Open the archive
+               archive = self.pakfire.open(path)
+
+               with self.tempdir() as t:
+                       # Perform extraction
+                       archive.extract(path=t)
+
+                       self.assertTrue(os.path.exists("%s/usr" % t))
+                       self.assertTrue(os.path.exists("%s/usr/bin" % t))
+                       self.assertTrue(os.path.exists("%s/usr/bin/beep" % t))
+                       self.assertTrue(os.path.exists("%s/usr/share" % t))
+                       self.assertTrue(os.path.exists("%s/usr/share/man" % t))
+                       self.assertTrue(os.path.exists("%s/usr/share/man/man1" % t))
+                       self.assertTrue(os.path.exists("%s/usr/share/man/man1/beep.1.xz" % t))
+
+                       # XXX how can we check that there isn't anything else?
+
+       def test_package(self):
+               """
+                       Tests reading package metadata
+               """
+               path = self.path("beep-1.3-2.ip3.x86_64.pfm")
+
+               # Open the archive
+               archive = self.pakfire.open(path)
+
+               # Fetch the package metadata
+               package = archive.get_package()
+
+               self.assertIsInstance(package, pakfire.Package)
+
+               # The rest will be tested in packages.py
+
+
+if __name__ == "__main__":
+       tests.main()
index bc8509479644f1cbbf87fc93ec8913672f3e2e1c..12009038f960e0549b229e8da329790fedd86c72 100644 (file)
@@ -20,6 +20,7 @@
 
 import pakfire
 import os
+import tempfile
 import unittest
 
 # Fetch TEST_DATA_DIR
@@ -71,6 +72,12 @@ class TestCase(unittest.TestCase):
                # Open the file
                return open(path, *args, **kwargs)
 
+       def tempdir(self):
+               """
+                       Creates a temporary directory
+               """
+               return tempfile.TemporaryDirectory(prefix="pakfire-test-")
+
 
 # Overlay for now
 main = unittest.main