blob: 8da3739c57f40d12c173900a7aa9e3f19411d8c8 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
6
Brad Bishopd7bf8c12018-02-25 22:55:05 -05007import os
8import re
9import shutil
10import datetime
11
12import oeqa.utils.ftools as ftools
13from oeqa.selftest.case import OESelftestTestCase
14from oeqa.utils.commands import runCmd, bitbake, get_bb_var
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015from oeqa.utils.network import get_free_port
16
Patrick Williams169d7bc2024-01-05 11:33:25 -060017import bb.utils
18
Brad Bishopd7bf8c12018-02-25 22:55:05 -050019class BitbakePrTests(OESelftestTestCase):
20
21 @classmethod
22 def setUpClass(cls):
23 super(BitbakePrTests, cls).setUpClass()
24 cls.pkgdata_dir = get_bb_var('PKGDATA_DIR')
25
Patrick Williams169d7bc2024-01-05 11:33:25 -060026 cls.exported_db_path = os.path.join(cls.builddir, 'export.inc')
27 cls.current_db_path = os.path.join(get_bb_var('PERSISTENT_DIR'), 'prserv.sqlite3')
28
29 def cleanup(self):
30 # Ensure any memory resident bitbake is stopped
31 bitbake("-m")
32 # Remove any existing export file or prserv database
33 bb.utils.remove(self.exported_db_path)
34 bb.utils.remove(self.current_db_path + "*")
35
Brad Bishopd7bf8c12018-02-25 22:55:05 -050036 def get_pr_version(self, package_name):
37 package_data_file = os.path.join(self.pkgdata_dir, 'runtime', package_name)
38 package_data = ftools.read_file(package_data_file)
Brad Bishop19323692019-04-05 15:28:33 -040039 find_pr = re.search(r"PKGR: r[0-9]+\.([0-9]+)", package_data)
Andrew Geisslerc9f78652020-09-18 14:11:35 -050040 self.assertTrue(find_pr, "No PKG revision found via regex 'PKGR: r[0-9]+\.([0-9]+)' in %s" % package_data_file)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050041 return int(find_pr.group(1))
42
43 def get_task_stamp(self, package_name, recipe_task):
44 stampdata = get_bb_var('STAMP', target=package_name).split('/')
45 prefix = stampdata[-1]
46 package_stamps_path = "/".join(stampdata[:-1])
47 stamps = []
48 for stamp in os.listdir(package_stamps_path):
Brad Bishop19323692019-04-05 15:28:33 -040049 find_stamp = re.match(r"%s\.%s\.([a-z0-9]{32})" % (re.escape(prefix), recipe_task), stamp)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050 if find_stamp:
51 stamps.append(find_stamp.group(1))
52 self.assertFalse(len(stamps) == 0, msg="Cound not find stamp for task %s for recipe %s" % (recipe_task, package_name))
53 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))
54 return str(stamps[0])
55
56 def increment_package_pr(self, package_name):
Patrick Williams213cb262021-08-07 19:21:33 -050057 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 Bishopd7bf8c12018-02-25 22:55:05 -050058 self.write_recipeinc(package_name, inc_data)
59 res = bitbake(package_name, ignore_status=True)
60 self.delete_recipeinc(package_name)
61 self.assertEqual(res.status, 0, msg=res.output)
62
63 def config_pr_tests(self, package_name, package_type='rpm', pr_socket='localhost:0'):
Patrick Williams169d7bc2024-01-05 11:33:25 -060064 self.cleanup()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050065 config_package_data = 'PACKAGE_CLASSES = "package_%s"' % package_type
66 self.write_config(config_package_data)
67 config_server_data = 'PRSERV_HOST = "%s"' % pr_socket
68 self.append_config(config_server_data)
69
70 def run_test_pr_service(self, package_name, package_type='rpm', track_task='do_package', pr_socket='localhost:0'):
71 self.config_pr_tests(package_name, package_type, pr_socket)
72
73 self.increment_package_pr(package_name)
74 pr_1 = self.get_pr_version(package_name)
75 stamp_1 = self.get_task_stamp(package_name, track_task)
76
77 self.increment_package_pr(package_name)
78 pr_2 = self.get_pr_version(package_name)
79 stamp_2 = self.get_task_stamp(package_name, track_task)
80
Andrew Geisslerc9f78652020-09-18 14:11:35 -050081 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 Bishopd7bf8c12018-02-25 22:55:05 -050082 self.assertTrue(stamp_1 != stamp_2, "Different pkg rev. but same stamp: %s" % stamp_1)
83
Patrick Williams169d7bc2024-01-05 11:33:25 -060084 self.cleanup()
85
Brad Bishopd7bf8c12018-02-25 22:55:05 -050086 def run_test_pr_export_import(self, package_name, replace_current_db=True):
87 self.config_pr_tests(package_name)
88
89 self.increment_package_pr(package_name)
90 pr_1 = self.get_pr_version(package_name)
91
Patrick Williams169d7bc2024-01-05 11:33:25 -060092 export_result = runCmd("bitbake-prserv-tool export %s" % self.exported_db_path, ignore_status=True)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093 self.assertEqual(export_result.status, 0, msg="PR Service database export failed: %s" % export_result.output)
Patrick Williams169d7bc2024-01-05 11:33:25 -060094 self.assertTrue(os.path.exists(self.exported_db_path), msg="%s didn't exist, tool output %s" % (self.exported_db_path, export_result.output))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095
96 if replace_current_db:
Patrick Williams169d7bc2024-01-05 11:33:25 -060097 self.assertTrue(os.path.exists(self.current_db_path), msg="Path to current PR Service database is invalid: %s" % self.current_db_path)
98 os.remove(self.current_db_path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099
Patrick Williams169d7bc2024-01-05 11:33:25 -0600100 import_result = runCmd("bitbake-prserv-tool import %s" % self.exported_db_path, ignore_status=True)
101 #os.remove(self.exported_db_path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102 self.assertEqual(import_result.status, 0, msg="PR Service database import failed: %s" % import_result.output)
103
104 self.increment_package_pr(package_name)
105 pr_2 = self.get_pr_version(package_name)
106
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500107 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 Bishopd7bf8c12018-02-25 22:55:05 -0500108
Patrick Williams169d7bc2024-01-05 11:33:25 -0600109 self.cleanup()
110
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 def test_import_export_replace_db(self):
112 self.run_test_pr_export_import('m4')
113
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500114 def test_import_export_override_db(self):
115 self.run_test_pr_export_import('m4', replace_current_db=False)
116
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500117 def test_pr_service_rpm_arch_dep(self):
118 self.run_test_pr_service('m4', 'rpm', 'do_package')
119
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 def test_pr_service_deb_arch_dep(self):
121 self.run_test_pr_service('m4', 'deb', 'do_package')
122
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123 def test_pr_service_ipk_arch_dep(self):
124 self.run_test_pr_service('m4', 'ipk', 'do_package')
125
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126 def test_pr_service_rpm_arch_indep(self):
127 self.run_test_pr_service('xcursor-transparent-theme', 'rpm', 'do_package')
128
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129 def test_pr_service_deb_arch_indep(self):
130 self.run_test_pr_service('xcursor-transparent-theme', 'deb', 'do_package')
131
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132 def test_pr_service_ipk_arch_indep(self):
133 self.run_test_pr_service('xcursor-transparent-theme', 'ipk', 'do_package')
134
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500135 def test_stopping_prservice_message(self):
136 port = get_free_port()
137
138 runCmd('bitbake-prserv --host localhost --port %s --loglevel=DEBUG --start' % port)
139 ret = runCmd('bitbake-prserv --host localhost --port %s --loglevel=DEBUG --stop' % port)
140
141 self.assertEqual(ret.status, 0)
142