test_configuration.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 os
  19. from pathlib import Path
  20. from typing import Any
  21. import pytest
  22. from pydolphinscheduler.core import configuration
  23. from pydolphinscheduler.core.configuration import (
  24. BUILD_IN_CONFIG_PATH,
  25. config_path,
  26. get_single_config,
  27. set_single_config,
  28. )
  29. from pydolphinscheduler.exceptions import PyDSConfException
  30. from pydolphinscheduler.utils.yaml_parser import YamlParser
  31. from tests.testing.constants import DEV_MODE, ENV_PYDS_HOME
  32. from tests.testing.file import get_file_content
  33. @pytest.fixture
  34. def teardown_file_env():
  35. """Util for deleting temp configuration file and pop env var after test finish."""
  36. yield
  37. config_file_path = config_path()
  38. if config_file_path.exists():
  39. config_file_path.unlink()
  40. os.environ.pop(ENV_PYDS_HOME, None)
  41. @pytest.mark.parametrize(
  42. "home, expect",
  43. [
  44. (None, "~/pydolphinscheduler/config.yaml"),
  45. ("/tmp/pydolphinscheduler", "/tmp/pydolphinscheduler/config.yaml"),
  46. ("/tmp/test_abc", "/tmp/test_abc/config.yaml"),
  47. ],
  48. )
  49. def test_config_path(home: Any, expect: str):
  50. """Test function :func:`config_path`."""
  51. if home:
  52. os.environ[ENV_PYDS_HOME] = home
  53. assert Path(expect).expanduser() == configuration.config_path()
  54. @pytest.mark.parametrize(
  55. "home",
  56. [
  57. None,
  58. "/tmp/pydolphinscheduler",
  59. "/tmp/test_abc",
  60. ],
  61. )
  62. def test_init_config_file(teardown_file_env, home: Any):
  63. """Test init config file."""
  64. if home:
  65. os.environ[ENV_PYDS_HOME] = home
  66. elif DEV_MODE:
  67. pytest.skip(
  68. "Avoid delete ~/pydolphinscheduler/config.yaml by accident when test locally."
  69. )
  70. assert not config_path().exists()
  71. configuration.init_config_file()
  72. assert config_path().exists()
  73. assert get_file_content(config_path()) == get_file_content(BUILD_IN_CONFIG_PATH)
  74. @pytest.mark.parametrize(
  75. "home",
  76. [
  77. None,
  78. "/tmp/pydolphinscheduler",
  79. "/tmp/test_abc",
  80. ],
  81. )
  82. def test_init_config_file_duplicate(teardown_file_env, home: Any):
  83. """Test raise error with init config file which already exists."""
  84. if home:
  85. os.environ[ENV_PYDS_HOME] = home
  86. elif DEV_MODE:
  87. pytest.skip(
  88. "Avoid delete ~/pydolphinscheduler/config.yaml by accident when test locally."
  89. )
  90. assert not config_path().exists()
  91. configuration.init_config_file()
  92. assert config_path().exists()
  93. with pytest.raises(PyDSConfException, match=".*file already exists.*"):
  94. configuration.init_config_file()
  95. def test_get_configs_build_in():
  96. """Test function :func:`get_configs` with build-in config file."""
  97. content = get_file_content(BUILD_IN_CONFIG_PATH)
  98. assert YamlParser(content).src_parser == configuration.get_configs().src_parser
  99. assert YamlParser(content).dict_parser == configuration.get_configs().dict_parser
  100. @pytest.mark.parametrize(
  101. "key, val, new_val",
  102. [
  103. ("java_gateway.address", "127.0.0.1", "127.1.1.1"),
  104. ("java_gateway.port", 25333, 25555),
  105. ("java_gateway.auto_convert", True, False),
  106. ("default.user.name", "userPythonGateway", "editUserPythonGateway"),
  107. ("default.user.password", "userPythonGateway", "editUserPythonGateway"),
  108. (
  109. "default.user.email",
  110. "userPythonGateway@dolphinscheduler.com",
  111. "userPythonGateway@edit.com",
  112. ),
  113. ("default.user.phone", 11111111111, 22222222222),
  114. ("default.user.state", 1, 0),
  115. ("default.workflow.project", "project-pydolphin", "eidt-project-pydolphin"),
  116. ("default.workflow.tenant", "tenant_pydolphin", "edit_tenant_pydolphin"),
  117. ("default.workflow.user", "userPythonGateway", "editUserPythonGateway"),
  118. ("default.workflow.queue", "queuePythonGateway", "editQueuePythonGateway"),
  119. ("default.workflow.worker_group", "default", "specific"),
  120. ("default.workflow.time_zone", "Asia/Shanghai", "Asia/Beijing"),
  121. ],
  122. )
  123. def test_single_config_get_set(teardown_file_env, key: str, val: Any, new_val: Any):
  124. """Test function :func:`get_single_config` and :func:`set_single_config`."""
  125. assert val == get_single_config(key)
  126. set_single_config(key, new_val)
  127. assert new_val == get_single_config(key)
  128. def test_single_config_get_set_not_exists_key():
  129. """Test function :func:`get_single_config` and :func:`set_single_config` error while key not exists."""
  130. not_exists_key = "i_am_not_exists_key"
  131. with pytest.raises(PyDSConfException, match=".*do not exists.*"):
  132. get_single_config(not_exists_key)
  133. with pytest.raises(PyDSConfException, match=".*do not exists.*"):
  134. set_single_config(not_exists_key, not_exists_key)
  135. @pytest.mark.parametrize(
  136. "config_name, expect",
  137. [
  138. ("JAVA_GATEWAY_ADDRESS", "127.0.0.1"),
  139. ("JAVA_GATEWAY_PORT", 25333),
  140. ("JAVA_GATEWAY_AUTO_CONVERT", True),
  141. ("USER_NAME", "userPythonGateway"),
  142. ("USER_PASSWORD", "userPythonGateway"),
  143. ("USER_EMAIL", "userPythonGateway@dolphinscheduler.com"),
  144. ("USER_PHONE", "11111111111"),
  145. ("USER_STATE", 1),
  146. ("WORKFLOW_PROJECT", "project-pydolphin"),
  147. ("WORKFLOW_TENANT", "tenant_pydolphin"),
  148. ("WORKFLOW_USER", "userPythonGateway"),
  149. ("WORKFLOW_QUEUE", "queuePythonGateway"),
  150. ("WORKFLOW_WORKER_GROUP", "default"),
  151. ("WORKFLOW_TIME_ZONE", "Asia/Shanghai"),
  152. ],
  153. )
  154. def test_get_configuration(config_name: str, expect: Any):
  155. """Test get exists attribute in :mod:`configuration`."""
  156. assert expect == getattr(configuration, config_name)