blob: 87d069de65a91d7c50a5e1f10bc3198d4b95ba1f [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005import os
6import tempfile
7import fnmatch
8
9from oeqa.selftest.case import OESelftestTestCase
10from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
Brad Bishopd7bf8c12018-02-25 22:55:05 -050011
12class OePkgdataUtilTests(OESelftestTestCase):
13
14 @classmethod
15 def setUpClass(cls):
16 super(OePkgdataUtilTests, cls).setUpClass()
17 # Ensure we have the right data in pkgdata
18 cls.logger.info('Running bitbake to generate pkgdata')
Brad Bishop19323692019-04-05 15:28:33 -040019 bitbake('target-sdk-provides-dummy -c clean')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020 bitbake('busybox zlib m4')
21
Brad Bishopd7bf8c12018-02-25 22:55:05 -050022 def test_lookup_pkg(self):
23 # Forward tests
24 result = runCmd('oe-pkgdata-util lookup-pkg "zlib busybox"')
25 self.assertEqual(result.output, 'libz1\nbusybox')
26 result = runCmd('oe-pkgdata-util lookup-pkg zlib-dev')
27 self.assertEqual(result.output, 'libz-dev')
28 result = runCmd('oe-pkgdata-util lookup-pkg nonexistentpkg', ignore_status=True)
29 self.assertEqual(result.status, 1, "Status different than 1. output: %s" % result.output)
30 self.assertEqual(result.output, 'ERROR: The following packages could not be found: nonexistentpkg')
31 # Reverse tests
32 result = runCmd('oe-pkgdata-util lookup-pkg -r "libz1 busybox"')
33 self.assertEqual(result.output, 'zlib\nbusybox')
34 result = runCmd('oe-pkgdata-util lookup-pkg -r libz-dev')
35 self.assertEqual(result.output, 'zlib-dev')
36 result = runCmd('oe-pkgdata-util lookup-pkg -r nonexistentpkg', ignore_status=True)
37 self.assertEqual(result.status, 1, "Status different than 1. output: %s" % result.output)
38 self.assertEqual(result.output, 'ERROR: The following packages could not be found: nonexistentpkg')
39
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 def test_read_value(self):
41 result = runCmd('oe-pkgdata-util read-value PN libz1')
42 self.assertEqual(result.output, 'zlib')
43 result = runCmd('oe-pkgdata-util read-value PKG libz1')
44 self.assertEqual(result.output, 'libz1')
45 result = runCmd('oe-pkgdata-util read-value PKGSIZE m4')
46 pkgsize = int(result.output.strip())
47 self.assertGreater(pkgsize, 1, "Size should be greater than 1. %s" % result.output)
48
Brad Bishopd7bf8c12018-02-25 22:55:05 -050049 def test_find_path(self):
Patrick Williamsdb4c27e2022-08-05 08:10:29 -050050 result = runCmd('oe-pkgdata-util find-path /usr/lib/libz.so.1')
51 self.assertEqual(result.output, 'zlib: /usr/lib/libz.so.1')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052 result = runCmd('oe-pkgdata-util find-path /usr/bin/m4')
53 self.assertEqual(result.output, 'm4: /usr/bin/m4')
54 result = runCmd('oe-pkgdata-util find-path /not/exist', ignore_status=True)
55 self.assertEqual(result.status, 1, "Status different than 1. output: %s" % result.output)
56 self.assertEqual(result.output, 'ERROR: Unable to find any package producing path /not/exist')
57
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058 def test_lookup_recipe(self):
59 result = runCmd('oe-pkgdata-util lookup-recipe "libz-staticdev busybox"')
60 self.assertEqual(result.output, 'zlib\nbusybox')
61 result = runCmd('oe-pkgdata-util lookup-recipe libz-dbg')
62 self.assertEqual(result.output, 'zlib')
63 result = runCmd('oe-pkgdata-util lookup-recipe nonexistentpkg', ignore_status=True)
64 self.assertEqual(result.status, 1, "Status different than 1. output: %s" % result.output)
65 self.assertEqual(result.output, 'ERROR: The following packages could not be found: nonexistentpkg')
66
Brad Bishopd7bf8c12018-02-25 22:55:05 -050067 def test_list_pkgs(self):
68 # No arguments
69 result = runCmd('oe-pkgdata-util list-pkgs')
70 pkglist = result.output.split()
71 self.assertIn('zlib', pkglist, "Listed packages: %s" % result.output)
72 self.assertIn('zlib-dev', pkglist, "Listed packages: %s" % result.output)
73 # No pkgspec, runtime
74 result = runCmd('oe-pkgdata-util list-pkgs -r')
75 pkglist = result.output.split()
76 self.assertIn('libz-dev', pkglist, "Listed packages: %s" % result.output)
77 # With recipe specified
78 result = runCmd('oe-pkgdata-util list-pkgs -p zlib')
79 pkglist = sorted(result.output.split())
80 try:
81 pkglist.remove('zlib-ptest') # in case ptest is disabled
82 except ValueError:
83 pass
Brad Bishop19323692019-04-05 15:28:33 -040084 self.assertEqual(pkglist, ['zlib', 'zlib-dbg', 'zlib-dev', 'zlib-doc', 'zlib-src', 'zlib-staticdev'], "Packages listed after remove: %s" % result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050085 # With recipe specified, runtime
86 result = runCmd('oe-pkgdata-util list-pkgs -p zlib -r')
87 pkglist = sorted(result.output.split())
88 try:
89 pkglist.remove('libz-ptest') # in case ptest is disabled
90 except ValueError:
91 pass
Brad Bishop19323692019-04-05 15:28:33 -040092 self.assertEqual(pkglist, ['libz-dbg', 'libz-dev', 'libz-doc', 'libz-src', 'libz-staticdev', 'libz1'], "Packages listed after remove: %s" % result.output)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093 # With recipe specified and unpackaged
94 result = runCmd('oe-pkgdata-util list-pkgs -p zlib -u')
95 pkglist = sorted(result.output.split())
96 self.assertIn('zlib-locale', pkglist, "Listed packages: %s" % result.output)
97 # With recipe specified and unpackaged, runtime
98 result = runCmd('oe-pkgdata-util list-pkgs -p zlib -u -r')
99 pkglist = sorted(result.output.split())
100 self.assertIn('libz-locale', pkglist, "Listed packages: %s" % result.output)
101 # With recipe specified and pkgspec
102 result = runCmd('oe-pkgdata-util list-pkgs -p zlib "*-d*"')
103 pkglist = sorted(result.output.split())
104 self.assertEqual(pkglist, ['zlib-dbg', 'zlib-dev', 'zlib-doc'], "Packages listed: %s" % result.output)
105 # With recipe specified and pkgspec, runtime
106 result = runCmd('oe-pkgdata-util list-pkgs -p zlib -r "*-d*"')
107 pkglist = sorted(result.output.split())
108 self.assertEqual(pkglist, ['libz-dbg', 'libz-dev', 'libz-doc'], "Packages listed: %s" % result.output)
109
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500110 def test_list_pkg_files(self):
111 def splitoutput(output):
112 files = {}
113 curpkg = None
114 for line in output.splitlines():
115 if line.startswith('\t'):
116 self.assertTrue(curpkg, 'Unexpected non-package line:\n%s' % line)
117 files[curpkg].append(line.strip())
118 else:
119 self.assertTrue(line.rstrip().endswith(':'), 'Invalid package line in output:\n%s' % line)
120 curpkg = line.split(':')[0]
121 files[curpkg] = []
122 return files
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500123 bb_vars = get_bb_vars(['libdir', 'includedir', 'mandir'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124 libdir = bb_vars['libdir']
125 includedir = bb_vars['includedir']
126 mandir = bb_vars['mandir']
127 # Test recipe-space package name
128 result = runCmd('oe-pkgdata-util list-pkg-files zlib-dev zlib-doc')
129 files = splitoutput(result.output)
130 self.assertIn('zlib-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
131 self.assertIn('zlib-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
132 self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev'])
133 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc'])
134 # Test runtime package name
135 result = runCmd('oe-pkgdata-util list-pkg-files -r libz1 libz-dev')
136 files = splitoutput(result.output)
137 self.assertIn('libz1', list(files.keys()), "listed pkgs. files: %s" %result.output)
138 self.assertIn('libz-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
139 self.assertGreater(len(files['libz1']), 1)
Patrick Williamsdb4c27e2022-08-05 08:10:29 -0500140 libspec = os.path.join(libdir, 'libz.so.1.*')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500141 found = False
142 for fileitem in files['libz1']:
143 if fnmatch.fnmatchcase(fileitem, libspec):
144 found = True
145 break
146 self.assertTrue(found, 'Could not find zlib library file %s in libz1 package file list: %s' % (libspec, files['libz1']))
147 self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev'])
148 # Test recipe
149 result = runCmd('oe-pkgdata-util list-pkg-files -p zlib')
150 files = splitoutput(result.output)
151 self.assertIn('zlib-dbg', list(files.keys()), "listed pkgs. files: %s" %result.output)
152 self.assertIn('zlib-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
153 self.assertIn('zlib-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
154 self.assertIn('zlib-staticdev', list(files.keys()), "listed pkgs. files: %s" %result.output)
155 self.assertIn('zlib', list(files.keys()), "listed pkgs. files: %s" %result.output)
156 self.assertNotIn('zlib-locale', list(files.keys()), "listed pkgs. files: %s" %result.output)
157 # (ignore ptest, might not be there depending on config)
158 self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev'])
159 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc'])
160 self.assertIn(os.path.join(libdir, 'libz.a'), files['zlib-staticdev'])
161 # Test recipe, runtime
162 result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -r')
163 files = splitoutput(result.output)
164 self.assertIn('libz-dbg', list(files.keys()), "listed pkgs. files: %s" %result.output)
165 self.assertIn('libz-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
166 self.assertIn('libz-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
167 self.assertIn('libz-staticdev', list(files.keys()), "listed pkgs. files: %s" %result.output)
168 self.assertIn('libz1', list(files.keys()), "listed pkgs. files: %s" %result.output)
169 self.assertNotIn('libz-locale', list(files.keys()), "listed pkgs. files: %s" %result.output)
170 self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev'])
171 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['libz-doc'])
172 self.assertIn(os.path.join(libdir, 'libz.a'), files['libz-staticdev'])
173 # Test recipe, unpackaged
174 result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -u')
175 files = splitoutput(result.output)
176 self.assertIn('zlib-dbg', list(files.keys()), "listed pkgs. files: %s" %result.output)
177 self.assertIn('zlib-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
178 self.assertIn('zlib-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
179 self.assertIn('zlib-staticdev', list(files.keys()), "listed pkgs. files: %s" %result.output)
180 self.assertIn('zlib', list(files.keys()), "listed pkgs. files: %s" %result.output)
181 self.assertIn('zlib-locale', list(files.keys()), "listed pkgs. files: %s" %result.output) # this is the key one
182 self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev'])
183 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc'])
184 self.assertIn(os.path.join(libdir, 'libz.a'), files['zlib-staticdev'])
185 # Test recipe, runtime, unpackaged
186 result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -r -u')
187 files = splitoutput(result.output)
188 self.assertIn('libz-dbg', list(files.keys()), "listed pkgs. files: %s" %result.output)
189 self.assertIn('libz-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
190 self.assertIn('libz-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
191 self.assertIn('libz-staticdev', list(files.keys()), "listed pkgs. files: %s" %result.output)
192 self.assertIn('libz1', list(files.keys()), "listed pkgs. files: %s" %result.output)
193 self.assertIn('libz-locale', list(files.keys()), "listed pkgs. files: %s" %result.output) # this is the key one
194 self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev'])
195 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['libz-doc'])
196 self.assertIn(os.path.join(libdir, 'libz.a'), files['libz-staticdev'])
197
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500198 def test_glob(self):
199 tempdir = tempfile.mkdtemp(prefix='pkgdataqa')
200 self.track_for_cleanup(tempdir)
201 pkglistfile = os.path.join(tempdir, 'pkglist')
202 with open(pkglistfile, 'w') as f:
203 f.write('libz1\n')
204 f.write('busybox\n')
205 result = runCmd('oe-pkgdata-util glob %s "*-dev"' % pkglistfile)
206 desiredresult = ['libz-dev', 'busybox-dev']
207 self.assertEqual(sorted(result.output.split()), sorted(desiredresult))
208 # The following should not error (because when we use this during rootfs construction, sometimes the complementary package won't exist)
209 result = runCmd('oe-pkgdata-util glob %s "*-nonexistent"' % pkglistfile)
210 self.assertEqual(result.output, '')
211 # Test exclude option
212 result = runCmd('oe-pkgdata-util glob %s "*-dev *-dbg" -x "^libz"' % pkglistfile)
213 resultlist = result.output.split()
214 self.assertNotIn('libz-dev', resultlist)
215 self.assertNotIn('libz-dbg', resultlist)
216
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500217 def test_specify_pkgdatadir(self):
218 result = runCmd('oe-pkgdata-util -p %s lookup-pkg zlib' % get_bb_var('PKGDATA_DIR'))
219 self.assertEqual(result.output, 'libz1')
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600220
221 def test_no_param(self):
222 result = runCmd('oe-pkgdata-util', ignore_status=True)
223 self.assertEqual(result.status, 2, "Status different than 2. output: %s" % result.output)
224 currpos = result.output.find('usage: oe-pkgdata-util')
225 self.assertTrue(currpos != -1, msg = "Test is Failed. Help is not Displayed in %s" % result.output)