test_rollback_worker.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import List
  15. from unittest import mock
  16. from synapse.app.generic_worker import GenericWorkerServer
  17. from synapse.storage.database import LoggingDatabaseConnection
  18. from synapse.storage.prepare_database import PrepareDatabaseException, prepare_database
  19. from synapse.storage.schema import SCHEMA_VERSION
  20. from tests.unittest import HomeserverTestCase
  21. def fake_listdir(filepath: str) -> List[str]:
  22. """
  23. A fake implementation of os.listdir which we can use to mock out the filesystem.
  24. Args:
  25. filepath: The directory to list files for.
  26. Returns:
  27. A list of files and folders in the directory.
  28. """
  29. if filepath.endswith("full_schemas"):
  30. return [str(SCHEMA_VERSION)]
  31. return ["99_add_unicorn_to_database.sql"]
  32. class WorkerSchemaTests(HomeserverTestCase):
  33. def make_homeserver(self, reactor, clock):
  34. hs = self.setup_test_homeserver(
  35. federation_http_client=None, homeserver_to_use=GenericWorkerServer
  36. )
  37. return hs
  38. def default_config(self):
  39. conf = super().default_config()
  40. # Mark this as a worker app.
  41. conf["worker_app"] = "yes"
  42. return conf
  43. def test_rolling_back(self):
  44. """Test that workers can start if the DB is a newer schema version"""
  45. db_pool = self.hs.get_datastores().main.db_pool
  46. db_conn = LoggingDatabaseConnection(
  47. db_pool._db_pool.connect(),
  48. db_pool.engine,
  49. "tests",
  50. )
  51. cur = db_conn.cursor()
  52. cur.execute("UPDATE schema_version SET version = ?", (SCHEMA_VERSION + 1,))
  53. db_conn.commit()
  54. prepare_database(db_conn, db_pool.engine, self.hs.config)
  55. def test_not_upgraded_old_schema_version(self):
  56. """Test that workers don't start if the DB has an older schema version"""
  57. db_pool = self.hs.get_datastores().main.db_pool
  58. db_conn = LoggingDatabaseConnection(
  59. db_pool._db_pool.connect(),
  60. db_pool.engine,
  61. "tests",
  62. )
  63. cur = db_conn.cursor()
  64. cur.execute("UPDATE schema_version SET version = ?", (SCHEMA_VERSION - 1,))
  65. db_conn.commit()
  66. with self.assertRaises(PrepareDatabaseException):
  67. prepare_database(db_conn, db_pool.engine, self.hs.config)
  68. def test_not_upgraded_current_schema_version_with_outstanding_deltas(self):
  69. """
  70. Test that workers don't start if the DB is on the current schema version,
  71. but there are still outstanding delta migrations to run.
  72. """
  73. db_pool = self.hs.get_datastores().main.db_pool
  74. db_conn = LoggingDatabaseConnection(
  75. db_pool._db_pool.connect(),
  76. db_pool.engine,
  77. "tests",
  78. )
  79. # Set the schema version of the database to the current version
  80. cur = db_conn.cursor()
  81. cur.execute("UPDATE schema_version SET version = ?", (SCHEMA_VERSION,))
  82. db_conn.commit()
  83. # Path `os.listdir` here to make synapse think that there is a migration
  84. # file ready to be run.
  85. # Note that we can't patch this function for the whole method, else Synapse
  86. # will try to find the file when building the database initially.
  87. with mock.patch("os.listdir", mock.Mock(side_effect=fake_listdir)):
  88. with self.assertRaises(PrepareDatabaseException):
  89. # Synapse should think that there is an outstanding migration file due to
  90. # patching 'os.listdir' in the function decorator.
  91. #
  92. # We expect Synapse to raise an exception to indicate the master process
  93. # needs to apply this migration file.
  94. prepare_database(db_conn, db_pool.engine, self.hs.config)