blob: 833a1803ba632c5a5b27100172965e1b8381131a [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):
50 result = runCmd('oe-pkgdata-util find-path /lib/libz.so.1')
51 self.assertEqual(result.output, 'zlib: /lib/libz.so.1')
52 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
123 bb_vars = get_bb_vars(['base_libdir', 'libdir', 'includedir', 'mandir'])
124 base_libdir = bb_vars['base_libdir']
125 libdir = bb_vars['libdir']
126 includedir = bb_vars['includedir']
127 mandir = bb_vars['mandir']
128 # Test recipe-space package name
129 result = runCmd('oe-pkgdata-util list-pkg-files zlib-dev zlib-doc')
130 files = splitoutput(result.output)
131 self.assertIn('zlib-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
132 self.assertIn('zlib-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
133 self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev'])
134 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc'])
135 # Test runtime package name
136 result = runCmd('oe-pkgdata-util list-pkg-files -r libz1 libz-dev')
137 files = splitoutput(result.output)
138 self.assertIn('libz1', list(files.keys()), "listed pkgs. files: %s" %result.output)
139 self.assertIn('libz-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
140 self.assertGreater(len(files['libz1']), 1)
141 libspec = os.path.join(base_libdir, 'libz.so.1.*')
142 found = False
143 for fileitem in files['libz1']:
144 if fnmatch.fnmatchcase(fileitem, libspec):
145 found = True
146 break
147 self.assertTrue(found, 'Could not find zlib library file %s in libz1 package file list: %s' % (libspec, files['libz1']))
148 self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev'])
149 # Test recipe
150 result = runCmd('oe-pkgdata-util list-pkg-files -p zlib')
151 files = splitoutput(result.output)
152 self.assertIn('zlib-dbg', list(files.keys()), "listed pkgs. files: %s" %result.output)
153 self.assertIn('zlib-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
154 self.assertIn('zlib-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
155 self.assertIn('zlib-staticdev', list(files.keys()), "listed pkgs. files: %s" %result.output)
156 self.assertIn('zlib', list(files.keys()), "listed pkgs. files: %s" %result.output)
157 self.assertNotIn('zlib-locale', list(files.keys()), "listed pkgs. files: %s" %result.output)
158 # (ignore ptest, might not be there depending on config)
159 self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev'])
160 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc'])
161 self.assertIn(os.path.join(libdir, 'libz.a'), files['zlib-staticdev'])
162 # Test recipe, runtime
163 result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -r')
164 files = splitoutput(result.output)
165 self.assertIn('libz-dbg', list(files.keys()), "listed pkgs. files: %s" %result.output)
166 self.assertIn('libz-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
167 self.assertIn('libz-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
168 self.assertIn('libz-staticdev', list(files.keys()), "listed pkgs. files: %s" %result.output)
169 self.assertIn('libz1', list(files.keys()), "listed pkgs. files: %s" %result.output)
170 self.assertNotIn('libz-locale', list(files.keys()), "listed pkgs. files: %s" %result.output)
171 self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev'])
172 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['libz-doc'])
173 self.assertIn(os.path.join(libdir, 'libz.a'), files['libz-staticdev'])
174 # Test recipe, unpackaged
175 result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -u')
176 files = splitoutput(result.output)
177 self.assertIn('zlib-dbg', list(files.keys()), "listed pkgs. files: %s" %result.output)
178 self.assertIn('zlib-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
179 self.assertIn('zlib-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
180 self.assertIn('zlib-staticdev', list(files.keys()), "listed pkgs. files: %s" %result.output)
181 self.assertIn('zlib', list(files.keys()), "listed pkgs. files: %s" %result.output)
182 self.assertIn('zlib-locale', list(files.keys()), "listed pkgs. files: %s" %result.output) # this is the key one
183 self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev'])
184 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc'])
185 self.assertIn(os.path.join(libdir, 'libz.a'), files['zlib-staticdev'])
186 # Test recipe, runtime, unpackaged
187 result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -r -u')
188 files = splitoutput(result.output)
189 self.assertIn('libz-dbg', list(files.keys()), "listed pkgs. files: %s" %result.output)
190 self.assertIn('libz-doc', list(files.keys()), "listed pkgs. files: %s" %result.output)
191 self.assertIn('libz-dev', list(files.keys()), "listed pkgs. files: %s" %result.output)
192 self.assertIn('libz-staticdev', list(files.keys()), "listed pkgs. files: %s" %result.output)
193 self.assertIn('libz1', list(files.keys()), "listed pkgs. files: %s" %result.output)
194 self.assertIn('libz-locale', list(files.keys()), "listed pkgs. files: %s" %result.output) # this is the key one
195 self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev'])
196 self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['libz-doc'])
197 self.assertIn(os.path.join(libdir, 'libz.a'), files['libz-staticdev'])
198
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500199 def test_glob(self):
200 tempdir = tempfile.mkdtemp(prefix='pkgdataqa')
201 self.track_for_cleanup(tempdir)
202 pkglistfile = os.path.join(tempdir, 'pkglist')
203 with open(pkglistfile, 'w') as f:
204 f.write('libz1\n')
205 f.write('busybox\n')
206 result = runCmd('oe-pkgdata-util glob %s "*-dev"' % pkglistfile)
207 desiredresult = ['libz-dev', 'busybox-dev']
208 self.assertEqual(sorted(result.output.split()), sorted(desiredresult))
209 # The following should not error (because when we use this during rootfs construction, sometimes the complementary package won't exist)
210 result = runCmd('oe-pkgdata-util glob %s "*-nonexistent"' % pkglistfile)
211 self.assertEqual(result.output, '')
212 # Test exclude option
213 result = runCmd('oe-pkgdata-util glob %s "*-dev *-dbg" -x "^libz"' % pkglistfile)
214 resultlist = result.output.split()
215 self.assertNotIn('libz-dev', resultlist)
216 self.assertNotIn('libz-dbg', resultlist)
217
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500218 def test_specify_pkgdatadir(self):
219 result = runCmd('oe-pkgdata-util -p %s lookup-pkg zlib' % get_bb_var('PKGDATA_DIR'))
220 self.assertEqual(result.output, 'libz1')