Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
puppeteer_tests: Reset test environment after each run.
Riken Shah99f8be6a1298
diff --git a/tools/test-js-with-puppeteer b/tools/test-js-with-puppeteerindex 891c78fe790..e8497bc7125 100755--- a/tools/test-js-with-puppeteer+++ b/tools/test-js-with-puppeteer@@ -5,10 +5,13 @@ import shleximport subprocessimport sys+import requests+sys.path.append(os.path.join(os.path.dirname(__file__), ".."))from scripts.lib.zulip_tools import ENDC, FAIL, OKGREENZULIP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+from zerver.lib.test_fixtures import reset_zulip_test_database# Request the special webpack setup for frontend integration tests,# where webpack assets are compiled up front rather than running in@@ -83,6 +86,13 @@ def run_tests(files: Iterable[str], external_host: str) -> None:flush=True,)ret = subprocess.call(cmd)++ # Resetting test environment.+ reset_zulip_test_database()+ # We are calling to /flush_caches to remove all the server-side caches.+ response = requests.get("http://zulip.zulipdev.com:9981/flush_caches")+ assert response.status_code == 200+if ret != 0:return ret, current_test_numcurrent_test_num += 1diff --git a/zerver/lib/test_fixtures.py b/zerver/lib/test_fixtures.pyindex 716c537c1cd..3db3a1d2a06 100644--- a/zerver/lib/test_fixtures.py+++ b/zerver/lib/test_fixtures.py@@ -386,3 +386,49 @@ def remove_test_run_directories(expiry_time: int = 60 * 60) -> int:except FileNotFoundError:passreturn removed+++def reset_zulip_test_database() -> None:+ """+ This function is used to reset the zulip_test database fastest way possible,+ i.e. First, it deletes the database and then clones it from zulip_test_template.+ This function is used with puppeteer tests, so it can quickly reset the test+ database after each run.+ """+ from zerver.lib.test_runner import destroy_test_databases++ # Make sure default database is 'zulip_test'.+ assert connections["default"].settings_dict["NAME"] == "zulip_test"++ # Clearing all the active PSQL sessions with 'zulip_test'.+ run(+ [+ "env",+ "PGHOST=localhost",+ "PGUSER=zulip_test",+ "scripts/setup/terminate-psql-sessions",+ "zulip_test",+ ]+ )++ destroy_test_databases()+ # Pointing default database to test database template, so we can instantly clone it.+ settings.DATABASES["default"]["NAME"] = settings.BACKEND_DATABASE_TEMPLATE+ connection = connections["default"]+ clone_database_suffix = "clone"+ connection.creation.clone_test_db(+ suffix=clone_database_suffix,+ )+ settings_dict = connection.creation.get_test_db_clone_settings(clone_database_suffix)+ # We manually rename the clone database to 'zulip_test' because when cloning it,+ # its name is set to original database name + some suffix.+ # Also, we need it to be 'zulip_test' so that our running server can recognize it.+ with connection.cursor() as cursor:+ cursor.execute("ALTER DATABASE zulip_test_template_clone RENAME TO zulip_test;")+ settings_dict["NAME"] = "zulip_test"+ # connection.settings_dict must be updated in place for changes to be+ # reflected in django.db.connections. If the following line assigned+ # connection.settings_dict = settings_dict, new threads would connect+ # to the default database instead of the appropriate clone.+ connection.settings_dict.update(settings_dict)+ connection.close()