From ba813665ee1fa7de2937f1bfabb82116ff6b33da Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 26 Jan 2024 18:11:40 +0000 Subject: [PATCH] python: Add tests for the Archive class Signed-off-by: Michael Tremer --- Makefile.am | 1 + tests/python/archive.py | 155 ++++++++++++++++++++++++++++++++++++++++ tests/python/tests.py | 7 ++ 3 files changed, 163 insertions(+) create mode 100755 tests/python/archive.py diff --git a/Makefile.am b/Makefile.am index 0eae4f2ce..d064a6d20 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 index 000000000..64229a0b7 --- /dev/null +++ b/tests/python/archive.py @@ -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() diff --git a/tests/python/tests.py b/tests/python/tests.py index bc8509479..12009038f 100644 --- a/tests/python/tests.py +++ b/tests/python/tests.py @@ -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 -- 2.39.5