Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame^] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 7 | import os |
| 8 | import re |
| 9 | import shutil |
| 10 | import datetime |
| 11 | |
| 12 | import oeqa.utils.ftools as ftools |
| 13 | from oeqa.selftest.case import OESelftestTestCase |
| 14 | from oeqa.utils.commands import runCmd, bitbake, get_bb_var |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 15 | from oeqa.utils.network import get_free_port |
| 16 | |
| 17 | class BitbakePrTests(OESelftestTestCase): |
| 18 | |
| 19 | @classmethod |
| 20 | def setUpClass(cls): |
| 21 | super(BitbakePrTests, cls).setUpClass() |
| 22 | cls.pkgdata_dir = get_bb_var('PKGDATA_DIR') |
| 23 | |
| 24 | def get_pr_version(self, package_name): |
| 25 | package_data_file = os.path.join(self.pkgdata_dir, 'runtime', package_name) |
| 26 | package_data = ftools.read_file(package_data_file) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 27 | find_pr = re.search(r"PKGR: r[0-9]+\.([0-9]+)", package_data) |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 28 | self.assertTrue(find_pr, "No PKG revision found via regex 'PKGR: r[0-9]+\.([0-9]+)' in %s" % package_data_file) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 29 | return int(find_pr.group(1)) |
| 30 | |
| 31 | def get_task_stamp(self, package_name, recipe_task): |
| 32 | stampdata = get_bb_var('STAMP', target=package_name).split('/') |
| 33 | prefix = stampdata[-1] |
| 34 | package_stamps_path = "/".join(stampdata[:-1]) |
| 35 | stamps = [] |
| 36 | for stamp in os.listdir(package_stamps_path): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 37 | find_stamp = re.match(r"%s\.%s\.([a-z0-9]{32})" % (re.escape(prefix), recipe_task), stamp) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 38 | if find_stamp: |
| 39 | stamps.append(find_stamp.group(1)) |
| 40 | self.assertFalse(len(stamps) == 0, msg="Cound not find stamp for task %s for recipe %s" % (recipe_task, package_name)) |
| 41 | self.assertFalse(len(stamps) > 1, msg="Found multiple %s stamps for the %s recipe in the %s directory." % (recipe_task, package_name, package_stamps_path)) |
| 42 | return str(stamps[0]) |
| 43 | |
| 44 | def increment_package_pr(self, package_name): |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 45 | inc_data = "do_package:append() {\n bb.build.exec_func('do_test_prserv', d)\n}\ndo_test_prserv() {\necho \"The current date is: %s\" > ${PKGDESTWORK}/${PN}.datestamp\n}" % datetime.datetime.now() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 46 | self.write_recipeinc(package_name, inc_data) |
| 47 | res = bitbake(package_name, ignore_status=True) |
| 48 | self.delete_recipeinc(package_name) |
| 49 | self.assertEqual(res.status, 0, msg=res.output) |
| 50 | |
| 51 | def config_pr_tests(self, package_name, package_type='rpm', pr_socket='localhost:0'): |
| 52 | config_package_data = 'PACKAGE_CLASSES = "package_%s"' % package_type |
| 53 | self.write_config(config_package_data) |
| 54 | config_server_data = 'PRSERV_HOST = "%s"' % pr_socket |
| 55 | self.append_config(config_server_data) |
| 56 | |
| 57 | def run_test_pr_service(self, package_name, package_type='rpm', track_task='do_package', pr_socket='localhost:0'): |
| 58 | self.config_pr_tests(package_name, package_type, pr_socket) |
| 59 | |
| 60 | self.increment_package_pr(package_name) |
| 61 | pr_1 = self.get_pr_version(package_name) |
| 62 | stamp_1 = self.get_task_stamp(package_name, track_task) |
| 63 | |
| 64 | self.increment_package_pr(package_name) |
| 65 | pr_2 = self.get_pr_version(package_name) |
| 66 | stamp_2 = self.get_task_stamp(package_name, track_task) |
| 67 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 68 | self.assertTrue(pr_2 - pr_1 == 1, "New PR %s did not increment as expected (from %s), difference should be 1" % (pr_2, pr_1)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 69 | self.assertTrue(stamp_1 != stamp_2, "Different pkg rev. but same stamp: %s" % stamp_1) |
| 70 | |
| 71 | def run_test_pr_export_import(self, package_name, replace_current_db=True): |
| 72 | self.config_pr_tests(package_name) |
| 73 | |
| 74 | self.increment_package_pr(package_name) |
| 75 | pr_1 = self.get_pr_version(package_name) |
| 76 | |
| 77 | exported_db_path = os.path.join(self.builddir, 'export.inc') |
| 78 | export_result = runCmd("bitbake-prserv-tool export %s" % exported_db_path, ignore_status=True) |
| 79 | self.assertEqual(export_result.status, 0, msg="PR Service database export failed: %s" % export_result.output) |
| 80 | self.assertTrue(os.path.exists(exported_db_path)) |
| 81 | |
| 82 | if replace_current_db: |
| 83 | current_db_path = os.path.join(get_bb_var('PERSISTENT_DIR'), 'prserv.sqlite3') |
| 84 | self.assertTrue(os.path.exists(current_db_path), msg="Path to current PR Service database is invalid: %s" % current_db_path) |
| 85 | os.remove(current_db_path) |
| 86 | |
| 87 | import_result = runCmd("bitbake-prserv-tool import %s" % exported_db_path, ignore_status=True) |
| 88 | os.remove(exported_db_path) |
| 89 | self.assertEqual(import_result.status, 0, msg="PR Service database import failed: %s" % import_result.output) |
| 90 | |
| 91 | self.increment_package_pr(package_name) |
| 92 | pr_2 = self.get_pr_version(package_name) |
| 93 | |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 94 | self.assertTrue(pr_2 - pr_1 == 1, "New PR %s did not increment as expected (from %s), difference should be 1" % (pr_2, pr_1)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 95 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 96 | def test_import_export_replace_db(self): |
| 97 | self.run_test_pr_export_import('m4') |
| 98 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 99 | def test_import_export_override_db(self): |
| 100 | self.run_test_pr_export_import('m4', replace_current_db=False) |
| 101 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 102 | def test_pr_service_rpm_arch_dep(self): |
| 103 | self.run_test_pr_service('m4', 'rpm', 'do_package') |
| 104 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 105 | def test_pr_service_deb_arch_dep(self): |
| 106 | self.run_test_pr_service('m4', 'deb', 'do_package') |
| 107 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 108 | def test_pr_service_ipk_arch_dep(self): |
| 109 | self.run_test_pr_service('m4', 'ipk', 'do_package') |
| 110 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 111 | def test_pr_service_rpm_arch_indep(self): |
| 112 | self.run_test_pr_service('xcursor-transparent-theme', 'rpm', 'do_package') |
| 113 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 114 | def test_pr_service_deb_arch_indep(self): |
| 115 | self.run_test_pr_service('xcursor-transparent-theme', 'deb', 'do_package') |
| 116 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 117 | def test_pr_service_ipk_arch_indep(self): |
| 118 | self.run_test_pr_service('xcursor-transparent-theme', 'ipk', 'do_package') |
| 119 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 120 | def test_stopping_prservice_message(self): |
| 121 | port = get_free_port() |
| 122 | |
| 123 | runCmd('bitbake-prserv --host localhost --port %s --loglevel=DEBUG --start' % port) |
| 124 | ret = runCmd('bitbake-prserv --host localhost --port %s --loglevel=DEBUG --stop' % port) |
| 125 | |
| 126 | self.assertEqual(ret.status, 0) |
| 127 | |