diff mbox series

[v2,1/3] prserv/tests: run ScriptTests on a free port with one database

Message ID 20260915160633.7-2-bbnpreetsingh@gmail.com
State New
Headers show
Series prserv: add --status, and make --start notice a dead server | expand

Commit Message

Babanpreet Singh Sept. 15, 2026, 4:06 p.m. UTC
ScriptTests starts bitbake-prserv on the default port (8585) and gives
each test its own temporary directory for the database. Anything
already listening on 8585 on the test machine stops the daemon from
binding.

Pick a free port in setUpClass and keep one database directory for the
whole class, which is what the oe-selftest prservice test already does
for its port.

AI-Generated: Uses Claude (claude-fable-5-1)
Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
---
 lib/prserv/tests.py | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/lib/prserv/tests.py b/lib/prserv/tests.py
index df0c00300..f3b2aae45 100644
--- a/lib/prserv/tests.py
+++ b/lib/prserv/tests.py
@@ -369,20 +369,28 @@  class PRUpstreamTests(PRTestSetup, unittest.TestCase):
 
 class ScriptTests(unittest.TestCase):
 
-    def setUp(self):
-
-        self.temp_dir = tempfile.TemporaryDirectory(prefix='bb-prserv')
-        self.addCleanup(self.temp_dir.cleanup)
-        self.dbfile = os.path.join(self.temp_dir.name, "prtest.sqlite3")
+    @classmethod
+    def setUpClass(cls):
+        cls.temp_dir = tempfile.TemporaryDirectory(prefix='bb-prserv')
+        cls.dbfile = os.path.join(cls.temp_dir.name, "prtest.sqlite3")
+        # The server test_1 starts runs until test_2 stops it, so use a
+        # port nothing else on the machine is listening on
+        with socket.socket() as s:
+            s.bind(("0.0.0.0", 0))
+            cls.port = str(s.getsockname()[1])
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.temp_dir.cleanup()
 
     def test_1_start_bitbake_prserv(self):
         try:
-            subprocess.check_call([BIN_DIR / "bitbake-prserv", "--start", "-f", self.dbfile])
+            subprocess.check_call([BIN_DIR / "bitbake-prserv", "--start", "-f", self.dbfile, "--port", self.port])
         except subprocess.CalledProcessError as e:
             self.fail("Failed to start bitbake-prserv: %s" % e.returncode)
 
     def test_2_stop_bitbake_prserv(self):
         try:
-            subprocess.check_call([BIN_DIR / "bitbake-prserv", "--stop"])
+            subprocess.check_call([BIN_DIR / "bitbake-prserv", "--stop", "--port", self.port])
         except subprocess.CalledProcessError as e:
             self.fail("Failed to stop bitbake-prserv: %s" % e.returncode)