]> git.ipfire.org Git - people/ms/pakfire.git/blame - tests/python/archive.py
python: Add tests for the Archive class
[people/ms/pakfire.git] / tests / python / archive.py
CommitLineData
ba813665
MT
1#!/usr/bin/python3
2
3import datetime
4import os.path
5import pakfire
6import stat
7
8import tests
9
10class ArchiveTest(tests.TestCase):
11 """
12 This tests the Package class
13 """
14 def setUp(self):
15 self.pakfire = self.setup_pakfire()
16
17 def test_open(self):
18 """
19 Test if we can open an archive
20 """
21 path = self.path("beep-1.3-2.ip3.x86_64.pfm")
22
23 # Open the archive
24 archive = self.pakfire.open(path)
25
26 # Read back the path
27 self.assertEqual(archive.path, path)
28
29 # The format should be 6
30 self.assertEqual(archive.format, 6)
31
32 def test_filelist(self):
33 """
34 Test parsing the filelist that is stored in the archive
35 """
36 path = self.path("beep-1.3-2.ip3.x86_64.pfm")
37
38 # Open the archive
39 archive = self.pakfire.open(path)
40
41 # Check that we read the entire list of 7 files
42 self.assertEqual(len(archive.filelist), 7)
43
44 # Check that we got files
45 for file in archive.filelist:
46 self.assertIsInstance(file, pakfire.File)
47
48 # Check attributes of a few files
49 for file in archive.filelist:
50 # Check attributes of /usr/bin/beep
51 if file.path == "/usr/bin/beep":
52 self.assertEqual(file.type, stat.S_IFREG)
53
54 # Size
55 self.assertEqual(file.size, 17192)
56
57 # Mode
58 self.assertEqual(file.mode, 0o100755)
59
60 # Ownership
61 self.assertEqual(file.uname, "root")
62 self.assertEqual(file.gname, "root")
63
64 # Creation/Modification Time
65 self.assertEqual(file.ctime, datetime.datetime(2023, 2, 28, 18, 23, 48))
66 self.assertEqual(file.mtime, datetime.datetime(2023, 2, 28, 18, 23, 48))
67
68 # This file has no capabilities
69 self.assertIsNone(file.capabilities)
70
71 # This file has no mimetype
72 self.assertIsNone(file.mimetype)
73
74 def test_read(self):
75 """
76 Test whether we can read a file from the archive
77 """
78 # XXX needs tests for reading symlinks
79
80 path = self.path("beep-1.3-2.ip3.x86_64.pfm")
81
82 # Open the archive
83 archive = self.pakfire.open(path)
84
85 # Try opening /usr/bin/beep
86 f = archive.read("/usr/bin/beep")
87
88 # Check if we got the correct type
89 self.assertIsInstance(f, pakfire.ArchiveFile)
90
91 # Read the entire payload
92 payload = f.readall()
93
94 # Check type
95 self.assertIsInstance(payload, bytes)
96
97 # Make sure we read the entire file
98 self.assertEqual(len(payload), 17192)
99
100 # Check that the payload starts like an ELF file
101 self.assertEqual(payload[:4], b"\x7fELF")
102
103 # Try to read more after we have read everything
104 self.assertEqual(b"", f.read())
105
106 # Try reading a file which does not exist
107 with self.assertRaises(FileNotFoundError):
108 archive.read("/usr/bin/does-not-exist")
109
110 # Try reading a directory
111 with self.assertRaises(FileNotFoundError):
112 archive.read("/usr/bin")
113
114 def test_extract(self):
115 """
116 Tests extracting an archive
117 """
118 path = self.path("beep-1.3-2.ip3.x86_64.pfm")
119
120 # Open the archive
121 archive = self.pakfire.open(path)
122
123 with self.tempdir() as t:
124 # Perform extraction
125 archive.extract(path=t)
126
127 self.assertTrue(os.path.exists("%s/usr" % t))
128 self.assertTrue(os.path.exists("%s/usr/bin" % t))
129 self.assertTrue(os.path.exists("%s/usr/bin/beep" % t))
130 self.assertTrue(os.path.exists("%s/usr/share" % t))
131 self.assertTrue(os.path.exists("%s/usr/share/man" % t))
132 self.assertTrue(os.path.exists("%s/usr/share/man/man1" % t))
133 self.assertTrue(os.path.exists("%s/usr/share/man/man1/beep.1.xz" % t))
134
135 # XXX how can we check that there isn't anything else?
136
137 def test_package(self):
138 """
139 Tests reading package metadata
140 """
141 path = self.path("beep-1.3-2.ip3.x86_64.pfm")
142
143 # Open the archive
144 archive = self.pakfire.open(path)
145
146 # Fetch the package metadata
147 package = archive.get_package()
148
149 self.assertIsInstance(package, pakfire.Package)
150
151 # The rest will be tested in packages.py
152
153
154if __name__ == "__main__":
155 tests.main()