test_configuration.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Test class :mod:`pydolphinscheduler.core.configuration`' method."""
  18. import importlib
  19. import os
  20. from pathlib import Path
  21. from typing import Any
  22. import pytest
  23. from pydolphinscheduler import configuration
  24. from pydolphinscheduler.configuration import (
  25. BUILD_IN_CONFIG_PATH,
  26. config_path,
  27. get_single_config,
  28. set_single_config,
  29. )
  30. from pydolphinscheduler.exceptions import PyDSConfException
  31. from pydolphinscheduler.utils.yaml_parser import YamlParser
  32. from tests.testing.constants import DEV_MODE, ENV_PYDS_HOME
  33. from tests.testing.file import get_file_content
  34. @pytest.fixture
  35. def teardown_file_env():
  36. """Util for deleting temp configuration file and pop env var after test finish."""
  37. yield
  38. config_file_path = config_path()
  39. if config_file_path.exists():
  40. config_file_path.unlink()
  41. os.environ.pop(ENV_PYDS_HOME, None)
  42. @pytest.mark.parametrize(
  43. "val, expect",
  44. [
  45. ("1", 1),
  46. ("123", 123),
  47. ("4567", 4567),
  48. (b"1234", 1234),
  49. ],
  50. )
  51. def test_get_int(val: Any, expect: int):
  52. """Test function :func:`configuration.get_int`."""
  53. assert configuration.get_int(val) == expect
  54. @pytest.mark.parametrize(
  55. "val",
  56. [
  57. "a",
  58. "1a",
  59. "1d2",
  60. "1723-",
  61. ],
  62. )
  63. def test_get_int_error(val: Any):
  64. """Test function :func:`configuration.get_int`."""
  65. with pytest.raises(ValueError):
  66. configuration.get_int(val)
  67. @pytest.mark.parametrize(
  68. "val, expect",
  69. [
  70. ("t", True),
  71. ("true", True),
  72. (1, True),
  73. (True, True),
  74. ("f", False),
  75. ("false", False),
  76. (0, False),
  77. (123, False),
  78. ("abc", False),
  79. ("abc1", False),
  80. (False, False),
  81. ],
  82. )
  83. def test_get_bool(val: Any, expect: bool):
  84. """Test function :func:`configuration.get_bool`."""
  85. assert configuration.get_bool(val) == expect
  86. @pytest.mark.parametrize(
  87. "home, expect",
  88. [
  89. (None, "~/pydolphinscheduler/config.yaml"),
  90. ("/tmp/pydolphinscheduler", "/tmp/pydolphinscheduler/config.yaml"),
  91. ("/tmp/test_abc", "/tmp/test_abc/config.yaml"),
  92. ],
  93. )
  94. def test_config_path(home: Any, expect: str):
  95. """Test function :func:`config_path`."""
  96. if home:
  97. os.environ[ENV_PYDS_HOME] = home
  98. assert Path(expect).expanduser() == configuration.config_path()
  99. @pytest.mark.parametrize(
  100. "home",
  101. [
  102. None,
  103. "/tmp/pydolphinscheduler",
  104. "/tmp/test_abc",
  105. ],
  106. )
  107. def test_init_config_file(teardown_file_env, home: Any):
  108. """Test init config file."""
  109. if home:
  110. os.environ[ENV_PYDS_HOME] = home
  111. elif DEV_MODE:
  112. pytest.skip(
  113. "Avoid delete ~/pydolphinscheduler/config.yaml by accident when test locally."
  114. )
  115. assert not config_path().exists()
  116. configuration.init_config_file()
  117. assert config_path().exists()
  118. assert get_file_content(config_path()) == get_file_content(BUILD_IN_CONFIG_PATH)
  119. @pytest.mark.parametrize(
  120. "home",
  121. [
  122. None,
  123. "/tmp/pydolphinscheduler",
  124. "/tmp/test_abc",
  125. ],
  126. )
  127. def test_init_config_file_duplicate(teardown_file_env, home: Any):
  128. """Test raise error with init config file which already exists."""
  129. if home:
  130. os.environ[ENV_PYDS_HOME] = home
  131. elif DEV_MODE:
  132. pytest.skip(
  133. "Avoid delete ~/pydolphinscheduler/config.yaml by accident when test locally."
  134. )
  135. assert not config_path().exists()
  136. configuration.init_config_file()
  137. assert config_path().exists()
  138. with pytest.raises(PyDSConfException, match=".*file already exists.*"):
  139. configuration.init_config_file()
  140. def test_get_configs_build_in():
  141. """Test function :func:`get_configs` with build-in config file."""
  142. content = get_file_content(BUILD_IN_CONFIG_PATH)
  143. assert YamlParser(content).src_parser == configuration.get_configs().src_parser
  144. assert YamlParser(content).dict_parser == configuration.get_configs().dict_parser
  145. @pytest.mark.parametrize(
  146. "key, val, new_val",
  147. [
  148. ("java_gateway.address", "127.0.0.1", "127.1.1.1"),
  149. ("java_gateway.port", 25333, 25555),
  150. ("java_gateway.auto_convert", True, False),
  151. ("default.user.name", "userPythonGateway", "editUserPythonGateway"),
  152. ("default.user.password", "userPythonGateway", "editUserPythonGateway"),
  153. (
  154. "default.user.email",
  155. "userPythonGateway@dolphinscheduler.com",
  156. "userPythonGateway@edit.com",
  157. ),
  158. ("default.user.phone", 11111111111, 22222222222),
  159. ("default.user.state", 1, 0),
  160. ("default.workflow.project", "project-pydolphin", "eidt-project-pydolphin"),
  161. ("default.workflow.tenant", "tenant_pydolphin", "edit_tenant_pydolphin"),
  162. ("default.workflow.user", "userPythonGateway", "editUserPythonGateway"),
  163. ("default.workflow.queue", "queuePythonGateway", "editQueuePythonGateway"),
  164. ("default.workflow.worker_group", "default", "specific"),
  165. ("default.workflow.time_zone", "Asia/Shanghai", "Asia/Beijing"),
  166. ("default.workflow.warning_type", "NONE", "ALL"),
  167. ],
  168. )
  169. def test_single_config_get_set(teardown_file_env, key: str, val: Any, new_val: Any):
  170. """Test function :func:`get_single_config` and :func:`set_single_config`."""
  171. assert val == get_single_config(key)
  172. set_single_config(key, new_val)
  173. assert new_val == get_single_config(key)
  174. def test_single_config_get_set_not_exists_key():
  175. """Test function :func:`get_single_config` and :func:`set_single_config` error while key not exists."""
  176. not_exists_key = "i_am_not_exists_key"
  177. with pytest.raises(PyDSConfException, match=".*do not exists.*"):
  178. get_single_config(not_exists_key)
  179. with pytest.raises(PyDSConfException, match=".*do not exists.*"):
  180. set_single_config(not_exists_key, not_exists_key)
  181. @pytest.mark.parametrize(
  182. "config_name, expect",
  183. [
  184. ("JAVA_GATEWAY_ADDRESS", "127.0.0.1"),
  185. ("JAVA_GATEWAY_PORT", 25333),
  186. ("JAVA_GATEWAY_AUTO_CONVERT", True),
  187. ("USER_NAME", "userPythonGateway"),
  188. ("USER_PASSWORD", "userPythonGateway"),
  189. ("USER_EMAIL", "userPythonGateway@dolphinscheduler.com"),
  190. ("USER_PHONE", "11111111111"),
  191. ("USER_STATE", 1),
  192. ("WORKFLOW_PROJECT", "project-pydolphin"),
  193. ("WORKFLOW_TENANT", "tenant_pydolphin"),
  194. ("WORKFLOW_USER", "userPythonGateway"),
  195. ("WORKFLOW_QUEUE", "queuePythonGateway"),
  196. ("WORKFLOW_WORKER_GROUP", "default"),
  197. ("WORKFLOW_TIME_ZONE", "Asia/Shanghai"),
  198. ("WORKFLOW_WARNING_TYPE", "NONE"),
  199. ],
  200. )
  201. def test_get_configuration(config_name: str, expect: Any):
  202. """Test get exists attribute in :mod:`configuration`."""
  203. assert expect == getattr(configuration, config_name)
  204. @pytest.mark.parametrize(
  205. "config_name, src, dest",
  206. [
  207. ("JAVA_GATEWAY_ADDRESS", "127.0.0.1", "192.168.1.1"),
  208. ("JAVA_GATEWAY_PORT", 25333, 25334),
  209. ("JAVA_GATEWAY_AUTO_CONVERT", True, False),
  210. ("USER_NAME", "userPythonGateway", "envUserPythonGateway"),
  211. ("USER_PASSWORD", "userPythonGateway", "envUserPythonGateway"),
  212. (
  213. "USER_EMAIL",
  214. "userPythonGateway@dolphinscheduler.com",
  215. "userPythonGateway@dolphinscheduler.com",
  216. ),
  217. ("USER_PHONE", "11111111111", "22222222222"),
  218. ("USER_STATE", 1, 0),
  219. ("WORKFLOW_PROJECT", "project-pydolphin", "env-project-pydolphin"),
  220. ("WORKFLOW_TENANT", "tenant_pydolphin", "env-tenant_pydolphin"),
  221. ("WORKFLOW_USER", "userPythonGateway", "envUserPythonGateway"),
  222. ("WORKFLOW_QUEUE", "queuePythonGateway", "envQueuePythonGateway"),
  223. ("WORKFLOW_WORKER_GROUP", "default", "custom"),
  224. ("WORKFLOW_TIME_ZONE", "Asia/Shanghai", "America/Los_Angeles"),
  225. ("WORKFLOW_WARNING_TYPE", "NONE", "ALL"),
  226. ],
  227. )
  228. def test_get_configuration_env(config_name: str, src: Any, dest: Any):
  229. """Test get exists attribute from environment variable in :mod:`configuration`."""
  230. assert getattr(configuration, config_name) == src
  231. env_name = f"PYDS_{config_name}"
  232. os.environ[env_name] = str(dest)
  233. # reload module configuration to re-get config from environment.
  234. importlib.reload(configuration)
  235. assert getattr(configuration, config_name) == dest
  236. # pop and reload configuration to test whether this config equal to `src` value
  237. os.environ.pop(env_name, None)
  238. importlib.reload(configuration)
  239. assert getattr(configuration, config_name) == src
  240. assert env_name not in os.environ