From 30743b08af064faf9ed7fa6ed2ac5c999f93ad0d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Thu, 9 Apr 2020 13:36:45 +0200
Subject: [PATCH] testUpgradeInstanceWithOldDataFs: change connection string in
 Data.fs

This test loads a reference Data.fs, but we don't want to connect to
the mysql referenced in the mariadb, because it might be used by another
test node.
Before the beginning of the test, rewrite the connection string to the
one this test node is using.
---
 product/ERP5Type/tests/ERP5TypeTestSuite.py | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/product/ERP5Type/tests/ERP5TypeTestSuite.py b/product/ERP5Type/tests/ERP5TypeTestSuite.py
index e5303c0946..8f6848f395 100644
--- a/product/ERP5Type/tests/ERP5TypeTestSuite.py
+++ b/product/ERP5Type/tests/ERP5TypeTestSuite.py
@@ -19,8 +19,10 @@ class ERP5TypeTestSuite(TestSuite):
     return self.runUnitTest(test)
 
   def runUnitTest(self, *args, **kw):
+    instance_home = self.instance and 'unit_test.%u' % self.instance \
+                                   or 'unit_test'
     if self.instance:
-      args = ('--instance_home=unit_test.%u' % self.instance,) + args
+      args = ('--instance_home=' + instance_home, ) + args
     if self.__dict__.has_key("bt5_path"):
       args = ("--bt5_path=%s" % self.bt5_path,) + args
     instance_number = self.instance or 1
@@ -41,6 +43,18 @@ class ERP5TypeTestSuite(TestSuite):
       args = ("--firefox_bin=%s" % firefox_bin,) + args
     if xvfb_bin:
       args = ("--xvfb_bin=%s" % xvfb_bin,) + args
+    if 'testUpgradeInstanceWithOldDataFs' in args:
+      # our reference Data.fs uses `CONNECTION_STRING_REPLACED_BY_TEST_INIT_______________________________`
+      # as a connection string. Before we start, replace this by the connection string
+      # that this test node is using.
+      marker_connection_string = b'CONNECTION_STRING_REPLACED_BY_TEST_INIT_______________________________'
+      actual_connection_string = mysql_db_list[0].ljust(len(marker_connection_string)).encode()
+      assert len(marker_connection_string) == len(actual_connection_string)
+      with open(os.path.join(instance_home, 'var', 'Data.fs'), 'rb') as f:
+        data_fs = f.read()
+      with open(os.path.join(instance_home, 'var', 'Data.fs'), 'wb') as f:
+        f.write(data_fs.replace(marker_connection_string, actual_connection_string))
+
     try:
       runUnitTest = os.environ.get('RUN_UNIT_TEST',
                                    'runUnitTest')
-- 
2.30.9