Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | import unittest |
| 2 | import os |
| 3 | import tempfile |
| 4 | import logging |
| 5 | import fnmatch |
| 6 | |
| 7 | import oeqa.utils.ftools as ftools |
| 8 | from oeqa.selftest.base import oeSelfTest |
| 9 | from oeqa.utils.commands import runCmd, bitbake, get_bb_var |
| 10 | from oeqa.utils.decorators import testcase |
| 11 | |
| 12 | class OePkgdataUtilTests(oeSelfTest): |
| 13 | |
| 14 | @classmethod |
| 15 | def setUpClass(cls): |
| 16 | # Ensure we have the right data in pkgdata |
| 17 | logger = logging.getLogger("selftest") |
| 18 | logger.info('Running bitbake to generate pkgdata') |
| 19 | bitbake('glibc busybox zlib bash') |
| 20 | |
| 21 | @testcase(1203) |
| 22 | def test_lookup_pkg(self): |
| 23 | # Forward tests |
| 24 | result = runCmd('oe-pkgdata-util lookup-pkg "glibc busybox"') |
| 25 | self.assertEqual(result.output, 'libc6\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 "libc6 busybox"') |
| 33 | self.assertEqual(result.output, 'glibc\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 | |
| 40 | @testcase(1205) |
| 41 | def test_read_value(self): |
| 42 | result = runCmd('oe-pkgdata-util read-value PN libz1') |
| 43 | self.assertEqual(result.output, 'zlib') |
| 44 | result = runCmd('oe-pkgdata-util read-value PKGSIZE bash') |
| 45 | pkgsize = int(result.output.strip()) |
| 46 | self.assertGreater(pkgsize, 1, "Size should be greater than 1. %s" % result.output) |
| 47 | |
| 48 | @testcase(1198) |
| 49 | def test_find_path(self): |
| 50 | result = runCmd('oe-pkgdata-util find-path /lib/libc.so.6') |
| 51 | self.assertEqual(result.output, 'glibc: /lib/libc.so.6') |
| 52 | result = runCmd('oe-pkgdata-util find-path /bin/bash') |
| 53 | self.assertEqual(result.output, 'bash: /bin/bash') |
| 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 | |
| 58 | @testcase(1204) |
| 59 | def test_lookup_recipe(self): |
| 60 | result = runCmd('oe-pkgdata-util lookup-recipe "libc6-staticdev busybox"') |
| 61 | self.assertEqual(result.output, 'glibc\nbusybox') |
| 62 | result = runCmd('oe-pkgdata-util lookup-recipe libz-dbg') |
| 63 | self.assertEqual(result.output, 'zlib') |
| 64 | result = runCmd('oe-pkgdata-util lookup-recipe nonexistentpkg', ignore_status=True) |
| 65 | self.assertEqual(result.status, 1, "Status different than 1. output: %s" % result.output) |
| 66 | self.assertEqual(result.output, 'ERROR: The following packages could not be found: nonexistentpkg') |
| 67 | |
| 68 | @testcase(1202) |
| 69 | def test_list_pkgs(self): |
| 70 | # No arguments |
| 71 | result = runCmd('oe-pkgdata-util list-pkgs') |
| 72 | pkglist = result.output.split() |
| 73 | self.assertIn('glibc-utils', pkglist, "Listed packages: %s" % result.output) |
| 74 | self.assertIn('zlib-dev', pkglist, "Listed packages: %s" % result.output) |
| 75 | # No pkgspec, runtime |
| 76 | result = runCmd('oe-pkgdata-util list-pkgs -r') |
| 77 | pkglist = result.output.split() |
| 78 | self.assertIn('libc6-utils', pkglist, "Listed packages: %s" % result.output) |
| 79 | self.assertIn('libz-dev', pkglist, "Listed packages: %s" % result.output) |
| 80 | # With recipe specified |
| 81 | result = runCmd('oe-pkgdata-util list-pkgs -p zlib') |
| 82 | pkglist = sorted(result.output.split()) |
| 83 | try: |
| 84 | pkglist.remove('zlib-ptest') # in case ptest is disabled |
| 85 | except ValueError: |
| 86 | pass |
| 87 | self.assertEqual(pkglist, ['zlib', 'zlib-dbg', 'zlib-dev', 'zlib-doc', 'zlib-staticdev'], "Packages listed after remove: %s" % result.output) |
| 88 | # With recipe specified, runtime |
| 89 | result = runCmd('oe-pkgdata-util list-pkgs -p zlib -r') |
| 90 | pkglist = sorted(result.output.split()) |
| 91 | try: |
| 92 | pkglist.remove('libz-ptest') # in case ptest is disabled |
| 93 | except ValueError: |
| 94 | pass |
| 95 | self.assertEqual(pkglist, ['libz-dbg', 'libz-dev', 'libz-doc', 'libz-staticdev', 'libz1'], "Packages listed after remove: %s" % result.output) |
| 96 | # With recipe specified and unpackaged |
| 97 | result = runCmd('oe-pkgdata-util list-pkgs -p zlib -u') |
| 98 | pkglist = sorted(result.output.split()) |
| 99 | self.assertIn('zlib-locale', pkglist, "Listed packages: %s" % result.output) |
| 100 | # With recipe specified and unpackaged, runtime |
| 101 | result = runCmd('oe-pkgdata-util list-pkgs -p zlib -u -r') |
| 102 | pkglist = sorted(result.output.split()) |
| 103 | self.assertIn('libz-locale', pkglist, "Listed packages: %s" % result.output) |
| 104 | # With recipe specified and pkgspec |
| 105 | result = runCmd('oe-pkgdata-util list-pkgs -p zlib "*-d*"') |
| 106 | pkglist = sorted(result.output.split()) |
| 107 | self.assertEqual(pkglist, ['zlib-dbg', 'zlib-dev', 'zlib-doc'], "Packages listed: %s" % result.output) |
| 108 | # With recipe specified and pkgspec, runtime |
| 109 | result = runCmd('oe-pkgdata-util list-pkgs -p zlib -r "*-d*"') |
| 110 | pkglist = sorted(result.output.split()) |
| 111 | self.assertEqual(pkglist, ['libz-dbg', 'libz-dev', 'libz-doc'], "Packages listed: %s" % result.output) |
| 112 | |
| 113 | @testcase(1201) |
| 114 | def test_list_pkg_files(self): |
| 115 | def splitoutput(output): |
| 116 | files = {} |
| 117 | curpkg = None |
| 118 | for line in output.splitlines(): |
| 119 | if line.startswith('\t'): |
| 120 | self.assertTrue(curpkg, 'Unexpected non-package line:\n%s' % line) |
| 121 | files[curpkg].append(line.strip()) |
| 122 | else: |
| 123 | self.assertTrue(line.rstrip().endswith(':'), 'Invalid package line in output:\n%s' % line) |
| 124 | curpkg = line.split(':')[0] |
| 125 | files[curpkg] = [] |
| 126 | return files |
| 127 | base_libdir = get_bb_var('base_libdir') |
| 128 | libdir = get_bb_var('libdir') |
| 129 | includedir = get_bb_var('includedir') |
| 130 | mandir = get_bb_var('mandir') |
| 131 | # Test recipe-space package name |
| 132 | result = runCmd('oe-pkgdata-util list-pkg-files zlib-dev zlib-doc') |
| 133 | files = splitoutput(result.output) |
| 134 | self.assertIn('zlib-dev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 135 | self.assertIn('zlib-doc', files.keys(), "listed pkgs. files: %s" %result.output) |
| 136 | self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev']) |
| 137 | self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc']) |
| 138 | # Test runtime package name |
| 139 | result = runCmd('oe-pkgdata-util list-pkg-files -r libz1 libz-dev') |
| 140 | files = splitoutput(result.output) |
| 141 | self.assertIn('libz1', files.keys(), "listed pkgs. files: %s" %result.output) |
| 142 | self.assertIn('libz-dev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 143 | self.assertGreater(len(files['libz1']), 1) |
| 144 | libspec = os.path.join(base_libdir, 'libz.so.1.*') |
| 145 | found = False |
| 146 | for fileitem in files['libz1']: |
| 147 | if fnmatch.fnmatchcase(fileitem, libspec): |
| 148 | found = True |
| 149 | break |
| 150 | self.assertTrue(found, 'Could not find zlib library file %s in libz1 package file list: %s' % (libspec, files['libz1'])) |
| 151 | self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev']) |
| 152 | # Test recipe |
| 153 | result = runCmd('oe-pkgdata-util list-pkg-files -p zlib') |
| 154 | files = splitoutput(result.output) |
| 155 | self.assertIn('zlib-dbg', files.keys(), "listed pkgs. files: %s" %result.output) |
| 156 | self.assertIn('zlib-doc', files.keys(), "listed pkgs. files: %s" %result.output) |
| 157 | self.assertIn('zlib-dev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 158 | self.assertIn('zlib-staticdev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 159 | self.assertIn('zlib', files.keys(), "listed pkgs. files: %s" %result.output) |
| 160 | self.assertNotIn('zlib-locale', files.keys(), "listed pkgs. files: %s" %result.output) |
| 161 | # (ignore ptest, might not be there depending on config) |
| 162 | self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev']) |
| 163 | self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc']) |
| 164 | self.assertIn(os.path.join(libdir, 'libz.a'), files['zlib-staticdev']) |
| 165 | # Test recipe, runtime |
| 166 | result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -r') |
| 167 | files = splitoutput(result.output) |
| 168 | self.assertIn('libz-dbg', files.keys(), "listed pkgs. files: %s" %result.output) |
| 169 | self.assertIn('libz-doc', files.keys(), "listed pkgs. files: %s" %result.output) |
| 170 | self.assertIn('libz-dev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 171 | self.assertIn('libz-staticdev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 172 | self.assertIn('libz1', files.keys(), "listed pkgs. files: %s" %result.output) |
| 173 | self.assertNotIn('libz-locale', files.keys(), "listed pkgs. files: %s" %result.output) |
| 174 | self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev']) |
| 175 | self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['libz-doc']) |
| 176 | self.assertIn(os.path.join(libdir, 'libz.a'), files['libz-staticdev']) |
| 177 | # Test recipe, unpackaged |
| 178 | result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -u') |
| 179 | files = splitoutput(result.output) |
| 180 | self.assertIn('zlib-dbg', files.keys(), "listed pkgs. files: %s" %result.output) |
| 181 | self.assertIn('zlib-doc', files.keys(), "listed pkgs. files: %s" %result.output) |
| 182 | self.assertIn('zlib-dev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 183 | self.assertIn('zlib-staticdev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 184 | self.assertIn('zlib', files.keys(), "listed pkgs. files: %s" %result.output) |
| 185 | self.assertIn('zlib-locale', files.keys(), "listed pkgs. files: %s" %result.output) # this is the key one |
| 186 | self.assertIn(os.path.join(includedir, 'zlib.h'), files['zlib-dev']) |
| 187 | self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['zlib-doc']) |
| 188 | self.assertIn(os.path.join(libdir, 'libz.a'), files['zlib-staticdev']) |
| 189 | # Test recipe, runtime, unpackaged |
| 190 | result = runCmd('oe-pkgdata-util list-pkg-files -p zlib -r -u') |
| 191 | files = splitoutput(result.output) |
| 192 | self.assertIn('libz-dbg', files.keys(), "listed pkgs. files: %s" %result.output) |
| 193 | self.assertIn('libz-doc', files.keys(), "listed pkgs. files: %s" %result.output) |
| 194 | self.assertIn('libz-dev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 195 | self.assertIn('libz-staticdev', files.keys(), "listed pkgs. files: %s" %result.output) |
| 196 | self.assertIn('libz1', files.keys(), "listed pkgs. files: %s" %result.output) |
| 197 | self.assertIn('libz-locale', files.keys(), "listed pkgs. files: %s" %result.output) # this is the key one |
| 198 | self.assertIn(os.path.join(includedir, 'zlib.h'), files['libz-dev']) |
| 199 | self.assertIn(os.path.join(mandir, 'man3/zlib.3'), files['libz-doc']) |
| 200 | self.assertIn(os.path.join(libdir, 'libz.a'), files['libz-staticdev']) |
| 201 | |
| 202 | @testcase(1200) |
| 203 | def test_glob(self): |
| 204 | tempdir = tempfile.mkdtemp(prefix='pkgdataqa') |
| 205 | self.track_for_cleanup(tempdir) |
| 206 | pkglistfile = os.path.join(tempdir, 'pkglist') |
| 207 | with open(pkglistfile, 'w') as f: |
| 208 | f.write('libc6\n') |
| 209 | f.write('libz1\n') |
| 210 | f.write('busybox\n') |
| 211 | result = runCmd('oe-pkgdata-util glob %s "*-dev"' % pkglistfile) |
| 212 | desiredresult = ['libc6-dev', 'libz-dev', 'busybox-dev'] |
| 213 | self.assertEqual(sorted(result.output.split()), sorted(desiredresult)) |
| 214 | # The following should not error (because when we use this during rootfs construction, sometimes the complementary package won't exist) |
| 215 | result = runCmd('oe-pkgdata-util glob %s "*-nonexistent"' % pkglistfile) |
| 216 | self.assertEqual(result.output, '') |
| 217 | # Test exclude option |
| 218 | result = runCmd('oe-pkgdata-util glob %s "*-dev *-dbg" -x "^libz"' % pkglistfile) |
| 219 | resultlist = result.output.split() |
| 220 | self.assertNotIn('libz-dev', resultlist) |
| 221 | self.assertNotIn('libz-dbg', resultlist) |
| 222 | |
| 223 | @testcase(1206) |
| 224 | def test_specify_pkgdatadir(self): |
| 225 | result = runCmd('oe-pkgdata-util -p %s lookup-pkg glibc' % get_bb_var('PKGDATA_DIR')) |
| 226 | self.assertEqual(result.output, 'libc6') |