test_workers.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. # Copyright 2022 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 Any, Mapping, Optional
  15. from unittest.mock import Mock
  16. from frozendict import frozendict
  17. from synapse.config import ConfigError
  18. from synapse.config.workers import WorkerConfig
  19. from tests.unittest import TestCase
  20. _EMPTY_FROZENDICT: Mapping[str, Any] = frozendict()
  21. class WorkerDutyConfigTestCase(TestCase):
  22. def _make_worker_config(
  23. self,
  24. worker_app: str,
  25. worker_name: Optional[str],
  26. extras: Mapping[str, Any] = _EMPTY_FROZENDICT,
  27. ) -> WorkerConfig:
  28. root_config = Mock()
  29. root_config.worker_app = worker_app
  30. root_config.worker_name = worker_name
  31. worker_config = WorkerConfig(root_config)
  32. worker_config_dict = {
  33. "worker_name": worker_name,
  34. "worker_app": worker_app,
  35. **extras,
  36. }
  37. worker_config.read_config(worker_config_dict)
  38. return worker_config
  39. def test_old_configs_master(self) -> None:
  40. """
  41. Tests old (legacy) config options. This is for the master's config.
  42. """
  43. main_process_config = self._make_worker_config(
  44. worker_app="synapse.app.homeserver", worker_name=None
  45. )
  46. self.assertTrue(
  47. main_process_config._should_this_worker_perform_duty(
  48. {},
  49. "notify_appservices",
  50. "synapse.app.appservice",
  51. "notify_appservices_from_worker",
  52. )
  53. )
  54. self.assertTrue(
  55. main_process_config._should_this_worker_perform_duty(
  56. {
  57. "notify_appservices": True,
  58. },
  59. "notify_appservices",
  60. "synapse.app.appservice",
  61. "notify_appservices_from_worker",
  62. )
  63. )
  64. self.assertFalse(
  65. main_process_config._should_this_worker_perform_duty(
  66. {
  67. "notify_appservices": False,
  68. },
  69. "notify_appservices",
  70. "synapse.app.appservice",
  71. "notify_appservices_from_worker",
  72. )
  73. )
  74. def test_old_configs_appservice_worker(self) -> None:
  75. """
  76. Tests old (legacy) config options. This is for the worker's config.
  77. """
  78. appservice_worker_config = self._make_worker_config(
  79. worker_app="synapse.app.appservice",
  80. worker_name="worker1",
  81. extras={
  82. # Set notify_appservices to false for the initialiser's config,
  83. # so that it doesn't raise an exception here.
  84. # (This is not read by `_should_this_worker_perform_duty`.)
  85. "notify_appservices": False,
  86. },
  87. )
  88. with self.assertRaises(ConfigError):
  89. # This raises because you need to set notify_appservices: False
  90. # before using the synapse.app.appservice worker type
  91. self.assertFalse(
  92. appservice_worker_config._should_this_worker_perform_duty(
  93. {},
  94. "notify_appservices",
  95. "synapse.app.appservice",
  96. "notify_appservices_from_worker",
  97. )
  98. )
  99. with self.assertRaises(ConfigError):
  100. # This also raises because you need to set notify_appservices: False
  101. # before using the synapse.app.appservice worker type
  102. appservice_worker_config._should_this_worker_perform_duty(
  103. {
  104. "notify_appservices": True,
  105. },
  106. "notify_appservices",
  107. "synapse.app.appservice",
  108. "notify_appservices_from_worker",
  109. )
  110. self.assertTrue(
  111. appservice_worker_config._should_this_worker_perform_duty(
  112. {
  113. "notify_appservices": False,
  114. },
  115. "notify_appservices",
  116. "synapse.app.appservice",
  117. "notify_appservices_from_worker",
  118. )
  119. )
  120. def test_transitional_configs_master(self) -> None:
  121. """
  122. Tests transitional (legacy + new) config options. This is for the master's config.
  123. """
  124. main_process_config = self._make_worker_config(
  125. worker_app="synapse.app.homeserver", worker_name=None
  126. )
  127. self.assertTrue(
  128. main_process_config._should_this_worker_perform_duty(
  129. {
  130. "notify_appservices": True,
  131. "notify_appservices_from_worker": "master",
  132. },
  133. "notify_appservices",
  134. "synapse.app.appservice",
  135. "notify_appservices_from_worker",
  136. )
  137. )
  138. self.assertFalse(
  139. main_process_config._should_this_worker_perform_duty(
  140. {
  141. "notify_appservices": False,
  142. "notify_appservices_from_worker": "worker1",
  143. },
  144. "notify_appservices",
  145. "synapse.app.appservice",
  146. "notify_appservices_from_worker",
  147. )
  148. )
  149. with self.assertRaises(ConfigError):
  150. # Contradictory because we say the master should notify appservices,
  151. # then we say worker1 is the designated worker to do that!
  152. main_process_config._should_this_worker_perform_duty(
  153. {
  154. "notify_appservices": True,
  155. "notify_appservices_from_worker": "worker1",
  156. },
  157. "notify_appservices",
  158. "synapse.app.appservice",
  159. "notify_appservices_from_worker",
  160. )
  161. with self.assertRaises(ConfigError):
  162. # Contradictory because we say the master shouldn't notify appservices,
  163. # then we say master is the designated worker to do that!
  164. main_process_config._should_this_worker_perform_duty(
  165. {
  166. "notify_appservices": False,
  167. "notify_appservices_from_worker": "master",
  168. },
  169. "notify_appservices",
  170. "synapse.app.appservice",
  171. "notify_appservices_from_worker",
  172. )
  173. def test_transitional_configs_appservice_worker(self) -> None:
  174. """
  175. Tests transitional (legacy + new) config options. This is for the worker's config.
  176. """
  177. appservice_worker_config = self._make_worker_config(
  178. worker_app="synapse.app.appservice",
  179. worker_name="worker1",
  180. extras={
  181. # Set notify_appservices to false for the initialiser's config,
  182. # so that it doesn't raise an exception here.
  183. # (This is not read by `_should_this_worker_perform_duty`.)
  184. "notify_appservices": False,
  185. },
  186. )
  187. self.assertTrue(
  188. appservice_worker_config._should_this_worker_perform_duty(
  189. {
  190. "notify_appservices": False,
  191. "notify_appservices_from_worker": "worker1",
  192. },
  193. "notify_appservices",
  194. "synapse.app.appservice",
  195. "notify_appservices_from_worker",
  196. )
  197. )
  198. with self.assertRaises(ConfigError):
  199. # This raises because this worker is the appservice app type, yet
  200. # another worker is the designated worker!
  201. appservice_worker_config._should_this_worker_perform_duty(
  202. {
  203. "notify_appservices": False,
  204. "notify_appservices_from_worker": "worker2",
  205. },
  206. "notify_appservices",
  207. "synapse.app.appservice",
  208. "notify_appservices_from_worker",
  209. )
  210. def test_new_configs_master(self) -> None:
  211. """
  212. Tests new config options. This is for the master's config.
  213. """
  214. main_process_config = self._make_worker_config(
  215. worker_app="synapse.app.homeserver", worker_name=None
  216. )
  217. self.assertTrue(
  218. main_process_config._should_this_worker_perform_duty(
  219. {"notify_appservices_from_worker": None},
  220. "notify_appservices",
  221. "synapse.app.appservice",
  222. "notify_appservices_from_worker",
  223. )
  224. )
  225. self.assertFalse(
  226. main_process_config._should_this_worker_perform_duty(
  227. {"notify_appservices_from_worker": "worker1"},
  228. "notify_appservices",
  229. "synapse.app.appservice",
  230. "notify_appservices_from_worker",
  231. )
  232. )
  233. def test_new_configs_appservice_worker(self) -> None:
  234. """
  235. Tests new config options. This is for the worker's config.
  236. """
  237. appservice_worker_config = self._make_worker_config(
  238. worker_app="synapse.app.generic_worker", worker_name="worker1"
  239. )
  240. self.assertTrue(
  241. appservice_worker_config._should_this_worker_perform_duty(
  242. {
  243. "notify_appservices_from_worker": "worker1",
  244. },
  245. "notify_appservices",
  246. "synapse.app.appservice",
  247. "notify_appservices_from_worker",
  248. )
  249. )
  250. self.assertFalse(
  251. appservice_worker_config._should_this_worker_perform_duty(
  252. {
  253. "notify_appservices_from_worker": "worker2",
  254. },
  255. "notify_appservices",
  256. "synapse.app.appservice",
  257. "notify_appservices_from_worker",
  258. )
  259. )
  260. def test_worker_duty_configs(self) -> None:
  261. """
  262. Additional tests for the worker duties
  263. """
  264. worker1_config = self._make_worker_config(
  265. worker_app="synapse.app.generic_worker",
  266. worker_name="worker1",
  267. extras={
  268. "notify_appservices_from_worker": "worker2",
  269. "update_user_directory_from_worker": "worker1",
  270. },
  271. )
  272. self.assertFalse(worker1_config.should_notify_appservices)
  273. self.assertTrue(worker1_config.should_update_user_directory)
  274. worker2_config = self._make_worker_config(
  275. worker_app="synapse.app.generic_worker",
  276. worker_name="worker2",
  277. extras={
  278. "notify_appservices_from_worker": "worker2",
  279. "update_user_directory_from_worker": "worker1",
  280. },
  281. )
  282. self.assertTrue(worker2_config.should_notify_appservices)
  283. self.assertFalse(worker2_config.should_update_user_directory)